… | |
… | |
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); |
… | |
… | |
203 | |
208 | |
204 | delete $blocked{$ip}; |
209 | delete $blocked{$ip}; |
205 | } |
210 | } |
206 | |
211 | |
207 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
212 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
208 | $self->slog("blocked ip $ip"); |
213 | $self->slog(2, "blocked ip $ip"); |
209 | $self->err_blocked; |
214 | $self->err_blocked; |
210 | } |
215 | } |
211 | |
216 | |
212 | $req =~ /^(?:\015\012)? |
217 | $req =~ /^(?:\015\012)? |
213 | (GET|HEAD) \040+ |
218 | (GET|HEAD) \040+ |
214 | ([^\040]+) \040+ |
219 | ([^\040]+) \040+ |
215 | HTTP\/([0-9]+\.[0-9]+) |
220 | HTTP\/([0-9]+\.[0-9]+) |
216 | \015\012/gx |
221 | \015\012/gx |
217 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
222 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
218 | |
223 | |
219 | $2 ne "1.0" |
224 | $2 ne "1.0" |
220 | or $self->err(506, "http protocol version not supported"); |
225 | or $self->err(506, "http protocol version not supported"); |
221 | |
226 | |
222 | $self->{method} = $1; |
227 | $self->{method} = $1; |
… | |
… | |
241 | while ($h, $v) = each %hdr; |
246 | while ($h, $v) = each %hdr; |
242 | } |
247 | } |
243 | |
248 | |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
249 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
245 | |
250 | |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
251 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
247 | |
252 | |
248 | $self->map_uri; |
253 | $self->map_uri; |
249 | $self->respond; |
254 | $self->respond; |
250 | #} |
255 | #} |
251 | } |
256 | } |
… | |
… | |
399 | $self->err(416, "not satisfiable", $hdr); |
404 | $self->err(416, "not satisfiable", $hdr); |
400 | |
405 | |
401 | satisfiable: |
406 | satisfiable: |
402 | # check for segmented downloads |
407 | # check for segmented downloads |
403 | if ($l && $::NO_SEGMENTED) { |
408 | if ($l && $::NO_SEGMENTED) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
409 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
405 | $self->slog("segmented download refused\n"); |
410 | Coro::Event::do_timer(after => 15); |
|
|
411 | |
406 | $self->err(400, "segmented downloads are not allowed"); |
412 | $self->err(400, "segmented downloads are not allowed"); |
407 | } |
413 | } |
408 | } |
414 | } |
409 | |
415 | |
410 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
416 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |