12 our @ISA=qw(HTTP::Daemon);
15 my $CRLF = "\015\012"; # "\r\n" is not portable
19 my ($self, $urls, $synch_callback) = @_;
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';
35 #if ($url_path =~ m{^/}) { # remove trailing '/'
36 # $url_path = substr ($url_path, 1);
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";
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");
50 my $url_rec = $urls->{$url_path};
51 $self->send_response($req, $url_rec, $con);
53 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
54 $con->send_error($HTTP::Status::RC_FORBIDDEN);
58 print STDERR "Closing connection\n" if $log;
64 my ($self, $req, $url_rec, $con) = @_;
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);
73 ($code, $msg) = @{$url_rec}{'code', 'msg'};
74 $headers = $url_rec->{headers};
76 my $resp = HTTP::Response->new ($code, $msg);
77 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
79 while (my ($name, $value) = each %{$headers}) {
80 # print STDERR "setting header: $name = $value\n";
81 $resp->header($name => $value);
83 print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
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);
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);
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);
113 print $con substr($content, $start, $len);
115 $con->send_basic_header(416, "Range Not Satisfiable",
117 $resp->header("Keep-Alive" => "timeout=15, max=100");
118 $resp->header("Connection" => "Keep-Alive");
124 $content = $self->_substitute_port($content);
125 $resp->content($content);
126 print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
129 $con->send_response($resp);
130 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
133 # Generates appropriate response content based on the authentication
136 my ($self, $req, $url_rec) = @_;
137 my ($send_content, $code, $msg, $headers);
138 # Catch failure to set code, msg:
140 $msg = "Didn't set response code in handle_auth";
141 # Most cases, we don't want to send content.
145 my $authhdr = $req->header('Authorization');
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).
154 $msg = "You sent auth before I sent challenge";
158 $msg = "Authorization Required";
159 $headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
160 . " realm=\"wget-test\"";
161 $url_rec->{auth_challenged} = 1;
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
169 $msg = "You didn't send auth after I sent challenge";
171 my ($sent_method) = ($authhdr =~ /^(\S+)/g);
172 unless ($sent_method eq $url_rec->{'auth_method'}) {
173 # Not the authorization type we were expecting.
175 $msg = "Expected auth type $url_rec->{'auth_method'} but got "
177 } elsif (($sent_method eq 'Digest'
178 && &verify_auth_digest($authhdr, $url_rec, \$msg))
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};
191 return ($send_content, $code, $msg, $headers);
194 sub verify_auth_digest {
195 return undef; # Not yet implemented.
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) {
207 $$msgref = "Wanted ${expected} got ${got}";
212 sub _substitute_port {
215 $ret =~ s/{{port}}/$self->sockport/eg;