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.39 by root, Sun Sep 2 12:23:04 2001 UTC vs.
Revision 1.43 by root, Wed Sep 12 20:29:43 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines