Hi,
I have been playing around with perl sockets and am trying to build a primiative web server. Using the IO::Socket module, I have had some success with this. I am able to point a web browser at port 7788 (on which I am serving) and the program is able to send html code and header tags to the browser. However, I am not sure to gather data from the web browsers header statements that it sends to the perl program.
For example, the web browser sends this information to the script:
GET /test.html HTTP/1.1 Host: localhost User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3b) Gecko/200 +30317 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9 +,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/ +*;q=0.1 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate,compress;q=0.9 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cookie: theme=Tabular Pragma: no-cache Cache-Control: no-cache
I want to be able to parse out those variables (which I am confident that I can do with some regular expressions), but I am not sure how to recieve this information in the first place. I want the script to wait for a web browser to send it the browser's header information, then parse the header information into variables, and finally send the browser back the html code and html header information (of which the last part it does now). I have played around with the IO::Socket's recv method a bit with no success. If anyone knows how to take in inforamtion in this way, I could greatly benifit from their experience. I appreciate your interest in this problem. Thanks for the read. Below is my code.
#!/usr/bin/perl -w use strict; use IO::Socket::INET; my $socket = IO::Socket::INET->new( LocalPort => 7788, Type => SOCK_STREAM, Reuse => 1, Listen => 10, ) or die "$!"; my ( $c, $content, $content_length, $server_type, $date, $text, $html_ +document, $header ); $text = &prepare_content; &serve; sub serve() { while ($c=$socket->accept()) { print $c "$text\n"; close($c); } close($socket); } sub prepare_content() { $html_document = $ARGV[0]; open(DOCUMENT, $html_document); my @content = <DOCUMENT>; close(DOCUMENT); $server_type = "Jazserv/1.0"; foreach my $temp (@content) { $content = $content . $temp; } $content_length = length($content); $date = `date`; print $content; $header = " HTTP/1.1 200 OK Server: $server_type ETag: \"\" Accept-Ranges: bytes Content-Length: $content_length Connection: close Content-Type: text/html; charset=ISO-8859-1 "; return "$header" . "$content"; }

In reply to IO::Socket server 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.