pp2@nereida:~/src/testing$ cat -n httpserver.pl 1 #!/usr/bin/perl 2 { 3 package MyWebServer; 4 5 use HTTP::Server::Simple::CGI; 6 use base qw(HTTP::Server::Simple::CGI); 7 8 my %dispatch = ( 9 '/hello' => \&resp_hello, 10 # ... 11 ); 12 13 sub handle_request { 14 my $self = shift; 15 my $cgi = shift; 16 17 my $path = $cgi->path_info(); 18 my $handler = $dispatch{$path}; 19 20 if (ref($handler) eq "CODE") { 21 print "HTTP/1.0 200 OK\r\n"; 22 $handler->($cgi); 23 24 } else { 25 print "HTTP/1.0 404 Not found\r\n"; 26 print $cgi->header, 27 $cgi->start_html('Not found'), 28 $cgi->h1('Not found'), 29 $cgi->end_html; 30 } 31 } 32 33 sub resp_hello { 34 my $cgi = shift; # CGI.pm object 35 return if !ref $cgi; 36 37 my $who = $cgi->param('name'); 38 39 print $cgi->header, 40 $cgi->start_html("Hello"), 41 $cgi->h1("Hello $who!"), 42 $cgi->end_html; 43 } 44 45 } 46 47 # start the server on port 8082 48 my $pid = MyWebServer->new(8082)->background(); 49 print "Use 'kill $pid' to stop server. Connect to 'http://localhost:8082/hello'\n";
Casiano Rodríguez León