VERSION

Version 0.93

SYNOPSIS

    use AnyEvent::HTTPD;

    my $httpd = AnyEvent::HTTPD->new (port => 9090);

    $httpd->reg_cb (
       '/' => sub {
          my ($httpd, $req) = @_;

          $req->respond ({ content => ['text/html',
             "<html><body><h1>Hello World!</h1>"
             . "<a href=\"/test\">another test page</a>"
             . "</body></html>"
          ]});
       },
       '/test' => sub {
          my ($httpd, $req) = @_;

          $req->respond ({ content => ['text/html',
             "<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 \s-1HTTPD\s0 for serving simple web application interfaces. It's completely event based and independend from any event loop by using the AnyEvent module.

It's \s-1HTTP\s0 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 \s-1HTTP\s0 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 \*(C`samples/\*(C' directory in the AnyEvent::HTTPD distribution for basic starting points.

FEATURES

  • support for \s-1GET\s0 and \s-1POST\s0 requests.

  • support for \s-1HTTP\s0 1.0 keep-alive.

  • processing of \*(C`x-www-form-urlencoded\*(C' and \*(C`multipart/form-data\*(C' (\*(C`multipart/mixed\*(C') encoded form parameters.

  • support for streaming responses.

  • with version 0.8 no more dependend on \s-1LWP\s0 for HTTP::Date.

  • (limited) support for \s-1SSL\s0

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 \s-1API\s0 (see the documentation of Object::Event for details).

For a list of available events see below in the \s-1EVENTS\s0 section.

new (%args)

This is the constructor for a AnyEvent::HTTPD object. The %args hash may contain one of these key/value pairs:

The \s-1TCP\s0 address of the \s-1HTTP\s0 server will listen on. Usually 0.0.0.0 (the default), for a public server, or 127.0.0.1 for a local server. The \s-1TCP\s0 port the \s-1HTTP\s0 server will listen on. If undefined some free port will be used. You can get it via the \*(C`port\*(C' method. If this option is given the server will listen for a \s-1SSL/TLS\s0 connection on the configured port. As $tls_ctx you can pass anything that you can pass as \*(C`tls_ctx\*(C' 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 (...), ); This will set the request timeout for connections. The default value is 60 seconds. 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 \*(C`man 2 listen\*(C'. By default will be set by AnyEvent::Socket\*(C`::tcp_server\*(C' to 128. 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. 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. This parameter sets the allowed \s-1HTTP\s0 methods for requests, defaulting to \s-1GET\s0, \s-1HEAD\s0 and \s-1POST\s0. 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 \s-1HTTP\s0 methods, possibly as set by the allowed_methods argument to the constructor.

stop_request

When the server walks the request \s-1URI\s0 path upwards you can stop the walk by calling this method. You can even stop further handling after the \*(C`request\*(C' 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' my ($httpd, $req) = @_;

# ... } );

run

This method is a simplification of the \*(C`AnyEvent\*(C' condition variable idiom. You can use it instead of writing: my $cvar = AnyEvent->condvar; $cvar->wait;

stop

This will stop the \s-1HTTP\s0 server and return from the \*(C`run\*(C' method if you started the server via that method!

EVENTS

Every request goes to a specific \s-1URL\s0. After a (\s-1GET\s0 or \s-1POST\s0) request is received the \s-1URL\s0's path segments are walked down and for each segment a event is generated. An example:

If the \s-1URL\s0 '/test/bla.jpg' is requestes following events will be generated:

'/test/bla.jpg' - the event for the last segment '/test' - the event for the 'test' segment '' - the root event of each request

To actually handle any request you just have to register a callback for the event name with the empty string. To handle all requests in the '/test' directory you have to register a callback for the event with the name '/test'. Here is an example how to register an event for the example \s-1URL\s0 above:

$httpd->reg_cb ( '/test/bla.jpg' => sub { my ($httpd, $req) = @_;

$req->respond ([200, 'ok', { 'Content-Type' => 'text/html' }, '<h1>Test</h1>' }]); } );

See also \*(C`stop_request\*(C' about stopping the walk of the path segments.

The first argument to such a callback is always the AnyEvent::HTTPD object itself. The second argument ($req) is the AnyEvent::HTTPD::Request object for this request. It can be used to get the (possible) form parameters for this request or the transmitted content and respond to the request.

Along with the above mentioned events these events are also provided: Every request also emits the \*(C`request\*(C' event, with the same arguments and semantics as the above mentioned path request events. You can use this to implement your own request multiplexing. You can use \*(C`stop_request\*(C' to stop any further processing of the request as the \*(C`request\*(C' event is the first thing that is executed for an incoming request. An example of one of many possible uses: $httpd->reg_cb ( request => sub { my ($httpd, $req) = @_;

my $url = $req->url;

if ($url->path =~ /\/images\/img_(\d+).jpg$/) { handle_image_request ($req, $1); # your task :)

# stop the request from emitting further events # so that the '/images/img_001.jpg' and the # '/images' and '' events are NOT emitted: $httpd->stop_request; } } ); These events are emitted whenever a client coming from \*(C`$host:$port\*(C' connects to your server or is disconnected from it.

CACHING

Any response from the \s-1HTTP\s0 server will have \*(C`Cache-Control\*(C' set to \*(C`max-age=0\*(C' and also the \*(C`Expires\*(C' header set to the \*(C`Date\*(C' header. Meaning: Caching is disabled.

You can of course set those headers yourself in the response, or remove them by setting them to undef, but keep in mind that the default for those headers are like mentioned above.

If you need more support here you can send me a mail or even better: a patch :)

AUTHOR

Robin Redeker, \*(C`<elmex at ta-sa.org>\*(C'

BUGS

Please report any bugs or feature requests to \*(C`bug-bs-httpd at rt.cpan.org\*(C', or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-HTTPD <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-HTTPD>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc AnyEvent::HTTPD

You can also look for information at:

  • Git repository http://git.ta-sa.org/AnyEvent-HTTPD.git <http://git.ta-sa.org/AnyEvent-HTTPD.git>

  • \s-1RT:\s0 \s-1CPAN\s0's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-HTTPD <http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-HTTPD>

  • AnnoCPAN: Annotated \s-1CPAN\s0 documentation http://annocpan.org/dist/AnyEvent-HTTPD <http://annocpan.org/dist/AnyEvent-HTTPD>

  • \s-1CPAN\s0 Ratings http://cpanratings.perl.org/d/AnyEvent-HTTPD <http://cpanratings.perl.org/d/AnyEvent-HTTPD>

  • Search \s-1CPAN\s0 http://search.cpan.org/dist/AnyEvent-HTTPD <http://search.cpan.org/dist/AnyEvent-HTTPD>

ACKNOWLEDGEMENTS

Andrey Smirnov - for keep-alive patches. Pedro Melo - for valuable input in general and patches. Nicholas Harteau - patch for ';' pair separator support, patch for allowed_methods support Chris Kastorff - patch for making default headers removable and more fault tolerant w.r.t. case. Mons Anderson - Optimizing the regexes in L<AnyEvent::HTTPD::HTTPConnection> and adding the C<backlog> option to L<AnyEvent::HTTPD>.

COPYRIGHT & LICENSE

Copyright 2008-2011 Robin Redeker, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.