File Coverage

blib/lib/AnyEvent/HTTPD/HTTPServer.pm
Criterion Covered Total %
statement 44 47 93.6
branch 2 4 50.0
condition 1 3 33.3
subroutine 13 14 92.8
pod 1 5 20.0
total 61 73 83.5


line stmt bran cond sub pod time code
1             package AnyEvent::HTTPD::HTTPServer;
2 12     12   77 use common::sense;
  12         17  
  12         72  
3 12     12   569 use Scalar::Util qw/weaken/;
  12         32  
  12         865  
4 12     12   15685 use Object::Event;
  12         88697  
  12         517  
5 12     12   12983 use AnyEvent::Handle;
  12         106480  
  12         468  
6 12     12   135 use AnyEvent::Socket;
  12         25  
  12         11832  
7              
8 12     12   9595 use AnyEvent::HTTPD::HTTPConnection;
  12         46  
  12         9330  
9              
10             our @ISA = qw/Object::Event/;
11              
12             =head1 NAME
13              
14             AnyEvent::HTTPD::HTTPServer - A simple and plain http server
15              
16             =head1 DESCRIPTION
17              
18             This class handles incoming TCP connections for HTTP clients.
19             It's used by L<AnyEvent::HTTPD> to do it's job.
20              
21             It has no public interface yet.
22              
23             =head1 COPYRIGHT & LICENSE
24              
25             Copyright 2008-2011 Robin Redeker, all rights reserved.
26              
27             This program is free software; you can redistribute it and/or modify it
28             under the same terms as Perl itself.
29              
30             =cut
31              
32             sub new {
33 11     11 1 28 my $this = shift;
34 11   33     71 my $class = ref($this) || $this;
35 11         96 my $self = {
36             connection_class => "AnyEvent::HTTPD::HTTPConnection",
37             allowed_methods => [ qw/GET HEAD POST/ ],
38             @_,
39             };
40 11         37 bless $self, $class;
41              
42 11         24 my $rself = $self;
43              
44 11         60 weaken $self;
45              
46             $self->{srv} =
47             tcp_server $self->{host}, $self->{port}, sub {
48 15     15   15944 my ($fh, $host, $port) = @_;
49              
50 15 50       65 unless ($fh) {
51 0         0 $self->event (error => "couldn't accept client: $!");
52 0         0 return;
53             }
54              
55 15         140 $self->accept_connection ($fh, $host, $port);
56             }, sub {
57 11     11   2594 my ($fh, $host, $port) = @_;
58 11         37 $self->{real_port} = $port;
59 11         32 $self->{real_host} = $host;
60 11         42 return $self->{backlog};
61 11         274 };
62              
63 11         890 return $self
64             }
65              
66 19     19 0 1215 sub port { $_[0]->{real_port} }
67              
68 0     0 0 0 sub host { $_[0]->{real_host} }
69              
70 2     2 0 288 sub allowed_methods { $_[0]->{allowed_methods} }
71              
72             sub accept_connection {
73 15     15 0 44 my ($self, $fh, $h, $p) = @_;
74              
75 15         216 my $htc =
76             $self->{connection_class}->new (
77             fh => $fh,
78             request_timeout => $self->{request_timeout},
79             allowed_methods => $self->{allowed_methods},
80             ssl => $self->{ssl},
81             host => $h,
82             port => $p);
83              
84 15         85 $self->{handles}->{$htc} = $htc;
85              
86 15         47 weaken $self;
87              
88             $htc->reg_cb (disconnect => sub {
89 12 50   12   271 if (defined $self) {
90 12         54 delete $self->{handles}->{$_[0]};
91 12         130 $self->event (disconnect => $_[0], $_[1]);
92             }
93 15         138 });
94              
95 15         1427 $self->event (connect => $htc);
96             }
97              
98             1;