package AnyEvent::HTTPD; use common::sense; use Scalar::Util qw/weaken/; use URI; use AnyEvent::HTTPD::Request; use AnyEvent::HTTPD::Util; use base qw/AnyEvent::HTTPD::HTTPServer/; =head1 NAME AnyEvent::HTTPD - A simple lightweight event based web (application) server =head1 VERSION Version 0.84 =cut our $VERSION = '0.84'; =head1 SYNOPSIS use AnyEvent::HTTPD; my $httpd = AnyEvent::HTTPD->new (port => 9090); $httpd->reg_cb ( '/' => sub { my ($httpd, $req) = @_; $req->respond ({ content => ['text/html', "

Hello World!

" . "another test page" . "" ]}); }, '/test' => sub { my ($httpd, $req) = @_; $req->respond ({ content => ['text/html', "

Test page

" . "Back to the main page" . "" ]}); }, ); $httpd->run; # making a AnyEvent condition variable would also work =head1 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 L 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 C directory in the L distribution for basic starting points. =head1 FEATURES =over 4 =item * support for GET and POST requests. =item * support for HTTP 1.0 keep-alive. =item * processing of C and C (C) encoded form parameters. =item * support for streaming responses. =item * with version 0.8 no more dependend on L for L. =back =head1 METHODS The L class inherits directly from L which inherits the event callback interface from L. Event callbacks can be registered via the L API (see the documentation of L for details). For a list of available events see below in the I section. =over 4 =item B This is the constructor for a L object. The C<%args> hash may contain one of these key/value pairs: =over 4 =item 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. =item 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 C method. =item request_timeout => $seconds This will set the request timeout for connections. The default value is 60 seconds. =item 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 C. By default will be set by LC<::tcp_server> to C<128>. =item connection_class => $class This is a special parameter that you can use to pass your own connection class to L. This is only of interest to you if you plan to subclass L. =item request_class => $class This is a special parameter that you can use to pass your own request class to L. This is only of interest to you if you plan to subclass L. =back =cut sub new { my $this = shift; my $class = ref($this) || $this; my $self = $class->SUPER::new ( request_class => "AnyEvent::HTTPD::Request", @_ ); $self->reg_cb ( connect => sub { my ($self, $con) = @_; weaken $self; $self->{conns}->{$con} = $con->reg_cb ( request => sub { my ($con, $meth, $url, $hdr, $cont) = @_; #d# warn "REQUEST: $meth, $url, [$cont] " . join (',', %$hdr) . "\n"; $url = URI->new ($url); if ($meth eq 'GET') { $cont = parse_urlencoded ($url->query); } if ($meth eq 'GET' or $meth eq 'POST') { weaken $con; $self->handle_app_req ( $meth, $url, $hdr, $cont, $con->{host}, $con->{port}, sub { $con->response (@_) if $con; }); } else { $con->response (200, "ok"); } } ); $self->event (client_connected => $con->{host}, $con->{port}); }, disconnect => sub { my ($self, $con) = @_; $con->unreg_cb (delete $self->{conns}->{$con}); $self->event (client_disconnected => $con->{host}, $con->{port}); }, ); $self->{state} ||= {}; return $self } sub handle_app_req { my ($self, $meth, $url, $hdr, $cont, $host, $port, $respcb) = @_; my $req = $self->{request_class}->new ( httpd => $self, method => $meth, url => $url, hdr => $hdr, parm => (ref $cont ? $cont : {}), content => (ref $cont ? undef : $cont), resp => $respcb, host => $host, port => $port, ); $self->{req_stop} = 0; $self->event (request => $req); return if $self->{req_stop}; my @evs; my $cururl = ''; for my $seg ($url->path_segments) { $cururl .= $seg; push @evs, $cururl; $cururl .= '/'; } for my $ev (reverse @evs) { $self->event ($ev => $req); last if $self->{req_stop}; } } =item B Returns the port number this server is bound to. =item B Returns the host/ip this server is bound to. =item B 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 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) = @_; # ... } ); =cut sub stop_request { my ($self) = @_; $self->{req_stop} = 1; } =item B This method is a simplification of the C condition variable idiom. You can use it instead of writing: my $cvar = AnyEvent->condvar; $cvar->wait; =cut sub run { my ($self) = @_; $self->{condvar} = AnyEvent->condvar; $self->{condvar}->wait; } =item B This will stop the HTTP server and return from the C method B =cut sub stop { $_[0]->{condvar}->broadcast if $_[0]->{condvar} } =back =head1 EVENTS Every request goes to a specific URL. After a (GET or POST) request is received the URL's path segments are walked down and for each segment a event is generated. An example: If the URL '/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 C<'/test'>. Here is an example how to register an event for the example URL above: $httpd->reg_cb ( '/test/bla.jpg' => sub { my ($httpd, $req) = @_; $req->respond ([200, 'ok', { 'Content-Type' => 'text/html' }, '

Test

' }]); } ); See also C about stopping the walk of the path segments. The first argument to such a callback is always the L object itself. The second argument (C<$req>) is the L 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: =over 4 =item request => $req Every request also emits the 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 to stop any further processing of the request as the C event is the first thing that is executed for an incoming request. =item client_connected => $host, $port =item client_disconnected => $host, $port These events are emitted whenever a client coming from C<$host:$port> connects to your server or is disconnected from it. =back =head1 CACHING Any response from the HTTP server will have C set to C and also the C header set to the C header. Meaning: Caching is disabled. If you need caching or would like to have it you can send me a mail or even better: a patch :) =head1 AUTHOR Robin Redeker, C<< >> =head1 ACKNOWLEDGEMENTS People who contributed to this module: =over 4 =item * Mons Anderson Optimizing the regexes in L and adding the C option to L. =back =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc AnyEvent::HTTPD You can also look for information at: =over 4 =item * Git repository L =item * RT: CPAN's request tracker L =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * Search CPAN L =back =head1 ACKNOWLEDGEMENTS Andrey Smirnov - for keep-alive patches. Pedro Melo - for valuable input in general and patches. =head1 COPYRIGHT & LICENSE Copyright 2008-2009 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. =cut 1; # End of AnyEvent::HTTPD