Hey monks,
I'm rather new to perl and I had the idea of making a simple forking http daemon to help me gain some experience.
Well...I'm having trouble with it, basically what happens is I start it up and I can request and recieve one page and then the daemon just dies.

I'll post the entire source as its not that big anyway.
#!/usr/bin/perl use strict; use warnings; use diagnostics; use IO::Socket; $SIG{CHLD} = \&WAIT; # Declare Globals my $crlf = "\015\012"; my $childpid; my $socket; my $method; my $client; my $oldfh; my $httpv; my $file; my $uri; # Create Socket $socket = IO::Socket::INET->new(Type => SOCK_STREAM, LocalPort => 8080, Listen => 10, Reuse => 1); die "Failed to start httpd: $!\n" unless $socket; while ($client = $socket->accept) { # Fork a child for each client request $childpid = fork; die "Failed to fork: $!\n" unless defined($childpid); # If child deal with client if ($childpid == 0) { while (<$client>) { # Split inital http headers ($method, $uri, $httpv) = split if (/\// && m/HTTP\/1\.(0|1)/) +; # exit if we receive a blank line and the headers are false exit if (/^($crlf|\n)$/ && not($method || $uri || $httpv)); # Send client a 501 if the client uses any of these http metho +ds if ($method =~ /POST|HEAD|PUT|DELETE|TRACE|CONNECT/) { print $client 'HTTP/1.0 501 Not Implemented' . $crlf x 2; exit; } # If no file is given assume index.html was requested if ($uri eq '/') { $file = 'index.html'; } else { $file = substr($uri, 1); } # If we receive a blank line then its the end of the request # so go onto sending a reply/file if (/^($crlf|\n)$/) { # If file exists go onto see if its readable if (-e $file) { # If file is readable go onto sending the file if (-r $file) { print $client 'HTTP/1.0 200 OK', $crlf; print $client 'Content-Type: text/html' . $crlf x 2; open(FILE, $file) || die "Failed to open file: $!\n" +; # Make FILE hot $oldfh = select FILE; $|++; select($oldfh); # Print to client while (<FILE>) { print $client $_; } close(FILE); exit; # Else if file is not readable send 403 } else { print $client 'HTTP/1.0 403 Forbidden' . $crlf x 2; exit; } # Else if file does not exist send 404 } else { print $client 'HTTP/1.0 404 Not Found' . $crlf x 2; exit; } } } } # If parent close client and go back # to the first while loop close($client); next; } sub WAIT { wait; }
I would be very, very grateful if anybody could help me.
Many Thanks,
Matt

PS/ I know this httpd has some security flaws (directory traversal etc.) but I do not intend on running it commercially.

In reply to Forking HTTP Daemon by Zola

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.