Mojo/UserAgent/Transactor.pm

filter: Statementstmt 96.9% 224 / 231 Branchbran 90.0% 117 / 130 Conditioncond 79.8% 79 / 99 MC/DCmcdc 59.6% 31 / 52 Subroutinesub 94.2% 33 / 35 Podpod 100% 10 / 10 totaltotal 88.6% 494 / 557 CC 105 SCAR 23.4
CC
105
Cov
91%
redirect
21.2
download
17.5
_parts
16.3
CRAP 10.4
« Mojo/Message/Request.pm ↑ Summary Mojolicious/Command/version.pm »
1 package Mojo::UserAgent::Transactor;
2 24 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 24 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Mojo::Asset::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Mojo::Content::MultiPart;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Mojo::Content::Single;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Mojo::Transaction::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Mojo::Transaction::WebSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use Mojo::Util qw(encode url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use Mojo::WebSocket qw(challenge client_handshake);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 21 ▶has compressed => sub { $ENV{MOJO_GZIP} // 1 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOJO_GZIP"} // 1
l!l
219
MC/DC100%1/1
$ENV{"MOJO_GZIP"} // 1
$ENV{"MOJO_GZIP"}
Truth table100%2/2
A
A= $ENV{"MOJO_GZIP"}
Aresult
11
00
18 4 ▶has generators => sub { {form => \&_form, json => \&_json, multipart => \&_multipart} };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 24 has name => 'Mojolicious (Perl)';
20  
21 1 ▶sub add_generator { $_[0]->generators->{$_[1]} = $_[2] and return $_[0] }
Subroutine add_generator: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->generators->{$_[1]} = $_[2] and return $_[0] 1 0 1
22  
23 9 ▶sub download {
Subroutine download: called CC=14 SCAR=28.6
Pod documented
24 9 my ($self, $head, $path) = @_;
25  
26 9 my $req = $head->req;
27 9 my $tx = $self->tx(GET => $req->url->clone => $req->headers->to_hash);
28 9 my $res = $tx->res;
29 9 ▶ if (my $error = $head->error) { $res->error($error) and return $tx }
Branch: 2 branches
BranchTrueFalseTotal
if (my $error = $head->error) 1 8 9
$res->error($error) and return $tx 1 0 1
30  
31 8 my $headers = $head->res->headers;
32 8 ▶ my $accept_ranges = ($headers->accept_ranges // '') =~ /bytes/;
Condition100%2/2
$headers->accept_ranges // ""
l!l
44
MC/DC100%1/1
$headers->accept_ranges // ""
$headers->accept_ranges
Truth table100%2/2
A
A= $headers->accept_ranges
Aresult
11
00
33 8 ▶ my $size = $headers->content_length // 0;
Condition100%2/2
$headers->content_length // 0
l!l
62
MC/DC100%1/1
$headers->content_length // 0
$headers->content_length
Truth table100%2/2
A
A= $headers->content_length
Aresult
11
00
34  
35 8 my $current_size = 0;
36 8 my $file = path($path);
37 8 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 7 1 8
38 7 $current_size = -s $file;
39 7 ▶ $res->error({message => 'Unknown file size'}) and return $tx unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 1 6 7
Condition60%3/5
$size || $res->error({'message', 'Unknown file size'}) && (return $tx)
l!l&&r!l&&!r
610
$res->error({"message", "Unknown file size"}) and return $tx
!ll
01
MC/DC0%0/2
$size || $res->error({'message', 'Unknown file size'}) && (return $tx)
$size $res->error({"message", "Unknown file size"})
Truth table66%2/3
A || B
A= $sizeB= $res->error({"message", "Unknown file size"})
ABresult
1X1
011
000
40 6 ▶ $res->error({message => 'File size mismatch'}) and return $tx if $current_size > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $current_size > $size 1 5 6
Condition60%3/5
$current_size > $size && ($res->error({'message', 'File size mismatch'}) && (return $tx))
!ll&&!rl&&r
510
$res->error({"message", "File size mismatch"}) and return $tx
!ll
01
MC/DC0%0/2
$current_size > $size && ($res->error({'message', 'File size mismatch'}) && (return $tx))
$current_size > $size $res->error({"message", "File size mismatch"})
Truth table33%1/3
A && B
A= $current_size > $sizeB= $res->error({"message", "File size mismatch"})
ABresult
0X0
100
111
41 5 ▶ $res->error({message => 'Download complete'}) and return $tx if $current_size == $size;
Branch: 1 branch
BranchTrueFalseTotal
if $current_size == $size 1 4 5
Condition60%3/5
$current_size == $size && ($res->error({'message', 'Download complete'}) && (return $tx))
!ll&&!rl&&r
410
$res->error({"message", "Download complete"}) and return $tx
!ll
01
MC/DC0%0/2
$current_size == $size && ($res->error({'message', 'Download complete'}) && (return $tx))
$current_size == $size $res->error({"message", "Download complete"})
Truth table33%1/3
A && B
A= $current_size == $sizeB= $res->error({"message", "Download complete"})
ABresult
0X0
100
111
42 4 ▶ $res->error({message => 'Server does not support partial requests'}) and return $tx unless $accept_ranges;
Branch: 1 branch
BranchTrueFalseTotal
unless $accept_ranges 2 2 4
Condition60%3/5
$accept_ranges || $res->error({'message', 'Server does not support partial requests'}) && (return $tx)
l!l&&r!l&&!r
220
$res->error({"message", "Server does not support partial requests"}) and return $tx
!ll
02
MC/DC0%0/2
$accept_ranges || $res->error({'message', 'Server does not support partial requests'}) && (return $tx)
$accept_ranges $res->error({"message", "Server does not support partial requests"})
Truth table66%2/3
A || B
A= $accept_rangesB= $res->error({"message", "Server does not support partial requests"})
ABresult
1X1
011
000
43 2 $tx->req->headers->range("bytes=$current_size-$size");
44 }
45  
46 3 my $fh = $file->open('>>');
47 $res->content->unsubscribe('read')->on(
48 0 ▶ read => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 my ($content, $bytes) = @_;
50 0 $current_size += length $bytes;
51 0 ▶ $fh->syswrite($bytes) == length $bytes or $res->error({message => qq/Can't write to file "$path": $!/});
Branch: 1 branch
BranchTrueFalseTotal
$fh->syswrite($bytes) == length $bytes or $res->error({"message", qq[Can't write to file "$path": $!]}) 0 0 0
52 }
53 3 );
54 $res->on(
55 0 ▶ finish => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
56 0 my $res = shift;
57 0 ▶ $res->error({message => 'Download incomplete'}) if $current_size < $size;
Branch: 1 branch
BranchTrueFalseTotal
if $current_size < $size 0 0 0
58 }
59 3 );
60  
61 3 return $tx;
62 }
63  
64 399 ▶sub endpoint {
Subroutine endpoint: called CC=8 SCAR=20.8
Pod documented
65 399 my ($self, $tx) = @_;
66  
67 # Basic endpoint
68 399 my $req = $tx->req;
69 399 my $url = $req->url;
70 399 ▶ my $proto = $url->protocol || 'http';
Condition50%1/2
$url->protocol || 'http'
l!l
3990
MC/DC0%0/1
$url->protocol || 'http'
$url->protocol
Truth table50%1/2
A
A= $url->protocol
Aresult
11
00
71 399 my $host = $url->ihost;
72 399 ▶ my $port = $url->port // ($proto eq 'https' ? 443 : 80);
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'https' ? : 24 36 60
Condition100%2/2
$url->port // ($proto eq "https" ? 443 : 80)
l!l
33960
MC/DC100%1/1
$url->port // ($proto eq "https" ? 443 : 80)
$url->port
Truth table100%2/2
A
A= $url->port
Aresult
11
00
73  
74 # Proxy for normal HTTP requests
75 399 my $socks;
76 399 ▶ if (my $proxy = $req->proxy) { $socks = $proxy->protocol eq 'socks' }
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $req->proxy) 48 351 399
77 399 ▶ return _proxy($tx, $proto, $host, $port) if $proto eq 'http' && !$req->is_handshake && !$socks;
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "http" and not $req->is_handshake and not $socks 362 37 399
Condition100%6/6
$proto eq "http" and not $req->is_handshake and not $socks
!ll&&!rl&&r
316362
$proto eq "http" and not $req->is_handshake
!ll&&!rl&&r
247368
MC/DC100%3/3
$proto eq "http" and not $req->is_handshake and not $socks
$proto eq "http" not $req->is_handshake not $socks
Truth table100%4/4
A and not(B) and not(C)
A= $proto eq "http"B= not $req->is_handshakeC= not $socks
ABCresult
0XX0
10X0
1100
1111
78  
79 37 return $proto, $host, $port;
80 }
81  
82 57 ▶sub peer { _proxy($_[1], $_[0]->endpoint($_[1])) }
Subroutine peer: called CC=1 SCAR=0.0
Pod documented
83  
84 6 ▶sub promisify {
Subroutine promisify: called CC=5 SCAR=16.2
Pod documented
85 6 my ($self, $promise, $tx) = @_;
86 6 my $err = $tx->error;
87 6 ▶ return $promise->reject($err->{message}) if $err && !$err->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $err and not $err->{"code"} 1 5 6
Condition66%2/3
$err and not $err->{"code"}
!ll&&!rl&&r
501
MC/DC50%1/2
$err and not $err->{"code"}
$err not $err->{"code"}
Truth table66%2/3
A and not(B)
A= $errB= not $err->{"code"}
ABresult
0X0
100
111
88 5 ▶ return $promise->reject('WebSocket handshake failed') if $tx->req->is_handshake && !$tx->is_websocket;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->req->is_handshake and not $tx->is_websocket 1 4 5
Condition66%2/3
$tx->req->is_handshake and not $tx->is_websocket
!ll&&!rl&&r
401
MC/DC50%1/2
$tx->req->is_handshake and not $tx->is_websocket
$tx->req->is_handshake not $tx->is_websocket
Truth table66%2/3
A and not(B)
A= $tx->req->is_handshakeB= not $tx->is_websocket
ABresult
0X0
100
111
89 4 $promise->resolve($tx);
90 }
91  
92 29 ▶sub proxy_connect {
Subroutine proxy_connect: called CC=7 SCAR=19.6
Pod documented
93 29 my ($self, $old) = @_;
94  
95 # Already a CONNECT request
96 29 my $req = $old->req;
97 29 ▶ return undef if uc $req->method eq 'CONNECT';
Branch: 1 branch
BranchTrueFalseTotal
if uc $req->method eq "CONNECT" 2 27 29
98  
99 # No proxy
100 27 ▶ return undef unless (my $proxy = $req->proxy) && $req->via_proxy;
Branch: 1 branch
BranchTrueFalseTotal
unless my $proxy = $req->proxy and $req->via_proxy 25 2 27
Condition100%3/3
my $proxy = $req->proxy and $req->via_proxy
!ll&&!rl&&r
2412
MC/DC100%2/2
my $proxy = $req->proxy and $req->via_proxy
my $proxy = $req->proxy $req->via_proxy
Truth table100%3/3
A and B
A= my $proxy = $req->proxyB= $req->via_proxy
ABresult
0X0
100
111
101 2 ▶ return undef if $proxy->protocol eq 'socks';
Branch: 1 branch
BranchTrueFalseTotal
if $proxy->protocol eq "socks" 1 1 2
102  
103 # WebSocket and/or HTTPS
104 1 my $url = $req->url;
105 1 ▶ return undef unless $req->is_handshake || $url->protocol eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
unless $req->is_handshake or $url->protocol eq "https" 0 1 1
Condition33%1/3
$req->is_handshake or $url->protocol eq "https"
l!l&&r!l&&!r
010
MC/DC0%0/2
$req->is_handshake or $url->protocol eq "https"
$req->is_handshake $url->protocol eq "https"
Truth table33%1/3
A or B
A= $req->is_handshakeB= $url->protocol eq "https"
ABresult
1X1
011
000
106  
107 # CONNECT request (expect a bad response)
108 1 my $new = $self->tx(CONNECT => $url->clone->userinfo(undef));
109 1 $new->req->proxy($proxy);
110 1 $new->res->content->auto_relax(0)->headers->connection('keep-alive');
111  
112 1 return $new;
113 }
114  
115 188 ▶sub redirect {
Subroutine redirect: called CC=21 SCAR=30.5
Pod documented
116 188 my ($self, $old) = @_;
117  
118 # Commonly used codes
119 188 my $res = $old->res;
120 188 ▶ my $code = $res->code // 0;
Condition50%1/2
$res->code // 0
l!l
1880
MC/DC0%0/1
$res->code // 0
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
121 188 ▶ return undef unless grep { $_ == $code } 301, 302, 303, 307, 308;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $code;} 301, 302, 303, 307, 308 160 28 188
122  
123 # CONNECT requests cannot be redirected
124 28 my $req = $old->req;
125 28 ▶ return undef if uc $req->method eq 'CONNECT';
Branch: 1 branch
BranchTrueFalseTotal
if uc $req->method eq "CONNECT" 1 27 28
126  
127 # Fix location without authority and/or scheme
128 27 ▶ return undef unless my $location = $res->headers->every_header('Location')->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless my $location = $res->headers->every_header("Location")->[0] 0 27 27
129 27 $location = Mojo::URL->new($location);
130 27 ▶ $location = $location->base($req->url)->to_abs unless $location->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
unless $location->is_abs 2 25 27
131 27 my $proto = $location->protocol;
132 27 ▶ return undef if ($proto ne 'http' && $proto ne 'https') || !$location->host;
Branch: 1 branch
BranchTrueFalseTotal
if $proto ne "http" and $proto ne "https" or not $location->host 2 25 27
Condition100%6/6
$proto ne "http" and $proto ne "https" or not $location->host
l!l&&r!l&&!r
1125
$proto ne "http" and $proto ne "https"
!ll&&!rl&&r
2511
MC/DC100%3/3
$proto ne "http" and $proto ne "https" or not $location->host
$proto ne "http" $proto ne "https" not $location->host
Truth table80%4/5
A and B or not(C)
A= $proto ne "http"B= $proto ne "https"C= not $location->host
ABCresult
11X1
0X11
1011
0X00
1000
133  
134 # Clone request if necessary (QUERY is safe and idempotent, so it keeps its content like 307 and 308)
135 25 my $new = Mojo::Transaction::HTTP->new;
136 25 my $method = uc $req->method;
137 25 ▶ if ($code == 307 || $code == 308 || ($method eq 'QUERY' && $code != 303)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 307 or $code == 308 or $method eq 'QUERY' and $code != 303) { } 10 15 25
Condition100%9/9
$code == 307 or $code == 308 or $method eq "QUERY" and $code != 303
l!l&&r!l&&!r
8215
$code == 307 or $code == 308
l!l&&r!l&&!r
5317
$method eq "QUERY" and $code != 303
!ll&&!rl&&r
1412
MC/DC100%4/4
$code == 307 or $code == 308 or $method eq "QUERY" and $code != 303
$code == 307 $code == 308 $method eq "QUERY" $code != 303
Truth table100%5/5
A or B or C and D
A= $code == 307B= $code == 308C= $method eq "QUERY"D= $code != 303
ABCDresult
1XXX1
01XX1
00111
000X0
00100
138 10 ▶ return undef unless my $clone = $req->clone;
Branch: 1 branch
BranchTrueFalseTotal
unless my $clone = $req->clone 2 8 10
139 8 $new->req($clone);
140 }
141 else {
142 15 ▶ $method = $code == 303 || $method eq 'POST' ? 'GET' : $method;
Branch: 1 branch
BranchTrueFalseTotal
$code == 303 || $method eq 'POST' ? : 12 3 15
Condition100%3/3
$code == 303 or $method eq "POST"
l!l&&r!l&&!r
483
MC/DC100%2/2
$code == 303 or $method eq "POST"
$code == 303 $method eq "POST"
Truth table100%3/3
A or B
A= $code == 303B= $method eq "POST"
ABresult
1X1
011
000
143 15 $new->req->method($method)->content->headers(my $headers = $req->headers->clone);
144 15 $headers->remove($_) for grep {/^content-/i} @{$headers->names};
145 }
146  
147 23 my $content = $new->res->content;
148 23 ▶ $content->auto_decompress(0) unless $self->compressed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->compressed 1 22 23
149 23 my $headers = $new->req->url($location)->headers;
150 23 $headers->remove($_) for qw(Authorization Cookie Host Referer);
151 0 ▶ if ($res->content->has_subscribers('sse')) { $content->on(sse => $_) for @{$res->content->subscribers('sse')} }
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content->has_subscribers("sse")) 0 23 23
152  
153 23 return $new->previous($old);
154 }
155  
156 279 ▶sub tx {
Subroutine tx: called CC=9 SCAR=22.0
Pod documented
157 279 my ($self, $method, $url) = (shift, shift, shift);
158  
159 # Method and URL
160 279 my $tx = Mojo::Transaction::HTTP->new;
161 279 my $req = $tx->req->method($method);
162 279 ▶ ref $url ? $req->url($url) : $req->url->parse($url =~ m!^/|://! ? $url : "http://$url");
Branch: 2 branches
BranchTrueFalseTotal
ref $url ? : 13 266 279
$url =~ m[^/|://]u ? : 260 6 266
163  
164 # Headers (we identify ourselves and accept gzip compression)
165 279 my $headers = $req->headers;
166 279 ▶ $headers->from_hash(shift) if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 85 194 279
167 279 ▶ $headers->user_agent($self->name) unless $headers->user_agent;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->user_agent 270 9 279
168 279 ▶ if (!$self->compressed) { $tx->res->content->auto_decompress(0) }
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->compressed) { } 3 276 279
elsif (not $headers->accept_encoding) { } 267 9 276
169 267 elsif (!$headers->accept_encoding) { $headers->accept_encoding('gzip') }
170  
171 # Generator
172 279 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 32 247 279
elsif (@_) { } 16 231 247
173 32 my $cb = $self->generators->{shift()};
174 32 $self->$cb($tx, @_);
175 }
176  
177 # Body
178 16 elsif (@_) { $req->body(shift) }
179  
180 279 return $tx;
181 }
182  
183 162 ▶sub upgrade {
Subroutine upgrade: called CC=5 SCAR=16.1
Pod documented
184 162 my ($self, $tx) = @_;
185 162 ▶ my $code = $tx->res->code // 0;
Condition100%2/2
$tx->res->code // 0
l!l
1611
MC/DC100%1/1
$tx->res->code // 0
$tx->res->code
Truth table100%2/2
A
A= $tx->res->code
Aresult
11
00
186 162 ▶ return undef unless $tx->req->is_handshake && $code == 101;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->req->is_handshake and $code == 101 160 2 162
Condition100%3/3
$tx->req->is_handshake and $code == 101
!ll&&!rl&&r
15912
MC/DC100%2/2
$tx->req->is_handshake and $code == 101
$tx->req->is_handshake $code == 101
Truth table100%3/3
A and B
A= $tx->req->is_handshakeB= $code == 101
ABresult
0X0
100
111
187 2 my $ws = Mojo::Transaction::WebSocket->new(handshake => $tx, masked => 1);
188 2 ▶ return challenge($ws) ? $ws->established(1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
challenge($ws) ? : 2 0 2
189 }
190  
191 11 ▶sub websocket {
Subroutine websocket: called CC=7 SCAR=19.5
Pod documented
192 11 my $self = shift;
193  
194 # New WebSocket transaction
195 11 ▶ my $sub = ref $_[-1] eq 'ARRAY' ? pop : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'ARRAY' ? : 2 9 11
196 11 my $tx = $self->tx(GET => @_);
197 11 my $req = $tx->req;
198 11 ▶ $req->headers->sec_websocket_protocol(join ', ', @$sub) if @$sub;
Branch: 1 branch
BranchTrueFalseTotal
if @$sub 2 9 11
199  
200 # Handshake protocol
201 11 my $url = $req->url;
202 11 ▶ my $proto = $url->protocol // '';
Condition50%1/2
$url->protocol // ""
l!l
110
MC/DC0%0/1
$url->protocol // ""
$url->protocol
Truth table50%1/2
A
A= $url->protocol
Aresult
11
00
203 11 ▶ if ($proto eq 'ws') { $url->scheme('http') }
Branch: 3 branches
BranchTrueFalseTotal
if ($proto eq 'ws') { } 3 8 11
elsif ($proto eq 'wss') { } 5 3 8
elsif ($proto eq 'ws+unix') { } 1 2 3
204 5 elsif ($proto eq 'wss') { $url->scheme('https') }
205 1 elsif ($proto eq 'ws+unix') { $url->scheme('http+unix') }
206  
207 11 return client_handshake $tx;
208 }
209  
210 16 ▶sub _content { Mojo::Content::MultiPart->new(headers => $_[0], parts => $_[1]) }
Subroutine _content: called CC=1 SCAR=0.0
211  
212 22 ▶sub _form {
Subroutine _form: called CC=10 SCAR=23.0
213 22 my ($self, $tx, $form, %options) = @_;
214 22 ▶ $options{charset} = 'UTF-8' unless exists $options{charset};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"charset"} 20 2 22
215  
216 # Check for uploads and force multipart if necessary
217 22 my $req = $tx->req;
218 22 my $headers = $req->headers;
219 22 ▶ my $multipart = ($headers->content_type // '') =~ m!multipart/form-data!i;
Condition100%2/2
$headers->content_type // ""
l!l
517
MC/DC100%1/1
$headers->content_type // ""
$headers->content_type
Truth table100%2/2
A
A= $headers->content_type
Aresult
11
00
220 22 ▶ for my $value (map { ref $_ eq 'ARRAY' ? @$_ : $_ } values %$form) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 5 36 41
221 41 ▶ ++$multipart and last if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 9 32 41
Condition60%3/5
ref $value eq 'HASH' && (++$multipart && (last))
!ll
3290
++$multipart and last
!ll
09
MC/DC0%0/2
ref $value eq 'HASH' && (++$multipart && (last))
ref $value eq 'HASH' ++$multipart
Truth table33%1/3
A && B
A= ref $value eq 'HASH'B= ++$multipart
ABresult
0X0
100
111
222 }
223  
224 # Multipart
225 22 ▶ if ($multipart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multipart) 12 10 22
226 12 $req->content(_content($headers, _form_parts($options{charset}, $form)));
227 12 _type($headers, 'multipart/form-data');
228 12 return $tx;
229 }
230  
231 # Query parameters or urlencoded
232 10 my $method = uc $req->method;
233 10 my @form = map { $_ => $form->{$_} } sort keys %$form;
234 10 ▶ if ($method eq 'GET' || $method eq 'HEAD') { $req->url->query->merge(@form) }
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'GET' or $method eq 'HEAD') { } 4 6 10
Condition100%3/3
$method eq "GET" or $method eq "HEAD"
l!l&&r!l&&!r
316
MC/DC100%2/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET" $method eq "HEAD"
Truth table100%3/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
ABresult
1X1
011
000
235 else {
236 6 $req->body(Mojo::Parameters->new(@form)->charset($options{charset})->to_string);
237 6 _type($headers, 'application/x-www-form-urlencoded');
238 }
239  
240 10 return $tx;
241 }
242  
243 12 ▶sub _form_parts {
Subroutine _form_parts: called CC=4 SCAR=13.9
244 12 my ($charset, $form) = @_;
245  
246 12 my @parts;
247 12 for my $name (sort keys %$form) {
248 19 ▶ next unless defined(my $values = $form->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $values = $form->{$name}) 1 18 19
249 18 ▶ $values = [$values] unless ref $values eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "ARRAY" 16 2 18
250 18 push @parts, @{_parts($charset, $name, $values)};
251 }
252  
253 12 return \@parts;
254 }
255  
256 4 ▶sub _json {
Subroutine _json: called CC=1 SCAR=0.0
257 4 my ($self, $tx, $data) = @_;
258 4 _type($tx->req->body(encode_json $data)->headers, 'application/json');
259 4 return $tx;
260 }
261  
262 4 ▶sub _multipart {
Subroutine _multipart: called CC=1 SCAR=0.0
263 4 my ($self, $tx, $parts) = @_;
264 4 my $req = $tx->req;
265 4 $req->content(_content($req->headers, _parts(undef, undef, $parts)));
266 4 return $tx;
267 }
268  
269 22 ▶sub _parts {
Subroutine _parts: called CC=16 SCAR=27.9
270 22 my ($charset, $name, $values) = @_;
271  
272 22 my @parts;
273 22 for my $value (@$values) {
274 27 push @parts, my $part = Mojo::Content::Single->new;
275  
276 27 my $filename;
277 27 my $headers = $part->headers;
278 27 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 14 13 27
279  
280 # File
281 14 ▶ if (my $file = delete $value->{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $file = delete $value->{'file'}) { } 7 7 14
elsif (defined(my $content = delete $value->{'content'})) { } 7 0 7
282 7 ▶ $file = Mojo::Asset::File->new(path => $file) unless ref $file;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 4 3 7
283 7 $part->asset($file);
284 7 ▶ $value->{filename} //= path($file->path)->basename if $file->isa('Mojo::Asset::File');
Branch: 1 branch
BranchTrueFalseTotal
if $file->isa("Mojo::Asset::File") 5 2 7
Condition33%1/3
$value->{"filename"} //= path($file->path)->basename
l!l&&r!l&&!r
050
MC/DC0%0/2
$value->{"filename"} //= path($file->path)->basename
$value->{"filename"} path($file->path)->basename
Truth table33%1/3
A //= B
A= $value->{"filename"}B= path($file->path)->basename
ABresult
1X1
011
000
285 }
286  
287 # Memory
288 elsif (defined(my $content = delete $value->{content})) {
289 7 $part->asset(Mojo::Asset::Memory->new->add_chunk($content));
290 }
291  
292 # Filename and headers
293 14 $filename = delete $value->{filename};
294 14 $headers->from_hash($value);
295 14 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 4 10 14
296 10 ▶ $filename = url_escape $filename // $name, '"';
Condition66%2/3
$filename // $name
l!l&&r!l&&!r
820
MC/DC0%0/2
$filename // $name
$filename $name
Truth table66%2/3
A // B
A= $filenameB= $name
ABresult
1X1
011
000
297 10 ▶ $filename = encode $charset, $filename if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 10 0 10
298 }
299  
300 # Field
301 else {
302 13 ▶ $value = encode $charset, $value if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 11 2 13
303 13 $part->asset(Mojo::Asset::Memory->new->add_chunk($value));
304 }
305  
306 # Content-Disposition
307 23 ▶ next if !defined $name || defined $headers->content_disposition;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or defined $headers->content_disposition 3 20 23
Condition100%3/3
not defined $name or defined $headers->content_disposition
l!l&&r!l&&!r
2120
MC/DC100%2/2
not defined $name or defined $headers->content_disposition
not defined $name defined $headers->content_disposition
Truth table100%3/3
not(A) or B
A= not defined $nameB= defined $headers->content_disposition
ABresult
1X1
011
000
308 20 $name = url_escape $name, '"';
309 20 ▶ $name = encode $charset, $name if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 20 0 20
310 20 my $disposition = qq{form-data; name="$name"};
311 20 ▶ $disposition .= qq{; filename="$filename"} if defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename 9 11 20
312 20 $headers->content_disposition($disposition);
313 }
314  
315 22 return \@parts;
316 }
317  
318 419 ▶sub _proxy {
Subroutine _proxy: called CC=5 SCAR=16.1
319 419 my ($tx, $proto, $host, $port) = @_;
320  
321 419 my $req = $tx->req;
322 419 ▶ if ($req->via_proxy && (my $proxy = $req->proxy)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->via_proxy and my $proxy = $req->proxy) 36 383 419
Condition100%3/3
$req->via_proxy and my $proxy = $req->proxy
!ll&&!rl&&r
1736636
MC/DC100%2/2
$req->via_proxy and my $proxy = $req->proxy
$req->via_proxy my $proxy = $req->proxy
Truth table100%3/3
A and B
A= $req->via_proxyB= my $proxy = $req->proxy
ABresult
0X0
100
111
323 36 ▶ return $proxy->protocol, $proxy->ihost, $proxy->port // ($proto eq 'https' ? 443 : 80);
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'https' ? : 3 9 12
Condition100%2/2
$proxy->port // ($proto eq "https" ? 443 : 80)
l!l
2412
MC/DC100%1/1
$proxy->port // ($proto eq "https" ? 443 : 80)
$proxy->port
Truth table100%2/2
A
A= $proxy->port
Aresult
11
00
324 }
325  
326 383 return $proto, $host, $port;
327 }
328  
329 22 ▶sub _type { $_[0]->content_type($_[1]) unless $_[0]->content_type }
Subroutine _type: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->content_type 16 6 22
330  
331 24 1;
332  
333 =encoding utf8
334  
335 =head1 NAME
336  
337 Mojo::UserAgent::Transactor - User agent transactor
338  
339 =head1 SYNOPSIS
340  
341 use Mojo::UserAgent::Transactor;
342  
343 # GET request with Accept header
344 my $t = Mojo::UserAgent::Transactor->new;
345 say $t->tx(GET => 'http://example.com' => {Accept => '*/*'})->req->to_string;
346  
347 # POST request with form-data
348 say $t->tx(POST => 'example.com' => form => {a => 'b'})->req->to_string;
349  
350 # PUT request with JSON data
351 say $t->tx(PUT => 'example.com' => json => {a => 'b'})->req->to_string;
352  
353 =head1 DESCRIPTION
354  
355 L<Mojo::UserAgent::Transactor> is the transaction building and manipulation framework used by L<Mojo::UserAgent>.
356  
357 =head1 GENERATORS
358  
359 These content generators are available by default.
360  
361 =head2 form
362  
363 $t->tx(POST => 'http://example.com' => form => {a => 'b'});
364  
365 Generate query string, C<application/x-www-form-urlencoded> or C<multipart/form-data> content. See L</"tx"> for more.
366  
367 =head2 json
368  
369 $t->tx(PATCH => 'http://example.com' => json => {a => 'b'});
370  
371 Generate JSON content with L<Mojo::JSON>. See L</"tx"> for more.
372  
373 =head2 multipart
374  
375 $t->tx(PUT => 'http://example.com' => multipart => ['Hello', 'World!']);
376  
377 Generate multipart content. See L</"tx"> for more.
378  
379 =head1 ATTRIBUTES
380  
381 L<Mojo::UserAgent::Transactor> implements the following attributes.
382  
383 =head2 compressed
384  
385 my $bool = $t->compressed;
386 $t = $t->compressed($bool);
387  
388 Try to negotiate compression for the response content and decompress it automatically, defaults to the value of the
389 C<MOJO_GZIP> environment variable or true.
390  
391 =head2 generators
392  
393 my $generators = $t->generators;
394 $t = $t->generators({foo => sub {...}});
395  
396 Registered content generators, by default only C<form>, C<json> and C<multipart> are already defined.
397  
398 =head2 name
399  
400 my $name = $t->name;
401 $t = $t->name('Mojolicious');
402  
403 Value for C<User-Agent> request header of generated transactions, defaults to C<Mojolicious (Perl)>.
404  
405 =head1 METHODS
406  
407 L<Mojo::UserAgent::Transactor> inherits all methods from L<Mojo::Base> and implements the following new ones.
408  
409 =head2 add_generator
410  
411 $t = $t->add_generator(foo => sub {...});
412  
413 Register a content generator.
414  
415 $t->add_generator(foo => sub ($t, $tx, @args) {...});
416  
417 =head2 download
418  
419 my $tx = $t->download(Mojo::Transaction::HTTP->new, '/home/sri/test.tar.gz');
420  
421 Build L<Mojo::Transaction::HTTP> resumable file download request as follow-up to a C<HEAD> request. Note that this
422 method is B<EXPERIMENTAL> and might change without warning!
423  
424 =head2 endpoint
425  
426 my ($proto, $host, $port) = $t->endpoint(Mojo::Transaction::HTTP->new);
427  
428 Actual endpoint for transaction.
429  
430 =head2 peer
431  
432 my ($proto, $host, $port) = $t->peer(Mojo::Transaction::HTTP->new);
433  
434 Actual peer for transaction.
435  
436 =head2 promisify
437  
438 $t->promisify(Mojo::Promise->new, Mojo::Transaction::HTTP->new);
439  
440 Resolve or reject L<Mojo::Promise> object with L<Mojo::Transaction::HTTP> object.
441  
442 =head2 proxy_connect
443  
444 my $tx = $t->proxy_connect(Mojo::Transaction::HTTP->new);
445  
446 Build L<Mojo::Transaction::HTTP> proxy C<CONNECT> request for transaction if possible.
447  
448 =head2 redirect
449  
450 my $tx = $t->redirect(Mojo::Transaction::HTTP->new);
451  
452 Build L<Mojo::Transaction::HTTP> follow-up request for C<301>, C<302>, C<303>, C<307> or C<308> redirect response if
453 possible. Since C<QUERY> requests are safe and idempotent, they are never redirected as C<GET> requests and keep their
454 content, except for C<303> responses.
455  
456 =head2 tx
457  
458 my $tx = $t->tx(GET => 'example.com');
459 my $tx = $t->tx(POST => 'http://example.com');
460 my $tx = $t->tx(GET => 'http://example.com' => {Accept => '*/*'});
461 my $tx = $t->tx(PUT => 'http://example.com' => 'Content!');
462 my $tx = $t->tx(PUT => 'http://example.com' => form => {a => 'b'});
463 my $tx = $t->tx(PUT => 'http://example.com' => json => {a => 'b'});
464 my $tx = $t->tx(PUT => 'https://example.com' => multipart => ['a', 'b']);
465 my $tx = $t->tx(POST => 'example.com' => {Accept => '*/*'} => 'Content!');
466 my $tx = $t->tx(PUT => 'example.com' => {Accept => '*/*'} => form => {a => 'b'});
467 my $tx = $t->tx(PUT => 'example.com' => {Accept => '*/*'} => json => {a => 'b'});
468 my $tx = $t->tx(PUT => 'example.com' => {Accept => '*/*'} => multipart => ['a', 'b']);
469  
470 Versatile general purpose L<Mojo::Transaction::HTTP> transaction builder for requests, with support for
471 L</"GENERATORS">.
472  
473 # Generate and inspect custom GET request with DNT header and content
474 say $t->tx(GET => 'example.com' => {DNT => 1} => 'Bye!')->req->to_string;
475  
476 # Stream response content to STDOUT
477 my $tx = $t->tx(GET => 'http://example.com');
478 $tx->res->content->unsubscribe('read')->on(read => sub { say $_[1] });
479  
480 # PUT request with content streamed from file
481 my $tx = $t->tx(PUT => 'http://example.com');
482 $tx->req->content->asset(Mojo::Asset::File->new(path => '/foo.txt'));
483  
484 The C<json> content generator uses L<Mojo::JSON> for encoding and sets the content type to C<application/json>.
485  
486 # POST request with "application/json" content
487 my $tx = $t->tx(POST => 'http://example.com' => json => {a => 'b', c => [1, 2, 3]});
488  
489 The C<form> content generator will automatically use query parameters for C<GET> and C<HEAD> requests.
490  
491 # GET request with query parameters
492 my $tx = $t->tx(GET => 'http://example.com' => form => {a => 'b'});
493  
494 For all other request methods the C<application/x-www-form-urlencoded> content type is used.
495  
496 # POST request with "application/x-www-form-urlencoded" content
497 my $tx = $t->tx(POST => 'http://example.com' => form => {a => 'b', c => 'd'});
498  
499 Parameters may be encoded with the C<charset> option.
500  
501 # PUT request with Shift_JIS encoded form values
502 my $tx = $t->tx(PUT => 'example.com' => form => {a => 'b'} => charset => 'Shift_JIS');
503  
504 An array reference can be used for multiple form values sharing the same name.
505  
506 # POST request with form values sharing the same name
507 my $tx = $t->tx(POST => 'http://example.com' => form => {a => ['b', 'c', 'd']});
508  
509 A hash reference with a C<content> or C<file> value can be used to switch to the C<multipart/form-data> content type
510 for file uploads.
511  
512 # POST request with "multipart/form-data" content
513 my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {content => 'lala'}});
514  
515 # POST request with multiple files sharing the same name
516 my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => [{content => 'first'}, {content => 'second'}]});
517  
518 The C<file> value should contain the path to the file you want to upload or an asset object, like L<Mojo::Asset::File>
519 or L<Mojo::Asset::Memory>.
520  
521 # POST request with upload streamed from file
522 my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {file => '/foo.txt'}});
523  
524 # POST request with upload streamed from asset
525 my $asset = Mojo::Asset::Memory->new->add_chunk('lalala');
526 my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {file => $asset}});
527  
528 A C<filename> value will be generated automatically, but can also be set manually if necessary. All remaining values in
529 the hash reference get merged into the C<multipart/form-data> content as headers.
530  
531 # POST request with form values and customized upload (filename and header)
532 my $tx = $t->tx(POST => 'http://example.com' => form => {
533 a => 'b',
534 c => 'd',
535 mytext => {
536 content => 'lalala',
537 filename => 'foo.txt',
538 'Content-Type' => 'text/plain'
539 }
540 });
541  
542 The C<multipart/form-data> content type can also be enforced by setting the C<Content-Type> header manually.
543  
544 # Force "multipart/form-data"
545 my $headers = {'Content-Type' => 'multipart/form-data'};
546 my $tx = $t->tx(POST => 'example.com' => $headers => form => {a => 'b'});
547  
548 The C<multipart> content generator can be used to build custom multipart requests and does not set a content type.
549  
550 # POST request with multipart content ("foo" and "bar")
551 my $tx = $t->tx(POST => 'http://example.com' => multipart => ['foo', 'bar']);
552  
553 Similar to the C<form> content generator you can also pass hash references with C<content> or C<file> values, as well
554 as headers.
555  
556 # POST request with multipart content streamed from file
557 my $tx = $t->tx(POST => 'http://example.com' => multipart => [{file => '/foo.txt'}]);
558  
559 # PUT request with multipart content streamed from asset
560 my $headers = {'Content-Type' => 'multipart/custom'};
561 my $asset = Mojo::Asset::Memory->new->add_chunk('lalala');
562 my $tx = $t->tx(PUT => 'http://example.com' => $headers => multipart => [{file => $asset}]);
563  
564 # POST request with multipart content and custom headers
565 my $tx = $t->tx(POST => 'http://example.com' => multipart => [
566 {
567 content => 'Hello',
568 'Content-Type' => 'text/plain',
569 'Content-Language' => 'en-US'
570 },
571 {
572 content => 'World!',
573 'Content-Type' => 'text/plain',
574 'Content-Language' => 'en-US'
575 }
576 ]);
577  
578 =head2 upgrade
579  
580 my $tx = $t->upgrade(Mojo::Transaction::HTTP->new);
581  
582 Build L<Mojo::Transaction::WebSocket> follow-up transaction for WebSocket handshake if possible.
583  
584 =head2 websocket
585  
586 my $tx = $t->websocket('ws://example.com');
587 my $tx = $t->websocket('ws://example.com' => {DNT => 1} => ['v1.proto']);
588  
589 Versatile L<Mojo::Transaction::HTTP> transaction builder for WebSocket handshake requests.
590  
591 =head1 SEE ALSO
592  
593 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
594  
595 =cut
« Mojo/Message/Request.pm ↑ Summary Mojolicious/Command/version.pm »