23
22
if (!$initialized) {
24
23
$synch_callback->();
27
26
my $con = $self->accept();
28
27
print STDERR "Accepted a new connection\n" if $log;
29
28
while (my $req = $con->get_request) {
30
my $url_path = $req->url->path;
29
#my $url_path = $req->url->path;
30
my $url_path = $req->url->as_string;
31
31
if ($url_path =~ m{/$}) { # append 'index.html'
32
32
$url_path .= 'index.html';
45
45
if (exists($urls->{$url_path})) {
46
46
print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
47
47
next unless ($req->method eq "HEAD" || $req->method eq "GET");
49
49
my $url_rec = $urls->{$url_path};
50
50
$self->send_response($req, $url_rec, $con);
52
52
print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
53
53
$con->send_error($HTTP::Status::RC_FORBIDDEN);
57
57
print STDERR "Closing connection\n" if $log;
68
68
if (exists $url_rec->{'auth_method'}) {
69
69
($send_content, $code, $msg, $headers) =
70
70
$self->handle_auth($req, $url_rec);
71
} elsif (!$self->verify_request_headers ($req, $url_rec)) {
72
($send_content, $code, $msg, $headers) =
73
('', 400, 'Mismatch on expected headers', {});
72
75
($code, $msg) = @{$url_rec}{'code', 'msg'};
73
76
$headers = $url_rec->{headers};
92
95
print $con $content;
95
if ($req->header("Range")) {
98
if ($req->header("Range") && !$url_rec->{'force_code'}) {
96
99
$req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
97
100
my $content_len = length($content);
98
101
my $start = $1 ? $1 : 0;
99
102
my $end = $2 ? $2 : ($content_len - 1);
100
103
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);
105
$resp->header("Accept-Ranges" => "bytes");
106
$resp->header("Content-Length" => $len);
107
$resp->header("Content-Range"
108
=> "bytes $start-$end/$content_len");
109
$resp->header("Keep-Alive" => "timeout=15, max=100");
110
$resp->header("Connection" => "Keep-Alive");
111
$con->send_basic_header(206,
112
"Partial Content", $resp->protocol);
113
print $con $resp->headers_as_string($CRLF);
115
print $con substr($content, $start, $len);
117
$con->send_basic_header(416, "Range Not Satisfiable",
119
$resp->header("Keep-Alive" => "timeout=15, max=100");
120
$resp->header("Connection" => "Keep-Alive");
112
125
# fill in content
126
$content = $self->_substitute_port($content) if defined $content;
113
127
$resp->content($content);
114
128
print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
133
147
my $authhdr = $req->header('Authorization');
135
149
# Have we sent the challenge yet?
136
unless (defined $url_rec->{auth_challenged}
137
&& $url_rec->{auth_challenged}) {
150
unless ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge}) {
138
151
# Since we haven't challenged yet, we'd better not
139
152
# have received authentication (for our testing purposes).
157
170
$msg = "You didn't send auth after I sent challenge";
171
if ($url_rec->{auth_no_challenge}) {
172
$msg = "--auth-no-challenge but no auth sent."
159
175
my ($sent_method) = ($authhdr =~ /^(\S+)/g);
160
176
unless ($sent_method eq $url_rec->{'auth_method'}) {
216
sub verify_request_headers {
217
my ($self, $req, $url_rec) = @_;
219
return 1 unless exists $url_rec->{'request_headers'};
220
for my $hdrname (keys %{$url_rec->{'request_headers'}}) {
221
my $rhdr = $req->header ($hdrname);
222
my $ehdr = $url_rec->{'request_headers'}{$hdrname};
223
unless (defined $rhdr && $rhdr =~ $ehdr) {
224
$rhdr = '' unless defined $rhdr;
225
print STDERR "\n*** Mismatch on $hdrname: $rhdr =~ $ehdr\n";
233
sub _substitute_port {
236
$ret =~ s/{{port}}/$self->sockport/eg;
202
242
# vim: et ts=4 sw=4