| 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 | 6 | our %DEFAULT_OPTS = ( | |||||||||||||||||||||||||||
| 10 | ignore_status_line => 0, | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 16 | ▶ | sub parse_cgi_output { | ||||||||||||||||||||||||||
|
Subroutine
parse_cgi_output: called CC=19 SCAR=29.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 16 | my $output = shift; | |||||||||||||||||||||||||||
| 15 | 16 | my $options = \%DEFAULT_OPTS; | |||||||||||||||||||||||||||
| 16 | 16 | ▶ | if (ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 4 | $options = { %DEFAULT_OPTS, %{ +shift } }; # Use default opts where none supplied | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 16 | my $length; | |||||||||||||||||||||||||||
| 21 | 16 | ▶ | if (ref $output eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 11 | $length = length $$output; | |||||||||||||||||||||||||||
| 23 | 11 | open my $io, "<", $output; | |||||||||||||||||||||||||||
| 24 | 11 | $output = $io; | |||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||
| 26 | 5 | ▶ | open my $tmp, '<&=:perlio:raw', fileno($output) or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 5 | $output = $tmp; | |||||||||||||||||||||||||||
| 28 | 5 | $length = -s $output; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 16 | my $headers; | |||||||||||||||||||||||||||
| 32 | 16 | ▶ | while ( my $line = $output->getline ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 49 | $headers .= $line; | |||||||||||||||||||||||||||
| 34 | 49 | ▶ | last if $headers =~ /\x0d?\x0a\x0d?\x0a$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 16 | ▶ | unless ( defined $headers ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a"; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 16 | ▶ | unless ( $headers =~ /^HTTP/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 10 | $headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 16 | my $response = HTTP::Response->parse($headers); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # RFC 3875 6.2.3 | ||||||||||||||||||||||||||||
| 47 | 16 | ▶ | 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")
| |||||||||||||||||||||||||||||
| 48 | 1 | $response->header('Status', 302); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $status = $options->{ignore_status_line}? | ||||||||||||||||||||||||||||
| 52 | 16 | ▶ | 200 : ($response->code || 200); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$response->code or 200
MC/DC0%0/1
$response->code or 200
$response->code
Truth table50%1/2
A
A= $response->code
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 16 | my $status_header = $response->header('Status'); | |||||||||||||||||||||||||||
| 55 | 16 | ▶ | if ($status_header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | # Use the header status preferentially, if present and well formed | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Extract the code from the header (should be 3 digits, non zero) | ||||||||||||||||||||||||||||
| 59 | 6 | my ($code) = ($status_header =~ /^ \s* (\d+) /x); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 6 | ▶ | $status = $code || $status; | ||||||||||||||||||||||||||
|
Condition33%1/3
$code || $status
MC/DC0%0/2
$code || $status
$code
$status
Truth table33%1/3
A || B
A= $codeB= $status
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 16 | $response->remove_header('Status'); # PSGI doesn't allow having Status header in the response | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 16 | my $remaining = $length - tell( $output ); | |||||||||||||||||||||||||||
| 67 | 16 | ▶ | 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
| |||||||||||||||||||||||||||||
| 68 | return [ | ||||||||||||||||||||||||||||
| 69 | 0 | 500, | |||||||||||||||||||||||||||
| 70 | [ 'Content-Type' => 'text/html' ], | ||||||||||||||||||||||||||||
| 71 | [ $response->error_as_HTML ] | ||||||||||||||||||||||||||||
| 72 | ]; | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # TODO we can pass $output to the response body without buffering all? | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 16 | my $length = 0; | |||||||||||||||||||||||||||
| 79 | 16 | ▶ | while ( $output->read( my $buffer, 4096 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 15 | $length += length($buffer); | |||||||||||||||||||||||||||
| 81 | 15 | $response->add_content($buffer); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 16 | ▶ | 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
| |||||||||||||||||||||||||||||
| 85 | 13 | $response->content_length($length); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | return [ | ||||||||||||||||||||||||||||
| 90 | $status, | ||||||||||||||||||||||||||||
| 91 | +[ | ||||||||||||||||||||||||||||
| 92 | map { | ||||||||||||||||||||||||||||
| 93 | 16 | my $k = $_; | |||||||||||||||||||||||||||
| 94 | 35 | map { ( $k => _cleanup_newline($_) ) } $response->headers->header($_); | |||||||||||||||||||||||||||
| 95 | } $response->headers->header_field_names | ||||||||||||||||||||||||||||
| 96 | ], | ||||||||||||||||||||||||||||
| 97 | [$response->content], | ||||||||||||||||||||||||||||
| 98 | ]; | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 35 | ▶ | sub _cleanup_newline { | ||||||||||||||||||||||||||
|
Subroutine
_cleanup_newline: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 35 | local $_ = shift; | |||||||||||||||||||||||||||
| 103 | 35 | s/\r?\n//g; | |||||||||||||||||||||||||||
| 104 | 35 | return $_; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 6 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||