Hi, I am designing a primitive web server using the IO::Socket module and am trying to read the request headers from the client. I have been successful in reading in the first header:"GET /index.html HTTP/1.1". I have accomplished this by issuing a statement like this:
#!/usr/bin/perl -w + use strict; use IO::Socket; my $socket_server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => 7788, Reuse => 1, Proto => 'tcp' ) or die "$!"; my ($c, $request_headers); while ($c = $socket_server->accept()) { $c->autoflush(1); my $request_headers = <$c>; print $request_headers; }
This sample is simplified for this question. As I mentioned, with this code, I can easily grab the first line of the headers. However, I want to grab the rest of the headers. For example, I need to grab the "Host: somehost.com" header, which is typically the second line. Thinking that I could treat this like any other file handel, I replaced the contents of the while loop with this:
$c->autoflush(1); my @request_headers = <$c>; # line 3 for $header_line (@request_headers) { print $header_line; }
However, line 3 causes the web browser to stall. I am guessing that the program doesn't know when all of the headers have been displayed and just hangs. I have tried a variety of different things to try to get the program to stop reading once the headers have been displayed, but I have had no success. I would be overjoyed if someone has any suggestions regarding this problem. Thank you for reading this, Joe

In reply to IO::Socket Get Headers by JoeJaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.