Skip to content

Commit

Permalink
added continue event to Mojo::Transaction::HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 22, 2012
1 parent e1c106a commit bfe7584
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,7 +1,9 @@

3.60 2012-11-22
- Added continue event to Mojo::Transaction::HTTP.
- Improved documentation.
- Improved tests.
- Fixed 100 Continue support in Mojo::UserAgent.

3.59 2012-11-20
- Improved tests.
Expand Down
18 changes: 16 additions & 2 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -11,11 +11,10 @@ sub client_read {
$res->content->skip_body(1) if $self->req->method eq 'HEAD';

# Parse response
my $state = $self->{state};
$self->{state} = 'finished' if $res->parse($chunk)->is_finished;

# Unexpected 100 Continue
$self->res($res->new)->{state} = $state
$self->res($res->new)->emit('continue')->{state} = 'write_body'
if $self->{state} eq 'finished' && ($res->code // '') eq '100';
}

Expand Down Expand Up @@ -243,6 +242,21 @@ in RFC 2616.
L<Mojo::Transaction::HTTP> inherits all events from L<Mojo::Transaction> and
can emit the following new ones.
=head2 C<continue>
$tx->on(continue => sub {
my $tx = shift;
...
});
Emitted when a C<100 Continue> response has been received and another response
will follow.
$tx->on(continue => sub {
my $tx = shift;
$tx->res->on(finish => sub { say 'Finished followup response.' });
});
=head2 C<request>
$tx->on(request => sub {
Expand Down
45 changes: 11 additions & 34 deletions t/mojo/app.t
Expand Up @@ -99,34 +99,11 @@ $app->routes->post(
# /*
$app->routes->any('/*whatever' => {text => 'Your Mojo is working!'});

# Continue
# Pipelined
my $port = $ua->app_url->port;
my $buffer = '';
my $id;
$id = Mojo::IOLoop->client(
{port => $port} => sub {
my ($loop, $err, $stream) = @_;
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
$buffer .= $chunk;
Mojo::IOLoop->remove($id) and Mojo::IOLoop->stop
if $buffer =~ s/ is working!$//;
$stream->write('4321')
if $buffer =~ m!HTTP/1.1 100 Continue.*\x0d\x0a\x0d\x0a!gs;
}
);
$stream->write("GET /1/ HTTP/1.1\x0d\x0a"
. "Expect: 100-continue\x0d\x0a"
. "Content-Length: 4\x0d\x0a\x0d\x0a");
}
);
Mojo::IOLoop->start;
like $buffer, qr!HTTP/1.1 100 Continue.*Mojo$!s, 'request was continued';

# Pipelined
$buffer = '';
$id = Mojo::IOLoop->client(
{port => $port} => sub {
my ($loop, $err, $stream) = @_;
$stream->on(
Expand All @@ -137,9 +114,9 @@ $id = Mojo::IOLoop->client(
if $buffer =~ s/ is working!.*is working!$//gs;
}
);
$stream->write("GET /2/ HTTP/1.1\x0d\x0a"
$stream->write("GET /1/ HTTP/1.1\x0d\x0a"
. "Content-Length: 0\x0d\x0a\x0d\x0a"
. "GET /3/ HTTP/1.1\x0d\x0a"
. "GET /2/ HTTP/1.1\x0d\x0a"
. "Content-Length: 0\x0d\x0a\x0d\x0a");
}
);
Expand All @@ -149,7 +126,7 @@ like $buffer, qr/Mojo$/, 'transactions were pipelined';
# Normal request
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/5/');
$tx->req->url->parse('/3/');
$ua->start($tx);
ok $tx->keep_alive, 'will be kept alive';
is $tx->res->code, 200, 'right status';
Expand All @@ -158,7 +135,7 @@ like $tx->res->body, qr/Mojo/, 'right content';
# Keep alive request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/6/');
$tx->req->url->parse('/4/');
$ua->start($tx);
ok $tx->keep_alive, 'will be kept alive';
ok $tx->kept_alive, 'was kept alive';
Expand All @@ -168,7 +145,7 @@ like $tx->res->body, qr/Mojo/, 'right content';
# Non keep alive request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/7/');
$tx->req->url->parse('/5/');
$tx->req->headers->connection('close');
$ua->start($tx);
ok !$tx->keep_alive, 'will not be kept alive';
Expand All @@ -180,7 +157,7 @@ like $tx->res->body, qr/Mojo/, 'right content';
# Second non keep alive request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/8/');
$tx->req->url->parse('/6/');
$tx->req->headers->connection('close');
$ua->start($tx);
ok !$tx->keep_alive, 'will not be kept alive';
Expand All @@ -192,7 +169,7 @@ like $tx->res->body, qr/Mojo/, 'right content';
# POST request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('POST');
$tx->req->url->parse('/9/');
$tx->req->url->parse('/7/');
$tx->req->headers->expect('fun');
$tx->req->body('foo bar baz' x 128);
$ua->start($tx);
Expand All @@ -202,7 +179,7 @@ like $tx->res->body, qr/Mojo/, 'right content';
# POST request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('POST');
$tx->req->url->parse('/10/');
$tx->req->url->parse('/8/');
$tx->req->headers->expect('fun');
$tx->req->body('bar baz foo' x 128);
$ua->start($tx);
Expand All @@ -213,10 +190,10 @@ like $tx->res->body, qr/Mojo/, 'right content';
# Multiple requests
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/11/');
$tx->req->url->parse('/9/');
my $tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse('/12/');
$tx2->req->url->parse('/10/');
$ua->start($tx);
$ua->start($tx2);
ok defined $tx->connection, 'has connection id';
Expand Down
17 changes: 17 additions & 0 deletions t/mojo/user_agent.t
Expand Up @@ -213,6 +213,23 @@ ok $tx->res->is_finished, 'response is finished';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# POST /echo (100 Continue)
my $continue;
$tx = $ua->build_tx(
POST => '/echo' => {'Content-Length' => 15, Expect => '100-continue'});
$tx->on(continue => sub { $continue++ });
my $cb;
$cb = sub {
my $req = shift;
$continue ? $req->write('Hello Continue!') : $req->write(undef, $cb);
};
$tx->req->$cb;
$tx = $ua->start($tx);
ok $tx->success, 'successful';
is $continue, 1, 'continue event has been emitted once';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Hello Continue!', 'right content';

# GET /no_length (missing Content-Length header)
($finished_req, $finished_tx, $finished_res) = ();
$tx = $ua->build_tx(GET => '/no_length');
Expand Down

0 comments on commit bfe7584

Please sign in to comment.