… | |
… | |
9 | # at least on my machine, this thingy serves files |
9 | # at least on my machine, this thingy serves files |
10 | # quite a bit faster than apache, ;) |
10 | # quite a bit faster than apache, ;) |
11 | # and quite a bit slower than thttpd :( |
11 | # and quite a bit slower than thttpd :( |
12 | |
12 | |
13 | $SIG{PIPE} = 'IGNORE'; |
13 | $SIG{PIPE} = 'IGNORE'; |
14 | |
14 | |
|
|
15 | our $accesslog; |
|
|
16 | |
|
|
17 | if ($ACCESS_LOG) { |
|
|
18 | use IO::Handle; |
|
|
19 | open $accesslog, ">>$ACCESS_LOG" |
|
|
20 | or die "$ACCESS_LOG: $!"; |
|
|
21 | $accesslog->autoflush(1); |
|
|
22 | } |
|
|
23 | |
15 | sub slog { |
24 | sub slog { |
16 | my $level = shift; |
25 | my $level = shift; |
17 | my $format = shift; |
26 | my $format = shift; |
18 | printf "---: $format\n", @_; |
27 | printf "---: $format\n", @_; |
19 | } |
28 | } |
… | |
… | |
43 | |
52 | |
44 | my $http_port = new Coro::Socket |
53 | my $http_port = new Coro::Socket |
45 | LocalAddr => $SERVER_HOST, |
54 | LocalAddr => $SERVER_HOST, |
46 | LocalPort => $SERVER_PORT, |
55 | LocalPort => $SERVER_PORT, |
47 | ReuseAddr => 1, |
56 | ReuseAddr => 1, |
48 | Listen => 1, |
57 | Listen => 50, |
49 | or die "unable to start server"; |
58 | or die "unable to start server"; |
50 | |
59 | |
51 | push @listen_sockets, $http_port; |
60 | push @listen_sockets, $http_port; |
52 | |
61 | |
53 | # the "main thread" |
62 | # the "main thread" |
… | |
… | |
70 | package conn; |
79 | package conn; |
71 | |
80 | |
72 | use Socket; |
81 | use Socket; |
73 | use HTTP::Date; |
82 | use HTTP::Date; |
74 | use Convert::Scalar 'weaken'; |
83 | use Convert::Scalar 'weaken'; |
|
|
84 | use Linux::AIO; |
75 | |
85 | |
|
|
86 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
|
|
87 | |
|
|
88 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
89 | |
|
|
90 | Event->io(fd => Linux::AIO::poll_fileno, |
|
|
91 | poll => 'r', async => 1, |
|
|
92 | cb => \&Linux::AIO::poll_cb); |
|
|
93 | |
76 | our %conn; # $conn{ip}{fh} => connobj |
94 | our %conn; # $conn{ip}{self} => connobj |
|
|
95 | our %uri; # $uri{ip}{uri}{self} |
77 | our %blocked; |
96 | our %blocked; |
78 | our %mimetype; |
97 | our %mimetype; |
79 | |
98 | |
80 | sub read_mimetypes { |
99 | sub read_mimetypes { |
81 | local *M; |
100 | local *M; |
… | |
… | |
104 | $self->{time} = $::NOW; |
123 | $self->{time} = $::NOW; |
105 | |
124 | |
106 | # enter ourselves into various lists |
125 | # enter ourselves into various lists |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
126 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
108 | |
127 | |
|
|
128 | $::conns++; |
|
|
129 | |
109 | $self; |
130 | $self; |
110 | } |
131 | } |
111 | |
132 | |
112 | sub DESTROY { |
133 | sub DESTROY { |
113 | my $self = shift; |
134 | my $self = shift; |
|
|
135 | |
|
|
136 | $::conns--; |
|
|
137 | |
|
|
138 | $self->eoconn; |
114 | delete $conn{$self->{remote_addr}}{$self*1}; |
139 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
140 | } |
|
|
141 | |
|
|
142 | # end of connection |
|
|
143 | sub eoconn { |
|
|
144 | my $self = shift; |
115 | delete $uri{$self->{uri}}{$self*1}; |
145 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
116 | } |
146 | } |
117 | |
147 | |
118 | sub slog { |
148 | sub slog { |
119 | my $self = shift; |
149 | my $self = shift; |
120 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
150 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
121 | } |
151 | } |
122 | |
152 | |
123 | sub response { |
153 | sub response { |
124 | my ($self, $code, $msg, $hdr, $content) = @_; |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
125 | my $res = "HTTP/1.0 $code $msg\015\012"; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
126 | |
156 | |
127 | $res .= "Connection: close\015\012"; |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
|
|
158 | |
128 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
129 | |
160 | |
130 | while (my ($h, $v) = each %$hdr) { |
161 | while (my ($h, $v) = each %$hdr) { |
131 | $res .= "$h: $v\015\012" |
162 | $res .= "$h: $v\015\012" |
132 | } |
163 | } |
133 | $res .= "\015\012"; |
164 | $res .= "\015\012"; |
134 | |
165 | |
135 | $res .= $content if defined $content and $self->{method} eq "GET"; |
166 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
136 | |
167 | |
137 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
168 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
|
|
169 | |
|
|
170 | print $accesslog $log if $accesslog; |
|
|
171 | print STDERR $log; |
138 | |
172 | |
139 | $self->{written} += |
173 | $self->{written} += |
140 | print {$self->{fh}} $res; |
174 | print {$self->{fh}} $res; |
141 | } |
175 | } |
142 | |
176 | |
… | |
… | |
147 | unless (defined $content) { |
181 | unless (defined $content) { |
148 | $content = "$code $msg"; |
182 | $content = "$code $msg"; |
149 | $hdr->{"Content-Type"} = "text/plain"; |
183 | $hdr->{"Content-Type"} = "text/plain"; |
150 | $hdr->{"Content-Length"} = length $content; |
184 | $hdr->{"Content-Length"} = length $content; |
151 | } |
185 | } |
|
|
186 | $hdr->{"Connection"} = "close"; |
152 | |
187 | |
153 | $self->response($code, $msg, $hdr, $content); |
188 | $self->response($code, $msg, $hdr, $content); |
154 | |
189 | |
155 | die bless {}, err::; |
190 | die bless {}, err::; |
156 | } |
191 | } |
… | |
… | |
158 | sub err_blocked { |
193 | sub err_blocked { |
159 | my $self = shift; |
194 | my $self = shift; |
160 | my $ip = $self->{remote_addr}; |
195 | my $ip = $self->{remote_addr}; |
161 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
196 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
162 | |
197 | |
163 | Coro::Event::do_timer(after => 5); |
198 | Coro::Event::do_timer(after => 20*rand); |
164 | |
199 | |
165 | $self->err(403, "too many connections", |
200 | $self->err(401, "too many connections", |
166 | { |
201 | { |
167 | "Content-Type" => "text/html", |
202 | "Content-Type" => "text/html", |
168 | "Retry-After" => $::BLOCKTIME |
203 | "Retry-After" => $::BLOCKTIME, |
|
|
204 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
205 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
|
|
206 | "Connection" => "close", |
169 | }, |
207 | }, |
170 | <<EOF); |
208 | <<EOF); |
171 | <html><p> |
209 | <html> |
|
|
210 | <head> |
|
|
211 | <title>Too many connections</title> |
|
|
212 | </head> |
|
|
213 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
|
|
214 | |
172 | You have been blocked because you opened too many connections. You |
215 | <p>You have been blocked because you opened too many connections. You |
173 | may retry at</p> |
216 | may retry at</p> |
174 | |
217 | |
175 | <p><blockquote>$time.</blockquote></p> |
218 | <p><blockquote>$time.</blockquote></p> |
176 | |
219 | |
177 | <p>Until then, each new access will renew the block. You might want to have a |
220 | <p>Until then, each new access will renew the block. You might want to have a |
178 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
221 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html#connectionlimit">FAQ</a>.</p> |
179 | </html> |
222 | |
|
|
223 | </body></html> |
180 | EOF |
224 | EOF |
181 | } |
225 | } |
182 | |
226 | |
183 | sub handle { |
227 | sub handle { |
184 | my $self = shift; |
228 | my $self = shift; |
185 | my $fh = $self->{fh}; |
229 | my $fh = $self->{fh}; |
186 | |
230 | |
|
|
231 | my $host; |
|
|
232 | |
|
|
233 | $fh->timeout($::REQ_TIMEOUT); |
187 | #while() { |
234 | while() { |
|
|
235 | $self->{reqs}++; |
|
|
236 | |
|
|
237 | # read request and parse first line |
|
|
238 | my $req = $fh->readline("\015\012\015\012"); |
|
|
239 | |
|
|
240 | unless (defined $req) { |
|
|
241 | if (exists $self->{version}) { |
|
|
242 | last; |
|
|
243 | } else { |
|
|
244 | $self->err(408, "request timeout"); |
|
|
245 | } |
|
|
246 | } |
|
|
247 | |
188 | $self->{h} = {}; |
248 | $self->{h} = {}; |
189 | |
249 | |
190 | # read request and parse first line |
|
|
191 | $fh->timeout($::REQ_TIMEOUT); |
|
|
192 | my $req = $fh->readline("\015\012\015\012"); |
|
|
193 | $fh->timeout($::RES_TIMEOUT); |
250 | $fh->timeout($::RES_TIMEOUT); |
194 | |
|
|
195 | defined $req or |
|
|
196 | $self->err(408, "request timeout"); |
|
|
197 | |
|
|
198 | my $ip = $self->{remote_addr}; |
251 | my $ip = $self->{remote_addr}; |
199 | |
252 | |
200 | if ($blocked{$ip}) { |
253 | if ($blocked{$ip}) { |
201 | $self->err_blocked($blocked{$ip}) |
254 | $self->err_blocked($blocked{$ip}) |
202 | if $blocked{$ip} > $::NOW; |
255 | if $blocked{$ip} > $::NOW; |
… | |
… | |
212 | $req =~ /^(?:\015\012)? |
265 | $req =~ /^(?:\015\012)? |
213 | (GET|HEAD) \040+ |
266 | (GET|HEAD) \040+ |
214 | ([^\040]+) \040+ |
267 | ([^\040]+) \040+ |
215 | HTTP\/([0-9]+\.[0-9]+) |
268 | HTTP\/([0-9]+\.[0-9]+) |
216 | \015\012/gx |
269 | \015\012/gx |
217 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
270 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
218 | |
|
|
219 | $2 ne "1.0" |
|
|
220 | or $self->err(506, "http protocol version not supported"); |
|
|
221 | |
271 | |
222 | $self->{method} = $1; |
272 | $self->{method} = $1; |
223 | $self->{uri} = $2; |
273 | $self->{uri} = $2; |
|
|
274 | $self->{version} = $3; |
|
|
275 | |
|
|
276 | $3 =~ /^1\./ |
|
|
277 | or $self->err(506, "http protocol version $3 not supported"); |
224 | |
278 | |
225 | # parse headers |
279 | # parse headers |
226 | { |
280 | { |
227 | my (%hdr, $h, $v); |
281 | my (%hdr, $h, $v); |
228 | |
282 | |
… | |
… | |
239 | |
293 | |
240 | $self->{h}{$h} = substr $v, 1 |
294 | $self->{h}{$h} = substr $v, 1 |
241 | while ($h, $v) = each %hdr; |
295 | while ($h, $v) = each %hdr; |
242 | } |
296 | } |
243 | |
297 | |
|
|
298 | # find out server name and port |
|
|
299 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
|
|
300 | $host = $1; |
|
|
301 | } else { |
|
|
302 | $host = $self->{h}{host}; |
|
|
303 | } |
|
|
304 | |
|
|
305 | if (defined $host) { |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
306 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
|
|
307 | } else { |
|
|
308 | ($self->{server_port}, $host) |
|
|
309 | = unpack_sockaddr_in $self->{fh}->getsockname |
|
|
310 | or $self->err(500, "unable to get socket name"); |
|
|
311 | $host = inet_ntoa $host; |
|
|
312 | } |
245 | |
313 | |
|
|
314 | $self->{server_name} = $host; |
|
|
315 | |
|
|
316 | # remote id should be unique per user |
|
|
317 | $self->{remote_id} = $self->{remote_addr}; |
|
|
318 | |
|
|
319 | if (exists $self->{h}{"client-ip"}) { |
|
|
320 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
321 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
322 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
323 | } |
|
|
324 | |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
325 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
247 | |
326 | |
|
|
327 | eval { |
248 | $self->map_uri; |
328 | $self->map_uri; |
249 | $self->respond; |
329 | $self->respond; |
|
|
330 | }; |
|
|
331 | |
|
|
332 | $self->eoconn; |
|
|
333 | |
|
|
334 | die if $@ && !ref $@; |
|
|
335 | |
|
|
336 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
|
|
337 | |
|
|
338 | $fh->timeout($::PER_TIMEOUT); |
250 | #} |
339 | } |
251 | } |
340 | } |
252 | |
341 | |
253 | # uri => path mapping |
342 | # uri => path mapping |
254 | sub map_uri { |
343 | sub map_uri { |
255 | my $self = shift; |
344 | my $self = shift; |
256 | my $host = $self->{h}{host} || "default"; |
345 | my $host = $self->{server_name}; |
257 | my $uri = $self->{uri}; |
346 | my $uri = $self->{uri}; |
258 | |
347 | |
259 | # some massaging, also makes it more secure |
348 | # some massaging, also makes it more secure |
260 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
349 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
261 | $uri =~ s%//+%/%g; |
350 | $uri =~ s%//+%/%g; |
… | |
… | |
271 | $self->{path} = "$::DOCROOT/$host$uri"; |
360 | $self->{path} = "$::DOCROOT/$host$uri"; |
272 | |
361 | |
273 | $self->access_check; |
362 | $self->access_check; |
274 | } |
363 | } |
275 | |
364 | |
276 | sub server_address { |
|
|
277 | my $self = shift; |
|
|
278 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
|
|
279 | or $self->err(500, "unable to get socket name"); |
|
|
280 | ((inet_ntoa $iaddr), $port); |
|
|
281 | } |
|
|
282 | |
|
|
283 | sub server_host { |
|
|
284 | my $self = shift; |
|
|
285 | if (exists $self->{h}{host}) { |
|
|
286 | return $self->{h}{host}; |
|
|
287 | } else { |
|
|
288 | return (($self->server_address)[0]); |
|
|
289 | } |
|
|
290 | } |
|
|
291 | |
|
|
292 | sub server_hostport { |
|
|
293 | my $self = shift; |
|
|
294 | my ($host, $port); |
|
|
295 | if (exists $self->{h}{host}) { |
|
|
296 | ($host, $port) = ($self->{h}{host}, $self->{server_port}); |
|
|
297 | } else { |
|
|
298 | ($host, $port) = $self->server_address; |
|
|
299 | } |
|
|
300 | $port = $port == 80 ? "" : ":$port"; |
|
|
301 | $host.$port; |
|
|
302 | } |
|
|
303 | |
|
|
304 | sub _cgi { |
365 | sub _cgi { |
305 | my $self = shift; |
366 | my $self = shift; |
306 | my $path = shift; |
367 | my $path = shift; |
307 | my $fh; |
368 | my $fh; |
308 | |
369 | |
309 | # no two-way xxx supported |
370 | # no two-way xxx supported |
310 | if (0 == fork) { |
371 | if (0 == fork) { |
311 | open STDOUT, ">&".fileno($self->{fh}); |
372 | open STDOUT, ">&".fileno($self->{fh}); |
312 | if (chdir $::DOCROOT) { |
373 | if (chdir $::DOCROOT) { |
313 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
374 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
314 | $ENV{HTTP_HOST} = $self->server_host; |
375 | $ENV{HTTP_HOST} = $self->{server_name}; |
315 | $ENV{HTTP_PORT} = $self->{server_host}; |
376 | $ENV{HTTP_PORT} = $self->{server_port}; |
316 | $ENV{SCRIPT_NAME} = $self->{name}; |
377 | $ENV{SCRIPT_NAME} = $self->{name}; |
317 | exec $path; |
378 | exec $path; |
318 | } |
379 | } |
319 | Coro::State::_exit(0); |
380 | Coro::State::_exit(0); |
320 | } else { |
381 | } else { |
|
|
382 | die; |
321 | } |
383 | } |
|
|
384 | } |
|
|
385 | |
|
|
386 | sub server_hostport { |
|
|
387 | $_[0]{server_port} == 80 |
|
|
388 | ? $_[0]{server_name} |
|
|
389 | : "$_[0]{server_name}:$_[0]{server_port}"; |
322 | } |
390 | } |
323 | |
391 | |
324 | sub respond { |
392 | sub respond { |
325 | my $self = shift; |
393 | my $self = shift; |
326 | my $path = $self->{path}; |
394 | my $path = $self->{path}; |
… | |
… | |
336 | |
404 | |
337 | if (-d _ && -r _) { |
405 | if (-d _ && -r _) { |
338 | # directory |
406 | # directory |
339 | if ($path !~ /\/$/) { |
407 | if ($path !~ /\/$/) { |
340 | # create a redirect to get the trailing "/" |
408 | # create a redirect to get the trailing "/" |
341 | my $host = $self->server_hostport; |
409 | # we don't try to avoid the :80 |
342 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
410 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
343 | } else { |
411 | } else { |
344 | $ims < $self->{stat}[9] |
412 | $ims < $self->{stat}[9] |
345 | or $self->err(304, "not modified"); |
413 | or $self->err(304, "not modified"); |
346 | |
414 | |
347 | if ($self->{method} eq "GET") { |
|
|
348 | if (-r "$path/index.html") { |
415 | if (-r "$path/index.html") { |
349 | $self->{path} .= "/index.html"; |
416 | $self->{path} .= "/index.html"; |
350 | $self->handle_file; |
417 | $self->handle_file; |
351 | } else { |
418 | } else { |
352 | $self->handle_dir; |
419 | $self->handle_dir; |
353 | } |
|
|
354 | } |
420 | } |
355 | } |
421 | } |
356 | } elsif (-f _ && -r _) { |
422 | } elsif (-f _ && -r _) { |
357 | -x _ and $self->err(403, "forbidden"); |
423 | -x _ and $self->err(403, "forbidden"); |
358 | $self->handle_file; |
424 | $self->handle_file; |
… | |
… | |
391 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
457 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
392 | } else { |
458 | } else { |
393 | ($l, $h) = (0, $length - 1); |
459 | ($l, $h) = (0, $length - 1); |
394 | goto ignore; |
460 | goto ignore; |
395 | } |
461 | } |
396 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
462 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
397 | } |
463 | } |
398 | $hdr->{"Content-Range"} = "bytes */$length"; |
464 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
465 | $hdr->{"Content-Length"} = $length; |
|
|
466 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
399 | $self->err(416, "not satisfiable", $hdr); |
467 | $self->err(416, "not satisfiable", $hdr, ""); |
400 | |
468 | |
401 | satisfiable: |
469 | satisfiable: |
402 | # check for segmented downloads |
470 | # check for segmented downloads |
403 | if ($l && $::NO_SEGMENTED) { |
471 | if ($l && $::NO_SEGMENTED) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
472 | my $delay = 60; |
|
|
473 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
|
|
474 | if ($delay <= 0) { |
405 | $self->err(400, "segmented downloads are not allowed"); |
475 | $self->err(400, "segmented downloads are not allowed", |
|
|
476 | { "Content-Type" => "text/html", Connection => "close" }, <<EOF); |
|
|
477 | <html> |
|
|
478 | <head> |
|
|
479 | <title>Segmented downloads are not allowed</title> |
|
|
480 | </head> |
|
|
481 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
|
|
482 | |
|
|
483 | <p>Segmented downloads are not allowed on this server. Please refer to the |
|
|
484 | <a href="http://www.goof.com/pcg/marc/animefaq.html#segmented_downloads">FAQ</a>.</p> |
|
|
485 | |
|
|
486 | </body></html> |
|
|
487 | EOF |
|
|
488 | } else { |
|
|
489 | Coro::Event::do_timer(after => 3); $delay -= 3; |
|
|
490 | } |
406 | } |
491 | } |
407 | } |
492 | } |
408 | |
493 | |
409 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
494 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
410 | @code = (206, "partial content"); |
495 | @code = (206, "partial content"); |
… | |
… | |
420 | $hdr->{"Content-Length"} = $length; |
505 | $hdr->{"Content-Length"} = $length; |
421 | |
506 | |
422 | $self->response(@code, $hdr, ""); |
507 | $self->response(@code, $hdr, ""); |
423 | |
508 | |
424 | if ($self->{method} eq "GET") { |
509 | if ($self->{method} eq "GET") { |
425 | my ($fh, $buf); |
510 | my ($fh, $buf, $r); |
|
|
511 | my $current = $Coro::current; |
426 | open $fh, "<", $self->{path} |
512 | open $fh, "<", $self->{path} |
427 | or die "$self->{path}: late open failure ($!)"; |
513 | or die "$self->{path}: late open failure ($!)"; |
428 | |
514 | |
429 | if ($l) { |
|
|
430 | sysseek $fh, $l, 0 |
|
|
431 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
432 | } |
|
|
433 | |
|
|
434 | $h -= $l - 1; |
515 | $h -= $l - 1; |
435 | |
516 | |
|
|
517 | if (0) { |
|
|
518 | if ($l) { |
|
|
519 | sysseek $fh, $l, 0; |
|
|
520 | } |
|
|
521 | } |
|
|
522 | |
436 | while ($h > 0) { |
523 | while ($h > 0) { |
|
|
524 | if (0) { |
437 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
525 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
526 | or last; |
|
|
527 | } else { |
|
|
528 | undef $buf; |
|
|
529 | $aio_requests->down; |
|
|
530 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
531 | $buf, 0, sub { |
|
|
532 | $r = $_[0]; |
|
|
533 | $current->ready; |
|
|
534 | }); |
|
|
535 | &Coro::schedule; |
|
|
536 | $aio_requests->up; |
|
|
537 | last unless $r; |
|
|
538 | } |
438 | my $w = $self->{fh}->syswrite($buf) |
539 | my $w = $self->{fh}->syswrite($buf) |
439 | or last; |
540 | or last; |
440 | $::written += $w; |
541 | $::written += $w; |
441 | $self->{written} += $w; |
542 | $self->{written} += $w; |
|
|
543 | $l += $r; |
442 | } |
544 | } |
443 | } |
545 | } |
444 | |
546 | |
445 | close $fh; |
547 | close $fh; |
446 | } |
548 | } |