… | |
… | |
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 => 256*1024*1024; |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
56 | our $queue_index = new transferqueue slots => 10; |
58 | our $queue_index = new transferqueue 10; |
57 | |
59 | |
58 | my @newcons; |
60 | my @newcons; |
59 | my @pool; |
61 | my @pool; |
60 | |
62 | |
61 | # one "execution thread" |
63 | # one "execution thread" |
… | |
… | |
185 | } |
187 | } |
186 | |
188 | |
187 | sub response { |
189 | sub response { |
188 | my ($self, $code, $msg, $hdr, $content) = @_; |
190 | my ($self, $code, $msg, $hdr, $content) = @_; |
189 | my $res = "HTTP/1.1 $code $msg\015\012"; |
191 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
192 | my $GZ = ""; |
190 | |
193 | |
191 | if (exists $hdr->{Connection}) { |
194 | if (exists $hdr->{Connection}) { |
192 | if ($hdr->{Connection} =~ /close/) { |
195 | if ($hdr->{Connection} =~ /close/) { |
193 | $self->{h}{connection} = "close" |
196 | $self->{h}{connection} = "close" |
194 | } |
197 | } |
… | |
… | |
200 | $self->{h}{connection} = "close" |
203 | $self->{h}{connection} = "close" |
201 | } |
204 | } |
202 | } |
205 | } |
203 | } |
206 | } |
204 | |
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 | |
205 | $res .= "Date: $HTTP_NOW\015\012"; |
220 | $res .= "Date: $HTTP_NOW\015\012"; |
206 | |
221 | |
207 | while (my ($h, $v) = each %$hdr) { |
222 | while (my ($h, $v) = each %$hdr) { |
208 | $res .= "$h: $v\015\012" |
223 | $res .= "$h: $v\015\012" |
209 | } |
224 | } |
210 | $res .= "\015\012"; |
225 | $res .= "\015\012"; |
211 | |
226 | |
212 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
227 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
213 | |
228 | |
214 | 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). |
215 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
230 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
216 | " \"$self->{h}{referer}\"\n"; |
231 | " \"$self->{h}{referer}\"\n"; |
217 | |
232 | |
218 | print $accesslog $log if $accesslog; |
233 | print $accesslog $log if $accesslog; |
219 | print STDERR $log; |
234 | print STDERR $log; |
220 | |
235 | |
… | |
… | |
282 | my (%hdr, $h, $v); |
297 | my (%hdr, $h, $v); |
283 | |
298 | |
284 | $hdr{lc $1} .= ",$2" |
299 | $hdr{lc $1} .= ",$2" |
285 | while $req =~ /\G |
300 | while $req =~ /\G |
286 | ([^:\000-\040]+): |
301 | ([^:\000-\040]+): |
287 | [\008\040]* |
302 | [\010\040]* |
288 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
303 | ((?: [^\015\012]+ | \015\012[\010\040] )*) |
289 | \015\012 |
304 | \015\012 |
290 | /gxc; |
305 | /gxc; |
291 | |
306 | |
292 | $req =~ /\G\015\012$/ |
307 | $req =~ /\G\015\012$/ |
293 | or $self->err(400, "bad request"); |
308 | or $self->err(400, "bad request"); |