]> sjero.net Git - wget/blob - tests/HTTPServer.pm
IDN test.
[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             my $url_path = $req->url->as_string;
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                 my $url_rec = $urls->{$url_path};
51                 $self->send_response($req, $url_rec, $con);
52             } else {
53                 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
54                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
55                 last;
56             }            
57         }
58         print STDERR "Closing connection\n" if $log;
59         $con->close;
60     }
61 }
62
63 sub send_response {
64     my ($self, $req, $url_rec, $con) = @_;
65
66     # create response
67     my ($code, $msg, $headers);
68     my $send_content = ($req->method eq "GET");
69     if (exists $url_rec->{'auth_method'}) {
70         ($send_content, $code, $msg, $headers) =
71             $self->handle_auth($req, $url_rec);
72     } else {
73         ($code, $msg) = @{$url_rec}{'code', 'msg'};
74         $headers = $url_rec->{headers};
75     }
76     my $resp = HTTP::Response->new ($code, $msg);
77     print STDERR "HTTP::Response: \n", $resp->as_string if $log;
78
79     while (my ($name, $value) = each %{$headers}) {
80         # print STDERR "setting header: $name = $value\n";
81         $resp->header($name => $value);
82     }
83     print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
84
85     if ($send_content) {
86         my $content = $url_rec->{content};
87         if (exists($url_rec->{headers}{"Content-Length"})) {
88             # Content-Length and length($content) don't match
89             # manually prepare the HTTP response
90             $con->send_basic_header($url_rec->{code}, $resp->message, $resp->protocol);
91             print $con $resp->headers_as_string($CRLF);
92             print $con $CRLF;
93             print $con $content;
94             next;
95         }
96         if ($req->header("Range") && !$url_rec->{'force_code'}) {
97             $req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
98             my $content_len = length($content);
99             my $start = $1 ? $1 : 0;
100             my $end = $2 ? $2 : ($content_len - 1);
101             my $len = $2 ? ($2 - $start) : ($content_len - $start);
102             if ($len > 0) {
103                 $resp->header("Accept-Ranges" => "bytes");
104                 $resp->header("Content-Length" => $len);
105                 $resp->header("Content-Range"
106                     => "bytes $start-$end/$content_len");
107                 $resp->header("Keep-Alive" => "timeout=15, max=100");
108                 $resp->header("Connection" => "Keep-Alive");
109                 $con->send_basic_header(206,
110                     "Partial Content", $resp->protocol);
111                 print $con $resp->headers_as_string($CRLF);
112                 print $con $CRLF;
113                 print $con substr($content, $start, $len);
114             } else {
115                 $con->send_basic_header(416, "Range Not Satisfiable",
116                     $resp->protocol);
117                 $resp->header("Keep-Alive" => "timeout=15, max=100");
118                 $resp->header("Connection" => "Keep-Alive");
119                 print $con $CRLF;
120             }
121             next;
122         }
123         # fill in content
124         $content = $self->_substitute_port($content);
125         $resp->content($content);
126         print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
127     }
128
129     $con->send_response($resp);
130     print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
131 }
132
133 # Generates appropriate response content based on the authentication
134 # status of the URL.
135 sub handle_auth {
136     my ($self, $req, $url_rec) = @_;
137     my ($send_content, $code, $msg, $headers);
138     # Catch failure to set code, msg:
139     $code = 500;
140     $msg  = "Didn't set response code in handle_auth";
141     # Most cases, we don't want to send content.
142     $send_content = 0;
143     # Initialize headers
144     $headers = {};
145     my $authhdr = $req->header('Authorization');
146
147     # Have we sent the challenge yet?
148     unless (defined $url_rec->{auth_challenged}
149         && $url_rec->{auth_challenged}) {
150         # Since we haven't challenged yet, we'd better not
151         # have received authentication (for our testing purposes).
152         if ($authhdr) {
153             $code = 400;
154             $msg  = "You sent auth before I sent challenge";
155         } else {
156             # Send challenge
157             $code = 401;
158             $msg  = "Authorization Required";
159             $headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
160                 . " realm=\"wget-test\"";
161             $url_rec->{auth_challenged} = 1;
162         }
163     } elsif (!defined($authhdr)) {
164         # We've sent the challenge; we should have received valid
165         # authentication with this one. A normal server would just
166         # resend the challenge; but since this is a test, wget just
167         # failed it.
168         $code = 400;
169         $msg  = "You didn't send auth after I sent challenge";
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