Mojo/Server/CGI.pm

filter: Statementstmt 13.1% 5 / 38 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 33.3% 1 / 3 Podpod 100% 1 / 1 totaltotal 7.4% 7 / 94 CC 18 SCAR 50.4
CC
18
Cov
6%
run
210.0
_write
30.0
BEGIN
1.0
CRAP 154.6
« Mojo/Server/Prefork.pm ↑ Summary Mojo/Server/Morbo.pm »
1 package Mojo::Server::CGI;
2 1 ▶use Mojo::Base 'Mojo::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 has 'nph';
5  
6 0 ▶sub run {
Subroutine run: not called CC=14 SCAR=53.5
Pod documented
7 0 my $self = shift;
8  
9 0 $self->app->server($self);
10 0 my $tx = $self->build_tx;
11 0 my $req = $tx->req->parse(\%ENV);
12 0 $tx->local_port($ENV{SERVER_PORT})->remote_address($ENV{REMOTE_ADDR});
13  
14 # Request body (may block if we try to read too much)
15 0 binmode STDIN;
16 0 my $len = $req->headers->content_length;
17 0 ▶ until ($req->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->is_finished) 0 0 0
18 0 ▶ my $chunk = ($len && $len < 131072) ? $len : 131072;
Branch: 1 branch
BranchTrueFalseTotal
$len && $len < 131072 ? : 0 0 0
Condition0%0/3
$len and $len < 131072
!ll&&!rl&&r
000
MC/DC0%0/2
$len and $len < 131072
$len $len < 131072
Truth table0%0/3
A and B
A= $lenB= $len < 131072
ABresult
0X0
100
111
19 0 ▶ last unless my $read = STDIN->read(my $buffer, $chunk, 0);
Branch: 1 branch
BranchTrueFalseTotal
unless my $read = "STDIN"->read(my $buffer, $chunk, 0) 0 0 0
20 0 $req->parse($buffer);
21 0 ▶ last if ($len -= $read) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($len -= $read) <= 0 0 0 0
22 }
23  
24 0 $self->emit(request => $tx);
25  
26 # Response start-line
27 0 STDOUT->autoflush(1);
28 0 binmode STDOUT;
29 0 my $res = $tx->res->fix_headers;
30 0 ▶ return undef if $self->nph && !_write($res, 'get_start_line_chunk');
Branch: 1 branch
BranchTrueFalseTotal
if $self->nph and not _write($res, "get_start_line_chunk") 0 0 0
Condition0%0/3
$self->nph and not _write($res, "get_start_line_chunk")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->nph and not _write($res, "get_start_line_chunk")
$self->nph not _write($res, "get_start_line_chunk")
Truth table0%0/3
A and not(B)
A= $self->nphB= not _write($res, "get_start_line_chunk")
ABresult
0X0
100
111
31  
32 # Response headers
33 0 ▶ my $code = $res->code || 404;
Condition0%0/2
$res->code || 404
l!l
00
MC/DC0%0/1
$res->code || 404
$res->code
Truth table0%0/2
A
A= $res->code
Aresult
11
00
34 0 ▶ my $msg = $res->message || $res->default_message;
Condition0%0/3
$res->message || $res->default_message
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->message || $res->default_message
$res->message $res->default_message
Truth table0%0/3
A || B
A= $res->messageB= $res->default_message
ABresult
1X1
011
000
35 0 ▶ $res->headers->status("$code $msg") unless $self->nph;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->nph 0 0 0
36 0 ▶ return undef unless _write($res, 'get_header_chunk');
Branch: 1 branch
BranchTrueFalseTotal
unless _write($res, "get_header_chunk") 0 0 0
37  
38 # Response body
39 0 ▶ return undef unless $tx->is_empty || _write($res, 'get_body_chunk');
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->is_empty or _write($res, "get_body_chunk") 0 0 0
Condition0%0/3
$tx->is_empty or _write($res, "get_body_chunk")
l!l&&r!l&&!r
000
MC/DC0%0/2
$tx->is_empty or _write($res, "get_body_chunk")
$tx->is_empty _write($res, "get_body_chunk")
Truth table0%0/3
A or B
A= $tx->is_emptyB= _write($res, "get_body_chunk")
ABresult
1X1
011
000
40  
41 # Finish transaction
42 0 $tx->closed;
43  
44 0 return $res->code;
45 }
46  
47 0 ▶sub _write {
Subroutine _write: not called CC=5 SCAR=34.0
48 0 my ($res, $method) = @_;
49  
50 0 my $offset = 0;
51 0 while (1) {
52  
53 # No chunk yet, try again
54 0 ▶ sleep 1 and next unless defined(my $chunk = $res->$method($offset));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $chunk = $res->$method($offset)) 0 0 0
Condition0%0/5
defined(my $chunk = $res->$method($offset)) || sleep 1 && (next)
l!l&&r!l&&!r
000
sleep 1 and next
!ll
00
MC/DC0%0/2
defined(my $chunk = $res->$method($offset)) || sleep 1 && (next)
defined(my $chunk = $res->$method($offset)) sleep 1
Truth table0%0/3
A || B
A= defined(my $chunk = $res->$method($offset))B= sleep 1
ABresult
1X1
011
000
55  
56 # End of part
57 0 ▶ last unless my $len = length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless my $len = length $chunk 0 0 0
58  
59 # Make sure we can still write
60 0 $offset += $len;
61 0 ▶ return undef unless STDOUT->opened;
Branch: 1 branch
BranchTrueFalseTotal
unless "STDOUT"->opened 0 0 0
62 0 print STDOUT $chunk;
63 }
64  
65 0 return 1;
66 }
67  
68 1 1;
69  
70 =encoding utf8
71  
72 =head1 NAME
73  
74 Mojo::Server::CGI - CGI server
75  
76 =head1 SYNOPSIS
77  
78 use Mojo::Server::CGI;
79  
80 my $cgi = Mojo::Server::CGI->new;
81 $cgi->unsubscribe('request')->on(request => sub ($cgi, $tx) {
82  
83 # Request
84 my $method = $tx->req->method;
85 my $path = $tx->req->url->path;
86  
87 # Response
88 $tx->res->code(200);
89 $tx->res->headers->content_type('text/plain');
90 $tx->res->body("$method request for $path!");
91  
92 # Resume transaction
93 $tx->resume;
94 });
95 $cgi->run;
96  
97 =head1 DESCRIPTION
98  
99 L<Mojo::Server::CGI> is a simple and portable implementation of L<RFC 3875|https://tools.ietf.org/html/rfc3875>.
100  
101 See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more.
102  
103 =head1 EVENTS
104  
105 L<Mojo::Server::CGI> inherits all events from L<Mojo::Server>.
106  
107 =head1 ATTRIBUTES
108  
109 L<Mojo::Server::CGI> inherits all attributes from L<Mojo::Server> and implements the following new ones.
110  
111 =head2 nph
112  
113 my $bool = $cgi->nph;
114 $cgi = $cgi->nph($bool);
115  
116 Activate non-parsed header mode.
117  
118 =head1 METHODS
119  
120 L<Mojo::Server::CGI> inherits all methods from L<Mojo::Server> and implements the following new ones.
121  
122 =head2 run
123  
124 my $status = $cgi->run;
125  
126 Run CGI.
127  
128 =head1 SEE ALSO
129  
130 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
131  
132 =cut
« Mojo/Server/Prefork.pm ↑ Summary Mojo/Server/Morbo.pm »