blib/lib/HTTP/Server/Simple/Er.pm

filter: Statementstmt 49.6% 76 / 153 Branchbran 17.3% 8 / 46 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 57.1% 16 / 28 Podpod 100% 10 / 10 totaltotal 42.1% 110 / 261 CC 31 SCAR 40.1
CC
31
Cov
39%
output
210.0
form_data
20.0
headers
12.0
CRAP 55.2
↑ Summary
1 package HTTP::Server::Simple::Er;
2 5 $VERSION = v0.0.4;
3  
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use HTTP::Status ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 HTTP::Server::Simple::Er - lightweight server and interface
17  
18 =head1 SYNOPSIS
19  
20 use HTTP::Server::Simple::Er;
21 HTTP::Server::Simple::Er->new(port => 8089,
22 req_handler => sub {
23 my $self = shift;
24 my $path = $self->path;
25 ...
26 $self->output(404, "can't find it");
27 }
28 )->run;
29  
30 =head1 ABOUT
31  
32 This is mostly an API experiment. You might be perfectly happy with
33 HTTP::Server::Simple, but I find that I often want to use it only in
34 tests and that the interface is a little clunky for that, so I'm
35 gathering some of the handiness that has been sitting on my hard drive
36 and starting to get it on CPAN.
37  
38 =cut
39  
40 5 my @PROPS = qw(method protocol query_string
41 request_uri path localname localport peername peeraddr);
42 5 ▶use Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 with 'new';
44 5 ri 'listener_cb';
45 5 rw @PROPS;
46 5 ri 'port';
47 5 ri 'req_handler';
48 5 ro 'headers';
49 5 ri 'child_pid';
50 5 ▶no Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # ick, make our accessors overwrite those
53 5 ▶use base; base->import('HTTP::Server::Simple');
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 =head2 new
56  
57 my $server = HTTP::Server::Simple::Er->new(%props);
58  
59 =cut
60  
61 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
62 4 my $class = shift;
63 4 ▶ croak('odd number of elements in argument list') if(@_ % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 4 4
64 4 my $self = {@_};
65 4 bless($self, $class);
66 4 return($self);
67 } # end subroutine new definition
68 ########################################################################
69  
70 =begin internals
71  
72 =head2 run
73  
74 $server->run;
75  
76 =cut
77  
78 2 ▶sub run {
Subroutine run: called CC=2 SCAR=7.1
Pod documented
79 2 my $self = shift;
80 2 ▶ $self->set_port(8080) unless($self->port);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->port 0 2 2
81 2 $self->SUPER::run(@_);
82 } # end subroutine run definition
83 ########################################################################
84  
85 =head2 setup_listener
86  
87 Used by child_server() to callback once we're setup.
88  
89 $server->setup_listener;
90  
91 =cut
92  
93 2 ▶sub setup_listener {
Subroutine setup_listener: called CC=2 SCAR=11.6
Pod documented
94 2 my $self = shift;
95 2 $self->SUPER::setup_listener;
96 0 ▶ if(my $cb = $self->listener_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->listener_cb) 0 0 0
97 0 $cb->();
98 }
99 } # end subroutine setup_listener definition
100 ########################################################################
101  
102 =head2 setup
103  
104 $self->setup(%blah);
105  
106 =cut
107  
108 0 ▶sub setup {
Subroutine setup: not called CC=2 SCAR=17.9
Pod documented
109 0 my $self = shift;
110 0 ▶ while(my ($item, $value) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
111 0 my $setter = 'set_' . $item;
112 0 $self->$setter($value);
113 }
114 } # end subroutine setup definition
115 ########################################################################
116  
117 =head2 headers
118  
119 $self->headers($ref);
120  
121 =cut
122  
123 0 ▶sub headers {
Subroutine headers: not called CC=3 SCAR=24.8
Pod documented
124 0 my $self = shift;
125 0 my ($ref) = @_;
126 0 ▶ $ref or return($self->{headers});
Branch: 1 branch
BranchTrueFalseTotal
$ref or return $self->{"headers"} 0 0 0
127 0 my %headers = @$ref;
128 0 my $h = $self->{headers} = HTTP::Headers->new;
129 0 ▶ while(my ($k, $v) = each(%headers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %headers) 0 0 0
130 0 $h->header($k, $v);
131 }
132 } # end subroutine headers definition
133 ########################################################################
134  
135 =end internals
136  
137 =head2 child_server
138  
139 Starts the server as a child process.
140  
141 my $url = $server->child_server;
142  
143 =cut
144  
145 4 ▶sub child_server {
Subroutine child_server: called CC=7 SCAR=23.8
Pod documented
146 4 my $self = shift;
147  
148 4 my $parent = $$;
149 4 my $win_event;
150 my $child;
151 4 my $cb;
152 4 my $kill_child;
153 4 ▶ if($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 4 4
154 0 require Win32::Event;
155 0 $win_event = Win32::Event->new();
156 0 ▶ $kill_child = sub { kill 9, $child; sleep 1 while kill 0, $child; };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
kill 0, $child and do { sleep 1 } 0 0 0
157 0 ▶ $cb = sub {$win_event->pulse};
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 }
159 else {
160 4 ▶ $kill_child = sub { kill INT => $child; };
Subroutine __ANON__: called CC=1 SCAR=0.0
161 0 ▶ $cb = sub {kill USR1 => $parent};
Subroutine __ANON__: not called CC=1 SCAR=1.2
162 }
163 4 $self->set_listener_cb($cb);
164  
165 4 my $child_loaded = 0;
166 4 local %SIG;
167 4 ▶ if(not $^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "MSWin32") 4 0 4
168 0 ▶ $SIG{USR1} = sub { $child_loaded = 1; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
169 }
170  
171 4 ▶ local *print_banner = sub {}; # silence this thing
Subroutine __ANON__: not called CC=1 SCAR=0.0
172  
173 4 ▶ $self->set_port(8080) unless($self->port);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->port 0 4 4
174 4 $child = $self->background;
175 2 ▶ $child =~ /^-?\d+$/ or
Branch: 1 branch
BranchTrueFalseTotal
$child =~ /^-?\d+$/ or croak("background() didn't return a valid pid") 2 0 2
176 croak("background() didn't return a valid pid");
177 2 $self->set_child_pid($child);
178  
179 # hooks to handle our zombies:
180 0 ▶ $SIG{INT} = sub { # TODO should really be stacked handlers?
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 warn "interrupt";
182 0 $kill_child->();
183 # rethrow: INT *shouldn't* run END blocks => exit/die is wrong
184 0 $SIG{INT} = 'DEFAULT'; kill INT => $$;
185 2 };
186 2 eval(q(END {&$kill_child}));
187  
188 2 ▶ if($win_event) {
Branch: 1 branch
BranchTrueFalseTotal
if ($win_event) { } 0 2 2
189 0 $win_event->wait;
190 }
191 else {
192 2 ▶ local $SIG{CHLD} = sub { croak "child died"; };
Subroutine __ANON__: called CC=1 SCAR=0.0
193 2 ▶ 1 while(not $child_loaded);
Branch: 1 branch
BranchTrueFalseTotal
$child_loaded or do { '???' } 0 2525979 2525979
194 }
195 0 return("http://localhost:" . $self->port);
196 } # end subroutine child_server definition
197 ########################################################################
198  
199 =head2 handler
200  
201 You may override this, or simply set C<req_handler> before calling run.
202  
203 $server->handler;
204  
205 =cut
206  
207 0 ▶sub handler {
Subroutine handler: not called CC=2 SCAR=17.9
Pod documented
208 0 my $self = shift;
209 0 ▶ my $h = $self->req_handler or
Branch: 1 branch
BranchTrueFalseTotal
my $h = $self->req_handler or croak("req_handler not defined or overridden") 0 0 0
210 croak("req_handler not defined or overridden");
211 0 $h->($self);
212 } # end subroutine handler definition
213 ########################################################################
214  
215 =head2 output
216  
217 Takes status code from $params{status} or a leading number. Otherwise,
218 sets it to 200.
219  
220 $self->output(\%params, @strings);
221  
222 $self->output(501, \%params, @strings);
223  
224 $self->output(501, @strings);
225  
226 $self->output(@strings);
227  
228 The code may also be an 'RC_*' string which corresponds to a constant
229 from HTTP::Status.
230  
231 $self->output(RC_NOT_FOUND => @error_html);
232  
233 =cut
234  
235 0 ▶sub output {
Subroutine output: not called CC=14 SCAR=53.5
Pod documented
236 0 my $self = shift;
237 0 my @args = @_;
238  
239 # allow leading code and/or leading params ref
240 0 ▶ my $code = ($args[0] =~ m/^RC_|^\d\d\d$/) ? shift(@args) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args[0] =~ /^RC_|^\d\d\d$/ ? : 0 0 0
241 0 my %p;
242 0 ▶ if((ref($args[0])||'') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $args[0] || "") eq "HASH") 0 0 0
Condition0%0/2
ref $args[0] || ''
l!l
00
MC/DC0%0/1
ref $args[0] || ''
ref $args[0]
Truth table0%0/2
A
A= ref $args[0]
Aresult
11
00
243 0 %p = %{shift(@args)};
244 0 ▶ ($code and $p{status}) and die "cannot have status twice"
Branch: 1 branch
BranchTrueFalseTotal
$code and $p{"status"} and die "cannot have status twice" 0 0 0
Condition0%0/3
$code and $p{"status"}
!ll&&!rl&&r
000
MC/DC0%0/2
$code and $p{"status"}
$code $p{"status"}
Truth table0%0/3
A and B
A= $codeB= $p{"status"}
ABresult
0X0
100
111
245 }
246 # let subclasses pass a trailing hashref
247 0 ▶ if(((ref($args[-1]))||'') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $args[-1] || "") eq "HASH") 0 0 0
Condition0%0/2
ref $args[-1] || ''
l!l
00
MC/DC0%0/1
ref $args[-1] || ''
ref $args[-1]
Truth table0%0/2
A
A= ref $args[-1]
Aresult
11
00
248 0 my $also = pop(@args);
249 0 my @k = keys(%$also);
250 0 @p{@k} = @$also{@k};
251 }
252 0 ▶ $code = $p{status} ||= $code ||= 200;
Condition0%0/5
$p{"status"} ||= ($code ||= 200)
l!l&&r!l&&!r
000
$code ||= 200
l!l
00
MC/DC0%0/2
$p{"status"} ||= ($code ||= 200)
$p{"status"} ($code ||= 200)
MC/DC0%0/1
$code ||= 200
$code
Truth table0%0/3
A ||= B
A= $p{"status"}B= ($code ||= 200)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $code
Aresult
11
00
253 0 ▶ if($code =~ m/^RC_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^RC_/) 0 0 0
254 0 ▶ my $sub = HTTP::Status->can($code) or
Branch: 1 branch
BranchTrueFalseTotal
my $sub = "HTTP::Status"->can($code) or croak("$code is not a valid RC_* constant in HTTP::Status") 0 0 0
255 croak("$code is not a valid RC_* constant in HTTP::Status");
256 0 $p{status} = $code = $sub->();
257 }
258  
259 # "servers MUST include a Date header"
260 0 ▶ $p{Date} ||= HTTP::Date::time2str(time);
Condition0%0/3
$p{"Date"} ||= &HTTP::Date::time2str(time)
l!l&&r!l&&!r
000
MC/DC0%0/2
$p{"Date"} ||= &HTTP::Date::time2str(time)
$p{"Date"} &HTTP::Date::time2str(time)
Truth table0%0/3
A ||= B
A= $p{"Date"}B= &HTTP::Date::time2str(time)
ABresult
1X1
011
000
261  
262 0 my $h = HTTP::Headers->new(%p);
263 0 ▶ $h->content_type('text/html') unless($h->content_type);
Branch: 1 branch
BranchTrueFalseTotal
unless $h->content_type 0 0 0
264  
265 0 my $data = join("\r\n", @args);
266 0 $h->content_length(length($data));
267  
268 0 my $message = HTTP::Status::status_message($code);
269 0 print join("\r\n",
270 "HTTP/1.1 $code $message",
271 $h->as_string, '');
272 0 print $data;
273 } # end subroutine output definition
274 ########################################################################
275  
276 =head2 params
277  
278 Return a hash of parameters parsed from $self->query_string;
279  
280 my %params = $server->params;
281  
282 =cut
283  
284 0 ▶sub params {
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
285 0 my $self = shift;
286  
287 0 my $s = $self->query_string;
288 # XXX check for correctness
289 0 return map({URI::Escape::uri_unescape($_)}
290 0 map({split(/=/, $_, 2)} split(/&/, $s)));
291 } # params #############################################################
292  
293 =head2 form_data
294  
295 Retrieve POSTed form data. If an element is mentioned twice, its value
296 automatically becomes an arrayref.
297  
298 my %form = $server->form_data;
299  
300 =cut
301  
302 0 ▶sub form_data {
Subroutine form_data: not called CC=4 SCAR=30.0
Pod documented
303 0 my $self = shift;
304  
305 0 my $h = $self->headers;
306 0 my $s;
307 0 my $fh = $self->stdio_handle;
308 0 read($fh, $s, $h->{'content-length'});
309  
310 # XXX check for correctness
311 0 my %d;
312 0 foreach my $pair (split(/&/, $s)) {
313 0 my ($k,$v) = map({$_ = URI::Escape::uri_unescape($_); s/\+/ /g; $_}
314 split(/=/, $pair, 2));
315 0 ▶ if($d{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d{$k}) { } 0 0 0
316 0 ▶ $d{$k} = [$d{$k}] unless(ref $d{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d{$k} 0 0 0
317 0 push(@{$d{$k}}, $v);
318 }
319 else {
320 0 $d{$k} = $v;
321 }
322 }
323 0 return(%d);
324 } # form_data ##########################################################
325  
326 =head1 AUTHOR
327  
328 Eric Wilhelm @ <ewilhelm at cpan dot org>
329  
330 http://scratchcomputing.com/
331  
332 =head1 BUGS
333  
334 If you found this module on CPAN, please report any bugs or feature
335 requests through the web interface at L<http://rt.cpan.org>. I will be
336 notified, and then you'll automatically be notified of progress on your
337 bug as I make changes.
338  
339 If you pulled this development version from my /svn/, please contact me
340 directly.
341  
342 =head1 COPYRIGHT
343  
344 Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
345  
346 =head1 NO WARRANTY
347  
348 Absolutely, positively NO WARRANTY, neither express or implied, is
349 offered with this software. You use this software at your own risk. In
350 case of loss, no person or entity owes you anything whatsoever. You
351 have been warned.
352  
353 =head1 LICENSE
354  
355 This program is free software; you can redistribute it and/or modify it
356 under the same terms as Perl itself.
357  
358 =cut
359  
360 # vi:ts=2:sw=2:et:sta
361 5 1;
↑ Summary