| AnyEvent-HTTPD documentation | Contained in the AnyEvent-HTTPD distribution. |
AnyEvent::HTTPD::HTTPServer - A simple and plain http server
This class handles incoming TCP connections for HTTP clients. It's used by AnyEvent::HTTPD to do it's job.
It has no public interface yet.
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.
| AnyEvent-HTTPD documentation | Contained in the AnyEvent-HTTPD distribution. |
package AnyEvent::HTTPD::HTTPServer; use common::sense; use Scalar::Util qw/weaken/; use Object::Event; use AnyEvent::Handle; use AnyEvent::Socket; use AnyEvent::HTTPD::HTTPConnection; our @ISA = qw/Object::Event/;
sub new { my $this = shift; my $class = ref($this) || $this; my $self = { connection_class => "AnyEvent::HTTPD::HTTPConnection", allowed_methods => [ qw/GET HEAD POST/ ], @_, }; bless $self, $class; my $rself = $self; weaken $self; $self->{srv} = tcp_server $self->{host}, $self->{port}, sub { my ($fh, $host, $port) = @_; unless ($fh) { $self->event (error => "couldn't accept client: $!"); return; } $self->accept_connection ($fh, $host, $port); }, sub { my ($fh, $host, $port) = @_; $self->{real_port} = $port; $self->{real_host} = $host; return $self->{backlog}; }; return $self } sub port { $_[0]->{real_port} } sub host { $_[0]->{real_host} } sub allowed_methods { $_[0]->{allowed_methods} } sub accept_connection { my ($self, $fh, $h, $p) = @_; my $htc = $self->{connection_class}->new ( fh => $fh, request_timeout => $self->{request_timeout}, allowed_methods => $self->{allowed_methods}, ssl => $self->{ssl}, host => $h, port => $p); $self->{handles}->{$htc} = $htc; weaken $self; $htc->reg_cb (disconnect => sub { if (defined $self) { delete $self->{handles}->{$_[0]}; $self->event (disconnect => $_[0], $_[1]); } }); $self->event (connect => $htc); } 1;