.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::Transaction 3pm" .TH Mojo::Transaction 3pm 2024-05-15 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojo::Transaction \- Transaction base class .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& package Mojo::Transaction::MyTransaction; \& use Mojo::Base \*(AqMojo::Transaction\*(Aq; \& \& sub client_read {...} \& sub client_write {...} \& sub server_read {...} \& sub server_write {...} .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojo::Transaction is an abstract base class for transactions, like Mojo::Transaction::HTTP and Mojo::Transaction::WebSocket. .SH EVENTS .IX Header "EVENTS" Mojo::Transaction inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS connection .IX Subsection "connection" .Vb 1 \& $tx\->on(connection => sub ($tx, $connection) {...}); .Ve .PP Emitted when a connection has been assigned to transaction. .SS finish .IX Subsection "finish" .Vb 1 \& $tx\->on(finish => sub ($tx) {...}); .Ve .PP Emitted when transaction is finished. .SH ATTRIBUTES .IX Header "ATTRIBUTES" Mojo::Transaction implements the following attributes. .SS kept_alive .IX Subsection "kept_alive" .Vb 2 \& my $bool = $tx\->kept_alive; \& $tx = $tx\->kept_alive($bool); .Ve .PP Connection has been kept alive. .SS local_address .IX Subsection "local_address" .Vb 2 \& my $address = $tx\->local_address; \& $tx = $tx\->local_address(\*(Aq127.0.0.1\*(Aq); .Ve .PP Local interface address. .SS local_port .IX Subsection "local_port" .Vb 2 \& my $port = $tx\->local_port; \& $tx = $tx\->local_port(8080); .Ve .PP Local interface port. .SS original_remote_address .IX Subsection "original_remote_address" .Vb 2 \& my $address = $tx\->original_remote_address; \& $tx = $tx\->original_remote_address(\*(Aq127.0.0.1\*(Aq); .Ve .PP Remote interface address. .SS remote_port .IX Subsection "remote_port" .Vb 2 \& my $port = $tx\->remote_port; \& $tx = $tx\->remote_port(8081); .Ve .PP Remote interface port. .SS req .IX Subsection "req" .Vb 2 \& my $req = $tx\->req; \& $tx = $tx\->req(Mojo::Message::Request\->new); .Ve .PP HTTP request, defaults to a Mojo::Message::Request object. .PP .Vb 10 \& # Access request information \& my $method = $tx\->req\->method; \& my $url = $tx\->req\->url\->to_abs; \& my $info = $tx\->req\->url\->to_abs\->userinfo; \& my $host = $tx\->req\->url\->to_abs\->host; \& my $agent = $tx\->req\->headers\->user_agent; \& my $custom = $tx\->req\->headers\->header(\*(AqCustom\-Header\*(Aq); \& my $bytes = $tx\->req\->body; \& my $str = $tx\->req\->text; \& my $hash = $tx\->req\->params\->to_hash; \& my $all = $tx\->req\->uploads; \& my $value = $tx\->req\->json; \& my $foo = $tx\->req\->json(\*(Aq/23/foo\*(Aq); \& my $dom = $tx\->req\->dom; \& my $bar = $tx\->req\->dom(\*(Aqdiv.bar\*(Aq)\->first\->text; .Ve .SS res .IX Subsection "res" .Vb 2 \& my $res = $tx\->res; \& $tx = $tx\->res(Mojo::Message::Response\->new); .Ve .PP HTTP response, defaults to a Mojo::Message::Response object. .PP .Vb 11 \& # Access response information \& my $code = $tx\->res\->code; \& my $message = $tx\->res\->message; \& my $server = $tx\->res\->headers\->server; \& my $custom = $tx\->res\->headers\->header(\*(AqCustom\-Header\*(Aq); \& my $bytes = $tx\->res\->body; \& my $str = $tx\->res\->text; \& my $value = $tx\->res\->json; \& my $foo = $tx\->res\->json(\*(Aq/23/foo\*(Aq); \& my $dom = $tx\->res\->dom; \& my $bar = $tx\->res\->dom(\*(Aqdiv.bar\*(Aq)\->first\->text; .Ve .SH METHODS .IX Header "METHODS" Mojo::Transaction inherits all methods from Mojo::EventEmitter and implements the following new ones. .SS client_read .IX Subsection "client_read" .Vb 1 \& $tx\->client_read($bytes); .Ve .PP Read data client-side, used to implement user agents such as Mojo::UserAgent. Meant to be overloaded in a subclass. .SS client_write .IX Subsection "client_write" .Vb 1 \& my $bytes = $tx\->client_write; .Ve .PP Write data client-side, used to implement user agents such as Mojo::UserAgent. Meant to be overloaded in a subclass. .SS closed .IX Subsection "closed" .Vb 1 \& $tx = $tx\->closed; .Ve .PP Same as "completed", but also indicates that all transaction data has been sent. .SS completed .IX Subsection "completed" .Vb 1 \& $tx = $tx\->completed; .Ve .PP Low-level method to finalize transaction. .SS connection .IX Subsection "connection" .Vb 2 \& my $id = $tx\->connection; \& $tx = $tx\->connection($id); .Ve .PP Connection identifier. .SS error .IX Subsection "error" .Vb 1 \& my $err = $tx\->error; .Ve .PP Get request or response error and return \f(CW\*(C`undef\*(C'\fR if there is no error. .PP .Vb 2 \& # Longer version \& my $err = $tx\->req\->error || $tx\->res\->error; \& \& # Check for 4xx/5xx response and connection errors \& if (my $err = $tx\->error) { \& die "$err\->{code} response: $err\->{message}" if $err\->{code}; \& die "Connection error: $err\->{message}"; \& } .Ve .SS is_finished .IX Subsection "is_finished" .Vb 1 \& my $bool = $tx\->is_finished; .Ve .PP Check if transaction is finished. .SS is_websocket .IX Subsection "is_websocket" .Vb 1 \& my $bool = $tx\->is_websocket; .Ve .PP False, this is not a Mojo::Transaction::WebSocket object. .SS remote_address .IX Subsection "remote_address" .Vb 2 \& my $address = $tx\->remote_address; \& $tx = $tx\->remote_address(\*(Aq127.0.0.1\*(Aq); .Ve .PP Same as "original_remote_address" unless "req" has been performed via a "reverse_proxy" in Mojo::Message::Request. If so then the last value of \f(CW\*(C`X\-Forwarded\-For\*(C'\fR header is returned. Additionally if "trusted_proxies" in Mojo::Message::Request are also provided then the original address must be trusted and any \&\f(CW\*(C`X\-Forwarded\-For\*(C'\fR entries that are trusted are ignored, returning the last untrusted address or the first address if all are trusted. .SS result .IX Subsection "result" .Vb 1 \& my $res = $tx\->result; .Ve .PP Returns the Mojo::Message::Response object from "res" or dies if a connection error has occurred. .PP .Vb 6 \& # Fine grained response handling (dies on connection errors) \& my $res = $tx\->result; \& if ($res\->is_success) { say $res\->body } \& elsif ($res\->is_error) { say $res\->message } \& elsif ($res\->code == 301) { say $res\->headers\->location } \& else { say \*(AqWhatever...\*(Aq } .Ve .SS server_read .IX Subsection "server_read" .Vb 1 \& $tx\->server_read($bytes); .Ve .PP Read data server-side, used to implement web servers such as Mojo::Server::Daemon. Meant to be overloaded in a subclass. .SS server_write .IX Subsection "server_write" .Vb 1 \& my $bytes = $tx\->server_write; .Ve .PP Write data server-side, used to implement web servers such as Mojo::Server::Daemon. Meant to be overloaded in a subclass. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .