move information from HTTP::Request to CGI.pm

Discussion in 'Perl Misc' started by Miroslav Suchy, Aug 17, 2005.

  1. Hello,
    I have following code:

    my $daemon = HTTP::Daemon->new(
    ReuseAddr => 1,
    LocalAddr => 'mydomain.com',
    LocalPort => 8888,
    ) or die "Cannot start daemon: $!\n";
    while (my $conn = $daemon->accept()) {
    while (my $req = $conn->get_request()) {
    if ($req->method() eq 'GET') { # I do not use GET
    $q = CGI->new($url->query());
    } elsif ($req->method() eq 'POST') { # I only use POST method
    $q = CGI->new($req->content);
    open FF, ">/tmp/debug"; #output to FF just for debug
    print FF $req->content;
    close FF;
    }
    print STDERR "params: ", join(',', $q->param), "\n";
    }
    }

    And it does not work as I expect. This is content of file "/tmp/debug":
    -----------------------------18004688081790386843384315354
    Content-Disposition: form-data; name="id"

    6
    -----------------------------18004688081790386843384315354
    Content-Disposition: form-data; name="pw"

    aa
    -----------------------------18004688081790386843384315354--

    I expect following output from script:
    params: id,pw
    but I get this output:
    params: -----------------------------18004688081790386843384315354
    Content-Disposition: form-data, name

    What I'm doing wrong? Thank you.

    Miroslav Suchy
    Miroslav Suchy, Aug 17, 2005
    #1
    1. Advertising

  2. Hello again,
    I was hard working on my problem (I've HTTP::Request and I to move its
    iformation to CGI object) and I come to the smallest code. But it still does not
    work and I do not know where can be a problem.

    8<--cut
    #!/usr/bin/perl
    use strict;
    use HTTP::Daemon ();
    use CGI ();

    my $daemon = HTTP::Daemon->new(
    LocalAddr => 'my.domain.com',
    LocalPort => 8888,
    ) or die "Cannot start daemon: $!\n";
    my $i;
    while (my $conn = $daemon->accept()) {
    while (my $req = $conn->get_request()) {
    my $url = $req->uri();
    $ENV{'REQUEST_METHOD'}=$req->method();
    $ENV{'CONTENT_TYPE'}=$req->header('Content-Type');
    $ENV{'CONTENT_LENGTH'}=$req->header('Content-Length');
    my $filename="/dev/shm/tmp.$$.".++$i;
    print STDERR "$filename\n";
    open INPUT, ">$filename" or die; # prepare stdin for CGI.pm
    binmode INPUT;
    print INPUT $req->content;
    close INPUT;
    open STDIN, "<$filename" or die; # redirect stdin
    my $q = CGI->new;
    my $id=$q->param('id');
    my $ff=$q->param('file');
    print STDERR "$id-$ff----\n";
    my $res = HTTP::Response->new();
    $res->header('Content-Type'=> 'text/html');
    my $content=$q->start_html.$q->end_html;
    $res->content_length(length($content));
    $res->content($content);
    $conn->send_response($res);
    close STDIN;
    }
    $conn->close();
    }
    8<---cut

    In first while loop is everything correct. But second and all additional loops
    have the same content of $q as in first loop!!!!
    STDERR look like this:
    /dev/shm/tmp.18353.1
    4159616-4159616.jpg----
    /dev/shm/tmp.18353.2
    4159616-4159616.jpg----
    /dev/shm/tmp.18353.3
    4159616-4159616.jpg----

    It means $q->param('id') returns id from the first file, which contain:
    $ head -n5 /dev/shm/tmp.18353.1
    --xYzZY
    Content-Disposition: form-data; name="id"

    4159616
    --xYzZY
    but it should return id from the second file, which contain:
    $ head -n5 /dev/shm/tmp.18353.2
    --xYzZY
    Content-Disposition: form-data; name="id"

    4159620
    --xYzZY

    If I try to dump STDIN after the line:
    open STDIN, "<$filename" or die; # redirect stdin
    it is the same as content of file $filename
    But CGI module somehow remember its state from first reading!!
    How can I force CGI to re-read STDIN?
    Or make I something wrong?

    BTW: The request for this daemon is created using this code:
    ....
    my $ua = LWP::UserAgent->new;
    my $response = $ua->post('http://my.domain.com:8888/',
    [
    id => $id,
    file => ["$path$id.jpg"] ,
    ],
    'Content_Type' => 'form-data',
    );

    Thanks in advance for any suggestion

    Miroslav Suchy
    Miroslav Suchy, Aug 25, 2005
    #2
    1. Advertising

  3. Re: move information from HTTP::Request to CGI.pm - Solved

    Hello again,
    I solved my problem. For archive purpose:
    If you want to read STDIN again with CGI you must undef @CGI::QUERY_PARAM (Yeah,
    that one with comment "Other globals that you shouldn't worry about." in CGI.pm :) )

    Miroslav Suchy
    Miroslav Suchy, Aug 25, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Birtle
    Replies:
    2
    Views:
    2,017
    John Saunders
    Oct 16, 2003
  2. nRk
    Replies:
    1
    Views:
    996
    Steven D'Aprano
    Feb 12, 2009
  3. Eduardo78
    Replies:
    0
    Views:
    246
    Eduardo78
    Nov 3, 2005
  4. Andrew Tomazos
    Replies:
    2
    Views:
    613
    Nobody
    Dec 12, 2011
  5. Gelonida N
    Replies:
    0
    Views:
    526
    Gelonida N
    Feb 27, 2012
Loading...

Share This Page