AnyEvent-HTTPD
view release on metacpan or search on metacpan
lib/AnyEvent/HTTPD.pm view on Meta::CPAN
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.93
=cut
our $VERSION = '0.93';
=head1 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
=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<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 C<samples/> directory
in the L<AnyEvent::HTTPD> distribution for basic starting points.
lib/AnyEvent/HTTPD.pm view on Meta::CPAN
);
Or:
my $httpd =
AnyEvent::HTTPD->new (
port => 443,
ssl => AnyEvent::TLS->new (...),
);
=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<man 2 listen>.
By default will be set by L<AnyEvent::Socket>C<::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<AnyEvent::HTTPD::HTTPServer>. This is only of interest to you if you plan
to subclass L<AnyEvent::HTTPD::HTTPConnection>.
=item request_class => $class
This is a special parameter that you can use to pass your own request class
to L<AnyEvent::HTTPD>. This is only of interest to you if you plan
to subclass L<AnyEvent::HTTPD::Request>.
=item 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.
=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 ( scalar grep { $meth eq $_ } @{ $self->{allowed_methods} } ) {
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};
}
}
( run in 1.831 second using v1.01-cache-2.11-cpan-39bf76dae61 )