1 # $Id: Daemon.pm,v 1.1.1.1 2003/08/02 23:39:39 takezoe Exp $
10 HTTP::Daemon - a simple http server class
17 my $d = HTTP::Daemon->new || die;
18 print "Please contact me at: <URL:", $d->url, ">\n";
19 while (my $c = $d->accept) {
20 while (my $r = $c->get_request) {
21 if ($r->method eq 'GET' and $r->url->path eq "/xyzzy") {
22 # remember, this is *not* recommened practice :-)
23 $c->send_file_response("/etc/passwd");
25 $c->send_error(RC_FORBIDDEN)
34 Instances of the I<HTTP::Daemon> class are HTTP/1.1 servers that
35 listen on a socket for incoming requests. The I<HTTP::Daemon> is a
36 sub-class of I<IO::Socket::INET>, so you can perform socket operations
39 The accept() method will return when a connection from a client is
40 available. In a scalar context the returned value will be a reference
41 to a object of the I<HTTP::Daemon::ClientConn> class which is another
42 I<IO::Socket::INET> subclass. In a list context a two-element array
43 is returned containing the new I<HTTP::Daemon::ClientConn> reference
44 and the peer address; the list will be empty upon failure. Calling
45 the get_request() method on the I<HTTP::Daemon::ClientConn> object
46 will read data from the client and return an I<HTTP::Request> object
49 This HTTP daemon does not fork(2) for you. Your application, i.e. the
50 user of the I<HTTP::Daemon> is reponsible for forking if that is
51 desirable. Also note that the user is responsible for generating
52 responses that conform to the HTTP/1.1 protocol. The
53 I<HTTP::Daemon::ClientConn> class provides some methods that make this easier.
57 The following is a list of methods that are new (or enhanced) relative
58 to the I<IO::Socket::INET> base class.
65 use vars qw($VERSION @ISA $PROTO $DEBUG);
67 $VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/);
69 use IO::Socket qw(AF_INET INADDR_ANY inet_ntoa);
70 @ISA=qw(IO::Socket::INET);
74 =item $d = new HTTP::Daemon
76 The constructor takes the same parameters as the
77 I<IO::Socket::INET> constructor. It can also be called without specifying
78 any parameters. The daemon will then set up a listen queue of 5
79 connections and allocate some random port number. A server that wants
80 to bind to some specific address on the standard HTTP port will be
81 constructed like this:
84 LocalAddr => 'www.someplace.com',
91 my($class, %args) = @_;
93 $args{Proto} ||= 'tcp';
94 return $class->SUPER::new(%args);
98 =item $c = $d->accept([$pkg])
100 This method is the same as I<IO::Socket::accept> but returns an
101 I<HTTP::Daemon::ClientConn> reference by default. It returns undef if
102 you specify a timeout and no connection is made within that time. In
103 a scalar context the returned value will be a reference to a object of
104 the I<HTTP::Daemon::ClientConn> class which is another
105 I<IO::Socket::INET> subclass. In a list context a two-element array
106 is returned containing the new I<HTTP::Daemon::ClientConn> reference
107 and the peer address; the list will be empty upon failure.
115 my $pkg = shift || "HTTP::Daemon::ClientConn";
116 my ($sock, $peer) = $self->SUPER::accept($pkg);
118 ${*$sock}{'httpd_daemon'} = $self;
119 return wantarray ? ($sock, $peer) : $sock;
128 Returns a URL string that can be used to access the server root.
136 my $addr = $self->sockaddr;
137 if (!$addr || $addr eq INADDR_ANY) {
138 require Sys::Hostname;
139 $url .= lc Sys::Hostname::hostname();
142 $url .= gethostbyaddr($addr, AF_INET) || inet_ntoa($addr);
144 my $port = $self->sockport;
145 $url .= ":$port" if $port != 80;
151 =item $d->product_tokens
153 Returns the name that this server will use to identify itself. This
154 is the string that is sent with the I<Server> response header. The
155 main reason to have this method is that subclasses can override it if
156 they want to use another product name.
162 "libwww-perl-daemon/$HTTP::Daemon::VERSION";
166 package HTTP::Daemon::ClientConn;
168 use vars qw(@ISA $DEBUG);
170 @ISA=qw(IO::Socket::INET);
171 *DEBUG = \$HTTP::Daemon::DEBUG;
173 use HTTP::Request ();
174 use HTTP::Response ();
176 use HTTP::Date qw(time2str);
177 use LWP::MediaTypes qw(guess_media_type);
180 my $CRLF = "\015\012"; # "\r\n" is not portable
181 my $HTTP_1_0 = _http_version("HTTP/1.0");
182 my $HTTP_1_1 = _http_version("HTTP/1.1");
186 The I<HTTP::Daemon::ClientConn> is also a I<IO::Socket::INET>
187 subclass. Instances of this class are returned by the accept() method
188 of I<HTTP::Daemon>. The following additional methods are
193 =item $c->get_request([$headers_only])
195 Read data from the client and turn it into an
196 I<HTTP::Request> object which is then returned. It returns C<undef>
197 if reading of the request fails. If it fails, then the
198 I<HTTP::Daemon::ClientConn> object ($c) should be discarded, and you
199 should not call this method again. The $c->reason method might give
200 you some information about why $c->get_request returned C<undef>.
202 The $c->get_request method supports HTTP/1.1 request content bodies,
203 including I<chunked> transfer encoding with footer and self delimiting
204 I<multipart/*> content types.
206 The $c->get_request method will normally not return until the whole
207 request has been received from the client. This might not be what you
208 want if the request is an upload of a multi-mega-byte file (and with
209 chunked transfer encoding HTTP can even support infinite request
210 messages - uploading live audio for instance). If you pass a TRUE
211 value as the $headers_only argument, then $c->get_request will return
212 immediately after parsing the request headers and you are responsible
213 for reading the rest of the request content. If you are going to
214 call $c->get_request again on the same connection you better read the
215 correct number of bytes.
221 my($self, $only_headers) = @_;
222 if (${*$self}{'httpd_nomore'}) {
223 $self->reason("No more requests from this connection");
228 my $buf = ${*$self}{'httpd_rbuf'};
229 $buf = "" unless defined $buf;
231 my $timeout = $ {*$self}{'io_socket_timeout'};
233 vec($fdset, $self->fileno, 1) = 1;
238 # loop until we have the whole header in $buf
239 $buf =~ s/^(?:\015?\012)+//; # ignore leading blank lines
240 if ($buf =~ /\012/) { # potential, has at least one line
241 if ($buf =~ /^\w+[^\012]+HTTP\/\d+\.\d+\015?\012/) {
242 if ($buf =~ /\015?\012\015?\012/) {
243 last READ_HEADER; # we have it
244 } elsif (length($buf) > 16*1024) {
245 $self->send_error(413); # REQUEST_ENTITY_TOO_LARGE
246 $self->reason("Very long header");
250 last READ_HEADER; # HTTP/0.9 client
252 } elsif (length($buf) > 16*1024) {
253 $self->send_error(414); # REQUEST_URI_TOO_LARGE
254 $self->reason("Very long first line");
257 print STDERR "Need more data for complete header\n" if $DEBUG;
258 return unless $self->_need_more($buf, $timeout, $fdset);
260 if ($buf !~ s/^(\S+)[ \t]+(\S+)(?:[ \t]+(HTTP\/\d+\.\d+))?[^\012]*\012//) {
261 ${*$self}{'httpd_client_proto'} = _http_version("HTTP/1.0");
262 $self->send_error(400); # BAD_REQUEST
263 $self->reason("Bad request line: $buf");
268 my $proto = $3 || "HTTP/0.9";
269 $uri = "http://$uri" if $method eq "CONNECT";
270 $uri = $HTTP::URI_CLASS->new($uri, $self->daemon->url);
271 my $r = HTTP::Request->new($method, $uri);
272 $r->protocol($proto);
273 ${*$self}{'httpd_client_proto'} = $proto = _http_version($proto);
275 if ($proto >= $HTTP_1_0) {
276 # we expect to find some headers
279 while ($buf =~ s/^([^\012]*)\012//) {
282 if (/^([\w\-]+)\s*:\s*(.*)/) {
283 $r->push_header($key, $val) if $key;
284 ($key, $val) = ($1, $2);
285 } elsif (/^\s+(.*)/) {
291 $r->push_header($key, $val) if $key;
294 my $conn = $r->header('Connection');
295 if ($proto >= $HTTP_1_1) {
296 ${*$self}{'httpd_nomore'}++ if $conn && lc($conn) =~ /\bclose\b/;
298 ${*$self}{'httpd_nomore'}++ unless $conn &&
299 lc($conn) =~ /\bkeep-alive\b/;
303 ${*$self}{'httpd_rbuf'} = $buf;
307 # Find out how much content to read
308 my $te = $r->header('Transfer-Encoding');
309 my $ct = $r->header('Content-Type');
310 my $len = $r->header('Content-Length');
312 if ($te && lc($te) eq 'chunked') {
313 # Handle chunked transfer encoding
317 print STDERR "Chunked\n" if $DEBUG;
318 if ($buf =~ s/^([^\012]*)\012//) {
320 unless ($chunk_head =~ /^([0-9A-Fa-f]+)/) {
321 $self->send_error(400);
322 $self->reason("Bad chunk header $chunk_head");
326 last CHUNK if $size == 0;
328 my $missing = $size - length($buf) + 2; # 2=CRLF at chunk end
329 # must read until we have a complete chunk
330 while ($missing > 0) {
331 print STDERR "Need $missing more bytes\n" if $DEBUG;
332 my $n = $self->_need_more($buf, $timeout, $fdset);
336 $body .= substr($buf, 0, $size);
337 substr($buf, 0, $size+2) = '';
340 # need more data in order to have a complete chunk header
341 return unless $self->_need_more($buf, $timeout, $fdset);
346 # pretend it was a normal entity body
347 $r->remove_header('Transfer-Encoding');
348 $r->header('Content-Length', length($body));
353 if ($buf !~ /\012/) {
354 # need at least one line to look at
355 return unless $self->_need_more($buf, $timeout, $fdset);
357 $buf =~ s/^([^\012]*)\012//;
360 if (/^([\w\-]+)\s*:\s*(.*)/) {
361 $r->push_header($key, $val) if $key;
362 ($key, $val) = ($1, $2);
363 } elsif (/^\s+(.*)/) {
368 $self->reason("Bad footer syntax");
373 $r->push_header($key, $val) if $key;
376 $self->send_error(501); # Unknown transfer encoding
377 $self->reason("Unknown transfer encoding '$te'");
380 } elsif ($ct && lc($ct) =~ m/^multipart\/\w+\s*;.*boundary\s*=\s*(\w+)/) {
381 # Handle multipart content type
382 my $boundary = "$CRLF--$1--$CRLF";
385 $index = index($buf, $boundary);
387 # end marker not yet found
388 return unless $self->_need_more($buf, $timeout, $fdset);
390 $index += length($boundary);
391 $r->content(substr($buf, 0, $index));
392 substr($buf, 0, $index) = '';
395 # Plain body specified by "Content-Length"
396 my $missing = $len - length($buf);
397 while ($missing > 0) {
398 print "Need $missing more bytes of content\n" if $DEBUG;
399 my $n = $self->_need_more($buf, $timeout, $fdset);
403 if (length($buf) > $len) {
404 $r->content(substr($buf,0,$len));
405 substr($buf, 0, $len) = '';
411 ${*$self}{'httpd_rbuf'} = $buf;
419 #my($buf,$timeout,$fdset) = @_;
421 my($timeout, $fdset) = @_[1,2];
422 print STDERR "select(,,,$timeout)\n" if $DEBUG;
423 my $n = select($fdset,undef,undef,$timeout);
425 $self->reason(defined($n) ? "Timeout" : "select: $!");
429 print STDERR "sysread()\n" if $DEBUG;
430 my $n = sysread($self, $_[0], 2048, length($_[0]));
431 $self->reason(defined($n) ? "Client closed" : "sysread: $!") unless $n;
435 =item $c->read_buffer([$new_value])
437 Bytes read by $c->get_request, but not used are placed in the I<read
438 buffer>. The next time $c->get_request is called it will consume the
439 bytes in this buffer before reading more data from the network
440 connection itself. The read buffer is invalid after $c->get_request
441 has returned an undefined value.
443 If you handle the reading of the request content yourself you need to
444 empty this buffer before you read more and you need to place
445 unconsumed bytes here. You also need this buffer if you implement
446 services like I<101 Switching Protocols>.
448 This method always return the old buffer content and can optionally
449 replace the buffer content if you pass it an argument.
456 my $old = ${*$self}{'httpd_rbuf'};
458 ${*$self}{'httpd_rbuf'} = shift;
466 When $c->get_request returns C<undef> you can obtain a short string
467 describing why it happened by calling $c->reason.
474 my $old = ${*$self}{'httpd_reason'};
476 ${*$self}{'httpd_reason'} = shift;
482 =item $c->proto_ge($proto)
484 Return TRUE if the client announced a protocol with version number
485 greater or equal to the given argument. The $proto argument can be a
486 string like "HTTP/1.1" or just "1.1".
493 ${*$self}{'httpd_client_proto'} >= _http_version(shift);
499 return 0 unless m,^(?:HTTP/)?(\d+)\.(\d+)$,i;
503 =item $c->antique_client
505 Return TRUE if the client speaks the HTTP/0.9 protocol. No status
506 code and no headers should be returned to such a client. This should
507 be the same as !$c->proto_ge("HTTP/1.0").
514 ${*$self}{'httpd_client_proto'} < $HTTP_1_0;
518 =item $c->force_last_request
520 Make sure that $c->get_request will not try to read more requests off
521 this connection. If you generate a response that is not self
522 delimiting, then you should signal this fact by calling this method.
524 This attribute is turned on automatically if the client announces
525 protocol HTTP/1.0 or worse and does not include a "Connection:
526 Keep-Alive" header. It is also turned on automatically when HTTP/1.1
527 or better clients send the "Connection: close" request header.
531 sub force_last_request
534 ${*$self}{'httpd_nomore'}++;
538 =item $c->send_status_line( [$code, [$mess, [$proto]]] )
540 Send the status line back to the client. If $code is omitted 200 is
541 assumed. If $mess is omitted, then a message corresponding to $code
542 is inserted. If $proto is missing the content of the
543 $HTTP::Daemon::PROTO variable is used.
549 my($self, $status, $message, $proto) = @_;
550 return if $self->antique_client;
552 $message ||= status_message($status) || "";
553 $proto ||= $HTTP::Daemon::PROTO || "HTTP/1.1";
554 print $self "$proto $status $message$CRLF";
559 Send the CRLF sequence to the client.
571 =item $c->send_basic_header( [$code, [$mess, [$proto]]] )
573 Send the status line and the "Date:" and "Server:" headers back to
574 the client. This header is assumed to be continued and does not end
575 with an empty CRLF line.
579 sub send_basic_header
582 return if $self->antique_client;
583 $self->send_status_line(@_);
584 print $self "Date: ", time2str(time), $CRLF;
585 my $product = $self->daemon->product_tokens;
586 print $self "Server: $product$CRLF" if $product;
590 =item $c->send_response( [$res] )
592 Write a I<HTTP::Response> object to the
593 client as a response. We try hard to make sure that the response is
594 self delimiting so that the connection can stay persistent for further
595 request/response exchanges.
597 The content attribute of the I<HTTP::Response> object can be a normal
598 string or a subroutine reference. If it is a subroutine, then
599 whatever this callback routine returns is written back to the
600 client as the response content. The routine will be called until it
601 return an undefined or empty value. If the client is HTTP/1.1 aware
602 then we will use chunked transfer encoding for the response.
612 $res = HTTP::Response->new($res, @_);
614 my $content = $res->content;
616 unless ($self->antique_client) {
617 my $code = $res->code;
618 $self->send_basic_header($code, $res->message, $res->protocol);
619 if ($code =~ /^(1\d\d|[23]04)$/) {
620 # make sure content is empty
621 $res->remove_header("Content-Length");
623 } elsif ($res->request && $res->request->method eq "HEAD") {
625 } elsif (ref($content) eq "CODE") {
626 if ($self->proto_ge("HTTP/1.1")) {
627 $res->push_header("Transfer-Encoding" => "chunked");
630 $self->force_last_request;
632 } elsif (length($content)) {
633 $res->header("Content-Length" => length($content));
635 $self->force_last_request;
637 print $self $res->headers_as_string($CRLF);
638 print $self $CRLF; # separates headers and content
640 if (ref($content) eq "CODE") {
642 my $chunk = &$content();
643 last unless defined($chunk) && length($chunk);
645 printf $self "%x%s%s%s", length($chunk), $CRLF, $chunk, $CRLF;
650 print $self "0$CRLF$CRLF" if $chunked; # no trailers either
651 } elsif (length $content) {
652 print $self $content;
657 =item $c->send_redirect( $loc, [$code, [$entity_body]] )
659 Send a redirect response back to the client. The location ($loc) can
660 be an absolute or relative URL. The $code must be one the redirect
661 status codes, and defaults to "301 Moved Permanently"
667 my($self, $loc, $status, $content) = @_;
668 $status ||= RC_MOVED_PERMANENTLY;
669 Carp::croak("Status '$status' is not redirect") unless is_redirect($status);
670 $self->send_basic_header($status);
671 my $base = $self->daemon->url;
672 $loc = $HTTP::URI_CLASS->new($loc, $base) unless ref($loc);
673 $loc = $loc->abs($base);
674 print $self "Location: $loc$CRLF";
676 my $ct = $content =~ /^\s*</ ? "text/html" : "text/plain";
677 print $self "Content-Type: $ct$CRLF";
680 print $self $content if $content;
681 $self->force_last_request; # no use keeping the connection open
685 =item $c->send_error( [$code, [$error_message]] )
687 Send an error response back to the client. If the $code is missing a
688 "Bad Request" error is reported. The $error_message is a string that
689 is incorporated in the body of the HTML entity body.
695 my($self, $status, $error) = @_;
696 $status ||= RC_BAD_REQUEST;
697 Carp::croak("Status '$status' is not an error") unless is_error($status);
698 my $mess = status_message($status);
701 <title>$status $mess</title>
702 <h1>$status $mess</h1>
705 unless ($self->antique_client) {
706 $self->send_basic_header($status);
707 print $self "Content-Type: text/html$CRLF";
708 print $self "Content-Length: " . length($mess) . $CRLF;
716 =item $c->send_file_response($filename)
718 Send back a response with the specified $filename as content. If the
719 file is a directory we try to generate an HTML index of it.
723 sub send_file_response
725 my($self, $file) = @_;
727 $self->send_dir($file);
731 sysopen(F, $file, 0) or
732 return $self->send_error(RC_FORBIDDEN);
734 my($ct,$ce) = guess_media_type($file);
735 my($size,$mtime) = (stat _)[7,9];
736 unless ($self->antique_client) {
737 $self->send_basic_header;
738 print $self "Content-Type: $ct$CRLF";
739 print $self "Content-Encoding: $ce$CRLF" if $ce;
740 print $self "Content-Length: $size$CRLF" if $size;
741 print $self "Last-Modified: ", time2str($mtime), "$CRLF" if $mtime;
744 $self->send_file(\*F);
747 $self->send_error(RC_NOT_FOUND);
754 my($self, $dir) = @_;
755 $self->send_error(RC_NOT_FOUND) unless -d $dir;
756 $self->send_error(RC_NOT_IMPLEMENTED);
760 =item $c->send_file($fd);
762 Copy the file to the client. The file can be a string (which
763 will be interpreted as a filename) or a reference to an I<IO::Handle>
770 my($self, $file) = @_;
774 open(F, $file) || return undef;
782 while ($n = sysread($file, $buf, 8*1024)) {
787 close($file) if $opened;
794 Return a reference to the corresponding I<HTTP::Daemon> object.
801 ${*$self}{'httpd_daemon'};
810 L<IO::Socket::INET>, L<Apache>
814 Copyright 1996-2001, Gisle Aas
816 This library is free software; you can redistribute it and/or
817 modify it under the same terms as Perl itself.