… | |
… | |
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); |
|
|
164 | |
|
|
165 | $self->err(403, "too many connections", |
168 | $self->err(403, "too many connections", |
166 | { |
169 | { |
167 | "Content-Type" => "text/html", |
170 | "Content-Type" => "text/html", |
168 | "Retry-After" => $::BLOCKTIME |
171 | "Retry-After" => $::BLOCKTIME |
169 | }, |
172 | }, |
… | |
… | |
241 | while ($h, $v) = each %hdr; |
244 | while ($h, $v) = each %hdr; |
242 | } |
245 | } |
243 | |
246 | |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
247 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
245 | |
248 | |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
249 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
247 | |
250 | |
248 | $self->map_uri; |
251 | $self->map_uri; |
249 | $self->respond; |
252 | $self->respond; |
250 | #} |
253 | #} |
251 | } |
254 | } |
… | |
… | |
399 | $self->err(416, "not satisfiable", $hdr); |
402 | $self->err(416, "not satisfiable", $hdr); |
400 | |
403 | |
401 | satisfiable: |
404 | satisfiable: |
402 | # check for segmented downloads |
405 | # check for segmented downloads |
403 | if ($l && $::NO_SEGMENTED) { |
406 | if ($l && $::NO_SEGMENTED) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
407 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
405 | $self->err(400, "segmented downloads are not allowed"); |
408 | $self->err(400, "segmented downloads are not allowed"); |
406 | } |
409 | } |
407 | } |
410 | } |
408 | |
411 | |
409 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
412 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |