名前

Mojo::Transaction::HTTP - HTTPトランザクション

使い方

use Mojo::Transaction::HTTP;

# クライアント
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://example.com');
$tx->req->headers->accept('application/json');
say $tx->res->code;
say $tx->res->headers->content_type;
say $tx->res->body;
say $tx->remote_address;

# サーバー
my $tx = Mojo::Transaction::HTTP->new;
say $tx->req->method;
say $tx->req->url->to_abs;
say $tx->req->headers->accept;
say $tx->remote_address;
$tx->res->code(200);
$tx->res->headers->content_type('text/plain');
$tx->res->body('Hello World!');

説明

Mojo::Transaction::HTTPは、 RFC 7230RFC 7231 に基づく HTTPトランザクションのコンテナです。

イベント

Mojo::Transaction::HTTPMojo::Transactionからすべてのイベントを継承しており、 次の新しいイベントを発行することができます。

request

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

リクエストが準備され、処理可能になったときに発行されます。

$tx->on(request => sub {
  my $tx = shift;
  $tx->res->on(finish => sub { say 'Follow-up response is finished.' });
});

resume

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

トランザクションが再開されたときに発行されます。

unexpected

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

無視される予期しない1xxレスポンスの場合に発行されます。

$tx->on(unexpected => sub {
  my $tx = shift;
  $tx->res->on(finish => sub { say 'Followup response is finished.' });
});

属性

Mojo::Transaction::HTTPMojo::Transactionから すべての属性を継承しており、次の新しい属性を 実装しています。

previous

my $previous = $tx->previous;
$tx          = $tx->previous(Mojo::Transaction::HTTP->new);

この後に続いて起こったトランザクションの引きがねとなった 以前のトランザクション。通常はMojo::Transaction::HTTP オブジェクト。

# 前のリクエストのパス
say $tx->previous->previous->req->url->path;
say $tx->previous->req->url->path;

メソッド

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

client_read

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

クライアントのデータを読み込み、処理します。 エージェントの実装に利用されます。

client_write

my $bytes = $tx->client_write;

クライアントのデータを書き込みます。 エージェントの実装に利用されます。

is_empty

my $bool = $tx->is_empty;

ヘッダリクエストあるいは、1xx, 204,304レスポンスかどうかをチェックします。

keep_alive

my $bool = $tx->keep_alive;

接続がキープアライブかどうかをチェックします。

redirects

my $redirects = $tx->redirects;

この後に続いて起こったトランザクションの引きがねとなった、すべての前のトランザクションのリストを返却します。

# すべての以前のリクエストのパス
say $_->req->url->path for @{$tx->redirects};

resume

$tx = $tx->resume;

トランザクションを再開する。

server_read

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

サーバーのデータを読み込み、処理する。 Webサーバーの実装に利用されます。

server_write

my $chunk = $tx->server_write;

サーバーのデータを書き込む。 Webサーバーの実装に利用されます。

参考

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

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

関連情報