| 1 | package CGI::Parse::PSGI; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | our @EXPORT_OK = qw( parse_cgi_output ); | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use IO::File; # perl bug: should be loaded to call ->getline etc. on filehandle/PerlIO | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | sub parse_cgi_output { | ||||||||||||||||||||||||||||
| 10 | 8 | ▶ | my $output = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse_cgi_output: called CC=15 SCAR=27.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8 | my $length; | |||||||||||||||||||||||||||
| 13 | 8 | ▶ | if (ref $output eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 3 | $length = length $$output; | |||||||||||||||||||||||||||
| 15 | 3 | open my $io, "<", $output; | |||||||||||||||||||||||||||
| 16 | 3 | $output = $io; | |||||||||||||||||||||||||||
| 17 | } else { | ||||||||||||||||||||||||||||
| 18 | 5 | ▶ | open my $tmp, '<&=:perlio:raw', fileno($output) or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 5 | $output = $tmp; | |||||||||||||||||||||||||||
| 20 | 5 | $length = -s $output; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 8 | my $headers; | |||||||||||||||||||||||||||
| 24 | 8 | ▶ | while ( my $line = $output->getline ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 24 | $headers .= $line; | |||||||||||||||||||||||||||
| 26 | 24 | ▶ | last if $headers =~ /\x0d?\x0a\x0d?\x0a$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 8 | ▶ | unless ( defined $headers ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a"; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 8 | ▶ | unless ( $headers =~ /^HTTP/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 8 | $headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 8 | my $response = HTTP::Response->parse($headers); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # RFC 3875 6.2.3 | ||||||||||||||||||||||||||||
| 39 | 8 | ▶ | if ($response->header('Location') && !$response->header('Status')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$response->header("Location") and not $response->header("Status")
MC/DC100%2/2
$response->header("Location") and not $response->header("Status")
$response->header("Location")
not $response->header("Status")
Truth table100%3/3
A and not(B)
A= $response->header("Location")B= not $response->header("Status")
| |||||||||||||||||||||||||||||
| 40 | 1 | $response->header('Status', 302); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 8 | ▶ | my $status = $response->header('Status') || 200; | ||||||||||||||||||||||||||
|
Condition100%2/2
$response->header('Status') || 200
MC/DC100%1/1
$response->header('Status') || 200
$response->header('Status')
Truth table100%2/2
A
A= $response->header('Status')
| |||||||||||||||||||||||||||||
| 44 | 8 | $status =~ s/\s+.*$//; # remove ' OK' in '200 OK' | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 8 | $response->remove_header('Status'); # PSGI doesn't allow having Status header in the response | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 8 | my $remaining = $length - tell( $output ); | |||||||||||||||||||||||||||
| 49 | 8 | ▶ | if ( $response->code == 500 && !$remaining ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response->code == 500 and not $remaining
MC/DC0%0/2
$response->code == 500 and not $remaining
$response->code == 500
not $remaining
Truth table33%1/3
A and not(B)
A= $response->code == 500B= not $remaining
| |||||||||||||||||||||||||||||
| 50 | return [ | ||||||||||||||||||||||||||||
| 51 | 0 | 500, | |||||||||||||||||||||||||||
| 52 | [ 'Content-Type' => 'text/html' ], | ||||||||||||||||||||||||||||
| 53 | [ $response->error_as_HTML ] | ||||||||||||||||||||||||||||
| 54 | ]; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # TODO we can pass $output to the response body without buffering all? | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 8 | my $length = 0; | |||||||||||||||||||||||||||
| 61 | 8 | ▶ | while ( $output->read( my $buffer, 4096 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 7 | $length += length($buffer); | |||||||||||||||||||||||||||
| 63 | 7 | $response->add_content($buffer); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 8 | ▶ | if ( $length && !$response->content_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$length and not $response->content_length
MC/DC100%2/2
$length and not $response->content_length
$length
not $response->content_length
Truth table100%3/3
A and not(B)
A= $lengthB= not $response->content_length
| |||||||||||||||||||||||||||||
| 67 | 5 | $response->content_length($length); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | return [ | ||||||||||||||||||||||||||||
| 72 | $status, | ||||||||||||||||||||||||||||
| 73 | +[ | ||||||||||||||||||||||||||||
| 74 | map { | ||||||||||||||||||||||||||||
| 75 | 8 | my $k = $_; | |||||||||||||||||||||||||||
| 76 | 19 | map { ( $k => _cleanup_newline($_) ) } $response->headers->header($_); | |||||||||||||||||||||||||||
| 77 | } $response->headers->header_field_names | ||||||||||||||||||||||||||||
| 78 | ], | ||||||||||||||||||||||||||||
| 79 | [$response->content], | ||||||||||||||||||||||||||||
| 80 | ]; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub _cleanup_newline { | ||||||||||||||||||||||||||||
| 84 | 19 | ▶ | local $_ = shift; | ||||||||||||||||||||||||||
|
Subroutine
_cleanup_newline: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 19 | s/\r?\n//g; | |||||||||||||||||||||||||||
| 86 | 19 | return $_; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 6 | 1; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||