Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- {
- package MyWebServer;
- use HTTP::Server::Simple::CGI;
- use base qw(HTTP::Server::Simple::CGI);
- my %dispatch = (
- '/' => \&resp_home, # Added
- '/home' => \&resp_home, # Added
- '/hello' => \&resp_hello,
- # ...
- );
- # Added hash
- my %http_codes = (
- '100' => 'Continue',
- '101' => 'Switching Protocols',
- '102' => 'Processing',
- '200' => 'OK',
- '201' => 'Created',
- '202' => 'Accepted',
- '203' => 'Non-Authoritative Information',
- '204' => 'No Content',
- '205' => 'Reset Content',
- '206' => 'Partial Content',
- '207' => 'Multi-Status',
- '208' => 'Already Reported',
- '226' => 'IM Used',
- '300' => 'Multiple Choices',
- '301' => 'Moved Permanently',
- '302' => 'Found',
- '303' => 'See Other',
- '304' => 'Not Modified',
- '305' => 'Use Proxy',
- '306' => 'Switch Proxy',
- '307' => 'Temporary Redirect',
- '308' => 'Permanent Redirect',
- '400' => 'Bad Request',
- '401' => 'Unauthorized',
- '402' => 'Payment Required',
- '403' => 'Forbidden',
- '404' => 'Not Found',
- '405' => 'Method Not Allowed',
- '406' => 'Not Acceptable',
- '407' => 'Proxy Authentication Required',
- '408' => 'Request Timeout',
- '409' => 'Conflict',
- '410' => 'Gone',
- '411' => 'Length Required',
- '412' => 'Precondition Failed',
- '413' => 'Payload Too Large',
- '414' => 'URI Too Long',
- '415' => 'Unsupported Media Type',
- '416' => 'Range Not Satisfiable',
- '417' => 'Expectation Failed',
- '418' => 'I\'m a teapot',
- '421' => 'Misdirected Request',
- '422' => 'Unprocessable Entity',
- '423' => 'Locked',
- '424' => 'Failed Dependency',
- '426' => 'Upgrade Required',
- '428' => 'Precondition Required',
- '429' => 'Too Many Requests',
- '431' => 'Request Header Fields Too Large',
- '451' => 'Unavailable For Legal Reasons',
- '500' => 'Internal Server Error',
- '501' => 'Not Implemented',
- '502' => 'Bad Gateway',
- '503' => 'Service Unavailable',
- '504' => 'Gateway Time-out',
- '505' => 'HTTP Version Not Supported',
- '506' => 'Variant Also Negotiates',
- '507' => 'Insufficient Storage',
- '508' => 'Loop Detected',
- '510' => 'Not Extended',
- '511' => 'Network Authentication Required',
- '500' => 'Internal Server Error'
- );
- # Added sub
- sub header {
- my $cgi = shift;
- my $code = shift;
- my $message = $http_codes{$code};
- print "HTTP/1.0 ${code} ${message}\r\n";
- if ($code eq 200) {
- return;
- }
- print $cgi->header,
- $cgi->start_html("Error"),
- $cgi->h1($code),
- $cgi->h1($message),
- $cgi->end_html;
- }
- sub handle_request {
- my $self = shift;
- my $cgi = shift;
- my $path = $cgi->path_info();
- my $handler = $dispatch{$path};
- if (ref($handler) eq "CODE") {
- # print "HTTP/1.0 200 OK\r\n";
- header($cgi, 200); # Use this to replace above line
- $handler->($cgi);
- } else {
- # print "HTTP/1.0 404 Not found\r\n";
- # print $cgi->header,
- # $cgi->start_html('Not found'),
- # $cgi->h1('Not found'),
- # $cgi->end_html;
- header($cgi, 404); # Use this to replace above lines
- }
- }
- # Added sub
- sub resp_home {
- my $cgi = shift; # CGI.pm object
- return if !ref $cgi;
- print $cgi->header,
- $cgi->start_html("Home"),
- $cgi->h1("Home"),
- $cgi->end_html;
- }
- sub resp_hello {
- my $cgi = shift; # CGI.pm object
- return if !ref $cgi;
- my $who = $cgi->param('name');
- print $cgi->header,
- $cgi->start_html("Hello"),
- $cgi->h1("Hello $who!"),
- $cgi->end_html;
- }
- }
- # start the server on port 8080
- my $pid = MyWebServer->new(8080)->background();
- print "Use 'kill $pid' to stop server.\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement