]> sjero.net Git - wget/blob - tests/HTTPServer.pm
[svn] New OO Architecture for Wget 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
14 my $CRLF = "\015\012"; # "\r\n" is not portable
15
16 sub run {
17     my ($self, $urls) = @_;
18                                 
19     while (my $con = $self->accept) {
20         while (my $req = $con->get_request) {
21             # print STDERR "method: ", $req->method, "\n";
22             if (exists($urls->{$req->url->path})) {
23                 next unless ($req->method eq "HEAD" || $req->method eq "GET");
24                 # print STDERR "requested URL: ", $req->url->path, "\n";
25                     
26                 # create response
27                 my $tmp = $urls->{$req->url->path};
28                 my $resp = HTTP::Response->new ($tmp->{code},
29                                                 $tmp->{msg});
30                 # print STDERR "HTTP::Response: \n", $resp->as_string;
31                 
32                 # fill in headers
33                 while (my ($name, $value) = each %{$tmp->{headers}}) {
34                     # print STDERR "setting header: $name = $value\n";
35                     $resp->header($name => $value);
36                 }
37                 # print STDERR "HTTP::Response with headers: \n", $resp->as_string;
38                 
39                 if ($req->method eq "GET") {
40                     if (exists($tmp->{headers}{"Content-Length"})) {
41                         # Content-Length and length($tmp->{content}) don't match
42                         # manually prepare the HTTP response
43                         $con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
44                         print $con $resp->headers_as_string($CRLF);
45                         print $con $CRLF;
46                         print $con $tmp->{content};                                
47                         next;
48                     }
49                     # fill in content
50                     $resp->content($tmp->{content});
51                     # print STDERR "HTTP::Response with content: \n", $resp->as_string;
52                 }
53                 
54                 $con->send_response($resp);
55                 # print STDERR "HTTP::Response sent: \n", $resp->as_string;
56             } else {
57                 # print STDERR "requested wrong URL: ", $req->url->path, "\n";
58                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
59             }
60         }
61         $con->close;
62         undef($con);
63     }
64 }
65
66 1;
67
68 # vim: et ts=4 sw=4
69