]> sjero.net Git - wget/blob - tests/HTTPServer.pm
[svn] Minor bugfixing for test suite.
[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         print STDERR "Accepted a new connection\n" if $log;
23         while (my $req = $con->get_request) {
24             my $url_path = $req->url->path;
25             if ($url_path =~ m{/$}) {
26                 $url_path .= 'index.html';
27             }
28             if ($log) {
29                 print STDERR "Method: ", $req->method, "\n";
30                 print STDERR "Path: ", $url_path, "\n";
31                 print STDERR "Available URLs: ", "\n";
32                 foreach my $key (keys %$urls) {
33                     print STDERR $key, "\n";
34                 }
35             }
36             if (exists($urls->{$url_path})) {
37                 print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
38                 next unless ($req->method eq "HEAD" || $req->method eq "GET");
39                 
40                 # create response
41                 my $tmp = $urls->{$url_path};
42                 my $resp = HTTP::Response->new ($tmp->{code}, $tmp->{msg});
43                 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
44                 
45                 #if (is_dynamic_url) { # dynamic resource
46                 #} else { # static resource
47                     # fill in headers
48                     while (my ($name, $value) = each %{$tmp->{headers}}) {
49                         # print STDERR "setting header: $name = $value\n";
50                         $resp->header($name => $value);
51                     }
52                     print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
53                     
54                     if ($req->method eq "GET") {
55                         if (exists($tmp->{headers}{"Content-Length"})) {
56                             # Content-Length and length($tmp->{content}) don't match
57                             # manually prepare the HTTP response
58                             $con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
59                             print $con $resp->headers_as_string($CRLF);
60                             print $con $CRLF;
61                             print $con $tmp->{content};                                
62                             next;
63                         }
64                         # fill in content
65                         $resp->content($tmp->{content});
66                         print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
67                     }
68                 #}
69                 
70                 $con->send_response($resp);
71                 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
72             } else {
73                 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
74                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
75                 last;
76             }            
77         }
78         print STDERR "Closing connection\n" if $log;
79         $con->close;
80         undef($con);
81     }
82 }
83
84 1;
85
86 # vim: et ts=4 sw=4
87