Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have been stuck on one problem for the past few hours. After reading everything I can find on HTTP Headers, other HTTP servers written in Perl, and gobs of other stuff, I'm still no closer to finding an answer.

I can read the request header just fine, though I am unable to get the POST data after the blank line. The only thing I have been able to figure out is that if there is a <CR> after the POST data then my script continues normally. Though without out the <CR> at the end of the POST data the connection just 'hangs', I assume it's waiting for more data (like a <CR>, which never comes). I've gone as far as trying to read the data in with
while ($input = <$client>) { $input =~ s/\r\n//g; # kill CR and NL chars $input =~ /^Content-Length: (\S*)/i && ($content_length=$1); ... more code .... ## find a blank line and we already have the content length then rea +d the POST data if (length($input) == 0 && $content_length > 0) { $client->recv($form_content,$content_length); } }
though still no luck. What am I doing wrong here?

Edit kudra, 2002-01-20 replaced pre with code

Replies are listed 'Best First'.
Re: HTTP POST Problems...
by rob_au (Abbot) on Jan 20, 2002 at 13:58 UTC
    While I must say I am curious as to why you are not using CGI for the parsing of posted HTTP data as it handles all of this overhead for you - Nevertheless, the following code may be of assistance to you, splitting the received request into HTTP header and body portions:

    { undef $/; my $input = <$client>; my ($headers, $body) = split(/(\r?\n){2}/, $input, 2); }

    Also too, from memory, if I recall correctly, the content length header also includes the header component of the response, not merely the response body component which may account for some of your difficulties. I am however willing to be corrected on this point ...

     

    Update - Updated code and comments to reflect the comments below ... ++Marcello

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

      Hi, The content length does NOT include the header component, only the POSTed data. Also, I would like to change your example to the following:
      { undef $/; my $input = <$client>; $input =~ s/\r//g; my ($headers, $body) = split(/\n\n/, $input, 2); }
      since the standard defines that headers and body should be separated by \r\n\r\n, and this solution works both on \r\n\r\n and \n\n.
Re: HTTP POST Problems...
by metadoktor (Hermit) on Jan 20, 2002 at 14:21 UTC
    Start out simple then add more complexity.

    Try to echo the data from the POST operation back to the browser. For your application, something like the following will probably work.

    if ($content_length>0) { print "Content-Type: text/html;\n\n"; while (<$client>) { print "$_<br>\n"; } }
    But you may want to consider CGI if you continue to have problems.

    metadoktor

    "The doktor is in."

Re: HTTP POST Problems...
by Anonymous Monk on Jan 21, 2002 at 05:54 UTC
    Okay.. so this was the problem. After reading in an HTTP header and you get the blank line, you MUST exit your loop. The 'auto-read' thing that perl does (which now all makes sense) depends on a <CR> at the end of the line to continue, otherwise it just keeps waiting for one. So once the header has been read and you know the 'content-length', exit the 'while' statment and
    $client->read($var,$length);
    It sounds so simple now, though at 2am it gets a little fuzzy, heh. Thanks again for the help!
Re: HTTP POST Problems...
by Anonymous Monk on Jan 21, 2002 at 05:33 UTC
    Thanks for the help, I will give those suggestions a try. I have another suggestion from someone else as well. I'll see what happens and post my results. BTW, The reason I am not using CGI is because this is running as a 'portal' for some command line scripts. A simple secure http listen server to execute command line functions.