… | |
… | |
43 | |
43 | |
44 | my $http_port = new Coro::Socket |
44 | my $http_port = new Coro::Socket |
45 | LocalAddr => $SERVER_HOST, |
45 | LocalAddr => $SERVER_HOST, |
46 | LocalPort => $SERVER_PORT, |
46 | LocalPort => $SERVER_PORT, |
47 | ReuseAddr => 1, |
47 | ReuseAddr => 1, |
48 | Listen => 1, |
48 | Listen => 50, |
49 | or die "unable to start server"; |
49 | or die "unable to start server"; |
50 | |
50 | |
51 | push @listen_sockets, $http_port; |
51 | push @listen_sockets, $http_port; |
52 | |
52 | |
53 | # the "main thread" |
53 | # the "main thread" |
… | |
… | |
70 | package conn; |
70 | package conn; |
71 | |
71 | |
72 | use Socket; |
72 | use Socket; |
73 | use HTTP::Date; |
73 | use HTTP::Date; |
74 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
|
|
75 | use Linux::AIO; |
|
|
76 | |
|
|
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
|
|
78 | |
|
|
79 | Event->io(fd => Linux::AIO::poll_fileno, |
|
|
80 | poll => 'r', async => 1, |
|
|
81 | cb => \&Linux::AIO::poll_cb ); |
75 | |
82 | |
76 | our %conn; # $conn{ip}{fh} => connobj |
83 | our %conn; # $conn{ip}{fh} => connobj |
77 | our %blocked; |
84 | our %blocked; |
78 | our %mimetype; |
85 | our %mimetype; |
79 | |
86 | |
80 | sub read_mimetypes { |
87 | sub read_mimetypes { |
81 | local *M; |
88 | local *M; |
82 | if (open M, "<mimetypes") { |
89 | if (open M, "<mime_types") { |
83 | while (<M>) { |
90 | while (<M>) { |
84 | if (/^([^#]\S+)\t+(\S+)$/) { |
91 | if (/^([^#]\S+)\t+(\S+)$/) { |
85 | $mimetype{lc $1} = $2; |
92 | $mimetype{lc $1} = $2; |
86 | } |
93 | } |
87 | } |
94 | } |
88 | } else { |
95 | } else { |
89 | $self->slog(1, "cannot open mimetypes\n"); |
96 | print "cannot open mime_types\n"; |
90 | } |
97 | } |
91 | } |
98 | } |
|
|
99 | |
|
|
100 | read_mimetypes; |
92 | |
101 | |
93 | sub new { |
102 | sub new { |
94 | my $class = shift; |
103 | my $class = shift; |
95 | my $peername = shift; |
104 | my $peername = shift; |
96 | my $fh = shift; |
105 | my $fh = shift; |
97 | my $self = bless { fh => $fh }, $class; |
106 | my $self = bless { fh => $fh }, $class; |
98 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
107 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
99 | or $self->err(500, "unable to decode peername"); |
108 | or $self->err(500, "unable to decode peername"); |
100 | |
109 | |
101 | $self->{remote_addr} = inet_ntoa $iaddr; |
110 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
111 | $self->{time} = $::NOW; |
102 | |
112 | |
103 | # enter ourselves into various lists |
113 | # enter ourselves into various lists |
104 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
114 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
105 | |
115 | |
|
|
116 | $::conns++; |
|
|
117 | |
106 | $self; |
118 | $self; |
107 | } |
119 | } |
108 | |
120 | |
109 | sub DESTROY { |
121 | sub DESTROY { |
110 | my $self = shift; |
122 | my $self = shift; |
|
|
123 | |
|
|
124 | $::conns--; |
|
|
125 | |
111 | delete $conn{$self->{remote_addr}}{$self*1}; |
126 | delete $conn{$self->{remote_addr}}{$self*1}; |
112 | delete $uri{$self->{uri}}{$self*1}; |
127 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
113 | } |
128 | } |
114 | |
129 | |
115 | sub slog { |
130 | sub slog { |
116 | my $self = shift; |
131 | my $self = shift; |
117 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
132 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
118 | } |
133 | } |
119 | |
134 | |
120 | sub response { |
135 | sub response { |
121 | my ($self, $code, $msg, $hdr, $content) = @_; |
136 | my ($self, $code, $msg, $hdr, $content) = @_; |
122 | my $res = "HTTP/1.0 $code $msg\015\012"; |
137 | my $res = "HTTP/1.1 $code $msg\015\012"; |
123 | |
138 | |
124 | $res .= "Connection: close\015\012"; |
139 | #$res .= "Connection: close\015\012"; |
125 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
140 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
126 | |
141 | |
127 | while (my ($h, $v) = each %$hdr) { |
142 | while (my ($h, $v) = each %$hdr) { |
128 | $res .= "$h: $v\015\012" |
143 | $res .= "$h: $v\015\012" |
129 | } |
144 | } |
|
|
145 | $res .= "\015\012"; |
130 | |
146 | |
131 | $res .= "\015\012$content" if defined $content; |
147 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
132 | |
148 | |
133 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
149 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
134 | |
150 | |
|
|
151 | $self->{written} += |
135 | print {$self->{fh}} $res; |
152 | print {$self->{fh}} $res; |
136 | } |
153 | } |
137 | |
154 | |
138 | sub err { |
155 | sub err { |
139 | my $self = shift; |
156 | my $self = shift; |
140 | my ($code, $msg, $hdr, $content) = @_; |
157 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
142 | unless (defined $content) { |
159 | unless (defined $content) { |
143 | $content = "$code $msg"; |
160 | $content = "$code $msg"; |
144 | $hdr->{"Content-Type"} = "text/plain"; |
161 | $hdr->{"Content-Type"} = "text/plain"; |
145 | $hdr->{"Content-Length"} = length $content; |
162 | $hdr->{"Content-Length"} = length $content; |
146 | } |
163 | } |
|
|
164 | $hdr->{"Connection"} = "close"; |
147 | |
165 | |
148 | $self->response($code, $msg, $hdr, $content); |
166 | $self->response($code, $msg, $hdr, $content); |
149 | |
167 | |
150 | die bless {}, err::; |
168 | die bless {}, err::; |
151 | } |
169 | } |
152 | |
170 | |
153 | sub err_blocked { |
171 | sub err_blocked { |
154 | my $self = shift; |
172 | my $self = shift; |
155 | my $ip = $self->{remote_addr}; |
173 | my $ip = $self->{remote_addr}; |
156 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
174 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
175 | |
|
|
176 | Coro::Event::do_timer(after => 15); |
|
|
177 | |
157 | $self->err(403, "too many connections", |
178 | $self->err(401, "too many connections", |
158 | { |
179 | { |
159 | "Content-Type" => "text/html", |
180 | "Content-Type" => "text/html", |
160 | "Retry-After" => $::BLOCKTIME |
181 | "Retry-After" => $::BLOCKTIME |
161 | }, |
182 | }, |
162 | <<EOF); |
183 | <<EOF); |
… | |
… | |
174 | |
195 | |
175 | sub handle { |
196 | sub handle { |
176 | my $self = shift; |
197 | my $self = shift; |
177 | my $fh = $self->{fh}; |
198 | my $fh = $self->{fh}; |
178 | |
199 | |
|
|
200 | $fh->timeout($::REQ_TIMEOUT); |
179 | #while() { |
201 | while() { |
|
|
202 | $self->{reqs}++; |
|
|
203 | |
|
|
204 | # read request and parse first line |
|
|
205 | my $req = $fh->readline("\015\012\015\012"); |
|
|
206 | |
|
|
207 | unless (defined $req) { |
|
|
208 | if (exists $self->{version}) { |
|
|
209 | last; |
|
|
210 | } else { |
|
|
211 | $self->err(408, "request timeout"); |
|
|
212 | } |
|
|
213 | } |
|
|
214 | |
180 | $self->{h} = {}; |
215 | $self->{h} = {}; |
181 | |
216 | |
182 | # read request and parse first line |
|
|
183 | $fh->timeout($::REQ_TIMEOUT); |
|
|
184 | my $req = $fh->readline("\015\012\015\012"); |
|
|
185 | $fh->timeout($::RES_TIMEOUT); |
217 | $fh->timeout($::RES_TIMEOUT); |
186 | |
|
|
187 | defined $req or |
|
|
188 | $self->err(408, "request timeout"); |
|
|
189 | |
|
|
190 | my $ip = $self->{remote_addr}; |
218 | my $ip = $self->{remote_addr}; |
191 | |
219 | |
192 | if ($blocked{$ip}) { |
220 | if ($blocked{$ip}) { |
193 | $self->err_blocked($blocked{$ip}) |
221 | $self->err_blocked($blocked{$ip}) |
194 | if $blocked{$ip} > $::NOW; |
222 | if $blocked{$ip} > $::NOW; |
195 | |
223 | |
196 | delete $blocked{$ip}; |
224 | delete $blocked{$ip}; |
197 | } |
225 | } |
198 | |
226 | |
199 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
227 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
200 | $self->slog("blocked ip $ip"); |
228 | $self->slog(2, "blocked ip $ip"); |
201 | $self->err_blocked; |
229 | $self->err_blocked; |
202 | } |
230 | } |
203 | |
231 | |
204 | $req =~ /^(?:\015\012)? |
232 | $req =~ /^(?:\015\012)? |
205 | (GET|HEAD) \040+ |
233 | (GET|HEAD) \040+ |
206 | ([^\040]+) \040+ |
234 | ([^\040]+) \040+ |
207 | HTTP\/([0-9]+\.[0-9]+) |
235 | HTTP\/([0-9]+\.[0-9]+) |
208 | \015\012/gx |
236 | \015\012/gx |
209 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
237 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
210 | |
|
|
211 | $2 ne "1.0" |
|
|
212 | or $self->err(506, "http protocol version not supported"); |
|
|
213 | |
238 | |
214 | $self->{method} = $1; |
239 | $self->{method} = $1; |
215 | $self->{uri} = $2; |
240 | $self->{uri} = $2; |
|
|
241 | $self->{version} = $3; |
|
|
242 | |
|
|
243 | $3 eq "1.0" or $3 eq "1.1" |
|
|
244 | or $self->err(506, "http protocol version $3 not supported"); |
216 | |
245 | |
217 | # parse headers |
246 | # parse headers |
218 | { |
247 | { |
219 | my (%hdr, $h, $v); |
248 | my (%hdr, $h, $v); |
220 | |
249 | |
… | |
… | |
233 | while ($h, $v) = each %hdr; |
262 | while ($h, $v) = each %hdr; |
234 | } |
263 | } |
235 | |
264 | |
236 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
265 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
237 | |
266 | |
238 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
267 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
239 | |
268 | |
240 | $self->map_uri; |
269 | $self->map_uri; |
241 | $self->respond; |
270 | $self->respond; |
|
|
271 | |
|
|
272 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
273 | |
|
|
274 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
275 | $fh->timeout($::PER_TIMEOUT); |
242 | #} |
276 | } |
243 | } |
277 | } |
244 | |
278 | |
245 | # uri => path mapping |
279 | # uri => path mapping |
246 | sub map_uri { |
280 | sub map_uri { |
247 | my $self = shift; |
281 | my $self = shift; |
… | |
… | |
304 | if (chdir $::DOCROOT) { |
338 | if (chdir $::DOCROOT) { |
305 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
339 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
306 | $ENV{HTTP_HOST} = $self->server_host; |
340 | $ENV{HTTP_HOST} = $self->server_host; |
307 | $ENV{HTTP_PORT} = $self->{server_host}; |
341 | $ENV{HTTP_PORT} = $self->{server_host}; |
308 | $ENV{SCRIPT_NAME} = $self->{name}; |
342 | $ENV{SCRIPT_NAME} = $self->{name}; |
309 | exec $::INDEXPROG; |
343 | exec $path; |
310 | } |
344 | } |
311 | Coro::State::_exit(0); |
345 | Coro::State::_exit(0); |
312 | } else { |
346 | } else { |
313 | } |
347 | } |
314 | } |
348 | } |
… | |
… | |
317 | my $self = shift; |
351 | my $self = shift; |
318 | my $path = $self->{path}; |
352 | my $path = $self->{path}; |
319 | |
353 | |
320 | stat $path |
354 | stat $path |
321 | or $self->err(404, "not found"); |
355 | or $self->err(404, "not found"); |
|
|
356 | |
|
|
357 | $self->{stat} = [stat _]; |
322 | |
358 | |
323 | # idiotic netscape sends idiotic headers AGAIN |
359 | # idiotic netscape sends idiotic headers AGAIN |
324 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
360 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
325 | ? str2time $1 : 0; |
361 | ? str2time $1 : 0; |
326 | |
362 | |
… | |
… | |
329 | if ($path !~ /\/$/) { |
365 | if ($path !~ /\/$/) { |
330 | # create a redirect to get the trailing "/" |
366 | # create a redirect to get the trailing "/" |
331 | my $host = $self->server_hostport; |
367 | my $host = $self->server_hostport; |
332 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
368 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
333 | } else { |
369 | } else { |
334 | $ims < (stat _)[9] |
370 | $ims < $self->{stat}[9] |
335 | or $self->err(304, "not modified"); |
371 | or $self->err(304, "not modified"); |
336 | |
372 | |
337 | if ($self->{method} eq "GET") { |
373 | if ($self->{method} eq "GET") { |
338 | if (-r "$path/index.html") { |
374 | if (-r "$path/index.html") { |
339 | $self->{path} .= "/index.html"; |
375 | $self->{path} .= "/index.html"; |
… | |
… | |
351 | } |
387 | } |
352 | } |
388 | } |
353 | |
389 | |
354 | sub handle_dir { |
390 | sub handle_dir { |
355 | my $self = shift; |
391 | my $self = shift; |
356 | $self->_cgi($::INDEXPROG); |
392 | my $idx = $self->diridx; |
|
|
393 | |
|
|
394 | $self->response(200, "ok", |
|
|
395 | { |
|
|
396 | "Content-Type" => "text/html", |
|
|
397 | "Content-Length" => length $idx, |
|
|
398 | }, |
|
|
399 | $idx); |
357 | } |
400 | } |
358 | |
401 | |
359 | sub handle_file { |
402 | sub handle_file { |
360 | my $self = shift; |
403 | my $self = shift; |
361 | my $length = -s _; |
404 | my $length = -s _; |
… | |
… | |
381 | $hdr->{"Content-Range"} = "bytes */$length"; |
424 | $hdr->{"Content-Range"} = "bytes */$length"; |
382 | $self->err(416, "not satisfiable", $hdr); |
425 | $self->err(416, "not satisfiable", $hdr); |
383 | |
426 | |
384 | satisfiable: |
427 | satisfiable: |
385 | # check for segmented downloads |
428 | # check for segmented downloads |
386 | if ($l && $NO_SEGMENTED) { |
429 | if ($l && $::NO_SEGMENTED) { |
387 | if (%{$uri{$self->{uri}}} > 1) { |
430 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
388 | $self->slog("segmented download refused\n"); |
431 | Coro::Event::do_timer(after => 15); |
|
|
432 | |
389 | $self->err(400, "segmented downloads are not allowed"); |
433 | $self->err(400, "segmented downloads are not allowed"); |
390 | } |
434 | } |
391 | } |
435 | } |
392 | |
436 | |
393 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
437 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
404 | $hdr->{"Content-Length"} = $length; |
448 | $hdr->{"Content-Length"} = $length; |
405 | |
449 | |
406 | $self->response(@code, $hdr, ""); |
450 | $self->response(@code, $hdr, ""); |
407 | |
451 | |
408 | if ($self->{method} eq "GET") { |
452 | if ($self->{method} eq "GET") { |
409 | my ($fh, $buf); |
453 | my ($fh, $buf, $r); |
|
|
454 | my $current = $Coro::current; |
410 | open $fh, "<", $self->{path} |
455 | open $fh, "<", $self->{path} |
411 | or die "$self->{path}: late open failure ($!)"; |
456 | or die "$self->{path}: late open failure ($!)"; |
412 | |
457 | |
413 | if ($l) { |
|
|
414 | sysseek $fh, $l, 0 |
|
|
415 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
416 | } |
|
|
417 | |
|
|
418 | $h -= $l - 1; |
458 | $h -= $l - 1; |
419 | |
459 | |
420 | while ($h > 0) { |
460 | while ($h > 0) { |
421 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
461 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
462 | $buf, 0, sub { |
|
|
463 | $r = $_[0]; |
|
|
464 | $current->ready; |
|
|
465 | }); |
|
|
466 | &Coro::schedule; |
|
|
467 | last unless $r; |
422 | $self->{fh}->syswrite($buf) |
468 | my $w = $self->{fh}->syswrite($buf) |
423 | or last; |
469 | or last; |
|
|
470 | $::written += $w; |
|
|
471 | $self->{written} += $w; |
|
|
472 | $l += $r; |
424 | } |
473 | } |
425 | } |
474 | } |
426 | |
475 | |
427 | close $fh; |
476 | close $fh; |
428 | } |
477 | } |