| 1 | package Furl::Response; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Furl::Headers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 8 | 3 | my ($class, $minor_version, $code, $message, $headers, $content) = @_; | |||||||||||||||||||||||||||
| 9 | 3 | bless { | |||||||||||||||||||||||||||
| 10 | minor_version => $minor_version, | ||||||||||||||||||||||||||||
| 11 | code => $code, | ||||||||||||||||||||||||||||
| 12 | message => $message, | ||||||||||||||||||||||||||||
| 13 | headers => Furl::Headers->new($headers), | ||||||||||||||||||||||||||||
| 14 | content => $content, | ||||||||||||||||||||||||||||
| 15 | }, $class; | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # DO NOT CALL this method DIRECTLY. | ||||||||||||||||||||||||||||
| 19 | 3 | ▶ | sub set_request_info { | ||||||||||||||||||||||||||
|
Subroutine
set_request_info: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 3 | my ($self, $request_src, $captured_req_headers, $captured_req_content) = @_; | |||||||||||||||||||||||||||
| 21 | 3 | $self->{request_src} = $request_src; | |||||||||||||||||||||||||||
| 22 | 3 | ▶ | if (defined $captured_req_headers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 2 | $self->{captured_req_headers} = $captured_req_headers; | |||||||||||||||||||||||||||
| 24 | 2 | $self->{captured_req_content} = $captured_req_content; | |||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||
| 26 | 1 | $self->{captured_req_headers} = undef; | |||||||||||||||||||||||||||
| 27 | 1 | $self->{captured_req_content} = undef; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 3 | return; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | sub captured_req_headers { | ||||||||||||||||||||||||||
|
Subroutine
captured_req_headers: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 1 | ▶ | unless (exists $self->{captured_req_headers}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | Carp::croak("You can't call cpatured_req_headers method without 'capture_request' options for Furl#new"); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 1 | return $self->{captured_req_headers}; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub captured_req_content { | ||||||||||||||||||||||||||
|
Subroutine
captured_req_content: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | unless (exists $self->{captured_req_content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | Carp::croak("You can't call cpatured_req_content method without 'capture_request' options for Furl#new"); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 1 | return $self->{captured_req_content}; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # accessors | ||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub code { shift->{code} } | ||||||||||||||||||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub message { shift->{message} } | ||||||||||||||||||||||||||
|
Subroutine
message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub headers { shift->{headers} } | ||||||||||||||||||||||||||
|
Subroutine
headers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub content { shift->{content} } | ||||||||||||||||||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (!exists $self->{request}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if (!exists $self->{request_src}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | Carp::croak("This request object does not have a request information"); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # my ($method, $uri, $headers, $content) = @_; | ||||||||||||||||||||||||||||
| 61 | $self->{request} = Furl::Request->new( | ||||||||||||||||||||||||||||
| 62 | $self->{request_src}->{method}, | ||||||||||||||||||||||||||||
| 63 | $self->{request_src}->{url}, | ||||||||||||||||||||||||||||
| 64 | $self->{request_src}->{headers}, | ||||||||||||||||||||||||||||
| 65 | $self->{request_src}->{content}, | ||||||||||||||||||||||||||||
| 66 | 0 | ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | return $self->{request}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # alias | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub status { shift->code() } | ||||||||||||||||||||||||||
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub body { shift->content() } | ||||||||||||||||||||||||||
|
Subroutine
body: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # shorthand | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub content_length { shift->headers->content_length() } | ||||||||||||||||||||||||||
|
Subroutine
content_length: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub content_type { shift->headers->content_type() } | ||||||||||||||||||||||||||
|
Subroutine
content_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub content_encoding { shift->headers->content_encoding() } | ||||||||||||||||||||||||||
|
Subroutine
content_encoding: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub header { shift->headers->header(@_) } | ||||||||||||||||||||||||||
|
Subroutine
header: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub protocol { "HTTP/1." . $_[0]->{minor_version} } | ||||||||||||||||||||||||||
|
Subroutine
protocol: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub decoded_content { | ||||||||||||||||||||||||||
|
Subroutine
decoded_content: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 0 | my $cloned = $self->headers->clone; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # 'HTTP::Message::decoded_content' tries to decompress content | ||||||||||||||||||||||||||||
| 88 | # if response header contains 'Content-Encoding' field. | ||||||||||||||||||||||||||||
| 89 | # However 'Furl' decompresses content by itself, 'Content-Encoding' field | ||||||||||||||||||||||||||||
| 90 | # whose value is supported encoding type should be removed from response header. | ||||||||||||||||||||||||||||
| 91 | 0 | my @removed = grep { ! m{\b(?:gzip|x-gzip|deflate)\b} } $cloned->header('content-encoding'); | |||||||||||||||||||||||||||
| 92 | 0 | $cloned->header('content-encoding', \@removed); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | $self->_as_http_response_internal([ $cloned->flatten ])->decoded_content(@_); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub as_http_response { | ||||||||||||||||||||||||||
|
Subroutine
as_http_response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 99 | 0 | return $self->_as_http_response_internal([ $self->headers->flatten ]) | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _as_http_response_internal { | ||||||||||||||||||||||||||
|
Subroutine
_as_http_response_internal: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($self, $flatten_headers) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | require HTTP::Response; | |||||||||||||||||||||||||||
| 106 | 0 | my $res = HTTP::Response->new( $self->code, $self->message, | |||||||||||||||||||||||||||
| 107 | $flatten_headers, | ||||||||||||||||||||||||||||
| 108 | $self->content ); | ||||||||||||||||||||||||||||
| 109 | 0 | $res->protocol($self->protocol); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($self->{request_src} || $self->{request}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"request_src"} or $self->{"request"}
MC/DC0%0/2
$self->{"request_src"} or $self->{"request"}
$self->{"request_src"}
$self->{"request"}
Truth table0%0/3
A or B
A= $self->{"request_src"}B= $self->{"request"}
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (my $req = $self->request) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $res->request($req->as_http_request); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | return $res; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub to_psgi { | ||||||||||||||||||||||||||
|
Subroutine
to_psgi: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 122 | return [ | ||||||||||||||||||||||||||||
| 123 | 0 | $self->code, | |||||||||||||||||||||||||||
| 124 | [$self->headers->flatten], | ||||||||||||||||||||||||||||
| 125 | [$self->content] | ||||||||||||||||||||||||||||
| 126 | ]; | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | return join("", | |||||||||||||||||||||||||||
| 132 | $self->status_line . "\015\012", | ||||||||||||||||||||||||||||
| 133 | $self->headers->as_string, | ||||||||||||||||||||||||||||
| 134 | "\015\012", | ||||||||||||||||||||||||||||
| 135 | $self->content, | ||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub as_hashref { | ||||||||||||||||||||||||||
|
Subroutine
as_hashref: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | return +{ | ||||||||||||||||||||||||||||
| 143 | 0 | code => $self->code, | |||||||||||||||||||||||||||
| 144 | message => $self->message, | ||||||||||||||||||||||||||||
| 145 | protocol => $self->protocol, | ||||||||||||||||||||||||||||
| 146 | headers => [$self->headers->flatten], | ||||||||||||||||||||||||||||
| 147 | content => $self->content, | ||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub is_success { substr( $_[0]->code, 0, 1 ) eq '2' } | ||||||||||||||||||||||||||
|
Subroutine
is_success: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub status_line { $_[0]->code . ' ' . $_[0]->message } | ||||||||||||||||||||||||||
|
Subroutine
status_line: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub charset { | ||||||||||||||||||||||||||
|
Subroutine
charset: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return $self->{__charset} if exists $self->{__charset}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($self->can('content_charset')){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | # To suppress: | ||||||||||||||||||||||||||||
| 160 | # Parsing of undecoded UTF-8 will give garbage when decoding entities | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | local $SIG{__WARN__} = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 162 | 0 | my $charset = $self->content_charset; | |||||||||||||||||||||||||||
| 163 | 0 | $self->{__charset} = $charset; | |||||||||||||||||||||||||||
| 164 | 0 | return $charset; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | my $content_type = $self->headers->header('Content-Type'); | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | return unless $content_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | $content_type =~ /charset=([A-Za-z0-9_\-]+)/io; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | $self->{__charset} = $1 || undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$1 || undef
MC/DC0%0/1
$1 || undef
$1
Truth table0%0/2
A
A= $1
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Detect charset from HTML | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | unless (defined($self->{__charset}) && $self->content_type =~ m{text/html}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"__charset"} and $self->content_type =~ m[text/html]
MC/DC0%0/2
defined $self->{"__charset"} and $self->content_type =~ m[text/html]
defined $self->{"__charset"}
$self->content_type =~ m[text/html]
Truth table0%0/3
A and B
A= defined $self->{"__charset"}B= $self->content_type =~ m[text/html]
| |||||||||||||||||||||||||||||
| 174 | # I guess, this is not so perfect regexp. patches welcome. | ||||||||||||||||||||||||||||
| 175 | # | ||||||||||||||||||||||||||||
| 176 | # <meta http-equiv="Content-Type" content="text/html; charset=EUC-JP"/> | ||||||||||||||||||||||||||||
| 177 | 0 | $self->content =~ m!<meta\s+http-equiv\s*=["']Content-Type["']\s+content\s*=\s*["']text/html;\s*charset=([^'">/]+)['"]\s*/?>!smi; | |||||||||||||||||||||||||||
| 178 | 0 | $self->{__charset} = $1; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | $self->{__charset}; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub encoder { | ||||||||||||||||||||||||||
|
Subroutine
encoder: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 0 | require Encode; | |||||||||||||||||||||||||||
| 186 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | return $self->{__encoder} if exists $self->{__encoder}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my $charset = $self->charset or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | my $enc = Encode::find_encoding($charset); | |||||||||||||||||||||||||||
| 190 | 0 | $self->{__encoder} = $enc; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub encoding { | ||||||||||||||||||||||||||
|
Subroutine
encoding: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my $enc = shift->encoder or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | $enc->name; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 11 | 1; | |||||||||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||||||||||