Mojo/Transaction.pm

filter: Statementstmt 84.0% 42 / 50 Branchbran 44.4% 8 / 18 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 95.2% 20 / 21 Podpod 100% 12 / 12 totaltotal 76.1% 83 / 109 CC 13 SCAR 22.4
CC
13
Cov
70%
remote_address
33.8
result
3.5
completed
2.1
CRAP 9.4
« Mojo/Exception.pm ↑ Summary Mojo/WebSocket.pm »
1 package Mojo::Transaction;
2 26 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 26 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use Mojo::Message::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use Mojo::Message::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use Mojo::Util qw(network_contains);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 26 has [qw(kept_alive local_address local_port original_remote_address remote_port)];
11 503 ▶has req => sub { Mojo::Message::Request->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 424 ▶has res => sub { Mojo::Message::Response->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 1 ▶sub client_read { croak 'Method "client_read" not implemented by subclass' }
Subroutine client_read: called CC=1 SCAR=0.0
Pod documented
15 1 ▶sub client_write { croak 'Method "client_write" not implemented by subclass' }
Subroutine client_write: called CC=1 SCAR=0.0
Pod documented
16  
17 323 ▶sub closed { shift->completed->emit('finish') }
Subroutine closed: called CC=1 SCAR=0.0
Pod documented
18  
19 651 ▶sub completed { ++$_[0]{completed} and return $_[0] }
Subroutine completed: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
++$_[0]{"completed"} and return $_[0] 651 0 651
20  
21 461 ▶sub connection {
Subroutine connection: called CC=2 SCAR=6.9
Pod documented
22 461 my $self = shift;
23 461 ▶ return $self->emit(connection => $self->{connection} = shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 323 138 461
24 138 return $self->{connection};
25 }
26  
27 481 ▶sub error { $_[0]->req->error || $_[0]->res->error }
Subroutine error: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->req->error or $_[0]->res->error 0 481 481
28  
29 367 ▶sub is_finished { !!shift->{completed} }
Subroutine is_finished: called CC=1 SCAR=0.0
Pod documented
30  
31 477 ▶sub is_websocket {undef}
Subroutine is_websocket: called CC=1 SCAR=0.0
Pod documented
32  
33 325 ▶sub remote_address {
Subroutine remote_address: called CC=8 SCAR=35.2
Pod documented
34 325 my $self = shift;
35  
36 325 ▶ return $self->original_remote_address(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 323 2 325
37 2 ▶ return $self->original_remote_address unless $self->req->reverse_proxy;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->req->reverse_proxy 2 0 2
38  
39 # Reverse proxy
40 0 ▶ my @addrs = split /\s*,\s*/, ($self->req->headers->header('X-Forwarded-For') // '');
Condition0%0/2
$self->req->headers->header("X-Forwarded-For") // ""
l!l
00
MC/DC0%0/1
$self->req->headers->header("X-Forwarded-For") // ""
$self->req->headers->header("X-Forwarded-For")
Truth table0%0/2
A
A= $self->req->headers->header("X-Forwarded-For")
Aresult
11
00
41 0 my $trusted = $self->req->trusted_proxies;
42 0 ▶ return @addrs ? $addrs[-1] : $self->original_remote_address unless @$trusted;
Branch: 2 branches
BranchTrueFalseTotal
unless @$trusted 0 0 0
@addrs ? : 0 0 0
43  
44 0 push @addrs, $self->original_remote_address;
45 0 for my $addr (reverse @addrs) {
46 0 ▶ return $addr unless any { network_contains($_, $addr) } @$trusted;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { network_contains($_, $addr); } , @$trusted) 0 0 0
47 }
48 0 return $addrs[0];
49 }
50  
51 4 ▶sub result {
Subroutine result: called CC=3 SCAR=12.5
Pod documented
52 4 my $self = shift;
53 4 my $err = $self->error;
54 4 ▶ return !$err || $err->{code} ? $self->res : croak $err->{message};
Branch: 1 branch
BranchTrueFalseTotal
!$err || $err->{'code'} ? : 4 0 4
Condition33%1/3
not $err or $err->{"code"}
l!l&&r!l&&!r
400
MC/DC0%0/2
not $err or $err->{"code"}
not $err $err->{"code"}
Truth table33%1/3
not(A) or B
A= not $errB= $err->{"code"}
ABresult
1X1
011
000
55 }
56  
57 1 ▶sub server_read { croak 'Method "server_read" not implemented by subclass' }
Subroutine server_read: called CC=1 SCAR=0.0
Pod documented
58 1 ▶sub server_write { croak 'Method "server_write" not implemented by subclass' }
Subroutine server_write: called CC=1 SCAR=0.0
Pod documented
59  
60 26 1;
61  
62 =encoding utf8
63  
64 =head1 NAME
65  
66 Mojo::Transaction - Transaction base class
67  
68 =head1 SYNOPSIS
69  
70 package Mojo::Transaction::MyTransaction;
71 use Mojo::Base 'Mojo::Transaction';
72  
73 sub client_read {...}
74 sub client_write {...}
75 sub server_read {...}
76 sub server_write {...}
77  
78 =head1 DESCRIPTION
79  
80 L<Mojo::Transaction> is an abstract base class for transactions, like L<Mojo::Transaction::HTTP> and
81 L<Mojo::Transaction::WebSocket>.
82  
83 =head1 EVENTS
84  
85 L<Mojo::Transaction> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones.
86  
87 =head2 connection
88  
89 $tx->on(connection => sub ($tx, $connection) {...});
90  
91 Emitted when a connection has been assigned to transaction.
92  
93 =head2 finish
94  
95 $tx->on(finish => sub ($tx) {...});
96  
97 Emitted when transaction is finished.
98  
99 =head1 ATTRIBUTES
100  
101 L<Mojo::Transaction> implements the following attributes.
102  
103 =head2 kept_alive
104  
105 my $bool = $tx->kept_alive;
106 $tx = $tx->kept_alive($bool);
107  
108 Connection has been kept alive.
109  
110 =head2 local_address
111  
112 my $address = $tx->local_address;
113 $tx = $tx->local_address('127.0.0.1');
114  
115 Local interface address.
116  
117 =head2 local_port
118  
119 my $port = $tx->local_port;
120 $tx = $tx->local_port(8080);
121  
122 Local interface port.
123  
124 =head2 original_remote_address
125  
126 my $address = $tx->original_remote_address;
127 $tx = $tx->original_remote_address('127.0.0.1');
128  
129 Remote interface address.
130  
131 =head2 remote_port
132  
133 my $port = $tx->remote_port;
134 $tx = $tx->remote_port(8081);
135  
136 Remote interface port.
137  
138 =head2 req
139  
140 my $req = $tx->req;
141 $tx = $tx->req(Mojo::Message::Request->new);
142  
143 HTTP request, defaults to a L<Mojo::Message::Request> object.
144  
145 # Access request information
146 my $method = $tx->req->method;
147 my $url = $tx->req->url->to_abs;
148 my $info = $tx->req->url->to_abs->userinfo;
149 my $host = $tx->req->url->to_abs->host;
150 my $agent = $tx->req->headers->user_agent;
151 my $custom = $tx->req->headers->header('Custom-Header');
152 my $bytes = $tx->req->body;
153 my $str = $tx->req->text;
154 my $hash = $tx->req->params->to_hash;
155 my $all = $tx->req->uploads;
156 my $value = $tx->req->json;
157 my $foo = $tx->req->json('/23/foo');
158 my $dom = $tx->req->dom;
159 my $bar = $tx->req->dom('div.bar')->first->text;
160  
161 =head2 res
162  
163 my $res = $tx->res;
164 $tx = $tx->res(Mojo::Message::Response->new);
165  
166 HTTP response, defaults to a L<Mojo::Message::Response> object.
167  
168 # Access response information
169 my $code = $tx->res->code;
170 my $message = $tx->res->message;
171 my $server = $tx->res->headers->server;
172 my $custom = $tx->res->headers->header('Custom-Header');
173 my $bytes = $tx->res->body;
174 my $str = $tx->res->text;
175 my $value = $tx->res->json;
176 my $foo = $tx->res->json('/23/foo');
177 my $dom = $tx->res->dom;
178 my $bar = $tx->res->dom('div.bar')->first->text;
179  
180 =head1 METHODS
181  
182 L<Mojo::Transaction> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
183  
184 =head2 client_read
185  
186 $tx->client_read($bytes);
187  
188 Read data client-side, used to implement user agents such as L<Mojo::UserAgent>. Meant to be overloaded in a subclass.
189  
190 =head2 client_write
191  
192 my $bytes = $tx->client_write;
193  
194 Write data client-side, used to implement user agents such as L<Mojo::UserAgent>. Meant to be overloaded in a subclass.
195  
196 =head2 closed
197  
198 $tx = $tx->closed;
199  
200 Same as L</"completed">, but also indicates that all transaction data has been sent.
201  
202 =head2 completed
203  
204 $tx = $tx->completed;
205  
206 Low-level method to finalize transaction.
207  
208 =head2 connection
209  
210 my $id = $tx->connection;
211 $tx = $tx->connection($id);
212  
213 Connection identifier.
214  
215 =head2 error
216  
217 my $err = $tx->error;
218  
219 Get request or response error and return C<undef> if there is no error.
220  
221 # Longer version
222 my $err = $tx->req->error || $tx->res->error;
223  
224 # Check for 4xx/5xx response and connection errors
225 if (my $err = $tx->error) {
226 die "$err->{code} response: $err->{message}" if $err->{code};
227 die "Connection error: $err->{message}";
228 }
229  
230 =head2 is_finished
231  
232 my $bool = $tx->is_finished;
233  
234 Check if transaction is finished.
235  
236 =head2 is_websocket
237  
238 my $bool = $tx->is_websocket;
239  
240 False, this is not a L<Mojo::Transaction::WebSocket> object.
241  
242 =head2 remote_address
243  
244 my $address = $tx->remote_address;
245 $tx = $tx->remote_address('127.0.0.1');
246  
247 Same as L</"original_remote_address"> unless L</"req"> has been performed via a L<Mojo::Message::Request/reverse_proxy>.
248 If so then the last value of C<X-Forwarded-For> header is returned. Additionally if
249 L<Mojo::Message::Request/trusted_proxies> are also provided then the original address must be trusted and any
250 C<X-Forwarded-For> entries that are trusted are ignored, returning the last untrusted address or the first address if
251 all are trusted.
252  
253 =head2 result
254  
255 my $res = $tx->result;
256  
257 Returns the L<Mojo::Message::Response> object from L</"res"> or dies if a connection error has occurred.
258  
259 # Fine grained response handling (dies on connection errors)
260 my $res = $tx->result;
261 if ($res->is_success) { say $res->body }
262 elsif ($res->is_error) { say $res->message }
263 elsif ($res->code == 301) { say $res->headers->location }
264 else { say 'Whatever...' }
265  
266 =head2 server_read
267  
268 $tx->server_read($bytes);
269  
270 Read data server-side, used to implement web servers such as L<Mojo::Server::Daemon>. Meant to be overloaded in a
271 subclass.
272  
273 =head2 server_write
274  
275 my $bytes = $tx->server_write;
276  
277 Write data server-side, used to implement web servers such as L<Mojo::Server::Daemon>. Meant to be overloaded in a
278 subclass.
279  
280 =head1 SEE ALSO
281  
282 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
283  
284 =cut
« Mojo/Exception.pm ↑ Summary Mojo/WebSocket.pm »