- 论坛徽章:
- 0
|
- #!/usr/bin/perl
- use strict;
- use POSIX 'WNOHANG';
- use HTTP::Daemon;
- use HTTP::Status;
- my $quit = 0;
- $SIG{CHLD} = sub { while ( waitpid(-1,WNOHANG)>0 ) { } };
- $SIG{INT} = sub { $quit++ };
- my $daemon = HTTP::Daemon->new(LocalPort=>8080,Reuse=>1) || die "Can't listen on 8080: $!";
- print "Please contact me at: <URL:", $daemon->url, ">\n";
- while (!$quit) {
- next unless my $connection = $daemon->accept;
- defined (my $child = fork()) or die "Can't fork: $!";
- if ($child == 0) {
- $daemon->close;
- process_req($connection);
- exit 0;
- }
- $connection->close;
- }
- sub process_req {
- my $sock = shift;
- while (my $request = $sock->get_request) {
- if ($request->method eq 'GET') {
- (my $file = $request->uri->path) =~ s#^/##;
- if ($file) {
- $sock->send_file_response($file) or $sock->send_error(RC_NOT_FOUND);
- }
- else {
- $sock->send_file_response('index.html');
- }
- }
- }
- }
复制代码 回复 1# CN_long |
|