Advertisement
Guest User

cj101

a guest
Apr 27th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.30 KB | None | 0 0
  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.         '/' => \&resp_home, # Added
  10.         '/home' => \&resp_home, # Added
  11.         '/hello' => \&resp_hello,
  12.         # ...
  13.     );
  14.  # Added hash
  15.     my %http_codes = (
  16.         '100' => 'Continue',
  17.         '101' => 'Switching Protocols',
  18.         '102' => 'Processing',
  19.         '200' => 'OK',
  20.         '201' => 'Created',
  21.         '202' => 'Accepted',
  22.         '203' => 'Non-Authoritative Information',
  23.         '204' => 'No Content',
  24.         '205' => 'Reset Content',
  25.         '206' => 'Partial Content',
  26.         '207' => 'Multi-Status',
  27.         '208' => 'Already Reported',
  28.         '226' => 'IM Used',
  29.         '300' => 'Multiple Choices',
  30.         '301' => 'Moved Permanently',
  31.         '302' => 'Found',
  32.         '303' => 'See Other',
  33.         '304' => 'Not Modified',
  34.         '305' => 'Use Proxy',
  35.         '306' => 'Switch Proxy',
  36.         '307' => 'Temporary Redirect',
  37.         '308' => 'Permanent Redirect',
  38.         '400' => 'Bad Request',
  39.         '401' => 'Unauthorized',
  40.         '402' => 'Payment Required',
  41.         '403' => 'Forbidden',
  42.         '404' => 'Not Found',
  43.         '405' => 'Method Not Allowed',
  44.         '406' => 'Not Acceptable',
  45.         '407' => 'Proxy Authentication Required',
  46.         '408' => 'Request Timeout',
  47.         '409' => 'Conflict',
  48.         '410' => 'Gone',
  49.         '411' => 'Length Required',
  50.         '412' => 'Precondition Failed',
  51.         '413' => 'Payload Too Large',
  52.         '414' => 'URI Too Long',
  53.         '415' => 'Unsupported Media Type',
  54.         '416' => 'Range Not Satisfiable',
  55.         '417' => 'Expectation Failed',
  56.         '418' => 'I\'m a teapot',
  57.         '421' => 'Misdirected Request',
  58.         '422' => 'Unprocessable Entity',
  59.         '423' => 'Locked',
  60.         '424' => 'Failed Dependency',
  61.         '426' => 'Upgrade Required',
  62.         '428' => 'Precondition Required',
  63.         '429' => 'Too Many Requests',
  64.         '431' => 'Request Header Fields Too Large',
  65.         '451' => 'Unavailable For Legal Reasons',
  66.         '500' => 'Internal Server Error',
  67.         '501' => 'Not Implemented',
  68.         '502' => 'Bad Gateway',
  69.         '503' => 'Service Unavailable',
  70.         '504' => 'Gateway Time-out',
  71.         '505' => 'HTTP Version Not Supported',
  72.         '506' => 'Variant Also Negotiates',
  73.         '507' => 'Insufficient Storage',
  74.         '508' => 'Loop Detected',
  75.         '510' => 'Not Extended',
  76.         '511' => 'Network Authentication Required',
  77.         '500' => 'Internal Server Error'
  78.     );
  79.  # Added sub
  80.     sub header {
  81.         my $cgi  = shift;
  82.  
  83.         my $code = shift;
  84.         my $message = $http_codes{$code};
  85.        
  86.         print "HTTP/1.0 ${code} ${message}\r\n";
  87.  
  88.         if ($code eq 200) {
  89.             return;
  90.         }
  91.  
  92.         print $cgi->header,
  93.             $cgi->start_html("Error"),
  94.             $cgi->h1($code),
  95.             $cgi->h1($message),
  96.             $cgi->end_html;
  97.     }
  98.  
  99.     sub handle_request {
  100.         my $self = shift;
  101.         my $cgi  = shift;
  102.  
  103.         my $path = $cgi->path_info();
  104.         my $handler = $dispatch{$path};
  105.  
  106.         if (ref($handler) eq "CODE") {
  107.             # print "HTTP/1.0 200 OK\r\n";
  108.             header($cgi, 200); # Use this to replace above line
  109.             $handler->($cgi);
  110.         } else {
  111.             # print "HTTP/1.0 404 Not found\r\n";
  112.             # print $cgi->header,
  113.             #     $cgi->start_html('Not found'),
  114.             #     $cgi->h1('Not found'),
  115.             #     $cgi->end_html;
  116.             header($cgi, 404); # Use this to replace above lines
  117.         }
  118.     }
  119. # Added sub
  120.     sub resp_home {
  121.         my $cgi  = shift;   # CGI.pm object
  122.         return if !ref $cgi;
  123.  
  124.         print $cgi->header,
  125.             $cgi->start_html("Home"),
  126.             $cgi->h1("Home"),
  127.             $cgi->end_html;
  128.     }
  129.  
  130.     sub resp_hello {
  131.         my $cgi  = shift;   # CGI.pm object
  132.         return if !ref $cgi;
  133.  
  134.         my $who = $cgi->param('name');
  135.  
  136.         print $cgi->header,
  137.             $cgi->start_html("Hello"),
  138.             $cgi->h1("Hello $who!"),
  139.             $cgi->end_html;
  140.     }
  141. }
  142.  
  143. # start the server on port 8080
  144. my $pid = MyWebServer->new(8080)->background();
  145. print "Use 'kill $pid' to stop server.\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement