1Mojo::Transaction(3)  User Contributed Perl Documentation Mojo::Transaction(3)
2
3
4

NAME

6       Mojo::Transaction - Transaction base class
7

SYNOPSIS

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

DESCRIPTION

18       Mojo::Transaction is an abstract base class for transactions, like
19       Mojo::Transaction::HTTP and Mojo::Transaction::WebSocket.
20

EVENTS

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 {
27           my ($tx, $connection) = @_;
28           ...
29         });
30
31       Emitted when a connection has been assigned to transaction.
32
33   finish
34         $tx->on(finish => sub {
35           my $tx = shift;
36           ...
37         });
38
39       Emitted when transaction is finished.
40

ATTRIBUTES

42       Mojo::Transaction implements the following attributes.
43
44   kept_alive
45         my $bool = $tx->kept_alive;
46         $tx      = $tx->kept_alive($bool);
47
48       Connection has been kept alive.
49
50   local_address
51         my $address = $tx->local_address;
52         $tx         = $tx->local_address('127.0.0.1');
53
54       Local interface address.
55
56   local_port
57         my $port = $tx->local_port;
58         $tx      = $tx->local_port(8080);
59
60       Local interface port.
61
62   original_remote_address
63         my $address = $tx->original_remote_address;
64         $tx         = $tx->original_remote_address('127.0.0.1');
65
66       Remote interface address.
67
68   remote_port
69         my $port = $tx->remote_port;
70         $tx      = $tx->remote_port(8081);
71
72       Remote interface port.
73
74   req
75         my $req = $tx->req;
76         $tx     = $tx->req(Mojo::Message::Request->new);
77
78       HTTP request, defaults to a Mojo::Message::Request object.
79
80         # Access request information
81         my $method = $tx->req->method;
82         my $url    = $tx->req->url->to_abs;
83         my $info   = $tx->req->url->to_abs->userinfo;
84         my $host   = $tx->req->url->to_abs->host;
85         my $agent  = $tx->req->headers->user_agent;
86         my $custom = $tx->req->headers->header('Custom-Header');
87         my $bytes  = $tx->req->body;
88         my $str    = $tx->req->text;
89         my $hash   = $tx->req->params->to_hash;
90         my $all    = $tx->req->uploads;
91         my $value  = $tx->req->json;
92         my $foo    = $tx->req->json('/23/foo');
93         my $dom    = $tx->req->dom;
94         my $bar    = $tx->req->dom('div.bar')->first->text;
95
96   res
97         my $res = $tx->res;
98         $tx     = $tx->res(Mojo::Message::Response->new);
99
100       HTTP response, defaults to a Mojo::Message::Response object.
101
102         # Access response information
103         my $code    = $tx->res->code;
104         my $message = $tx->res->message;
105         my $server  = $tx->res->headers->server;
106         my $custom  = $tx->res->headers->header('Custom-Header');
107         my $bytes   = $tx->res->body;
108         my $str     = $tx->res->text;
109         my $value   = $tx->res->json;
110         my $foo     = $tx->res->json('/23/foo');
111         my $dom     = $tx->res->dom;
112         my $bar     = $tx->res->dom('div.bar')->first->text;
113

METHODS

115       Mojo::Transaction inherits all methods from Mojo::EventEmitter and
116       implements the following new ones.
117
118   client_read
119         $tx->client_read($bytes);
120
121       Read data client-side, used to implement user agents such as
122       Mojo::UserAgent. Meant to be overloaded in a subclass.
123
124   client_write
125         my $bytes = $tx->client_write;
126
127       Write data client-side, used to implement user agents such as
128       Mojo::UserAgent. Meant to be overloaded in a subclass.
129
130   closed
131         $tx = $tx->closed;
132
133       Same as "completed", but also indicates that all transaction data has
134       been sent.
135
136   completed
137         $tx = $tx->completed;
138
139       Low-level method to finalize transaction.
140
141   connection
142         my $id = $tx->connection;
143         $tx    = $tx->connection($id);
144
145       Connection identifier.
146
147   error
148         my $err = $tx->error;
149
150       Get request or response error and return "undef" if there is no error.
151
152         # Longer version
153         my $err = $tx->req->error || $tx->res->error;
154
155         # Check for 4xx/5xx response and connection errors
156         if (my $err = $tx->error) {
157           die "$err->{code} response: $err->{message}" if $err->{code};
158           die "Connection error: $err->{message}";
159         }
160
161   is_finished
162         my $bool = $tx->is_finished;
163
164       Check if transaction is finished.
165
166   is_websocket
167         my $bool = $tx->is_websocket;
168
169       False, this is not a Mojo::Transaction::WebSocket object.
170
171   remote_address
172         my $address = $tx->remote_address;
173         $tx         = $tx->remote_address('127.0.0.1');
174
175       Same as "original_remote_address" or the last value of the
176       "X-Forwarded-For" header if "req" has been performed through a reverse
177       proxy.
178
179   result
180         my $res = $tx->result;
181
182       Returns the Mojo::Message::Response object from "res" or dies if a
183       connection error has occurred.
184
185         # Fine grained response handling (dies on connection errors)
186         my $res = $tx->result;
187         if    ($res->is_success)  { say $res->body }
188         elsif ($res->is_error)    { say $res->message }
189         elsif ($res->code == 301) { say $res->headers->location }
190         else                      { say 'Whatever...' }
191
192   server_read
193         $tx->server_read($bytes);
194
195       Read data server-side, used to implement web servers such as
196       Mojo::Server::Daemon. Meant to be overloaded in a subclass.
197
198   server_write
199         my $bytes = $tx->server_write;
200
201       Write data server-side, used to implement web servers such as
202       Mojo::Server::Daemon. Meant to be overloaded in a subclass.
203

SEE ALSO

205       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
206
207
208
209perl v5.32.0                      2020-07-28              Mojo::Transaction(3)
Impressum