]> sjero.net Git - wget/blob - tests/HTTPServer.pm
Automated merge with file:/home/micah/devel/wget/eleven
[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             $resp->header("Accept-Ranges" => "bytes");
102             $resp->header("Content-Length" => $len);
103             $resp->header("Content-Range" => "bytes $start-$end/$content_len");
104             $resp->header("Keep-Alive" => "timeout=15, max=100");
105             $resp->header("Connection" => "Keep-Alive");
106             $con->send_basic_header(206, "Partial Content", $resp->protocol);
107             print $con $resp->headers_as_string($CRLF);
108             print $con $CRLF;
109             print $con substr($content, $start, $len);
110             next;
111         }
112         # fill in content
113         $resp->content($content);
114         print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
115     }
116
117     $con->send_response($resp);
118     print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
119 }
120
121 # Generates appropriate response content based on the authentication
122 # status of the URL.
123 sub handle_auth {
124     my ($self, $req, $url_rec) = @_;
125     my ($send_content, $code, $msg, $headers);
126     # Catch failure to set code, msg:
127     $code = 500;
128     $msg  = "Didn't set response code in handle_auth";
129     # Most cases, we don't want to send content.
130     $send_content = 0;
131     # Initialize headers
132     $headers = {};
133     my $authhdr = $req->header('Authorization');
134
135     # Have we sent the challenge yet?
136     unless (defined $url_rec->{auth_challenged}
137         && $url_rec->{auth_challenged}) {
138         # Since we haven't challenged yet, we'd better not
139         # have received authentication (for our testing purposes).
140         if ($authhdr) {
141             $code = 400;
142             $msg  = "You sent auth before I sent challenge";
143         } else {
144             # Send challenge
145             $code = 401;
146             $msg  = "Authorization Required";
147             $headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
148                 . " realm=\"wget-test\"";
149             $url_rec->{auth_challenged} = 1;
150         }
151     } elsif (!defined($authhdr)) {
152         # We've sent the challenge; we should have received valid
153         # authentication with this one. A normal server would just
154         # resend the challenge; but since this is a test, wget just
155         # failed it.
156         $code = 400;
157         $msg  = "You didn't send auth after I sent challenge";
158     } else {
159         my ($sent_method) = ($authhdr =~ /^(\S+)/g);
160         unless ($sent_method eq $url_rec->{'auth_method'}) {
161             # Not the authorization type we were expecting.
162             $code = 400;
163             $msg = "Expected auth type $url_rec->{'auth_method'} but got "
164                 . "$sent_method";
165         } elsif (($sent_method eq 'Digest'
166                   && &verify_auth_digest($authhdr, $url_rec, \$msg))
167                  ||
168                  ($sent_method eq 'Basic'
169                   && &verify_auth_basic($authhdr, $url_rec, \$msg))) {
170             # SUCCESSFUL AUTH: send expected message, headers, content.
171             ($code, $msg) = @{$url_rec}{'code', 'msg'};
172             $headers = $url_rec->{headers};
173             $send_content = 1;
174         } else {
175             $code = 400;
176         }
177     }
178
179     return ($send_content, $code, $msg, $headers);
180 }
181
182 sub verify_auth_digest {
183     return undef; # Not yet implemented.
184 }
185
186 sub verify_auth_basic {
187     require MIME::Base64;
188     my ($authhdr, $url_rec, $msgref) = @_;
189     my $expected = MIME::Base64::encode_base64($url_rec->{'user'} . ':'
190         . $url_rec->{'passwd'}, '');
191     my ($got) = $authhdr =~ /^Basic (.*)$/;
192     if ($got eq $expected) {
193         return 1;
194     } else {
195         $$msgref = "Wanted ${expected} got ${got}";
196         return undef;
197     }
198 }
199
200 1;
201
202 # vim: et ts=4 sw=4
203