11 our @ISA=qw(HTTP::Daemon);
14 my $CRLF = "\015\012"; # "\r\n" is not portable
18 my ($self, $urls, $synch_callback) = @_;
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';
33 #if ($url_path =~ m{^/}) { # remove trailing '/'
34 # $url_path = substr ($url_path, 1);
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";
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");
48 my $url_rec = $urls->{$url_path};
49 $self->send_response($req, $url_rec, $con);
51 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
52 $con->send_error($HTTP::Status::RC_FORBIDDEN);
56 print STDERR "Closing connection\n" if $log;
62 my ($self, $req, $url_rec, $con) = @_;
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);
71 ($code, $msg) = @{$url_rec}{'code', 'msg'};
72 $headers = $url_rec->{headers};
74 my $resp = HTTP::Response->new ($code, $msg);
75 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
77 while (my ($name, $value) = each %{$headers}) {
78 # print STDERR "setting header: $name = $value\n";
79 $resp->header($name => $value);
81 print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
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);
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);
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);
111 print $con substr($content, $start, $len);
113 $con->send_basic_header(416, "Range Not Satisfiable",
115 $resp->header("Keep-Alive" => "timeout=15, max=100");
116 $resp->header("Connection" => "Keep-Alive");
122 $content = $self->_substitute_port($content);
123 $resp->content($content);
124 print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
127 $con->send_response($resp);
128 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
131 # Generates appropriate response content based on the authentication
134 my ($self, $req, $url_rec) = @_;
135 my ($send_content, $code, $msg, $headers);
136 # Catch failure to set code, msg:
138 $msg = "Didn't set response code in handle_auth";
139 # Most cases, we don't want to send content.
143 my $authhdr = $req->header('Authorization');
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).
151 $msg = "You sent auth before I sent challenge";
155 $msg = "Authorization Required";
156 $headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
157 . " realm=\"wget-test\"";
158 $url_rec->{auth_challenged} = 1;
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
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."
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;