]> sjero.net Git - wget/blob - tests/HTTPServer.pm
Allow proxied-https-auth test to function when building outside of source dir.
[wget] / tests / HTTPServer.pm
1 package HTTPServer;
2
3 use strict;
4 use warnings;
5
6 use HTTP::Daemon;
7 use HTTP::Status;
8 use HTTP::Headers;
9 use HTTP::Response;
10
11 our @ISA=qw(HTTP::Daemon);
12 my $VERSION = 0.01;
13
14 my $CRLF = "\015\012"; # "\r\n" is not portable
15 my $log = undef;
16
17 sub run {
18     my ($self, $urls, $synch_callback) = @_;
19     my $initialized = 0;
20
21     while (1) {
22         if (!$initialized) {
23             $synch_callback->();
24             $initialized = 1;
25         }
26         my $con = $self->accept();
27         print STDERR "Accepted a new connection\n" if $log;
28         while (my $req = $con->get_request) {
29             my $url_path = $req->url->path;
30             if ($url_path =~ m{/$}) { # append 'index.html'
31                 $url_path .= 'index.html';
32             }
33             #if ($url_path =~ m{^/}) { # remove trailing '/'
34             #    $url_path = substr ($url_path, 1);
35             #}
36             if ($log) {
37                 print STDERR "Method: ", $req->method, "\n";
38                 print STDERR "Path: ", $url_path, "\n";
39                 print STDERR "Available URLs: ", "\n";
40                 foreach my $key (keys %$urls) {
41                     print STDERR $key, "\n";
42                 }
43             }
44             if (exists($urls->{$url_path})) {
45                 print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
46                 next unless ($req->method eq "HEAD" || $req->method eq "GET");
47
48                 my $url_rec = $urls->{$url_path};
49                 $self->send_response($req, $url_rec, $con);
50             } else {
51                 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
52                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
53                 last;
54             }
55         }
56         print STDERR "Closing connection\n" if $log;
57         $con->close;
58     }
59 }
60
61 sub send_response {
62     my ($self, $req, $url_rec, $con) = @_;
63
64     # create response
65     my ($code, $msg, $headers);
66     my $send_content = ($req->method eq "GET");
67     if (exists $url_rec->{'auth_method'}) {
68         ($send_content, $code, $msg, $headers) =
69             $self->handle_auth($req, $url_rec);
70     } else {
71         ($code, $msg) = @{$url_rec}{'code', 'msg'};
72         $headers = $url_rec->{headers};
73     }
74     my $resp = HTTP::Response->new ($code, $msg);
75     print STDERR "HTTP::Response: \n", $resp->as_string if $log;
76
77     while (my ($name, $value) = each %{$headers}) {
78         # print STDERR "setting header: $name = $value\n";
79         $resp->header($name => $value);
80     }
81     print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
82
83     if ($send_content) {
84         my $content = $url_rec->{content};
85         if (exists($url_rec->{headers}{"Content-Length"})) {
86             # Content-Length and length($content) don't match
87             # manually prepare the HTTP response
88             $con->send_basic_header($url_rec->{code}, $resp->message, $resp->protocol);
89             print $con $resp->headers_as_string($CRLF);
90             print $con $CRLF;
91             print $con $content;
92             next;
93         }
94         if ($req->header("Range") && !$url_rec->{'force_code'}) {
95             $req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
96             my $content_len = length($content);
97             my $start = $1 ? $1 : 0;
98             my $end = $2 ? $2 : ($content_len - 1);
99             my $len = $2 ? ($2 - $start) : ($content_len - $start);
100             if ($len > 0) {
101                 $resp->header("Accept-Ranges" => "bytes");
102                 $resp->header("Content-Length" => $len);
103                 $resp->header("Content-Range"
104                     => "bytes $start-$end/$content_len");
105                 $resp->header("Keep-Alive" => "timeout=15, max=100");
106                 $resp->header("Connection" => "Keep-Alive");
107                 $con->send_basic_header(206,
108                     "Partial Content", $resp->protocol);
109                 print $con $resp->headers_as_string($CRLF);
110                 print $con $CRLF;
111                 print $con substr($content, $start, $len);
112             } else {
113                 $con->send_basic_header(416, "Range Not Satisfiable",
114                     $resp->protocol);
115                 $resp->header("Keep-Alive" => "timeout=15, max=100");
116                 $resp->header("Connection" => "Keep-Alive");
117                 print $con $CRLF;
118             }
119             next;
120         }
121         # fill in content
122         $content = $self->_substitute_port($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 ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge}) {
147         # Since we haven't challenged yet, we'd better not
148         # have received authentication (for our testing purposes).
149         if ($authhdr) {
150             $code = 400;
151             $msg  = "You sent auth before I sent challenge";
152         } else {
153             # Send challenge
154             $code = 401;
155             $msg  = "Authorization Required";
156             $headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
157                 . " realm=\"wget-test\"";
158             $url_rec->{auth_challenged} = 1;
159         }
160     } elsif (!defined($authhdr)) {
161         # We've sent the challenge; we should have received valid
162         # authentication with this one. A normal server would just
163         # resend the challenge; but since this is a test, wget just
164         # failed it.
165         $code = 400;
166         $msg  = "You didn't send auth after I sent challenge";
167         if ($url_rec->{auth_no_challenge}) {
168             $msg = "--auth-no-challenge but no auth sent."
169         }
170     } else {
171         my ($sent_method) = ($authhdr =~ /^(\S+)/g);
172         unless ($sent_method eq $url_rec->{'auth_method'}) {
173             # Not the authorization type we were expecting.
174             $code = 400;
175             $msg = "Expected auth type $url_rec->{'auth_method'} but got "
176                 . "$sent_method";
177         } elsif (($sent_method eq 'Digest'
178                   && &verify_auth_digest($authhdr, $url_rec, \$msg))
179                  ||
180                  ($sent_method eq 'Basic'
181                   && &verify_auth_basic($authhdr, $url_rec, \$msg))) {
182             # SUCCESSFUL AUTH: send expected message, headers, content.
183             ($code, $msg) = @{$url_rec}{'code', 'msg'};
184             $headers = $url_rec->{headers};
185             $send_content = 1;
186         } else {
187             $code = 400;
188         }
189     }
190
191     return ($send_content, $code, $msg, $headers);
192 }
193
194 sub verify_auth_digest {
195     return undef; # Not yet implemented.
196 }
197
198 sub verify_auth_basic {
199     require MIME::Base64;
200     my ($authhdr, $url_rec, $msgref) = @_;
201     my $expected = MIME::Base64::encode_base64($url_rec->{'user'} . ':'
202         . $url_rec->{'passwd'}, '');
203     my ($got) = $authhdr =~ /^Basic (.*)$/;
204     if ($got eq $expected) {
205         return 1;
206     } else {
207         $$msgref = "Wanted ${expected} got ${got}";
208         return undef;
209     }
210 }
211
212 sub _substitute_port {
213     my $self = shift;
214     my $ret = shift;
215     $ret =~ s/{{port}}/$self->sockport/eg;
216     return $ret;
217 }
218
219 1;
220
221 # vim: et ts=4 sw=4
222