Lambda/HTTP/Server.pm

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