| 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 |
{ |
| 19 |
my ($self, $urls, $synch_callback) = @_; |
| 20 |
my $initialized = 0; |
| 21 |
|
| 22 |
while (1) |
| 23 |
{ |
| 24 |
if (!$initialized) |
| 25 |
{ |
| 26 |
$synch_callback->(); |
| 27 |
$initialized = 1; |
| 28 |
} |
| 29 |
my $con = $self->accept(); |
| 30 |
print STDERR "Accepted a new connection\n" if $log; |
| 31 |
while (my $req = $con->get_request) |
| 32 |
{ |
| 33 |
#my $url_path = $req->url->path; |
| 34 |
my $url_path = $req->url->as_string; |
| 35 |
if ($url_path =~ m{/$}) |
| 36 |
{ # append 'index.html' |
| 37 |
$url_path .= 'index.html'; |
| 38 |
} |
| 39 |
|
| 40 |
#if ($url_path =~ m{^/}) { # remove trailing '/' |
| 41 |
# $url_path = substr ($url_path, 1); |
| 42 |
#} |
| 43 |
if ($log) |
| 44 |
{ |
| 45 |
print STDERR "Method: ", $req->method, "\n"; |
| 46 |
print STDERR "Path: ", $url_path, "\n"; |
| 47 |
print STDERR "Available URLs: ", "\n"; |
| 48 |
foreach my $key (keys %$urls) |
| 49 |
{ |
| 50 |
print STDERR $key, "\n"; |
| 51 |
} |
| 52 |
} |
| 53 |
if (exists($urls->{$url_path})) |
| 54 |
{ |
| 55 |
print STDERR "Serving requested URL: ", $url_path, "\n" if $log; |
| 56 |
next unless ($req->method eq "HEAD" || $req->method eq "GET"); |
| 57 |
|
| 58 |
my $url_rec = $urls->{$url_path}; |
| 59 |
$self->send_response($req, $url_rec, $con); |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
print STDERR "Requested wrong URL: ", $url_path, "\n" if $log; |
| 64 |
$con->send_error($HTTP::Status::RC_FORBIDDEN); |
| 65 |
last; |
| 66 |
} |
| 67 |
} |
| 68 |
print STDERR "Closing connection\n" if $log; |
| 69 |
$con->close; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
sub send_response |
| 74 |
{ |
| 75 |
my ($self, $req, $url_rec, $con) = @_; |
| 76 |
|
| 77 |
# create response |
| 78 |
my ($code, $msg, $headers); |
| 79 |
my $send_content = ($req->method eq "GET"); |
| 80 |
if (exists $url_rec->{'auth_method'}) |
| 81 |
{ |
| 82 |
($send_content, $code, $msg, $headers) = |
| 83 |
$self->handle_auth($req, $url_rec); |
| 84 |
} |
| 85 |
elsif (!$self->verify_request_headers($req, $url_rec)) |
| 86 |
{ |
| 87 |
($send_content, $code, $msg, $headers) = |
| 88 |
('', 400, 'Mismatch on expected headers', {}); |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
($code, $msg) = @{$url_rec}{'code', 'msg'}; |
| 93 |
$headers = $url_rec->{headers}; |
| 94 |
} |
| 95 |
my $resp = HTTP::Response->new($code, $msg); |
| 96 |
print STDERR "HTTP::Response: \n", $resp->as_string if $log; |
| 97 |
|
| 98 |
while (my ($name, $value) = each %{$headers}) |
| 99 |
{ |
| 100 |
# print STDERR "setting header: $name = $value\n"; |
| 101 |
$value = $self->_substitute_port($value); |
| 102 |
$resp->header($name => $value); |
| 103 |
} |
| 104 |
print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log; |
| 105 |
|
| 106 |
if ($send_content) |
| 107 |
{ |
| 108 |
my $content = $url_rec->{content}; |
| 109 |
if (exists($url_rec->{headers}{"Content-Length"})) |
| 110 |
{ |
| 111 |
# Content-Length and length($content) don't match |
| 112 |
# manually prepare the HTTP response |
| 113 |
$con->send_basic_header($url_rec->{code}, $resp->message, |
| 114 |
$resp->protocol); |
| 115 |
print $con $resp->headers_as_string($CRLF); |
| 116 |
print $con $CRLF; |
| 117 |
print $con $content; |
| 118 |
next; |
| 119 |
} |
| 120 |
if ($req->header("Range") && !$url_rec->{'force_code'}) |
| 121 |
{ |
| 122 |
$req->header("Range") =~ m/bytes=(\d*)-(\d*)/; |
| 123 |
my $content_len = length($content); |
| 124 |
my $start = $1 ? $1 : 0; |
| 125 |
my $end = $2 ? $2 : ($content_len - 1); |
| 126 |
my $len = $2 ? ($2 - $start) : ($content_len - $start); |
| 127 |
if ($len > 0) |
| 128 |
{ |
| 129 |
$resp->header("Accept-Ranges" => "bytes"); |
| 130 |
$resp->header("Content-Length" => $len); |
| 131 |
$resp->header( |
| 132 |
"Content-Range" => "bytes $start-$end/$content_len"); |
| 133 |
$resp->header("Keep-Alive" => "timeout=15, max=100"); |
| 134 |
$resp->header("Connection" => "Keep-Alive"); |
| 135 |
$con->send_basic_header(206, |
| 136 |
"Partial Content", $resp->protocol); |
| 137 |
print $con $resp->headers_as_string($CRLF); |
| 138 |
print $con $CRLF; |
| 139 |
print $con substr($content, $start, $len); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
$con->send_basic_header(416, "Range Not Satisfiable", |
| 144 |
$resp->protocol); |
| 145 |
$resp->header("Keep-Alive" => "timeout=15, max=100"); |
| 146 |
$resp->header("Connection" => "Keep-Alive"); |
| 147 |
print $con $CRLF; |
| 148 |
} |
| 149 |
next; |
| 150 |
} |
| 151 |
|
| 152 |
# fill in content |
| 153 |
$content = $self->_substitute_port($content) if defined $content; |
| 154 |
$resp->content($content); |
| 155 |
print STDERR "HTTP::Response with content: \n", $resp->as_string |
| 156 |
if $log; |
| 157 |
} |
| 158 |
|
| 159 |
$con->send_response($resp); |
| 160 |
print STDERR "HTTP::Response sent: \n", $resp->as_string if $log; |
| 161 |
} |
| 162 |
|
| 163 |
# Generates appropriate response content based on the authentication |
| 164 |
# status of the URL. |
| 165 |
sub handle_auth |
| 166 |
{ |
| 167 |
my ($self, $req, $url_rec) = @_; |
| 168 |
my ($send_content, $code, $msg, $headers); |
| 169 |
|
| 170 |
# Catch failure to set code, msg: |
| 171 |
$code = 500; |
| 172 |
$msg = "Didn't set response code in handle_auth"; |
| 173 |
|
| 174 |
# Most cases, we don't want to send content. |
| 175 |
$send_content = 0; |
| 176 |
|
| 177 |
# Initialize headers |
| 178 |
$headers = {}; |
| 179 |
my $authhdr = $req->header('Authorization'); |
| 180 |
|
| 181 |
# Have we sent the challenge yet? |
| 182 |
unless ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge}) |
| 183 |
{ |
| 184 |
# Since we haven't challenged yet, we'd better not |
| 185 |
# have received authentication (for our testing purposes). |
| 186 |
if ($authhdr) |
| 187 |
{ |
| 188 |
$code = 400; |
| 189 |
$msg = "You sent auth before I sent challenge"; |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
# Send challenge |
| 194 |
$code = 401; |
| 195 |
$msg = "Authorization Required"; |
| 196 |
$headers->{'WWW-Authenticate'} = |
| 197 |
$url_rec->{'auth_method'} . " realm=\"wget-test\""; |
| 198 |
$url_rec->{auth_challenged} = 1; |
| 199 |
} |
| 200 |
} |
| 201 |
elsif (!defined($authhdr)) |
| 202 |
{ |
| 203 |
# We've sent the challenge; we should have received valid |
| 204 |
# authentication with this one. A normal server would just |
| 205 |
# resend the challenge; but since this is a test, wget just |
| 206 |
# failed it. |
| 207 |
$code = 400; |
| 208 |
$msg = "You didn't send auth after I sent challenge"; |
| 209 |
if ($url_rec->{auth_no_challenge}) |
| 210 |
{ |
| 211 |
$msg = "--auth-no-challenge but no auth sent."; |
| 212 |
} |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
my ($sent_method) = ($authhdr =~ /^(\S+)/g); |
| 217 |
unless ($sent_method eq $url_rec->{'auth_method'}) |
| 218 |
{ |
| 219 |
# Not the authorization type we were expecting. |
| 220 |
$code = 400; |
| 221 |
$msg = "Expected auth type $url_rec->{'auth_method'} but got " |
| 222 |
. "$sent_method"; |
| 223 |
} |
| 224 |
elsif ( |
| 225 |
( |
| 226 |
$sent_method eq 'Digest' |
| 227 |
&& &verify_auth_digest($authhdr, $url_rec, \$msg) |
| 228 |
) |
| 229 |
|| ( $sent_method eq 'Basic' |
| 230 |
&& &verify_auth_basic($authhdr, $url_rec, \$msg)) |
| 231 |
) |
| 232 |
{ |
| 233 |
# SUCCESSFUL AUTH: send expected message, headers, content. |
| 234 |
($code, $msg) = @{$url_rec}{'code', 'msg'}; |
| 235 |
$headers = $url_rec->{headers}; |
| 236 |
$send_content = 1; |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
$code = 400; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return ($send_content, $code, $msg, $headers); |
| 245 |
} |
| 246 |
|
| 247 |
sub verify_auth_digest |
| 248 |
{ |
| 249 |
return undef; # Not yet implemented. |
| 250 |
} |
| 251 |
|
| 252 |
sub verify_auth_basic |
| 253 |
{ |
| 254 |
require MIME::Base64; |
| 255 |
my ($authhdr, $url_rec, $msgref) = @_; |
| 256 |
my $expected = |
| 257 |
MIME::Base64::encode_base64( |
| 258 |
$url_rec->{'user'} . ':' . $url_rec->{'passwd'}, |
| 259 |
''); |
| 260 |
my ($got) = $authhdr =~ /^Basic (.*)$/; |
| 261 |
if ($got eq $expected) |
| 262 |
{ |
| 263 |
return 1; |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
$$msgref = "Wanted ${expected} got ${got}"; |
| 268 |
return undef; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
sub verify_request_headers |
| 273 |
{ |
| 274 |
my ($self, $req, $url_rec) = @_; |
| 275 |
|
| 276 |
return 1 unless exists $url_rec->{'request_headers'}; |
| 277 |
for my $hdrname (keys %{$url_rec->{'request_headers'}}) |
| 278 |
{ |
| 279 |
my $must_not_match; |
| 280 |
my $ehdr = $url_rec->{'request_headers'}{$hdrname}; |
| 281 |
if ($must_not_match = ($hdrname =~ /^!(\w+)/)) |
| 282 |
{ |
| 283 |
$hdrname = $1; |
| 284 |
} |
| 285 |
my $rhdr = $req->header($hdrname); |
| 286 |
if ($must_not_match) |
| 287 |
{ |
| 288 |
if (defined $rhdr && $rhdr =~ $ehdr) |
| 289 |
{ |
| 290 |
$rhdr = '' unless defined $rhdr; |
| 291 |
print STDERR "\n*** Match forbidden $hdrname: $rhdr =~ $ehdr\n"; |
| 292 |
return undef; |
| 293 |
} |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
unless (defined $rhdr && $rhdr =~ $ehdr) |
| 298 |
{ |
| 299 |
$rhdr = '' unless defined $rhdr; |
| 300 |
print STDERR "\n*** Mismatch on $hdrname: $rhdr =~ $ehdr\n"; |
| 301 |
return undef; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
return 1; |
| 307 |
} |
| 308 |
|
| 309 |
sub _substitute_port |
| 310 |
{ |
| 311 |
my $self = shift; |
| 312 |
my $ret = shift; |
| 313 |
$ret =~ s/\Q{{port}}/$self->sockport/eg; |
| 314 |
return $ret; |
| 315 |
} |
| 316 |
|
| 317 |
1; |
| 318 |
|
| 319 |
# vim: et ts=4 sw=4 |