名前

Mojo::Transaction - トランザクションの基底クラス

使用方法

package Mojo::Transaction::MyTransaction;
use Mojo::Base 'Mojo::Transaction';

sub client_read  {...}
sub client_write {...}
sub server_read  {...}
sub server_write {...}

説明

Mojo::Transactionは、 Mojo::Transaction::HTTPMojo::Transaction::WebSocketのような トランザクションのための抽象基底クラスです。

イベント

Mojo::TransactionMojo::EventEmitterのすべてのイベントを継承しており、 次の新しいイベントを発行します。

connection

$tx->on(connection => sub {
  my ($tx, $connection) = @_;
  ...
});

接続がトランザクションに割り当てられたときに発行されます。

finish

$tx->on(finish => sub {
  my $tx = shift;
  ...
});

トランザクションが終了したときに発行されます。

属性

Mojo::Transactionは次の属性を実装しています。

kept_alive

my $kept_alive = $tx->kept_alive;
$tx            = $tx->kept_alive(1);

接続がケプトアライブ(kept alive)になっている。

local_address

my $local_address = $tx->local_address;
$tx               = $tx->local_address($address);

ローカルインターフェースのアドレス。

local_port

my $local_port = $tx->local_port;
$tx            = $tx->local_port($port);

ローカルインターフェースのポート。

original_remote_address

my $address = $tx->original_remote_address;
$tx         = $tx->original_remote_address('127.0.0.1');

リモートインターフェースアドレス。

remote_port

my $remote_port = $tx->remote_port;
$tx             = $tx->remote_port($port);

リモートインターフェースのポート。

req

my $req = $tx->req;

トランザクションのリクエスト。デフォルトはMojo::Message::Requestオブジェクト。

res

my $res = $tx->res;
$tx     = $tx->res(Mojo::Message::Response->new);

トランザクションのレスポンス。デフォルトはMojo::Message::Responseオブジェクト。

# レスポンスの情報にアクセスする
my $code    = $tx->res->code;
my $message = $tx->res->message;
my $server  = $tx->res->headers->server;
my $custom  = $tx->res->headers->header('Custom-Header');
my $bytes   = $tx->res->body;
my $str     = $tx->res->text;
my $value   = $tx->res->json;
my $foo     = $tx->res->json('/23/foo');
my $dom     = $tx->res->dom;
my $bar     = $tx->res->dom('div.bar')->first->text;

メソッド

Mojo::TransactionMojo::EventEmitterからすべてのメソッドを実装しており、 次の新しいメソッドを実装しています。

client_read

$tx = $tx->client_read($chunk);

クライアントのデータを読み込み、処理します。

client_write

my $chunk = $tx->client_write;

クライアントのデータを書き込みます。

closed

$tx = $tx->closed;

"completed"と同じですが、すべてのトランザクションのデータが送信されたことを示します。

completed

$tx = $tx->completed;

トランザクションをファイナライズするための低レベルメソッド。

connection

my $id = $tx->connection;
$tx    = $tx->connection($id);

コネクションの識別子。

error

my $err = $tx->error;

リクエストとレスポンスのエラーを取得します。 エラーがなければundefを返します。 通常はsuccessと共に利用されます。

# 長いバージョン
my $err = $tx->req->error || $tx->res->error;

# 4xx/5xxレスポンスと接続エラーをチェックする
if (my $err = $tx->error) {
  die "$err->{code} response: $err->{message}" if $err->{code};
  die "Connection error: $err->{message}";
}

is_finished

my $bool = $tx->is_finished;

トランザクションが終了したかどうかをチェックします。

is_websocket

my $bool = $tx->is_writing;

トランザクションがWebSocketかどうかをチェックします。

remote_address

my $remote_address = $tx->remote_address;
$tx                = $tx->remote_address($address);

リモートインターフェースのアドレス。

original_remote_addressと同じか、 もし、リクエスト(req)がリバースプロキシによって実行されていれば、 X-Forwarded-Forの値です。

result

my $res = $tx->result;

"res"からMojo::Message::Responseオブジェクトを返すか、コネクションエラーが発生した場合は、例外を発生させます。

# よい粒度のレスポンスのハンドリング (接続エラーの場合は例外発生)
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 'Whatever...' }

server_read

$tx = $tx->server_read($chunk);

サーバーのデータを読み込み、処理する。 サブクラスでオーバーロードされます。

server_write

my $chunk = $tx->server_write;

サーバーのデータを書き込む。 Webサーバーを実装するのに利用されます。 サブクラスでオーバーロードされます。

参考

Mojolicious, Mojolicious::Guides, http://mojolicio.us.

(Mojolicious 8.12を反映。2019年5月29日更新)

関連情報