AnyUA/Util.pm

filter: Statementstmt 61.0% 72 / 118 Branchbran 25.7% 18 / 70 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 62.5% 10 / 16 Podpod 100% 10 / 10 totaltotal 48.1% 116 / 241 CC 46 SCAR 34.2
CC
46
Cov
47%
split_url
110.0
parse_http_date
56.0
coderef_content_to_string
20.0
CRAP 30.5
« AnyUA/Backend/Mojo/UserAgent.pm ↑ Summary AnyUA.pm »
1 package HTTP::AnyUA::Util;
2 # ABSTRACT: Utility subroutines for HTTP::AnyUA backends and middleware
3  
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = '0.904'; # VERSION
8  
9 26 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 26 our @EXPORT_OK = qw(
13 http_headers_to_native
14 native_to_http_request
15 coderef_content_to_string
16 normalize_headers
17 internal_exception
18 http_date
19 parse_http_date
20 uri_escape
21 www_form_urlencode
22 );
23  
24  
25 0 ▶sub _croak { require Carp; Carp::croak(@_) }
Subroutine _croak: not called CC=1 SCAR=6.9
26 0 ▶sub _usage { _croak("Usage: @_\n") }
Subroutine _usage: not called CC=1 SCAR=6.9
27  
28  
29 0 ▶sub coderef_content_to_string {
Subroutine coderef_content_to_string: not called CC=4 SCAR=30.0
Pod documented
30 0 my $content = shift;
31  
32 0 ▶ return $content if !$content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
33  
34 0 ▶ if (ref($content) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "CODE") 0 0 0
35 # drain the request body
36 0 my $body = '';
37 0 ▶ while (my $chunk = $content->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chunk = &$content()) 0 0 0
38 0 $body .= $chunk;
39 }
40 0 $content = $body;
41 }
42  
43 0 return $content;
44 }
45  
46  
47 1 ▶sub native_to_http_request {
Subroutine native_to_http_request: called CC=6 SCAR=21.6
Pod documented
48 1 my $method = shift;
49 1 my $url = shift;
50 1 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
51  
52 1 my $headers = [];
53 1 my $content = $args->{content}; # works as either scalar or coderef
54  
55 # flatten headers
56 1 ▶ for my $header (keys %{$args->{headers} || {}}) {
Condition50%1/2
$args->{'headers'} || {}
l!l
01
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table50%1/2
A
A= $args->{'headers'}
Aresult
11
00
57 0 my $value = $args->{headers}{$header};
58 0 ▶ my @values = ref($value) eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
59 0 for my $v (@values) {
60 0 push @$headers, ($header => $v);
61 }
62 }
63  
64 1 require HTTP::Request;
65 1 return HTTP::Request->new($method, $url, $headers, $content);
66 }
67  
68  
69 1 ▶sub http_headers_to_native {
Subroutine http_headers_to_native: called CC=3 SCAR=11.0
Pod documented
70 1 my $http_headers = shift;
71  
72 1 my $native;
73  
74 1 for my $header ($http_headers->header_field_names) {
75 3 my @values = $http_headers->header($header);
76 3 ▶ $native->{lc($header)} = @values == 1 ? $values[0] : [@values];
Branch: 1 branch
BranchTrueFalseTotal
@values == 1 ? : 3 0 3
77 }
78  
79 1 return $native;
80 }
81  
82  
83 9 ▶sub normalize_headers {
Subroutine normalize_headers: called CC=4 SCAR=13.9
Pod documented
84 9 my $headers_in = shift;
85  
86 9 my $headers = {};
87  
88 9 ▶ if (defined $headers_in) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers_in) 5 4 9
89 5 ▶ while (my ($key, $value) = each %{$headers_in || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$headers_in or {};}) 7 5 12
Condition50%1/2
$headers_in || {}
l!l
120
MC/DC0%0/1
$headers_in || {}
$headers_in
Truth table50%1/2
A
A= $headers_in
Aresult
11
00
90 7 $headers->{lc($key)} = $value;
91 }
92 }
93  
94 9 return $headers;
95 }
96  
97  
98 1 ▶sub internal_exception {
Subroutine internal_exception: called CC=5 SCAR=16.5
Pod documented
99 1 ▶ my $e = shift or _usage(q{internal_exception($exception)});
Branch: 1 branch
BranchTrueFalseTotal
my $e = shift() or _usage("internal_exception(\$exception)") 1 0 1
100 1 ▶ my $resp = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
101  
102 1 $e = "$e";
103  
104 1 ▶ $resp->{headers}{'client-original-status'} = $resp->{status} if $resp->{status};
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"status"} 1 0 1
105 1 ▶ $resp->{headers}{'client-original-reason'} = $resp->{reason} if $resp->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"reason"} 1 0 1
106  
107 1 $resp->{success} = '';
108 1 $resp->{status} = 599;
109 1 $resp->{reason} = 'Internal Exception';
110 1 $resp->{content} = $e;
111 1 $resp->{headers}{'content-type'} = 'text/plain';
112 1 $resp->{headers}{'content-length'} = length $e;
113  
114 1 return $resp;
115 }
116  
117  
118 # adapted from HTTP/Tiny.pm
119 0 ▶sub split_url {
Subroutine split_url: not called CC=10 SCAR=47.0
Pod documented
120 0 ▶ my $url = shift or _usage(q{split_url($url)});
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or _usage("split_url(\$url)") 0 0 0
121  
122 # URI regex adapted from the URI module
123 0 ▶ my ($scheme, $host, $path_query) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#]*)>
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ m[\A([^:/?#]+)://([^/?#]*)([^#]*)] or die "Cannot parse URL: '${url}'\n" 0 0 0
124 or die(qq/Cannot parse URL: '$url'\n/);
125  
126 0 $scheme = lc $scheme;
127 0 ▶ $path_query = "/$path_query" unless $path_query =~ m<\A/>;
Branch: 1 branch
BranchTrueFalseTotal
unless $path_query =~ m[\A/] 0 0 0
128  
129 0 my $auth = '';
130 0 ▶ if ( (my $i = index $host, '@') != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($host, "\@")) != -1) 0 0 0
131 # user:pass@host
132 0 $auth = substr $host, 0, $i, ''; # take up to the @ for auth
133 0 substr $host, 0, 1, ''; # knock the @ off the host
134  
135 # userinfo might be percent escaped, so recover real auth info
136 0 $auth =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
137 }
138 0 ▶ my $port = $host =~ s/:(\d*)\z// && length $1 ? $1
Branch: 3 branches
BranchTrueFalseTotal
$host =~ s/:(\d*)\z// && length $1 ? : 0 0 0
$scheme eq 'http' ? : 0 0 0
$scheme eq 'https' ? : 0 0 0
Condition0%0/3
$host =~ s/:(\d*)\z// and length $1
!ll&&!rl&&r
000
MC/DC0%0/2
$host =~ s/:(\d*)\z// and length $1
$host =~ s/:(\d*)\z// length $1
Truth table0%0/3
A and B
A= $host =~ s/:(\d*)\z//B= length $1
ABresult
0X0
100
111
139 : $scheme eq 'http' ? 80
140 : $scheme eq 'https' ? 443
141 : undef;
142  
143 0 ▶ return ($scheme, (length $host ? lc $host : "localhost") , $port, $path_query, $auth);
Branch: 1 branch
BranchTrueFalseTotal
length $host ? : 0 0 0
144 }
145  
146  
147 # Date conversions adapted from HTTP::Date
148 # adapted from HTTP/Tiny.pm
149 26 my $DoW = 'Sun|Mon|Tue|Wed|Thu|Fri|Sat';
150 26 my $MoY = 'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec';
151 0 ▶sub http_date {
Subroutine http_date: not called CC=2 SCAR=17.9
Pod documented
152 0 ▶ my $time = shift or _usage(q{http_date($time)});
Branch: 1 branch
BranchTrueFalseTotal
my $time = shift() or _usage("http_date(\$time)") 0 0 0
153 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
154 0 return sprintf('%s, %02d %s %04d %02d:%02d:%02d GMT',
155 substr($DoW,$wday*4,3),
156 $mday, substr($MoY,$mon*4,3), $year+1900,
157 $hour, $min, $sec
158 );
159 }
160  
161  
162 # adapted from HTTP/Tiny.pm
163 0 ▶sub parse_http_date {
Subroutine parse_http_date: not called CC=7 SCAR=40.3
Pod documented
164 0 ▶ my $str = shift or _usage(q{parse_http_date($str)});
Branch: 1 branch
BranchTrueFalseTotal
my $str = shift() or _usage("parse_http_date(\$str)") 0 0 0
165 0 my @tl_parts;
166 0 ▶ if ($str =~ /^[SMTWF][a-z]+, +(\d{1,2}) ($MoY) +(\d\d\d\d) +(\d\d):(\d\d):(\d\d) +GMT$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ /^[SMTWF][a-z]+, +(\d{1,2}) ($MoY) +(\d\d\d\d) +(\d\d):(\d\d):(\d\d) +GMT$/) { } 0 0 0
elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/) { } 0 0 0
elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/) { } 0 0 0
167 0 @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
168 }
169 elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/ ) {
170 0 @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
171 }
172 elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/ ) {
173 0 @tl_parts = ($5, $4, $3, $2, (index($MoY,$1)/4), $6);
174 }
175 0 require Time::Local;
176 0 return eval {
177 0 ▶ my $t = @tl_parts ? Time::Local::timegm(@tl_parts) : -1;
Branch: 1 branch
BranchTrueFalseTotal
@tl_parts ? : 0 0 0
178 0 ▶ $t < 0 ? undef : $t;
Branch: 1 branch
BranchTrueFalseTotal
$t < 0 ? : 0 0 0
179 };
180 }
181  
182  
183 # URI escaping adapted from URI::Escape
184 # c.f. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
185 # perl 5.6 ready UTF-8 encoding adapted from JSON::PP
186 # adapted from HTTP/Tiny.pm
187 26 my %escapes = map { chr($_) => sprintf('%%%02X', $_) } 0..255;
188 26 $escapes{' '} = '+';
189 26 my $unsafe_char = qr/[^A-Za-z0-9\-\._~]/;
190  
191 4 ▶sub uri_escape {
Subroutine uri_escape: called CC=4 SCAR=16.9
Pod documented
192 4 ▶ my $str = shift or _usage(q{uri_escape($str)});
Branch: 1 branch
BranchTrueFalseTotal
my $str = shift() or _usage("uri_escape(\$str)") 4 0 4
193 4 ▶ if ($] ge '5.008') {
Branch: 1 branch
BranchTrueFalseTotal
if ($] ge '5.008') { } 4 0 4
194 4 utf8::encode($str);
195 }
196 else {
197 $str = pack('U*', unpack('C*', $str)) # UTF-8 encode a byte string
198 0 ▶ if (length $str == do { use bytes; length $str });
Subroutine BEGIN: called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if length $str == do { use bytes; length $str } 0 0 0
199 0 $str = pack('C*', unpack('C*', $str)); # clear UTF-8 flag
200 }
201 0 $str =~ s/($unsafe_char)/$escapes{$1}/ge;
202 4 return $str;
203 }
204  
205  
206 # adapted from HTTP/Tiny.pm
207 1 ▶sub www_form_urlencode {
Subroutine www_form_urlencode: called CC=10 SCAR=26.7
Pod documented
208 1 my $data = shift;
209 1 ▶ ($data && ref $data)
Branch: 1 branch
BranchTrueFalseTotal
$data and ref $data or _usage("www_form_urlencode(\$dataref)") 1 0 1
Condition33%1/3
$data and ref $data
!ll&&!rl&&r
001
MC/DC0%0/2
$data and ref $data
$data ref $data
Truth table33%1/3
A and B
A= $dataB= ref $data
ABresult
0X0
100
111
210 or _usage(q{www_form_urlencode($dataref)});
211 1 ▶ (ref $data eq 'HASH' || ref $data eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or ref $data eq "ARRAY" or _croak("form data must be a hash or array reference\n") 1 0 1
Condition33%1/3
ref $data eq "HASH" or ref $data eq "ARRAY"
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $data eq "HASH" or ref $data eq "ARRAY"
ref $data eq "HASH" ref $data eq "ARRAY"
Truth table33%1/3
A or B
A= ref $data eq "HASH"B= ref $data eq "ARRAY"
ABresult
1X1
011
000
212 or _croak("form data must be a hash or array reference\n");
213  
214 1 ▶ my @params = ref $data eq 'HASH' ? %$data : @$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 1 0 1
215 1 ▶ @params % 2 == 0
Branch: 1 branch
BranchTrueFalseTotal
@params % 2 == 0 or _croak("form data reference must have an even number of terms\n") 1 0 1
216 or _croak("form data reference must have an even number of terms\n");
217  
218 1 my @terms;
219 1 ▶ while (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 2 1 3
220 2 my ($key, $value) = splice(@params, 0, 2);
221 2 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 2 2
222 0 unshift @params, map { $key => $_ } @$value;
223 }
224 else {
225 2 push @terms, join('=', map { uri_escape($_) } $key, $value);
226 }
227 }
228  
229 1 ▶ return join('&', ref($data) eq 'ARRAY' ? @terms : sort @terms);
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 0 1 1
230 }
231  
232 26 1;
233  
234 __END__
« AnyUA/Backend/Mojo/UserAgent.pm ↑ Summary AnyUA.pm »