| 1 | package AnyEvent::HTTPD::HTTPServer; | ||||||||||||||||||||
| 2 | 12 | ▶ | use common::sense; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 12 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 12 | ▶ | use Object::Event; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 12 | ▶ | use AnyEvent::Handle; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 12 | ▶ | use AnyEvent::Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 12 | ▶ | use AnyEvent::HTTPD::HTTPConnection; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 12 | 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 | 11 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 33 | 11 | my $this = shift; | |||||||||||||||||||
| 34 | 11 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 35 | 11 | my $self = { | |||||||||||||||||||
| 36 | connection_class => "AnyEvent::HTTPD::HTTPConnection", | ||||||||||||||||||||
| 37 | allowed_methods => [ qw/GET HEAD POST/ ], | ||||||||||||||||||||
| 38 | @_, | ||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||
| 40 | 11 | bless $self, $class; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 11 | my $rself = $self; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 11 | weaken $self; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | $self->{srv} = | ||||||||||||||||||||
| 47 | 15 | ▶ | tcp_server $self->{host}, $self->{port}, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.4
| |||||||||||||||||||||
| 48 | 15 | my ($fh, $host, $port) = @_; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 15 | ▶ | unless ($fh) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | $self->event (error => "couldn't accept client: $!"); | |||||||||||||||||||
| 52 | 0 | return; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 15 | $self->accept_connection ($fh, $host, $port); | |||||||||||||||||||
| 56 | 11 | ▶ | }, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 57 | 11 | my ($fh, $host, $port) = @_; | |||||||||||||||||||
| 58 | 11 | $self->{real_port} = $port; | |||||||||||||||||||
| 59 | 11 | $self->{real_host} = $host; | |||||||||||||||||||
| 60 | 11 | return $self->{backlog}; | |||||||||||||||||||
| 61 | 11 | }; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 11 | return $self | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 19 | ▶ | sub port { $_[0]->{real_port} } | ||||||||||||||||||
|
Subroutine
port: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | ▶ | sub host { $_[0]->{real_host} } | ||||||||||||||||||
|
Subroutine
host: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 2 | ▶ | sub allowed_methods { $_[0]->{allowed_methods} } | ||||||||||||||||||
|
Subroutine
allowed_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 15 | ▶ | sub accept_connection { | ||||||||||||||||||
|
Subroutine
accept_connection: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 73 | 15 | my ($self, $fh, $h, $p) = @_; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 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 | 15 | host => $h, | |||||||||||||||||||
| 82 | port => $p); | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 15 | $self->{handles}->{$htc} = $htc; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 15 | weaken $self; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 12 | ▶ | $htc->reg_cb (disconnect => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 89 | 12 | ▶ | if (defined $self) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 12 | delete $self->{handles}->{$_[0]}; | |||||||||||||||||||
| 91 | 12 | $self->event (disconnect => $_[0], $_[1]); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 15 | }); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 15 | $self->event (connect => $htc); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 12 | 1; | |||||||||||||||||||