… | |
… | |
3 | use Coro::Event; |
3 | use Coro::Event; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | use Coro::Signal; |
5 | use Coro::Signal; |
6 | |
6 | |
7 | use HTTP::Date; |
7 | use HTTP::Date; |
|
|
8 | use POSIX (); |
8 | |
9 | |
9 | no utf8; |
10 | no utf8; |
10 | use bytes; |
11 | use bytes; |
11 | |
12 | |
12 | # at least on my machine, this thingy serves files |
13 | # at least on my machine, this thingy serves files |
… | |
… | |
183 | |
184 | |
184 | sub response { |
185 | sub response { |
185 | my ($self, $code, $msg, $hdr, $content) = @_; |
186 | my ($self, $code, $msg, $hdr, $content) = @_; |
186 | my $res = "HTTP/1.1 $code $msg\015\012"; |
187 | my $res = "HTTP/1.1 $code $msg\015\012"; |
187 | |
188 | |
|
|
189 | if (exists $hdr->{Connection}) { |
|
|
190 | if ($hdr->{Connection} =~ /close/) { |
188 | $self->{h}{connection} = "close" |
191 | $self->{h}{connection} = "close" |
189 | if exists $hdr->{Connection} # to avoid "empty" header lines due to vivification |
192 | } |
190 | and $hdr->{Connection} =~ /close/; |
193 | } else { |
|
|
194 | if ($self->{version} < 1.1) { |
|
|
195 | if ($self->{h}{connection} =~ /keep-alive/i) { |
|
|
196 | $hdr->{Connection} = "Keep-Alive"; |
|
|
197 | } else { |
|
|
198 | $self->{h}{connection} = "close" |
|
|
199 | } |
|
|
200 | } |
|
|
201 | } |
191 | |
202 | |
192 | $res .= "Date: $HTTP_NOW\015\012"; |
203 | $res .= "Date: $HTTP_NOW\015\012"; |
193 | |
204 | |
194 | while (my ($h, $v) = each %$hdr) { |
205 | while (my ($h, $v) = each %$hdr) { |
195 | $res .= "$h: $v\015\012" |
206 | $res .= "$h: $v\015\012" |
196 | } |
207 | } |
197 | $res .= "\015\012"; |
208 | $res .= "\015\012"; |
198 | |
209 | |
199 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
210 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
200 | |
211 | |
|
|
212 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
201 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
213 | " $self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
202 | |
214 | |
203 | print $accesslog $log if $accesslog; |
215 | print $accesslog $log if $accesslog; |
204 | print STDERR $log; |
216 | print STDERR $log; |
205 | |
217 | |
206 | $self->{written} += |
218 | $self->{written} += |
… | |
… | |
340 | |
352 | |
341 | $self->eoconn; |
353 | $self->eoconn; |
342 | |
354 | |
343 | die if $@ && !ref $@; |
355 | die if $@ && !ref $@; |
344 | |
356 | |
345 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
357 | last if $self->{h}{connection} =~ /close/; |
346 | |
358 | |
347 | $httpevent->broadcast; |
359 | $httpevent->broadcast; |
348 | |
360 | |
349 | $fh->timeout($::PER_TIMEOUT); |
361 | $fh->timeout($::PER_TIMEOUT); |
350 | } |
362 | } |
… | |
… | |
445 | |
457 | |
446 | $self->response(200, "ok", |
458 | $self->response(200, "ok", |
447 | { |
459 | { |
448 | "Content-Type" => "text/html", |
460 | "Content-Type" => "text/html", |
449 | "Content-Length" => length $idx, |
461 | "Content-Length" => length $idx, |
|
|
462 | "Last-Modified" => time2str ((stat _)[9]), |
450 | }, |
463 | }, |
451 | $idx); |
464 | $idx); |
452 | } |
465 | } |
453 | |
466 | |
454 | sub handle_file { |
467 | sub handle_file { |
… | |
… | |
484 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
497 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
485 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
498 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
486 | if ($delay <= $::NOW) { |
499 | if ($delay <= $::NOW) { |
487 | $self->err_segmented_download; |
500 | $self->err_segmented_download; |
488 | } else { |
501 | } else { |
489 | $httpevent->broadcast; |
502 | $httpevent->wait; |
490 | } |
503 | } |
491 | } |
504 | } |
492 | } |
505 | } |
493 | |
506 | |
494 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
507 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |