HTTP::Daemon - A simple http server class |
HTTP::Daemon - A simple http server class
version 6.04
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->uri->path eq "/xyzzy") { # remember, this is *not* recommended 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
subclass 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. The returned value will be an HTTP::Daemon::ClientConn
object which is another IO::Socket::INET
subclass. Calling the
get_request()
method on this object will read data from the client and
return an HTTP::Request
object. The ClientConn object also provide
methods to send back various responses.
This HTTP daemon does not fork(2)
for you. Your application, i.e. the
user of the HTTP::Daemon
is responsible 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 following methods of HTTP::Daemon
are new (or enhanced) relative
to the IO::Socket::INET
base class:
IO::Socket::INET
constructor, but unlike its base class it can also
be called without any arguments. The daemon will then set up a listen
queue of 5 connections and allocate some random port number.
A server that wants to bind to some specific address on the standard HTTP port will be constructed like this:
$d = HTTP::Daemon->new( LocalAddr => 'www.thisplace.com', LocalPort => 80, );
See the IO::Socket::INET manpage for a description of other arguments that can be used configure the daemon during construction.
HTTP::Daemon::ClientConn
reference by default. If a
package name is provided as argument, then the returned object will be
blessed into the given class. It is probably a good idea to make that
class a subclass of HTTP::Daemon::ClientConn
.
The accept method will return undef
if timeouts have been enabled
and no connection is made within the given time. The timeout()
method
is described in the IO::Socket manpage.
In list context both the client object and the peer address will be returned; see the description of the accept method the IO::Socket manpage for details.
Server
response header. The
main reason to have this method is that subclasses can override it if
they want to use another product name.
The default is the string ``libwww-perl-daemon/#.##'' where ``#.##'' is replaced with the version number of this module.
The HTTP::Daemon::ClientConn
is a IO::Socket::INET
subclass. Instances of this class are returned by the accept()
method
of HTTP::Daemon
. The following methods are provided:
HTTP::Request
object which is returned. It returns undef
if reading fails. If it fails, then the HTTP::Daemon::ClientConn
object ($c) should be discarded, and you should not try call this
method again on it. The $c->reason method might give you some
information about why $c->get_request failed.
The 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 large 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 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.
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 returns 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.
HEAD
request. No content
body must be generated for these requests.
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.
See the description of send_status_line()
for the description of the
accepted arguments.
HTTP::Response
object to the
client as a response. We try hard to make sure that the response is
self delimiting so that the connection can stay persistent for further
request/response exchanges.
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.
IO::Handle
or glob.
HTTP::Daemon
object.
RFC 2616
the IO::Socket::INET manpage, the IO::Socket manpage
bugs may be submitted through https://github.com/libwww-perl/HTTP-Daemon/issues.
There is also a mailing list available for users of this distribution, at mailto:libwww@perl.org.
There is also an irc channel available for users of this distribution, at
#lwp
on irc.perl.org
.
Gisle Aas <gisle@activestate.com>
This software is copyright (c) 1995 by Gisle Aas.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
HTTP::Daemon - A simple http server class |