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