HTTP::Daemon - a simple http server class |
HTTP::Daemon - a simple http server class
use HTTP::Daemon; use HTTP::Status;
my $d = HTTP::Daemon->new || die; print "Please contact me at: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->method eq 'GET' and $r->url->path eq "/xyzzy") { # remember, this is *not* recommened practice :-) $c->send_file_response("/etc/passwd"); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }
Instances of the HTTP::Daemon class are HTTP/1.1 servers that listen on a socket for incoming requests. The HTTP::Daemon is a sub-class of IO::Socket::INET, so you can perform socket operations directly on it too.
The accept()
method will return when a connection from a client is
available. In a scalar context the returned value will be a reference
to a object of the HTTP::Daemon::ClientConn class which is another
IO::Socket::INET subclass. In a list context a two-element array
is returned containing the new HTTP::Daemon::ClientConn reference
and the peer address; the list will be empty upon failure. Calling
the get_request()
method on the HTTP::Daemon::ClientConn object
will read data from the client and return an HTTP::Request object
reference.
This HTTP daemon does not fork(2)
for you. Your application, i.e. the
user of the HTTP::Daemon is reponsible for forking if that is
desirable. Also note that the user is responsible for generating
responses that conform to the HTTP/1.1 protocol. The
HTTP::Daemon::ClientConn class provides some methods that make this easier.
The following is a list of methods that are new (or enhanced) relative to the IO::Socket::INET base class.
$d = new HTTP::Daemon LocalAddr => 'www.someplace.com', LocalPort => 80;
accept([$pkg])
The HTTP::Daemon::ClientConn is also a IO::Socket::INET
subclass. Instances of this class are returned by the accept()
method
of HTTP::Daemon. The following additional methods are
provided:
get_request([$headers_only])
undef
if reading of the request fails. If it fails, then the
HTTP::Daemon::ClientConn object ($c) should be discarded, and you
should not call this method again. The $c->reason method might give
you some information about why $c->get_request returned undef
.
The $c->get_request method supports HTTP/1.1 request content bodies, including chunked transfer encoding with footer and self delimiting multipart/* content types.
The $c->get_request method will normally not return until the whole request has been received from the client. This might not be what you want if the request is an upload of a multi-mega-byte file (and with chunked transfer encoding HTTP can even support infinite request messages - uploading live audio for instance). If you pass a TRUE value as the $headers_only argument, then $c->get_request will return immediately after parsing the request headers and you are responsible for reading the rest of the request content. If you are going to call $c->get_request again on the same connection you better read the correct number of bytes.
read_buffer([$new_value])
If you handle the reading of the request content yourself you need to empty this buffer before you read more and you need to place unconsumed bytes here. You also need this buffer if you implement services like 101 Switching Protocols.
This method always return the old buffer content and can optionally replace the buffer content if you pass it an argument.
undef
you can obtain a short string
describing why it happened by calling $c->reason.
proto_ge($proto)
This attribute is turned on automatically if the client announces protocol HTTP/1.0 or worse and does not include a ``Connection: Keep-Alive'' header. It is also turned on automatically when HTTP/1.1 or better clients send the ``Connection: close'' request header.
The content attribute of the HTTP::Response object can be a normal string or a subroutine reference. If it is a subroutine, then whatever this callback routine returns is written back to the client as the response content. The routine will be called until it return an undefined or empty value. If the client is HTTP/1.1 aware then we will use chunked transfer encoding for the response.
send_file_response($filename)
RFC 2068
the IO::Socket::INET manpage, the Apache manpage
Copyright 1996-2001, Gisle Aas
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
HTTP::Daemon - a simple http server class |