| 1 | package IO::Lambda::HTTP::Server; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use vars qw(@ISA @EXPORT_OK $DEBUG); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | @EXPORT = qw(http_server); | |||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use base qw(Exporter IO::Lambda); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | our $DEBUG = $IO::Lambda::DEBUG{httpd} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$IO::Lambda::DEBUG{'httpd'} || 0
MC/DC0%0/1
$IO::Lambda::DEBUG{'httpd'} || 0
$IO::Lambda::DEBUG{'httpd'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'httpd'}
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Socket; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use IO::Lambda qw(:lambda :stream); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use IO::Lambda::Socket qw(accept); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | my $CRLF = "\x0d\x0a"; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | sub _close($$) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | warn "$_[1]\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_close: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | close($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | sub _msg | ||||||||||||||||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my ( $status, $msg, $close) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_msg: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $resp = "HTTP/1.1 $status${CRLF}Content-Length: ".length($msg)."$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $resp .= "Connection: ".($close ? 'close' : 'keep-alive')."$CRLF"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $resp .= "Date: ". scalar(localtime).$CRLF; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $resp .= "Content-Type: text/plain$CRLF" if length($msg); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $resp .= $CRLF . $msg; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | return $resp; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | sub _bye | ||||||||||||||||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my ( $self, $conn, $close, $msg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bye: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $resp = _msg( $msg, '', $close); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | context writebuf, $conn, \$resp, length($resp), 0, $self->{timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ( $close ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | warn "[$self->{sessions}->{$conn}->{remote}] disconnect\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( !close($conn)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | warn "close error:$!\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | }} | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | sub _bad_request | ||||||||||||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ( $self, $conn, $close) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bad_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->_bye( $conn, $close, "400 Bad Request"); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | sub _timeout | ||||||||||||||||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my ( $self, $conn) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timeout: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $self->_bye($conn, 1, "408 Timeout"); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | sub handle_connection | ||||||||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my ($self, $conn, $cb) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_connection: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my %session; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $session_data = $self->{sessions}->{"$conn"}; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $buf = ''; | |||||||||||||||||||||||||||||||||||||||||
| 72 | lambda { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $session_data->{active} = 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | context readbuf, $conn, \$buf, 1, $self->{timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 75 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $session_data->{active} = 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | context readbuf, $conn, \$buf, qr/^.*?\x{0D}?\x{0A}\x{0D}?\x{0A}/s, $self->{timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my ( $match, $error) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return $self->_timeout($conn) if defined($error) and $error eq 'timeout'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $error and $error eq "timeout"
MC/DC0%0/2
defined $error and $error eq "timeout"
defined $error
$error eq "timeout"
Truth table0%0/3
A and B
A= defined $errorB= $error eq "timeout"
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return _close $conn, $error unless defined $match; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | warn length($buf), " bytes read\n" if $DEBUG > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $req = HTTP::Request-> parse( $match); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return $self->_bad_request($conn, 1) unless $req; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $proto = (( $req->protocol // '') =~ /^HTTP\/([\d\.]+)$/i) ? $1 : 1.0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$req->protocol // ""
MC/DC0%0/1
$req->protocol // ""
$req->protocol
Truth table0%0/2
A
A= $req->protocol
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $keep_alive = | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$proto >= 1.1 && lc($req->header('Connection') // 'keep-alive') eq 'keep-alive'
$req->header("Connection") // "keep-alive"
MC/DC0%0/2
$proto >= 1.1 && lc($req->header('Connection') // 'keep-alive') eq 'keep-alive'
$proto >= 1.1
$req->header("Connection")
Truth table0%0/3
A && B
A= $proto >= 1.1B= $req->header("Connection")
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | $proto >= 1.1 && | ||||||||||||||||||||||||||||||||||||||||||
| 90 | (lc( $req->header('Connection') // 'keep-alive') eq 'keep-alive'); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $keep_alive = 0 if $self->{shutdown}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $cl = length($match) + ($req->header('Content-Length') // 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$req->header("Content-Length") // 0
MC/DC0%0/1
$req->header("Content-Length") // 0
$req->header("Content-Length")
Truth table0%0/2
A
A= $req->header("Content-Length")
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | context readbuf, $conn, \$buf, $cl, $self->{timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my ( undef, $error) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return $self->_timeout($conn) if defined($error) and $error eq 'timeout'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $error and $error eq "timeout"
MC/DC0%0/2
defined $error and $error eq "timeout"
defined $error
$error eq "timeout"
Truth table0%0/3
A and B
A= defined $errorB= $error eq "timeout"
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return _close $conn, $error if defined $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | warn length($buf), " bytes read\n" if $DEBUG > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | unless ($req = HTTP::Request-> parse( $buf)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | return lambda { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | context $self->_bad_request($conn, !$keep_alive); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | this->start if $keep_alive && !($self->{shutdown} && !length($buf)); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
Condition0%0/6
$keep_alive and not $self->{"shutdown"} && !length($buf)
$self->{'shutdown'} && !length($buf)
MC/DC0%0/3
$keep_alive and not $self->{"shutdown"} && !length($buf)
$keep_alive
$self->{'shutdown'}
!length($buf)
Truth table0%0/4
A and not(B && not(C))
A= $keep_aliveB= $self->{'shutdown'}C= !length($buf)
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | }}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | substr( $buf, 0, $cl, ''); | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $resp; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | eval { ($resp, $error) = $cb->($req, \%session); }; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $error = $@; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | context UNIVERSAL::isa( $resp, 'IO::Lambda') ? | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $resp : lambda { $resp, $error }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ($resp, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $keep_alive = 0 if $self->{shutdown}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( $error ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $resp = _msg("500 Server Error", $error, !$keep_alive); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } elsif ( UNIVERSAL::isa( $resp, 'HTTP::Response')) { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $resp->header(Connection => ($keep_alive ? 'keep-alive' : 'close')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $resp = "HTTP/1.1 " . $resp->as_string($CRLF); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $resp = _msg("200 OK", $resp // '', !$keep_alive); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$resp // ""
MC/DC0%0/1
$resp // ""
$resp
Truth table0%0/2
A
A= $resp
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | context writebuf, $conn, \$resp, length($resp), 0, $self->{timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 131 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my ( undef, $error) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | return _close $conn, $error if defined $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | warn length($resp), " bytes written\n" if $DEBUG > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return this->start if $keep_alive && !($self->{shutdown} && !length($buf)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$keep_alive and not $self->{"shutdown"} && !length($buf)
$self->{'shutdown'} && !length($buf)
MC/DC0%0/3
$keep_alive and not $self->{"shutdown"} && !length($buf)
$keep_alive
$self->{'shutdown'}
!length($buf)
Truth table0%0/4
A and not(B && not(C))
A= $keep_aliveB= $self->{'shutdown'}C= !length($buf)
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | warn "[$session_data->{remote}] disconnect\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ( !close($conn)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | warn "error during response:$!\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | }}}}}} | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | sub http_server(&$;@) | ||||||||||||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | my ( $cb, $listen, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
http_server: called CC=5 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | my $port = 80; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | unless ( ref $listen ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | ($listen, $port) = ($1, $2) if $listen =~ /^(.*)\:(\d+)$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | $listen = IO::Socket::INET->new( | |||||||||||||||||||||||||||||||||||||||||
| 152 | Listen => 5, | ||||||||||||||||||||||||||||||||||||||||||
| 153 | LocalAddr => $listen, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | LocalPort => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 155 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||
| 156 | ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | ▶ | unless ( $listen ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | warn "$!\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | return (undef, $!); | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $port = $listen->sockport; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | return __PACKAGE__->new( | |||||||||||||||||||||||||||||||||||||||||
| 166 | socket => $listen, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | port => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 168 | callback => $cb, | ||||||||||||||||||||||||||||||||||||||||||
| 169 | %opt | ||||||||||||||||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | ▶ | my ( $class, %opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | my $cb = delete $opt{callback}; | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | my $self; | |||||||||||||||||||||||||||||||||||||||||
| 180 | $self = lambda { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | context $self->{socket}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=5.2
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | $self->{accept_event} = accept { | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | return if $self->{shutdown}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=31.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | my $conn = shift; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | $self->{accept_event} = again; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | unless ( ref($conn)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | warn "accept() error:$conn\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | $conn-> blocking(0); | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | my $sess = $self->{sessions}->{"$conn"} = { | |||||||||||||||||||||||||||||||||||||||||
| 194 | active => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 195 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $sess->{remote} = inet_ntoa((sockaddr_in(getsockname($conn)))[1]); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | warn "[$sess->{remote}] connect\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $sess->{handler} = handle_connection($self, $conn, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | context $sess->{handler}; | |||||||||||||||||||||||||||||||||||||||||
| 201 | tail { | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | delete $self->{sessions}->{"$conn"}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | }}}; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | $self->{$_} = $opt{$_} for qw(socket port timeout); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | $self->{sessions} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | $self->{shutdown} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | sub shutdown | ||||||||||||||||||||||||||||||||||||||||||
| 212 | { | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shutdown: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $self->{shutdown} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | $self->cancel_event($self->{accept_event}); | |||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $_->{handler}->terminate for grep { !$_->{active}} values %{ $self->{sessions}}; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | IO::Lambda::HTTP::Server - simple httpd server | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | The module exports a single function C<http_server> that accepts a callback | ||||||||||||||||||||||||||||||||||||||||||
| 228 | and a socket, with optional parameters. The callback accepts a C<HTTP::Request> | ||||||||||||||||||||||||||||||||||||||||||
| 229 | object, and is expected to return either a C<HTTP::Response> object or a lambda | ||||||||||||||||||||||||||||||||||||||||||
| 230 | that in turn returns a a C<HTTP::Response> object. | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||||
| 235 | use IO::Lambda qw(:all); | ||||||||||||||||||||||||||||||||||||||||||
| 236 | use IO::Lambda::HTTP qw(http_request); | ||||||||||||||||||||||||||||||||||||||||||
| 237 | use IO::Lambda::HTTP::Server; | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | my $server = http_server { | ||||||||||||||||||||||||||||||||||||||||||
| 240 | my $req = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 241 | if ( $req->uri =~ /weather/) { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | context( HTTP::Request-> new( GET => "http://www.google.com/?q=weather")); | ||||||||||||||||||||||||||||||||||||||||||
| 243 | return &http_request; | ||||||||||||||||||||||||||||||||||||||||||
| 244 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 245 | return HTTP::Response->new(200, 'OK', ['Content-Type' => 'text/plain'], "hello world"); | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | } "localhost:80"; | ||||||||||||||||||||||||||||||||||||||||||
| 248 | $server->start; # runs in 'background' now | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | =head1 API | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =item http_server &callback, $socket, [ %options ] | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | Creates lambda that listens on C<$socket>, that is either a C<IO::Socket::INET> object | ||||||||||||||||||||||||||||||||||||||||||
| 257 | or a string such as C<"localhost"> or C<"127.0.0.1:9999">. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | The callback accepts a C<HTTP::Request> object, and is expected to return | ||||||||||||||||||||||||||||||||||||||||||
| 260 | either a C<HTTP::Response> object or a lambda that in turn returns a a | ||||||||||||||||||||||||||||||||||||||||||
| 261 | C<HTTP::Response> object. | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | Options: | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | =item timeout $integer | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | Connection timeout or a deadline. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | =item shutdown | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | Enter a graceful shutdown mode - stop accepting new connections, handle the | ||||||||||||||||||||||||||||||||||||||||||
| 276 | running ones, and stop after all connections are served. | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | L<IO::Lambda>, L<HTTP::Request>, L<HTTP::Response> | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||||||||||||||||