blib/lib/PAGI/App/WrapCGI.pm

filter: Statementstmt 78.6% 48 / 61 Branchbran 40.9% 9 / 22 Conditioncond 50.0% 13 / 26 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 58.4% 76 / 130 CC 27 SCAR 41.4
CC
27
Cov
64%
__ANON__
76.9
new
2.1
to_app
1.1
CRAP 62.7
« lib/PAGI/App/WebSocket/Broadcast.pm ↑ Summary blib/lib/PAGI/App/WrapPSGI.pm »
1 package PAGI::App::WrapCGI;
2 3 $PAGI::App::WrapCGI::VERSION = '0.002002';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 PAGI::App::WrapCGI - Execute CGI scripts as PAGI apps
10  
11 =head1 SYNOPSIS
12  
13 use PAGI::App::WrapCGI;
14  
15 my $app = PAGI::App::WrapCGI->new(
16 script => '/path/to/script.cgi',
17 )->to_app;
18  
19 =cut
20  
21 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
22 2 my ($class, %args) = @_;
23  
24 return bless {
25 script => $args{script},
26 2 ▶ timeout => $args{timeout} // 30,
Condition50%1/2
$args{"timeout"} // 30
l!l
02
MC/DC0%0/1
$args{"timeout"} // 30
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
27 }, $class;
28 }
29  
30 2 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.6
Pod undocumented
31 2 my ($self) = @_;
32  
33 2 my $script = $self->{script};
34 2 my $timeout = $self->{timeout};
35  
36 2 ▶ return async sub {
Subroutine __ANON__: called CC=26 SCAR=43.4
37 2 my ($scope, $receive, $send) = @_;
38 2 ▶ die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} ne "http" 0 2 2
39  
40 # Build CGI environment
41 my %env = (
42 REQUEST_METHOD => $scope->{method},
43 SCRIPT_NAME => $scope->{root_path} // '',
44 PATH_INFO => $scope->{path},
45 QUERY_STRING => $scope->{query_string} // '',
46 SERVER_PROTOCOL => 'HTTP/' . ($scope->{http_version} // '1.1'),
47 SERVER_NAME => $scope->{server}[0] // 'localhost',
48 SERVER_PORT => $scope->{server}[1] // 80,
49 REMOTE_ADDR => $scope->{client}[0] // '',
50 2 ▶ REMOTE_PORT => $scope->{client}[1] // 0,
Condition50%7/14
$scope->{"root_path"} // ""
l!l
20
$scope->{"query_string"} // ""
l!l
02
$scope->{"http_version"} // "1.1"
l!l
02
$scope->{"server"}[0] // "localhost"
l!l
02
$scope->{"server"}[1] // 80
l!l
02
$scope->{"client"}[0] // ""
l!l
02
$scope->{"client"}[1] // 0
l!l
02
MC/DC0%0/1
$scope->{"root_path"} // ""
$scope->{"root_path"}
MC/DC0%0/1
$scope->{"query_string"} // ""
$scope->{"query_string"}
MC/DC0%0/1
$scope->{"http_version"} // "1.1"
$scope->{"http_version"}
MC/DC0%0/1
$scope->{"server"}[0] // "localhost"
$scope->{"server"}[0]
MC/DC0%0/1
$scope->{"server"}[1] // 80
$scope->{"server"}[1]
MC/DC0%0/1
$scope->{"client"}[0] // ""
$scope->{"client"}[0]
MC/DC0%0/1
$scope->{"client"}[1] // 0
$scope->{"client"}[1]
Truth table50%1/2
A
A= $scope->{"root_path"}
Aresult
11
00
Truth table50%1/2
A
A= $scope->{"query_string"}
Aresult
11
00
Truth table50%1/2
A
A= $scope->{"http_version"}
Aresult
11
00
Truth table50%1/2
A
A= $scope->{"server"}[0]
Aresult
11
00
Truth table50%1/2
A
A= $scope->{"server"}[1]
Aresult
11
00
Truth table50%1/2
A
A= $scope->{"client"}[0]
Aresult
11
00
Truth table50%1/2
A
A= $scope->{"client"}[1]
Aresult
11
00
51 GATEWAY_INTERFACE => 'CGI/1.1',
52 );
53  
54 # Add headers
55 2 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
20
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
56 0 my ($name, $value) = @$h;
57 0 my $key = uc($name);
58 0 $key =~ s/-/_/g;
59 0 ▶ if ($key eq 'CONTENT_TYPE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'CONTENT_TYPE') { } 0 0 0
elsif ($key eq 'CONTENT_LENGTH') { } 0 0 0
60 0 $env{CONTENT_TYPE} = $value;
61 } elsif ($key eq 'CONTENT_LENGTH') {
62 0 $env{CONTENT_LENGTH} = $value;
63 } else {
64 0 $env{"HTTP_$key"} = $value;
65 }
66 }
67  
68 # Collect body
69 2 my $body = '';
70 2 while (1) {
71 2 my $event = await $receive->();
72 2 ▶ last if $event->{type} ne 'http.request';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"type"} ne "http.request" 0 2 2
73 2 ▶ $body .= $event->{body} // '';
Condition50%1/2
$event->{"body"} // ""
l!l
20
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
Aresult
11
00
74 2 ▶ last unless $event->{more};
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"more"} 2 0 2
75 }
76  
77 # Execute CGI
78 2 local %ENV = %env;
79 2 my $pid = open my $fh, '-|';
80  
81 2 ▶ unless (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 2 2
82 0 await $send->({
83 type => 'http.response.start',
84 status => 500,
85 headers => [['content-type', 'text/plain']],
86 });
87 0 await $send->({ type => 'http.response.body', body => 'Internal Server Error', more => 0 });
88 0 return;
89 }
90  
91 2 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 1 1 2
92 # Child - run CGI
93 1 ▶ if (length $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body) 0 1 1
94 0 open my $stdin, '<', \$body;
95 0 *STDIN = $stdin;
96 }
97 1 ▶ exec($script) or exit(1);
Branch: 1 branch
BranchTrueFalseTotal
exec $script or exit 1 0 0 0
98 }
99  
100 # Parent - read output
101 1 local $/;
102 1 my $output = <$fh>;
103 1 close $fh;
104  
105 # Parse CGI output
106 1 ▶ my ($headers, $resp_body) = split /\r?\n\r?\n/, $output // '', 2;
Condition50%1/2
$output // ""
l!l
10
MC/DC0%0/1
$output // ""
$output
Truth table50%1/2
A
A= $output
Aresult
11
00
107 1 my @resp_headers;
108 1 my $status = 200;
109  
110 1 ▶ for my $line (split /\r?\n/, $headers // '') {
Condition50%1/2
$headers // ""
l!l
10
MC/DC0%0/1
$headers // ""
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
111 1 my ($name, $value) = split /:\s*/, $line, 2;
112 1 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 1 1
113 1 ▶ if (lc($name) eq 'status') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq 'status') { } 0 1 1
114 0 ($status) = $value =~ /^(\d+)/;
115 } else {
116 1 push @resp_headers, [lc($name), $value];
117 }
118 }
119  
120 await $send->({
121 type => 'http.response.start',
122 status => $status,
123 headers => \@resp_headers,
124 1 });
125 1 ▶ await $send->({ type => 'http.response.body', body => $resp_body // '', more => 0 });
Condition50%1/2
$resp_body // ""
l!l
10
MC/DC0%0/1
$resp_body // ""
$resp_body
Truth table50%1/2
A
A= $resp_body
Aresult
11
00
126 2 };
127 }
128  
129 3 1;
130  
131 __END__
« lib/PAGI/App/WebSocket/Broadcast.pm ↑ Summary blib/lib/PAGI/App/WrapPSGI.pm »