AnyEvent-HTTPD
view release on metacpan or search on metacpan
"<html><body><h1>Test page</h1>"
. "<a href=\"/\">Back to the main page</a>"
. "</body></html>"
]});
},
);
$httpd->run; # making a AnyEvent condition variable would also work
DESCRIPTION
This module provides a simple HTTPD for serving simple web application
interfaces. It's completly event based and independend from any event
loop by using the AnyEvent module.
It's HTTP implementation is a bit hacky, so before using this module
make sure it works for you and the expected deployment. Feel free to
improve the HTTP support and send in patches!
The documentation is currently only the source code, but next versions
of this module will be better documented hopefully. See also the
"samples/" directory in the AnyEvent::HTTPD distribution for basic
starting points.
FEATURES
* support for GET and POST requests.
* support for HTTP 1.0 keep-alive.
* processing of "x-www-form-urlencoded" and "multipart/form-data"
("multipart/mixed") encoded form parameters.
* support for streaming responses.
* with version 0.8 no more dependend on LWP for HTTP::Date.
* (limited) support for SSL
METHODS
The AnyEvent::HTTPD class inherits directly from
AnyEvent::HTTPD::HTTPServer which inherits the event callback interface
from Object::Event.
Event callbacks can be registered via the Object::Event API (see the
documentation of Object::Event for details).
For a list of available events see below in the *EVENTS* section.
new (%args)
This is the constructor for a AnyEvent::HTTPD object. The %args hash
may contain one of these key/value pairs:
host => $host
The TCP address of the HTTP server will listen on. Usually
0.0.0.0 (the default), for a public server, or 127.0.0.1 for a
local server.
port => $port
The TCP port the HTTP server will listen on. If undefined some
free port will be used. You can get it via the "port" method.
ssl => $tls_ctx
If this option is given the server will listen for a SSL/TLS
connection on the configured port. As $tls_ctx you can pass
anything that you can pass as "tls_ctx" to an AnyEvent::Handle
object.
Example:
my $httpd =
AnyEvent::HTTPD->new (
port => 443,
ssl => { cert_file => "/path/to/my/server_cert_and_key.pem" }
);
Or:
my $httpd =
AnyEvent::HTTPD->new (
port => 443,
ssl => AnyEvent::TLS->new (...),
);
request_timeout => $seconds
This will set the request timeout for connections. The default
value is 60 seconds.
backlog => $int
The backlog argument defines the maximum length the queue of
pending connections may grow to. The real maximum queue length
will be 1.5 times more than the value specified in the backlog
argument.
See also "man 2 listen".
By default will be set by AnyEvent::Socket"::tcp_server" to 128.
connection_class => $class
This is a special parameter that you can use to pass your own
connection class to AnyEvent::HTTPD::HTTPServer. This is only of
interest to you if you plan to subclass
AnyEvent::HTTPD::HTTPConnection.
request_class => $class
This is a special parameter that you can use to pass your own
request class to AnyEvent::HTTPD. This is only of interest to
you if you plan to subclass AnyEvent::HTTPD::Request.
allowed_methods => $arrayref
This parameter sets the allowed HTTP methods for requests,
defaulting to GET, HEAD and POST. Each request received is
matched against this list, and a '501 not implemented' is
returned if no match is found. Requests using disallowed
handlers will never trigger callbacks.
port
Returns the port number this server is bound to.
host
Returns the host/ip this server is bound to.
allowed_methods
Returns an arrayref of allowed HTTP methods, possibly as set by the
allowed_methods argument to the constructor.
stop_request
When the server walks the request URI path upwards you can stop the
walk by calling this method. You can even stop further handling
after the "request" event.
Example:
$httpd->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
# ...
$httpd->stop_request; # will prevent that the callback below is called
},
'' => sub { # this one wont be called by a request to '/test'
( run in 1.124 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )