in reply to How do I write a simple webserver

Here's something super cheezy I just wrote up as a quick excersize for myself. It does what you ask for...

use strict; use IO::Socket; my $server = IO::Socket::INET->new ( Proto => "tcp", LocalPort => 3000, Reuse=>1, Listen=>10 ); my ( $pid, $host, $date ); $date = scalar localtime(); sub spawn_srvr { my ( $vers, $page, %hdrs ); while (<$host>) { /^(GET)\s(.*)/ && do { $hdrs{$1} = $2; next; }; /^((\w|-)+):(.*)/ && do { $hdrs{$1} = $3; next; }; /\n/ and do { ( $page, $vers ) = split(/\s/, $hdrs{'GET'}, 2); if ( open(FH, "<$page") ) { print $host <<EOF; HTTP/1.1 200 OK Date: $date Server: LameWeb Connection: close Content-Type: text/html EOF print $host $_ while ( <FH> ); } else { print $host <<EOF; HTTP/1.1 404 Not Found Date: $date Server: LameWeb Connection: close Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>404 Not Found</TITLE> </HEAD><BODY> <H1>Not Found</H1> The requested URL $page was not found on this server.<P> </BODY></HTML> EOF } last; }; } kill(9, $pid) && print "killed pid: $pid\n"; } while () { $host = $server->accept(); if ($pid = fork()) { spawn_srvr(); } $host->close; }

Replies are listed 'Best First'.
RE: RE: How do I write a simple webserver
by tye (Sage) on Sep 18, 2000 at 09:16 UTC

    To tell the browser that you are done, you need to close the socket: close $host

            - tye (but my friends call me "Tye")