… | |
… | |
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); |
|
|
162 | |
|
|
163 | $self->err(403, "too many connections", |
168 | $self->err(403, "too many connections", |
164 | { |
169 | { |
165 | "Content-Type" => "text/html", |
170 | "Content-Type" => "text/html", |
166 | "Retry-After" => $::BLOCKTIME |
171 | "Retry-After" => $::BLOCKTIME |
167 | }, |
172 | }, |
… | |
… | |
201 | |
206 | |
202 | delete $blocked{$ip}; |
207 | delete $blocked{$ip}; |
203 | } |
208 | } |
204 | |
209 | |
205 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
210 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
206 | $self->slog("blocked ip $ip"); |
211 | $self->slog(2, "blocked ip $ip"); |
207 | $self->err_blocked; |
212 | $self->err_blocked; |
208 | } |
213 | } |
209 | |
214 | |
210 | $req =~ /^(?:\015\012)? |
215 | $req =~ /^(?:\015\012)? |
211 | (GET|HEAD) \040+ |
216 | (GET|HEAD) \040+ |
… | |
… | |
239 | while ($h, $v) = each %hdr; |
244 | while ($h, $v) = each %hdr; |
240 | } |
245 | } |
241 | |
246 | |
242 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
247 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
243 | |
248 | |
244 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
249 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
245 | |
250 | |
246 | $self->map_uri; |
251 | $self->map_uri; |
247 | $self->respond; |
252 | $self->respond; |
248 | #} |
253 | #} |
249 | } |
254 | } |
… | |
… | |
397 | $self->err(416, "not satisfiable", $hdr); |
402 | $self->err(416, "not satisfiable", $hdr); |
398 | |
403 | |
399 | satisfiable: |
404 | satisfiable: |
400 | # check for segmented downloads |
405 | # check for segmented downloads |
401 | if ($l && $::NO_SEGMENTED) { |
406 | if ($l && $::NO_SEGMENTED) { |
402 | if (%{$uri{$self->{uri}}} > 1) { |
407 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
403 | $self->slog("segmented download refused\n"); |
|
|
404 | $self->err(400, "segmented downloads are not allowed"); |
408 | $self->err(400, "segmented downloads are not allowed"); |
405 | } |
409 | } |
406 | } |
410 | } |
407 | |
411 | |
408 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
412 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
432 | |
436 | |
433 | $h -= $l - 1; |
437 | $h -= $l - 1; |
434 | |
438 | |
435 | while ($h > 0) { |
439 | while ($h > 0) { |
436 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
440 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
437 | $self->{fh}->syswrite($buf) |
441 | my $w = $self->{fh}->syswrite($buf) |
438 | or last; |
442 | or last; |
|
|
443 | $::written += $w; |
|
|
444 | $self->{written} += $w; |
439 | } |
445 | } |
440 | } |
446 | } |
441 | |
447 | |
442 | close $fh; |
448 | close $fh; |
443 | } |
449 | } |