]> sjero.net Git - wget/blob - tests/HTTPServer.pm
[svn] Added testing for -np option.
[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{/$}) { # append 'index.html'
33                 $url_path .= 'index.html';
34             }
35             if ($url_path =~ m{^/}) { # remove trailing '/'
36                 $url_path = substr ($url_path, 1);
37             }
38             if ($log) {
39                 print STDERR "Method: ", $req->method, "\n";
40                 print STDERR "Path: ", $url_path, "\n";
41                 print STDERR "Available URLs: ", "\n";
42                 foreach my $key (keys %$urls) {
43                     print STDERR $key, "\n";
44                 }
45             }
46             if (exists($urls->{$url_path})) {
47                 print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
48                 next unless ($req->method eq "HEAD" || $req->method eq "GET");
49                 
50                 # create response
51                 my $tmp = $urls->{$url_path};
52                 my $resp = HTTP::Response->new ($tmp->{code}, $tmp->{msg});
53                 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
54                 
55                 #if (is_dynamic_url) { # dynamic resource
56                 #} else { # static resource
57                     # fill in headers
58                     while (my ($name, $value) = each %{$tmp->{headers}}) {
59                         # print STDERR "setting header: $name = $value\n";
60                         $resp->header($name => $value);
61                     }
62                     print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
63                     
64                     if ($req->method eq "GET") {
65                         if (exists($tmp->{headers}{"Content-Length"})) {
66                             # Content-Length and length($tmp->{content}) don't match
67                             # manually prepare the HTTP response
68                             $con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
69                             print $con $resp->headers_as_string($CRLF);
70                             print $con $CRLF;
71                             print $con $tmp->{content};
72                             next;
73                         }
74                         if ($req->header("Range")) {
75                             $req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
76                             my $content_len = length($tmp->{content});
77                             my $start = $1 ? $1 : 0;
78                             my $end = $2 ? $2 : ($content_len - 1);
79                             my $len = $2 ? ($2 - $start) : ($content_len - $start);
80                             $resp->header("Accept-Ranges" => "bytes");
81                             $resp->header("Content-Length" => $len);
82                             $resp->header("Content-Range" => "bytes $start-$end/$content_len");
83                             $resp->header("Keep-Alive" => "timeout=15, max=100");
84                             $resp->header("Connection" => "Keep-Alive");
85                             $con->send_basic_header(206, "Partial Content", $resp->protocol);
86                             print $con $resp->headers_as_string($CRLF);
87                             print $con $CRLF;
88                             print $con substr($tmp->{content}, $start, $len);
89                             next;
90                         }
91                         # fill in content
92                         $resp->content($tmp->{content});
93                         print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
94                     }
95                 #}
96                 
97                 $con->send_response($resp);
98                 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
99             } else {
100                 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
101                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
102                 last;
103             }            
104         }
105         print STDERR "Closing connection\n" if $log;
106         $con->close;
107     }
108 }
109
110 1;
111
112 # vim: et ts=4 sw=4
113