added host parameter.
[AnyEvent-HTTPD.git] / lib / AnyEvent / HTTPD / HTTPServer.pm
blobf01343e4bd58e04872322a338e96cfb7f5a675db
1 package AnyEvent::HTTPD::HTTPServer;
2 use strict;
3 no warnings;
5 use Object::Event;
6 use AnyEvent::Handle;
7 use AnyEvent::Socket;
9 use AnyEvent::HTTPD::HTTPConnection;
11 our @ISA = qw/Object::Event/;
13 =head1 NAME
15 AnyEvent::HTTPD::HTTPServer - A simple and plain http server
17 =head1 DESCRIPTION
19 This class handles incoming TCP connections for HTTP clients.
20 It's used by L<AnyEvent::HTTPD> to do it's job.
22 It has no public interface yet.
24 =head1 COPYRIGHT & LICENSE
26 Copyright 2008 Robin Redeker, all rights reserved.
28 This program is free software; you can redistribute it and/or modify it
29 under the same terms as Perl itself.
31 =cut
33 sub new {
34 my $this = shift;
35 my $class = ref($this) || $this;
36 my $self = { @_ };
37 bless $self, $class;
39 tcp_server $self->{host}, $self->{port}, sub {
40 my ($fh) = @_;
41 unless ($fh) {
42 $self->event (error => "couldn't accept client: $!");
43 return;
45 $self->accept_connection ($fh);
48 return $self
51 sub accept_connection {
52 my ($self, $fh) = @_;
54 my $htc = AnyEvent::HTTPD::HTTPConnection->new (fh => $fh);
55 $self->{handles}->{$htc} = $htc;
57 $htc->reg_cb (disconnect => sub {
58 delete $self->{handles}->{$_[0]};
59 $self->event (disconnect => $_[0], $_[1])
60 });
62 $self->event (connect => $htc);