]> sjero.net Git - wget/blob - tests/HTTPServer.pm
Automated merge.
[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         my $con = $self->accept();
28         print STDERR "Accepted a new connection\n" if $log;
29         while (my $req = $con->get_request) {
30             my $url_path = $req->url->path;
31             if ($url_path =~ m{/$}) { # append 'index.html'
32                 $url_path .= 'index.html';
33             }
34             #if ($url_path =~ m{^/}) { # remove trailing '/'
35             #    $url_path = substr ($url_path, 1);
36             #}
37             if ($log) {
38                 print STDERR "Method: ", $req->method, "\n";
39                 print STDERR "Path: ", $url_path, "\n";
40                 print STDERR "Available URLs: ", "\n";
41                 foreach my $key (keys %$urls) {
42                     print STDERR $key, "\n";
43                 }
44             }
45             if (exists($urls->{$url_path})) {
46                 print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
47                 next unless ($req->method eq "HEAD" || $req->method eq "GET");
48                 
49                 my $url_rec = $urls->{$url_path};
50                 $self->send_response($req, $url_rec, $con);
51             } else {
52                 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
53                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
54                 last;
55             }            
56         }
57         print STDERR "Closing connection\n" if $log;
58         $con->close;
59     }
60 }
61
62 sub send_response {
63     my ($self, $req, $url_rec, $con) = @_;
64
65     # create response
66     my ($code, $msg, $headers);
67     my $send_content = ($req->method eq "GET");
68     if (exists $url_rec->{'auth_method'}) {
69         ($send_content, $code, $msg, $headers) =
70             $self->handle_auth($req, $url_rec);
71     } else {
72         ($code, $msg) = @{$url_rec}{'code', 'msg'};
73         $headers = $url_rec->{headers};
74     }
75     my $resp = HTTP::Response->new ($code, $msg);
76     print STDERR "HTTP::Response: \n", $resp->as_string if $log;
77
78     while (my ($name, $value) = each %{$headers}) {
79         # print STDERR "setting header: $name = $value\n";
80         $resp->header($name => $value);
81     }
82     print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
83
84     if ($send_content) {
85         my $content = $url_rec->{content};
86         if (exists($url_rec->{headers}{"Content-Length"})) {
87             # Content-Length and length($content) don't match
88             # manually prepare the HTTP response
89             $con->send_basic_header($url_rec->{code}, $resp->message, $resp->protocol);
90             print $con $resp->headers_as_string($CRLF);
91             print $con $CRLF;
92             print $con $content;
93             next;
94         }
95         if ($req->header("Range")) {
96             $req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
97             my $content_len = length($content);
98             my $start = $1 ? $1 : 0;
99             my $end = $2 ? $2 : ($content_len - 1);
100             my $len = $2 ? ($2 - $start) : ($content_len - $start);
101             if ($len) {
102                 $resp->header("Accept-Ranges" => "bytes");
103                 $resp->header("Content-Length" => $len);
104                 $resp->header("Content-Range"
105                     => "bytes $start-$end/$content_len");
106                 $resp->header("Keep-Alive" => "timeout=15, max=100");
107                 $resp->header("Connection" => "Keep-Alive");
108                 $con->send_basic_header(206,
109                     "Partial Content", $resp->protocol);
110                 print $con $resp->headers_as_string($CRLF);
111                 print $con $CRLF;
112                 print $con substr($content, $start, $len);
113             } else {
114                 $con->send_basic_header(416, "Range Not Satisfiable",
115                     $resp->protocol);
116                 $resp->header("Keep-Alive" => "timeout=15, max=100");
117                 $resp->header("Connection" => "Keep-Alive");
118                 print $con $CRLF;
119             }
120             next;
121         }
122         # fill in content
123         $resp->content($content);
124         print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
125     }
126
127     $con->send_response($resp);
128     print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
129 }
130
131 # Generates appropriate response content based on the authentication
132 # status of the URL.
133 sub handle_auth {
134     my ($self, $req, $url_rec) = @_;
135     my ($send_content, $code, $msg, $headers);
136     # Catch failure to set code, msg:
137     $code = 500;
138     $msg  = "Didn't set response code in handle_auth";
139     # Most cases, we don't want to send content.
140     $send_content = 0;
141     # Initialize headers
142     $headers = {};
143     my $authhdr = $req->header('Authorization');
144
145     # Have we sent the challenge yet?
146     unless (defined $url_rec->{auth_challenged}
147         && $url_rec->{auth_challenged}) {
148         # Since we haven't challenged yet, we'd better not
149         # have received authentication (for our testing purposes).
150         if ($authhdr) {
151             $code = 400;
152             $msg  = "You sent auth before I sent challenge";
153         } else {
154             # Send challenge
155             $code = 401;
156             $msg  = "Authorization Required";
157             $headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
158                 . " realm=\"wget-test\"";
159             $url_rec->{auth_challenged} = 1;
160         }
161     } elsif (!defined($authhdr)) {
162         # We've sent the challenge; we should have received valid
163         # authentication with this one. A normal server would just
164         # resend the challenge; but since this is a test, wget just
165         # failed it.
166         $code = 400;
167         $msg  = "You didn't send auth after I sent challenge";
168     } else {
169         my ($sent_method) = ($authhdr =~ /^(\S+)/g);
170         unless ($sent_method eq $url_rec->{'auth_method'}) {
171             # Not the authorization type we were expecting.
172             $code = 400;
173             $msg = "Expected auth type $url_rec->{'auth_method'} but got "
174                 . "$sent_method";
175         } elsif (($sent_method eq 'Digest'
176                   && &verify_auth_digest($authhdr, $url_rec, \$msg))
177                  ||
178                  ($sent_method eq 'Basic'
179                   && &verify_auth_basic($authhdr, $url_rec, \$msg))) {
180             # SUCCESSFUL AUTH: send expected message, headers, content.
181             ($code, $msg) = @{$url_rec}{'code', 'msg'};
182             $headers = $url_rec->{headers};
183             $send_content = 1;
184         } else {
185             $code = 400;
186         }
187     }
188
189     return ($send_content, $code, $msg, $headers);
190 }
191
192 sub verify_auth_digest {
193     return undef; # Not yet implemented.
194 }
195
196 sub verify_auth_basic {
197     require MIME::Base64;
198     my ($authhdr, $url_rec, $msgref) = @_;
199     my $expected = MIME::Base64::encode_base64($url_rec->{'user'} . ':'
200         . $url_rec->{'passwd'}, '');
201     my ($got) = $authhdr =~ /^Basic (.*)$/;
202     if ($got eq $expected) {
203         return 1;
204     } else {
205         $$msgref = "Wanted ${expected} got ${got}";
206         return undef;
207     }
208 }
209
210 1;
211
212 # vim: et ts=4 sw=4
213