ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing Coro/myhttpd/httpd.pl (file contents):
Revision 1.58 by root, Mon Dec 3 21:32:35 2001 UTC vs.
Revision 1.63 by root, Wed Jan 23 03:07:05 2002 UTC

4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6 6
7use HTTP::Date; 7use HTTP::Date;
8use POSIX (); 8use POSIX ();
9
10use Compress::Zlib ();
9 11
10no utf8; 12no utf8;
11use bytes; 13use bytes;
12 14
13# at least on my machine, this thingy serves files 15# at least on my machine, this thingy serves files
161 my (undef, $iaddr) = unpack_sockaddr_in $peername 163 my (undef, $iaddr) = unpack_sockaddr_in $peername
162 or $self->err(500, "unable to decode peername"); 164 or $self->err(500, "unable to decode peername");
163 165
164 $self->{remote_addr} = 166 $self->{remote_addr} =
165 $self->{remote_id} = inet_ntoa $iaddr; 167 $self->{remote_id} = inet_ntoa $iaddr;
168
166 $self->{time} = $::NOW; 169 $self->{time} = $::NOW;
167 170
168 weaken ($Coro::current->{conn} = $self); 171 weaken ($Coro::current->{conn} = $self);
169 172
170 $::conns++; 173 $::conns++;
184} 187}
185 188
186sub response { 189sub response {
187 my ($self, $code, $msg, $hdr, $content) = @_; 190 my ($self, $code, $msg, $hdr, $content) = @_;
188 my $res = "HTTP/1.1 $code $msg\015\012"; 191 my $res = "HTTP/1.1 $code $msg\015\012";
192 my $GZ = "";
189 193
190 if (exists $hdr->{Connection}) { 194 if (exists $hdr->{Connection}) {
191 if ($hdr->{Connection} =~ /close/) { 195 if ($hdr->{Connection} =~ /close/) {
192 $self->{h}{connection} = "close" 196 $self->{h}{connection} = "close"
193 } 197 }
199 $self->{h}{connection} = "close" 203 $self->{h}{connection} = "close"
200 } 204 }
201 } 205 }
202 } 206 }
203 207
208 if ($self->{method} ne "HEAD"
209 && $self->{h}{"accept-encoding"} =~ /\bgzip\b/
210 && $hdr->{"Content-Length"} == length $content
211 && !exists $hdr->{"Content-Encoding"}
212 ) {
213 my $orig = length $content;
214 $hdr->{"Content-Encoding"} = "gzip";
215 $content = Compress::Zlib::memGzip(\$content);
216 $hdr->{"Content-Length"} = length $content;
217 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
218 }
219
204 $res .= "Date: $HTTP_NOW\015\012"; 220 $res .= "Date: $HTTP_NOW\015\012";
205 221
206 while (my ($h, $v) = each %$hdr) { 222 while (my ($h, $v) = each %$hdr) {
207 $res .= "$h: $v\015\012" 223 $res .= "$h: $v\015\012"
208 } 224 }
209 $res .= "\015\012"; 225 $res .= "\015\012";
210 226
211 $res .= $content if defined $content and $self->{method} ne "HEAD"; 227 $res .= $content if defined $content and $self->{method} ne "HEAD";
212 228
213 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). 229 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
214 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. 230 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
215 " \"$self->{h}{referer}\"\n"; 231 " \"$self->{h}{referer}\"\n";
216 232
217 print $accesslog $log if $accesslog; 233 print $accesslog $log if $accesslog;
218 print STDERR $log; 234 print STDERR $log;
219 235
281 my (%hdr, $h, $v); 297 my (%hdr, $h, $v);
282 298
283 $hdr{lc $1} .= ",$2" 299 $hdr{lc $1} .= ",$2"
284 while $req =~ /\G 300 while $req =~ /\G
285 ([^:\000-\040]+): 301 ([^:\000-\040]+):
286 [\008\040]* 302 [\010\040]*
287 ((?: [^\015\012]+ | \015\012[\008\040] )*) 303 ((?: [^\015\012]+ | \015\012[\010\040] )*)
288 \015\012 304 \015\012
289 /gxc; 305 /gxc;
290 306
291 $req =~ /\G\015\012$/ 307 $req =~ /\G\015\012$/
292 or $self->err(400, "bad request"); 308 or $self->err(400, "bad request");
566 } 582 }
567 } 583 }
568 584
569 if ($blocked{$self->{remote_id}}) { 585 if ($blocked{$self->{remote_id}}) {
570 $self->{h}{connection} = "close"; 586 $self->{h}{connection} = "close";
571 die bless {}, err:: 587 die bless {}, err::;
572 } 588 }
573 589
574 if (0) { # !AIO 590 if (0) { # !AIO
575 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h 591 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
576 or last; 592 or last;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines