in reply to IO::Socket Get Headers

my @request_headers = <$c>;
reads until the end of the file, pushing each line onto the array. However, you're suppose to stop reading when you see CRLFCRLF. To read just the next line, do:
my $next_line = <$c>;

You're reinventing the wheel, though. (And you're already doing things wrong, like reading too far, and not setting $/ to "\015\012".) There's already at least one mini perl httpd you can use, HTTP::Daemon, and I think it even comes standard with perl.

Replies are listed 'Best First'.
Re^2: IO::Socket Get Headers
by JoeJaz (Monk) on Oct 02, 2004 at 05:02 UTC
    Hi, thanks. That's just the info I needed and the solution was easier than I was making it to be. That problem stumped me for hours. I'll look into the HTTP::Deamon module as a future alternative. Thanks again for your help. Joe
Re^2: IO::Socket Get Headers
by JoeJaz (Monk) on Oct 04, 2004 at 08:55 UTC
    Hi, I am having some luck based on the suggestions that you gave me. However, what do you mean by:
    setting $/ to "\015\012"
    Also, if the code that you provided reads only one line at a time, how do I test for the condition of having two control feeds? Forgive me for the ignorant questions. Joe

      If you check the spec, you'll notice HTTP header lines are terminated by CRLF. Your code checks for only LF. 015 is the octal value for CR, 012 is the octal value for LF. ("\r\n" isn't good cause they're not always CR and LF.)

      You can tell you received two CRLF in a row when reading a line at a time by checking for a blank line.

      my @headers; { local $/ = "\015\012"; while (<IN>) { chomp; last if $_ eq ''; push(@headers, $_); } } my $request_line = shift(@headers);

      That handles HTTP/1.0 and HTTP/1.1 requests, but I don't know about HTTP/0.9 requests.

        Thank you for clarifying this for me. Indeed, it works and works very nicely. I was able to integrate it without any problem. If you would permit me to ask one more related question, I would also like to know how to how to check for the end of HTML content. With your help, I am able read in all of the GET headers. I am still having difficulty reading in the HTML response since it the program doesn't know when it has reached the end of the HTML stream. (I am making a very simple PROXY server). Is there a control character that I can check for at the end of an HTML transfer? Forgive me for asking all of these questions, but I have had a hard time finding any useful documentation on IO::Socket. Thanks for reading this and for your previous help and have a nice day. Joe