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 if ($url_path =~ m{/$}) { # append 'index.html'
32 $url_path .= 'index.html';
34 #if ($url_path =~ m{^/}) { # remove trailing '/'
35 # $url_path = substr ($url_path, 1);
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";
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");
49 my $url_rec = $urls->{$url_path};
50 $self->send_response($req, $url_rec, $con);
52 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
53 $con->send_error($HTTP::Status::RC_FORBIDDEN);
57 print STDERR "Closing connection\n" if $log;
63 my ($self, $req, $url_rec, $con) = @_;
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);
72 ($code, $msg) = @{$url_rec}{'code', 'msg'};
73 $headers = $url_rec->{headers};
75 my $resp = HTTP::Response->new ($code, $msg);
76 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
78 while (my ($name, $value) = each %{$headers}) {
79 # print STDERR "setting header: $name = $value\n";
80 $resp->header($name => $value);
82 print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
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);
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);
109 print $con substr($content, $start, $len);
113 $resp->content($content);
114 print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
117 $con->send_response($resp);
118 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
121 # Generates appropriate response content based on the authentication
124 my ($self, $req, $url_rec) = @_;
125 my ($send_content, $code, $msg, $headers);
126 # Catch failure to set code, msg:
128 $msg = "Didn't set response code in handle_auth";
129 # Most cases, we don't want to send content.
133 my $authhdr = $req->header('Authorization');
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).
142 $msg = "You sent auth before I sent challenge";
146 $msg = "Authorization Required";
147 $headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
148 . " realm=\"wget-test\"";
149 $url_rec->{auth_challenged} = 1;
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
157 $msg = "You didn't send auth after I sent challenge";
159 my ($sent_method) = ($authhdr =~ /^(\S+)/g);
160 unless ($sent_method eq $url_rec->{'auth_method'}) {
161 # Not the authorization type we were expecting.
163 $msg = "Expected auth type $url_rec->{'auth_method'} but got "
165 } elsif (($sent_method eq 'Digest'
166 && &verify_auth_digest($authhdr, $url_rec, \$msg))
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};
179 return ($send_content, $code, $msg, $headers);
182 sub verify_auth_digest {
183 return undef; # Not yet implemented.
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) {
195 $$msgref = "Wanted ${expected} got ${got}";