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

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.59 by root, Wed Dec 5 02:02:23 2001 UTC vs.
Revision 1.64 by root, Wed Jan 23 04:49:50 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 && 400 < length $content
211 && $hdr->{"Content-Length"} == length $content
212 && !exists $hdr->{"Content-Encoding"}
213 ) {
214 my $orig = length $content;
215 $hdr->{"Content-Encoding"} = "gzip";
216 $content = Compress::Zlib::memGzip(\$content);
217 $hdr->{"Content-Length"} = length $content;
218 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
219 }
220
204 $res .= "Date: $HTTP_NOW\015\012"; 221 $res .= "Date: $HTTP_NOW\015\012";
205 222
206 while (my ($h, $v) = each %$hdr) { 223 while (my ($h, $v) = each %$hdr) {
207 $res .= "$h: $v\015\012" 224 $res .= "$h: $v\015\012"
208 } 225 }
209 $res .= "\015\012"; 226 $res .= "\015\012";
210 227
211 $res .= $content if defined $content and $self->{method} ne "HEAD"; 228 $res .= $content if defined $content and $self->{method} ne "HEAD";
212 229
213 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). 230 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
214 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. 231 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
215 " \"$self->{h}{referer}\"\n"; 232 " \"$self->{h}{referer}\"\n";
216 233
217 print $accesslog $log if $accesslog; 234 print $accesslog $log if $accesslog;
218 print STDERR $log; 235 print STDERR $log;
219 236
281 my (%hdr, $h, $v); 298 my (%hdr, $h, $v);
282 299
283 $hdr{lc $1} .= ",$2" 300 $hdr{lc $1} .= ",$2"
284 while $req =~ /\G 301 while $req =~ /\G
285 ([^:\000-\040]+): 302 ([^:\000-\040]+):
286 [\008\040]* 303 [\010\040]*
287 ((?: [^\015\012]+ | \015\012[\008\040] )*) 304 ((?: [^\015\012]+ | \015\012[\010\040] )*)
288 \015\012 305 \015\012
289 /gxc; 306 /gxc;
290 307
291 $req =~ /\G\015\012$/ 308 $req =~ /\G\015\012$/
292 or $self->err(400, "bad request"); 309 or $self->err(400, "bad request");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines