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