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.37 by root, Sun Sep 2 01:03:53 2001 UTC vs.
Revision 1.43 by root, Wed Sep 12 20:29:43 2001 UTC

42my @pool; 42my @pool;
43 43
44# one "execution thread" 44# one "execution thread"
45sub handler { 45sub handler {
46 while () { 46 while () {
47 my $new = pop @newcons;
48 if ($new) { 47 if (@newcons) {
49 eval { 48 eval {
50 conn->new(@$new)->handle; 49 conn->new(@{pop @newcons})->handle;
51 }; 50 };
52 slog 1, "$@" if $@ && !ref $@; 51 slog 1, "$@" if $@ && !ref $@;
53 $connections->up; 52 $connections->up;
54 } else { 53 } else {
55 last if @pool >= $MAX_POOL; 54 last if @pool >= $MAX_POOL;
57 schedule; 56 schedule;
58 } 57 }
59 } 58 }
60} 59}
61 60
61sub listen_on {
62 my $listen = $_[0];
63
64 push @listen_sockets, $listen;
65
66 # the "main thread"
67 async {
68 slog 1, "accepting connections";
69 while () {
70 $connections->down;
71 push @newcons, [$listen->accept];
72 #slog 3, "accepted @$connections ".scalar(@pool);
73 if (@pool) {
74 (pop @pool)->ready;
75 } else {
76 async \&handler;
77 }
78
79 }
80 };
81}
82
62my $http_port = new Coro::Socket 83my $http_port = new Coro::Socket
63 LocalAddr => $SERVER_HOST, 84 LocalAddr => $SERVER_HOST,
64 LocalPort => $SERVER_PORT, 85 LocalPort => $SERVER_PORT,
65 ReuseAddr => 1, 86 ReuseAddr => 1,
66 Listen => 50, 87 Listen => 50,
67 or die "unable to start server"; 88 or die "unable to start server";
68 89
69push @listen_sockets, $http_port; 90listen_on $http_port;
91
92if ($SERVER_PORT2) {
93 my $http_port = new Coro::Socket
94 LocalAddr => $SERVER_HOST,
95 LocalPort => $SERVER_PORT2,
96 ReuseAddr => 1,
97 Listen => 50,
98 or die "unable to start server";
99
100 listen_on $http_port;
101}
70 102
71our $NOW; 103our $NOW;
72our $HTTP_NOW; 104our $HTTP_NOW;
73 105
74Event->timer(interval => 1, hard => 1, cb => sub { 106Event->timer(interval => 1, hard => 1, cb => sub {
75 $NOW = time; 107 $NOW = time;
76 $HTTP_NOW = time2str $NOW; 108 $HTTP_NOW = time2str $NOW;
77})->now; 109})->now;
78
79# the "main thread"
80async {
81 slog 1, "accepting connections";
82 while () {
83 $connections->down;
84 push @newcons, [$http_port->accept];
85 #slog 3, "accepted @$connections ".scalar(@pool);
86 if (@pool) {
87 (pop @pool)->ready;
88 } else {
89 async \&handler;
90 }
91
92 }
93};
94 110
95package conn; 111package conn;
96 112
97use Socket; 113use Socket;
98use HTTP::Date; 114use HTTP::Date;
125 141
126read_mimetypes; 142read_mimetypes;
127 143
128sub new { 144sub new {
129 my $class = shift; 145 my $class = shift;
146 my $fh = shift;
130 my $peername = shift; 147 my $peername = shift;
131 my $fh = shift;
132 my $self = bless { fh => $fh }, $class; 148 my $self = bless { fh => $fh }, $class;
133 my (undef, $iaddr) = unpack_sockaddr_in $peername 149 my (undef, $iaddr) = unpack_sockaddr_in $peername
134 or $self->err(500, "unable to decode peername"); 150 or $self->err(500, "unable to decode peername");
135 151
136 $self->{remote_addr} = inet_ntoa $iaddr; 152 $self->{remote_addr} = inet_ntoa $iaddr;
141 $self; 157 $self;
142} 158}
143 159
144sub DESTROY { 160sub DESTROY {
145 my $self = shift; 161 my $self = shift;
146
147 $::conns--; 162 $::conns--;
148
149 $self->eoconn; 163 $self->eoconn;
150} 164}
151 165
152# end of connection 166# end of connection
153sub eoconn { 167sub eoconn {
165 179
166sub response { 180sub response {
167 my ($self, $code, $msg, $hdr, $content) = @_; 181 my ($self, $code, $msg, $hdr, $content) = @_;
168 my $res = "HTTP/1.1 $code $msg\015\012"; 182 my $res = "HTTP/1.1 $code $msg\015\012";
169 183
170 $self->{h}{connection} ||= $hdr->{Connection}; 184 $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/;
171 185
172 $res .= "Date: $HTTP_NOW\015\012"; 186 $res .= "Date: $HTTP_NOW\015\012";
173 187
174 while (my ($h, $v) = each %$hdr) { 188 while (my ($h, $v) = each %$hdr) {
175 $res .= "$h: $v\015\012" 189 $res .= "$h: $v\015\012"
300 314
301 if (defined $host) { 315 if (defined $host) {
302 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; 316 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
303 } else { 317 } else {
304 ($self->{server_port}, $host) 318 ($self->{server_port}, $host)
305 = unpack_sockaddr_in $self->{fh}->getsockname 319 = unpack_sockaddr_in $self->{fh}->sockname
306 or $self->err(500, "unable to get socket name"); 320 or $self->err(500, "unable to get socket name");
307 $host = inet_ntoa $host; 321 $host = inet_ntoa $host;
308 } 322 }
309 323
310 $self->{server_name} = $host; 324 $self->{server_name} = $host;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines