A Minimal Perl Web Server

5.2 A Minimal Perl Web Server

If you want to build a full-featured HTTP server, you have some work to do. The core of the Apache web server has over 50,000 lines of code, and optional processing modules make that number much bigger.

All this software is needed to support HTTP/1.1 features: rich resource support, virtual hosting, access control, logging, configuration, monitoring, and performance features. That said, you can create a minimally functional HTTP server in under 30 lines of Perl. Let's take a look.

Example 5-1 shows a tiny Perl program called type-o-serve . This program is a useful diagnostic tool for testing interactions with clients and proxies. Like any web server, type-o-serve waits for an HTTP connection. As soon as type-o-serve gets the request message, it prints the message on the screen; then it waits for you to type (or paste) in a response message, which is sent back to the client. This way, type-o-serve pretends to be a web server, records the exact HTTP request messages, and allows you to send back any HTTP response message.

This simple type-o-serve utility doesn't implement most HTTP functionality, but it is a useful tool to generate server response messages the same way you can use Telnet to generate client request messages (refer back to Example 5-1 ). You can download the type-o-serve program from http://www.http-guide.com/tools/type-o-serve.pl .

Example 5-1. type-o-servea minimal Perl web server used for HTTP debugging
 #!/usr/bin/perl 
 
 use Socket; 
 use Carp; 
 use FileHandle; 
 
 # (1) use port 8080 by default, unless overridden on command line 
 $port = (@ARGV ? $ARGV[0] : 8080); 
 
 # (2) create local TCP socket and set it to listen for connections 
 $proto = getprotobyname('tcp'); 
 socket(S, PF_INET, SOCK_STREAM, $proto)  die; 
 setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))  die; 
 bind(S, sockaddr_in($port, INADDR_ANY))  die; 
 listen(S, SOMAXCONN)  die; 
 
 # (3) print a startup message 
 printf(" <<<Type-O-Serve Accepting on Port %d>>>\n\n",$port); 
 
 while (1) 
 { 
 # (4) wait for a connection C 
 $cport_caddr = accept(C, S); 
 ($cport,$caddr) = sockaddr_in($cport_caddr); 
 C->autoflush(1); 
 
 # (5) print who the connection is from 
 $cname = gethostbyaddr($caddr,AF_INET); 
 printf(" <<<Request From '%s'>>>\n",$cname); 
 
 # (6) read request msg until blank line, and print on screen 
 while ($line = <C>) 
 { 
 print $line; 
 if ($line =~ /^\r/) { last; } 
 } 
 
 # (7) prompt for response message, and input response lines, 
 # sending response lines to client, until solitary "." 
 printf(" <<<Type Response Followed by '.'>>>\n"); 
 
 while ($line = <STDIN>) 
 { 
 $line =~ s/\r//; 
 $line =~ s/\n//; 
 if ($line =~ /^\./) { last; } 
 print C $line . "\r\n"; 
 } 
 close(C); 
 } 

Figure 5-2 shows how the administrator of Joe's Hardware store might use type-o-serve to test HTTP communication:

                First, the administrator starts the type-o-serve diagnostic server, listening on a particular port. Because Joe's Hardware store already has a production web server listing on port 80, the administrator starts the type-o-serve server on port 8080 (you can pick any unused port) with this command line:

 %  type-o-serve.pl 8080  

                Once type-o-serve is running, you can point a browser to this web server. In Figure 5-2 , we browse to http://www.joes-hardware.com:8080/foo/bar/blah.txt .

                The type-o-serve program receives the HTTP request message from the browser and prints the contents of the HTTP request message on screen. The type-o-serve diagnostic tool then waits for the user to type in a simple response message, followed by a period on a blank line.

                type-o-serve sends the HTTP response message back to the browser, and the browser displays the body of the response message.

Figure 5-2. The type-o-serve utility lets you type in server responses to send back to clients

figs/http_0502.gif

 



HTTP. The Definitive Guide
HTTP: The Definitive Guide
ISBN: 1565925092
EAN: 2147483647
Year: 2001
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net