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

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"; }

Replies are listed 'Best First'.
•Re: IO::Socket server
by merlyn (Sage) on Sep 08, 2003 at 02:21 UTC
      That looks like an excellent resource. I should have known that there would be a perl module for such a thing. Thanks for your post!
Re: IO::Socket server
by DigitalKitty (Parson) on Sep 08, 2003 at 02:57 UTC
    Hi JoeJaz.

    Writing your own web server is an enjoyable (and popular) project. I have taken the liberty of including a link to an introductory article that you might enjoy. Good luck with everything.

    http://www.perl.com/lpt/a/2002/09/17/ewispp.html

    If you have any questions regarding it's development, don't hesitate to ask.

    Thanks,

    -Katie
      WOW! This is exactly what I need to get a better grasp of some of the finer points of header information. It sure beats sniffing my own web requests and speculating as to what the meaning of some of this information! :-) Thanks for your help.