… | |
… | |
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" |
… | |
… | |
104 | $self->{time} = $::NOW; |
104 | $self->{time} = $::NOW; |
105 | |
105 | |
106 | # enter ourselves into various lists |
106 | # enter ourselves into various lists |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
108 | |
108 | |
|
|
109 | $::conns++; |
|
|
110 | |
109 | $self; |
111 | $self; |
110 | } |
112 | } |
111 | |
113 | |
112 | sub DESTROY { |
114 | sub DESTROY { |
113 | my $self = shift; |
115 | my $self = shift; |
|
|
116 | |
|
|
117 | $::conns--; |
|
|
118 | |
114 | delete $conn{$self->{remote_addr}}{$self*1}; |
119 | delete $conn{$self->{remote_addr}}{$self*1}; |
115 | delete $uri{$self->{uri}}{$self*1}; |
120 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
116 | } |
121 | } |
117 | |
122 | |
118 | sub slog { |
123 | sub slog { |
119 | my $self = shift; |
124 | my $self = shift; |
120 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
125 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
… | |
… | |
130 | while (my ($h, $v) = each %$hdr) { |
135 | while (my ($h, $v) = each %$hdr) { |
131 | $res .= "$h: $v\015\012" |
136 | $res .= "$h: $v\015\012" |
132 | } |
137 | } |
133 | $res .= "\015\012"; |
138 | $res .= "\015\012"; |
134 | |
139 | |
135 | $res .= $content if defined $content and $self->{method} eq "GET"; |
140 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
136 | |
141 | |
137 | 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# |
138 | |
143 | |
139 | $self->{written} += |
144 | $self->{written} += |
140 | print {$self->{fh}} $res; |
145 | print {$self->{fh}} $res; |
… | |
… | |
158 | sub err_blocked { |
163 | sub err_blocked { |
159 | my $self = shift; |
164 | my $self = shift; |
160 | my $ip = $self->{remote_addr}; |
165 | my $ip = $self->{remote_addr}; |
161 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
166 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
162 | |
167 | |
163 | Coro::Event::do_timer(after => 5); |
168 | Coro::Event::do_timer(after => 15); |
164 | |
169 | |
165 | $self->err(403, "too many connections", |
170 | $self->err(401, "too many connections", |
166 | { |
171 | { |
167 | "Content-Type" => "text/html", |
172 | "Content-Type" => "text/html", |
168 | "Retry-After" => $::BLOCKTIME |
173 | "Retry-After" => $::BLOCKTIME |
169 | }, |
174 | }, |
170 | <<EOF); |
175 | <<EOF); |
… | |
… | |
195 | defined $req or |
200 | defined $req or |
196 | $self->err(408, "request timeout"); |
201 | $self->err(408, "request timeout"); |
197 | |
202 | |
198 | my $ip = $self->{remote_addr}; |
203 | my $ip = $self->{remote_addr}; |
199 | |
204 | |
|
|
205 | $self->err_blocked($blocked{$ip}); |
200 | if ($blocked{$ip}) { |
206 | if ($blocked{$ip}) { |
201 | $self->err_blocked($blocked{$ip}) |
207 | $self->err_blocked($blocked{$ip}) |
202 | if $blocked{$ip} > $::NOW; |
208 | if $blocked{$ip} > $::NOW; |
203 | |
209 | |
204 | delete $blocked{$ip}; |
210 | delete $blocked{$ip}; |
… | |
… | |
212 | $req =~ /^(?:\015\012)? |
218 | $req =~ /^(?:\015\012)? |
213 | (GET|HEAD) \040+ |
219 | (GET|HEAD) \040+ |
214 | ([^\040]+) \040+ |
220 | ([^\040]+) \040+ |
215 | HTTP\/([0-9]+\.[0-9]+) |
221 | HTTP\/([0-9]+\.[0-9]+) |
216 | \015\012/gx |
222 | \015\012/gx |
217 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
223 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
218 | |
224 | |
219 | $2 ne "1.0" |
225 | $2 ne "1.0" |
220 | or $self->err(506, "http protocol version not supported"); |
226 | or $self->err(506, "http protocol version not supported"); |
221 | |
227 | |
222 | $self->{method} = $1; |
228 | $self->{method} = $1; |
… | |
… | |
241 | while ($h, $v) = each %hdr; |
247 | while ($h, $v) = each %hdr; |
242 | } |
248 | } |
243 | |
249 | |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
250 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
245 | |
251 | |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
252 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
247 | |
253 | |
248 | $self->map_uri; |
254 | $self->map_uri; |
249 | $self->respond; |
255 | $self->respond; |
250 | #} |
256 | #} |
251 | } |
257 | } |
… | |
… | |
399 | $self->err(416, "not satisfiable", $hdr); |
405 | $self->err(416, "not satisfiable", $hdr); |
400 | |
406 | |
401 | satisfiable: |
407 | satisfiable: |
402 | # check for segmented downloads |
408 | # check for segmented downloads |
403 | if ($l && $::NO_SEGMENTED) { |
409 | if ($l && $::NO_SEGMENTED) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
410 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
|
|
411 | Coro::Event::do_timer(after => 15); |
|
|
412 | |
405 | $self->err(400, "segmented downloads are not allowed"); |
413 | $self->err(400, "segmented downloads are not allowed"); |
406 | } |
414 | } |
407 | } |
415 | } |
408 | |
416 | |
409 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
417 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |