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

Greetings Perl Advisors.

I have an interesting question that FAQ searches and O'Reilly's rodent book have been unable to help me with. As neither perl nor http are my bag normally, my question may border on the stupid side, so be warned.

I'm attempting to write a cgi script that will parse the content body of a custom crafted http request. The idea is that a very large Content-Length value is transmitted with the request, and that the content body works a bit like a data stream from the client to the cgi script.

My question is whether or not it's possible to get a file-like handle to the content body that will allow me to do things like

while ($line = <CONTENT_BODY_HANDLE>)
and if so, how would I go about it?

Thoughts? Exclamations of how wrong my approach is?
Cheers,
Mike

Replies are listed 'Best First'.
Re: Skirting the edges of RFC2616 in perl...
by tachyon (Chancellor) on Dec 10, 2002 at 01:54 UTC

    You should be sending/getting a data stream like:

    200 OK\015\012 Some-Header: value\015\012 Other-Header: value\015\012 \015\012 Body\015\012 Body\015\012 Body\015\012

    Presuming you send it by POST (as opposed to GET) it will arrive on STDIN for your script. You can then do:

    # set input operator to CRLF $/ = "\015\012"; # read header line by line while ( my $head = <STDIN> ) { last if m/^\015\012/; # must be a header line # do stuff } # header finished so read body line-wise while ( my $body = <STDIN> ) { # do whatever }

    I can't think of many good reasons to be doing all this by hand. There are plenty of ways to move data without getting back to basics as it were. Perhaps if you mention what your are trying to achieve we may be able to suggest the easy way to you.... You may well find that the library modules HTTP::Request HTTP::Message and LWP::UserAgent do everyting you need (and probably more).

    Note that you have to wrap long lines if you want to conform to the RFCs with soft line breaks when you send data. You therefore have to unwrap it at the other end.

    some very very very very very very very very very long single line that just keeps on and on and on going and going and going finally we get to a real new line.....

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Skirting the edges of RFC2616 in perl...
by dmitri (Priest) on Dec 10, 2002 at 01:36 UTC
    If it's a POST request, you can simply read it from STDIN.
Re: Skirting the edges of RFC2616 in perl...
by tid (Beadle) on Dec 10, 2002 at 02:10 UTC
    Thanks guys, that should cover it.

    To give you a bit of context, it's a prototyping cgi script. Before we expend any serious effort in implementing a fall-back transport engine using http as a carriage protocol on our server, we'd like to know that it'll work in the deployment environment. Therefore we're going to emulate some trivial server activity using a perl CGI script for testing purposes.

    Cheers,
    Mike