]> sjero.net Git - wget/blob - tests/HTTPServer.pm
[svn] HTTPServer.pm: Serve index.html if no filename is given.
[wget] / tests / HTTPServer.pm
1 #!/usr/bin/perl -w
2
3 package HTTPServer;
4
5 use strict;
6
7 use HTTP::Daemon;
8 use HTTP::Status;
9 use HTTP::Headers;
10 use HTTP::Response;
11
12 our @ISA=qw(HTTP::Daemon);
13 my $VERSION = 0.01;
14
15 my $CRLF = "\015\012"; # "\r\n" is not portable
16 my $log = undef;
17
18 sub run {
19     my ($self, $urls) = @_;
20                                 
21     while (my $con = $self->accept) {
22         while (my $req = $con->get_request) {
23             my $url_path = $req->url->path;
24             if ($url_path =~ m{/$}) {
25                 $url_path .= 'index.html';
26             }
27             if ($log) {
28                 print STDERR "Method: ", $req->method, "\n";
29                 print STDERR "Path: ", $url_path, "\n";
30                 print STDERR "Available URLs: ", "\n";
31                 foreach my $key (keys %$urls) {
32                     print STDERR $key, "\n";
33                 }
34             }
35             if (exists($urls->{$url_path})) {
36                 print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
37                 next unless ($req->method eq "HEAD" || $req->method eq "GET");
38                 
39                 # create response
40                 my $tmp = $urls->{$url_path};
41                 my $resp = HTTP::Response->new ($tmp->{code}, $tmp->{msg});
42                 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
43                 
44                 #if (is_dynamic_url) { # dynamic resource
45                 #} else { # static resource
46                     # fill in headers
47                     while (my ($name, $value) = each %{$tmp->{headers}}) {
48                         # print STDERR "setting header: $name = $value\n";
49                         $resp->header($name => $value);
50                     }
51                     print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
52                     
53                     if ($req->method eq "GET") {
54                         if (exists($tmp->{headers}{"Content-Length"})) {
55                             # Content-Length and length($tmp->{content}) don't match
56                             # manually prepare the HTTP response
57                             $con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
58                             print $con $resp->headers_as_string($CRLF);
59                             print $con $CRLF;
60                             print $con $tmp->{content};                                
61                             next;
62                         }
63                         # fill in content
64                         $resp->content($tmp->{content});
65                         print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
66                     }
67                 #}
68                 
69                 $con->send_response($resp);
70                 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
71             } else {
72                 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
73                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
74             }
75         }
76         $con->close;
77         undef($con);
78     }
79 }
80
81 1;
82
83 # vim: et ts=4 sw=4
84