1Mojo::Transaction(3) User Contributed Perl Documentation Mojo::Transaction(3)
2
3
4
6 Mojo::Transaction - Transaction base class
7
9 package Mojo::Transaction::MyTransaction;
10 use Mojo::Base 'Mojo::Transaction';
11
12 sub client_read {...}
13 sub client_write {...}
14 sub server_read {...}
15 sub server_write {...}
16
18 Mojo::Transaction is an abstract base class for transactions, like
19 Mojo::Transaction::HTTP and Mojo::Transaction::WebSocket.
20
22 Mojo::Transaction inherits all events from Mojo::EventEmitter and can
23 emit the following new ones.
24
25 connection
26 $tx->on(connection => sub ($tx, $connection) {...});
27
28 Emitted when a connection has been assigned to transaction.
29
30 finish
31 $tx->on(finish => sub ($tx) {...});
32
33 Emitted when transaction is finished.
34
36 Mojo::Transaction implements the following attributes.
37
38 kept_alive
39 my $bool = $tx->kept_alive;
40 $tx = $tx->kept_alive($bool);
41
42 Connection has been kept alive.
43
44 local_address
45 my $address = $tx->local_address;
46 $tx = $tx->local_address('127.0.0.1');
47
48 Local interface address.
49
50 local_port
51 my $port = $tx->local_port;
52 $tx = $tx->local_port(8080);
53
54 Local interface port.
55
56 original_remote_address
57 my $address = $tx->original_remote_address;
58 $tx = $tx->original_remote_address('127.0.0.1');
59
60 Remote interface address.
61
62 remote_port
63 my $port = $tx->remote_port;
64 $tx = $tx->remote_port(8081);
65
66 Remote interface port.
67
68 req
69 my $req = $tx->req;
70 $tx = $tx->req(Mojo::Message::Request->new);
71
72 HTTP request, defaults to a Mojo::Message::Request object.
73
74 # Access request information
75 my $method = $tx->req->method;
76 my $url = $tx->req->url->to_abs;
77 my $info = $tx->req->url->to_abs->userinfo;
78 my $host = $tx->req->url->to_abs->host;
79 my $agent = $tx->req->headers->user_agent;
80 my $custom = $tx->req->headers->header('Custom-Header');
81 my $bytes = $tx->req->body;
82 my $str = $tx->req->text;
83 my $hash = $tx->req->params->to_hash;
84 my $all = $tx->req->uploads;
85 my $value = $tx->req->json;
86 my $foo = $tx->req->json('/23/foo');
87 my $dom = $tx->req->dom;
88 my $bar = $tx->req->dom('div.bar')->first->text;
89
90 res
91 my $res = $tx->res;
92 $tx = $tx->res(Mojo::Message::Response->new);
93
94 HTTP response, defaults to a Mojo::Message::Response object.
95
96 # Access response information
97 my $code = $tx->res->code;
98 my $message = $tx->res->message;
99 my $server = $tx->res->headers->server;
100 my $custom = $tx->res->headers->header('Custom-Header');
101 my $bytes = $tx->res->body;
102 my $str = $tx->res->text;
103 my $value = $tx->res->json;
104 my $foo = $tx->res->json('/23/foo');
105 my $dom = $tx->res->dom;
106 my $bar = $tx->res->dom('div.bar')->first->text;
107
109 Mojo::Transaction inherits all methods from Mojo::EventEmitter and
110 implements the following new ones.
111
112 client_read
113 $tx->client_read($bytes);
114
115 Read data client-side, used to implement user agents such as
116 Mojo::UserAgent. Meant to be overloaded in a subclass.
117
118 client_write
119 my $bytes = $tx->client_write;
120
121 Write data client-side, used to implement user agents such as
122 Mojo::UserAgent. Meant to be overloaded in a subclass.
123
124 closed
125 $tx = $tx->closed;
126
127 Same as "completed", but also indicates that all transaction data has
128 been sent.
129
130 completed
131 $tx = $tx->completed;
132
133 Low-level method to finalize transaction.
134
135 connection
136 my $id = $tx->connection;
137 $tx = $tx->connection($id);
138
139 Connection identifier.
140
141 error
142 my $err = $tx->error;
143
144 Get request or response error and return "undef" if there is no error.
145
146 # Longer version
147 my $err = $tx->req->error || $tx->res->error;
148
149 # Check for 4xx/5xx response and connection errors
150 if (my $err = $tx->error) {
151 die "$err->{code} response: $err->{message}" if $err->{code};
152 die "Connection error: $err->{message}";
153 }
154
155 is_finished
156 my $bool = $tx->is_finished;
157
158 Check if transaction is finished.
159
160 is_websocket
161 my $bool = $tx->is_websocket;
162
163 False, this is not a Mojo::Transaction::WebSocket object.
164
165 remote_address
166 my $address = $tx->remote_address;
167 $tx = $tx->remote_address('127.0.0.1');
168
169 Same as "original_remote_address" unless "req" has been performed via a
170 "reverse_proxy" in Mojo::Message::Request. If so then the last value
171 of "X-Forwarded-For" header is returned. Additionally if
172 "trusted_proxies" in Mojo::Message::Request are also provided then the
173 original address must be trusted and any "X-Forwarded-For" entries that
174 are trusted are ignored, returning the last untrusted address or the
175 first address if all are trusted.
176
177 result
178 my $res = $tx->result;
179
180 Returns the Mojo::Message::Response object from "res" or dies if a
181 connection error has occurred.
182
183 # Fine grained response handling (dies on connection errors)
184 my $res = $tx->result;
185 if ($res->is_success) { say $res->body }
186 elsif ($res->is_error) { say $res->message }
187 elsif ($res->code == 301) { say $res->headers->location }
188 else { say 'Whatever...' }
189
190 server_read
191 $tx->server_read($bytes);
192
193 Read data server-side, used to implement web servers such as
194 Mojo::Server::Daemon. Meant to be overloaded in a subclass.
195
196 server_write
197 my $bytes = $tx->server_write;
198
199 Write data server-side, used to implement web servers such as
200 Mojo::Server::Daemon. Meant to be overloaded in a subclass.
201
203 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
204
205
206
207perl v5.34.0 2022-01-21 Mojo::Transaction(3)