… | |
… | |
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" |
… | |
… | |
99 | my $self = bless { fh => $fh }, $class; |
99 | my $self = bless { fh => $fh }, $class; |
100 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
100 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
101 | or $self->err(500, "unable to decode peername"); |
101 | or $self->err(500, "unable to decode peername"); |
102 | |
102 | |
103 | $self->{remote_addr} = inet_ntoa $iaddr; |
103 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
104 | $self->{time} = $::NOW; |
104 | |
105 | |
105 | # enter ourselves into various lists |
106 | # enter ourselves into various lists |
106 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | |
108 | |
|
|
109 | $::conns++; |
|
|
110 | |
108 | $self; |
111 | $self; |
109 | } |
112 | } |
110 | |
113 | |
111 | sub DESTROY { |
114 | sub DESTROY { |
112 | my $self = shift; |
115 | my $self = shift; |
|
|
116 | |
|
|
117 | $::conns--; |
|
|
118 | |
113 | delete $conn{$self->{remote_addr}}{$self*1}; |
119 | delete $conn{$self->{remote_addr}}{$self*1}; |
114 | delete $uri{$self->{uri}}{$self*1}; |
120 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
115 | } |
121 | } |
116 | |
122 | |
117 | sub slog { |
123 | sub slog { |
118 | my $self = shift; |
124 | my $self = shift; |
119 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
125 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
… | |
… | |
129 | while (my ($h, $v) = each %$hdr) { |
135 | while (my ($h, $v) = each %$hdr) { |
130 | $res .= "$h: $v\015\012" |
136 | $res .= "$h: $v\015\012" |
131 | } |
137 | } |
132 | $res .= "\015\012"; |
138 | $res .= "\015\012"; |
133 | |
139 | |
134 | $res .= $content if defined $content and $self->{method} eq "GET"; |
140 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
135 | |
141 | |
136 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
142 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
137 | |
143 | |
|
|
144 | $self->{written} += |
138 | print {$self->{fh}} $res; |
145 | print {$self->{fh}} $res; |
139 | } |
146 | } |
140 | |
147 | |
141 | sub err { |
148 | sub err { |
142 | my $self = shift; |
149 | my $self = shift; |
143 | my ($code, $msg, $hdr, $content) = @_; |
150 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
156 | sub err_blocked { |
163 | sub err_blocked { |
157 | my $self = shift; |
164 | my $self = shift; |
158 | my $ip = $self->{remote_addr}; |
165 | my $ip = $self->{remote_addr}; |
159 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
166 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
160 | |
167 | |
161 | Coro::Event::do_timer(after => 5); |
168 | Coro::Event::do_timer(after => 15); |
162 | |
169 | |
163 | $self->err(403, "too many connections", |
170 | $self->err(401, "too many connections", |
164 | { |
171 | { |
165 | "Content-Type" => "text/html", |
172 | "Content-Type" => "text/html", |
166 | "Retry-After" => $::BLOCKTIME |
173 | "Retry-After" => $::BLOCKTIME |
167 | }, |
174 | }, |
168 | <<EOF); |
175 | <<EOF); |
… | |
… | |
193 | defined $req or |
200 | defined $req or |
194 | $self->err(408, "request timeout"); |
201 | $self->err(408, "request timeout"); |
195 | |
202 | |
196 | my $ip = $self->{remote_addr}; |
203 | my $ip = $self->{remote_addr}; |
197 | |
204 | |
|
|
205 | $self->err_blocked($blocked{$ip}); |
198 | if ($blocked{$ip}) { |
206 | if ($blocked{$ip}) { |
199 | $self->err_blocked($blocked{$ip}) |
207 | $self->err_blocked($blocked{$ip}) |
200 | if $blocked{$ip} > $::NOW; |
208 | if $blocked{$ip} > $::NOW; |
201 | |
209 | |
202 | delete $blocked{$ip}; |
210 | delete $blocked{$ip}; |
203 | } |
211 | } |
204 | |
212 | |
205 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
213 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
206 | $self->slog("blocked ip $ip"); |
214 | $self->slog(2, "blocked ip $ip"); |
207 | $self->err_blocked; |
215 | $self->err_blocked; |
208 | } |
216 | } |
209 | |
217 | |
210 | $req =~ /^(?:\015\012)? |
218 | $req =~ /^(?:\015\012)? |
211 | (GET|HEAD) \040+ |
219 | (GET|HEAD) \040+ |
212 | ([^\040]+) \040+ |
220 | ([^\040]+) \040+ |
213 | HTTP\/([0-9]+\.[0-9]+) |
221 | HTTP\/([0-9]+\.[0-9]+) |
214 | \015\012/gx |
222 | \015\012/gx |
215 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
223 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
216 | |
224 | |
217 | $2 ne "1.0" |
225 | $2 ne "1.0" |
218 | or $self->err(506, "http protocol version not supported"); |
226 | or $self->err(506, "http protocol version not supported"); |
219 | |
227 | |
220 | $self->{method} = $1; |
228 | $self->{method} = $1; |
… | |
… | |
239 | while ($h, $v) = each %hdr; |
247 | while ($h, $v) = each %hdr; |
240 | } |
248 | } |
241 | |
249 | |
242 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
250 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
243 | |
251 | |
244 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
252 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
245 | |
253 | |
246 | $self->map_uri; |
254 | $self->map_uri; |
247 | $self->respond; |
255 | $self->respond; |
248 | #} |
256 | #} |
249 | } |
257 | } |
… | |
… | |
397 | $self->err(416, "not satisfiable", $hdr); |
405 | $self->err(416, "not satisfiable", $hdr); |
398 | |
406 | |
399 | satisfiable: |
407 | satisfiable: |
400 | # check for segmented downloads |
408 | # check for segmented downloads |
401 | if ($l && $::NO_SEGMENTED) { |
409 | if ($l && $::NO_SEGMENTED) { |
402 | if (%{$uri{$self->{uri}}} > 1) { |
410 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
403 | $self->slog("segmented download refused\n"); |
411 | Coro::Event::do_timer(after => 15); |
|
|
412 | |
404 | $self->err(400, "segmented downloads are not allowed"); |
413 | $self->err(400, "segmented downloads are not allowed"); |
405 | } |
414 | } |
406 | } |
415 | } |
407 | |
416 | |
408 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
417 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
432 | |
441 | |
433 | $h -= $l - 1; |
442 | $h -= $l - 1; |
434 | |
443 | |
435 | while ($h > 0) { |
444 | while ($h > 0) { |
436 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
445 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
437 | $self->{fh}->syswrite($buf) |
446 | my $w = $self->{fh}->syswrite($buf) |
438 | or last; |
447 | or last; |
|
|
448 | $::written += $w; |
|
|
449 | $self->{written} += $w; |
439 | } |
450 | } |
440 | } |
451 | } |
441 | |
452 | |
442 | close $fh; |
453 | close $fh; |
443 | } |
454 | } |