… | |
… | |
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 | use POSIX (); |
|
|
9 | |
|
|
10 | use Compress::Zlib (); |
9 | |
11 | |
10 | no utf8; |
12 | no utf8; |
11 | use bytes; |
13 | use bytes; |
12 | |
14 | |
13 | # at least on my machine, this thingy serves files |
15 | # at least on my machine, this thingy serves files |
… | |
… | |
50 | } |
52 | } |
51 | |
53 | |
52 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
54 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
53 | our $httpevent = new Coro::Signal; |
55 | our $httpevent = new Coro::Signal; |
54 | |
56 | |
55 | our $queue_file = new transferqueue slots => $MAX_TRANSFERS, maxsize => 250_000_000; |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
56 | our $queue_index = new transferqueue slots => 10; |
58 | our $queue_index = new transferqueue 10; |
57 | |
|
|
58 | our $requests; |
|
|
59 | |
59 | |
60 | my @newcons; |
60 | my @newcons; |
61 | my @pool; |
61 | my @pool; |
62 | |
62 | |
63 | # one "execution thread" |
63 | # one "execution thread" |
… | |
… | |
187 | } |
187 | } |
188 | |
188 | |
189 | sub response { |
189 | sub response { |
190 | my ($self, $code, $msg, $hdr, $content) = @_; |
190 | my ($self, $code, $msg, $hdr, $content) = @_; |
191 | my $res = "HTTP/1.1 $code $msg\015\012"; |
191 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
192 | my $GZ = ""; |
192 | |
193 | |
193 | if (exists $hdr->{Connection}) { |
194 | if (exists $hdr->{Connection}) { |
194 | if ($hdr->{Connection} =~ /close/) { |
195 | if ($hdr->{Connection} =~ /close/) { |
195 | $self->{h}{connection} = "close" |
196 | $self->{h}{connection} = "close" |
196 | } |
197 | } |
… | |
… | |
202 | $self->{h}{connection} = "close" |
203 | $self->{h}{connection} = "close" |
203 | } |
204 | } |
204 | } |
205 | } |
205 | } |
206 | } |
206 | |
207 | |
|
|
208 | if ($self->{method} ne "HEAD" |
|
|
209 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
|
|
210 | && $hdr->{"Content-Length"} == length $content |
|
|
211 | && !exists $hdr->{"Content-Encoding"} |
|
|
212 | ) { |
|
|
213 | my $orig = length $content; |
|
|
214 | $hdr->{"Content-Encoding"} = "gzip"; |
|
|
215 | $content = Compress::Zlib::memGzip(\$content); |
|
|
216 | $hdr->{"Content-Length"} = length $content; |
|
|
217 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
|
|
218 | } |
|
|
219 | |
207 | $res .= "Date: $HTTP_NOW\015\012"; |
220 | $res .= "Date: $HTTP_NOW\015\012"; |
208 | |
221 | |
209 | while (my ($h, $v) = each %$hdr) { |
222 | while (my ($h, $v) = each %$hdr) { |
210 | $res .= "$h: $v\015\012" |
223 | $res .= "$h: $v\015\012" |
211 | } |
224 | } |
212 | $res .= "\015\012"; |
225 | $res .= "\015\012"; |
213 | |
226 | |
214 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
227 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
215 | |
228 | |
216 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
229 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
217 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
230 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
218 | " \"$self->{h}{referer}\"\n"; |
231 | " \"$self->{h}{referer}\"\n"; |
219 | |
232 | |
220 | print $accesslog $log if $accesslog; |
233 | print $accesslog $log if $accesslog; |
221 | print STDERR $log; |
234 | print STDERR $log; |
222 | |
235 | |
… | |
… | |
284 | my (%hdr, $h, $v); |
297 | my (%hdr, $h, $v); |
285 | |
298 | |
286 | $hdr{lc $1} .= ",$2" |
299 | $hdr{lc $1} .= ",$2" |
287 | while $req =~ /\G |
300 | while $req =~ /\G |
288 | ([^:\000-\040]+): |
301 | ([^:\000-\040]+): |
289 | [\008\040]* |
302 | [\010\040]* |
290 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
303 | ((?: [^\015\012]+ | \015\012[\010\040] )*) |
291 | \015\012 |
304 | \015\012 |
292 | /gxc; |
305 | /gxc; |
293 | |
306 | |
294 | $req =~ /\G\015\012$/ |
307 | $req =~ /\G\015\012$/ |
295 | or $self->err(400, "bad request"); |
308 | or $self->err(400, "bad request"); |
296 | |
309 | |
297 | $self->{h}{$h} = substr $v, 1 |
310 | $self->{h}{$h} = substr $v, 1 |
298 | while ($h, $v) = each %hdr; |
311 | while ($h, $v) = each %hdr; |
299 | } |
312 | } |
300 | |
|
|
301 | $requests++; |
|
|
302 | |
313 | |
303 | # remote id should be unique per user |
314 | # remote id should be unique per user |
304 | my $id = $self->{remote_addr}; |
315 | my $id = $self->{remote_addr}; |
305 | |
316 | |
306 | if (exists $self->{h}{"client-ip"}) { |
317 | if (exists $self->{h}{"client-ip"}) { |