Skip to content

Commit

Permalink
added upgrade method to Mojo::UserAgent::Transactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 8, 2012
1 parent d058f31 commit 34de6f4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

3.65 2012-12-07
3.65 2012-12-08
- Added upgrade method to Mojo::UserAgent::Transactor.
- Added is_range method to Mojo::Asset.
- Improved documentation.
- Improved tests.
Expand Down
15 changes: 3 additions & 12 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -7,7 +7,6 @@ use Carp 'croak';
use List::Util 'first';
use Mojo::IOLoop;
use Mojo::Server::Daemon;
use Mojo::Transaction::WebSocket;
use Mojo::URL;
use Mojo::UserAgent::CookieJar;
use Mojo::UserAgent::Transactor;
Expand Down Expand Up @@ -474,20 +473,12 @@ sub _start {
sub _upgrade {
my ($self, $id) = @_;

# Check if connection needs to be upgraded
my $c = $self->{connections}{$id};
my $old = $c->{tx};
my $code = $old->res->code // '';
return undef unless $old->req->headers->upgrade && $code eq '101';

# Check challenge and upgrade to WebSocket transaction
my $new = Mojo::Transaction::WebSocket->new(handshake => $old, masked => 1);
return undef unless $new->client_challenge;
$c->{tx} = $new;
my $c = $self->{connections}{$id};
return undef unless my $new = $self->transactor->upgrade($c->{tx});
weaken $self;
$new->on(resume => sub { $self->_write($id) });

return $new;
return $c->{tx} = $new;
}

sub _write {
Expand Down
30 changes: 23 additions & 7 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -173,6 +173,14 @@ sub tx {
return $tx;
}

sub upgrade {
my ($self, $tx) = @_;
my $code = $tx->res->code // '';
return undef unless $tx->req->headers->upgrade && $code eq '101';
my $ws = Mojo::Transaction::WebSocket->new(handshake => $tx, masked => 1);
return $ws->client_challenge ? $ws : undef;
}

sub websocket {
my $self = shift;

Expand Down Expand Up @@ -297,8 +305,8 @@ Actual endpoint for transaction.
my $tx = $t->form('http://kraih.com' => {a => 'b'} => {DNT => 1});
my $tx = $t->form('http://kraih.com', 'UTF-8', {a => 'b'}, {DNT => 1});
Versatile L<Mojo::Transaction::HTTP> builder for C<POST> requests with form
data.
Versatile L<Mojo::Transaction::HTTP> transaction builder for C<POST> requests
with form data.
# Multipart upload with filename
my $tx = $t->form(
Expand All @@ -325,8 +333,8 @@ enforce it by setting the header manually.
my $tx = $t->json('http://kraih.com' => {a => 'b'} => {DNT => 1});
my $tx = $t->json('http://kraih.com' => [1, 2, 3] => {DNT => 1});
Versatile L<Mojo::Transaction::HTTP> builder for C<POST> requests with JSON
data.
Versatile L<Mojo::Transaction::HTTP> transaction builder for C<POST> requests
with JSON data.
# Change method
my $tx = $t->json('mojolicio.us/hello', {hello => 'world'});
Expand Down Expand Up @@ -360,7 +368,8 @@ C<307> or C<308> redirect response if possible.
my $tx = $t->tx(PUT => 'http://kraih.com' => 'Hi!');
my $tx = $t->tx(POST => 'http://kraih.com' => {DNT => 1} => 'Hi!');
Versatile general purpose L<Mojo::Transaction::HTTP> builder for requests.
Versatile general purpose L<Mojo::Transaction::HTTP> transaction builder for
requests.
# Inspect generated request
say $t->tx(GET => 'mojolicio.us' => {DNT => 1} => 'Bye!')->req->to_string;
Expand All @@ -373,13 +382,20 @@ Versatile general purpose L<Mojo::Transaction::HTTP> builder for requests.
my $tx = $t->tx(GET => 'http://mojolicio.us');
$tx->connection($sock);
=head2 C<upgrade>
my $tx = $t->upgrade(Mojo::Transaction::HTTP->new);
Build L<Mojo::Transaction::WebSocket> followup transaction for WebSocket
handshake if possible.
=head2 C<websocket>
my $tx = $t->websocket('ws://localhost:3000');
my $tx = $t->websocket('ws://localhost:3000' => {DNT => 1});
Versatile L<Mojo::Transaction::WebSocket> builder for WebSocket handshake
requests.
Versatile L<Mojo::Transaction::HTTP> transaction builder for WebSocket
handshake requests.
=head1 SEE ALSO
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/transactor.t
Expand Up @@ -5,6 +5,7 @@ use utf8;
use Test::More;
use File::Spec::Functions 'catdir';
use FindBin;
use Mojo::Transaction::WebSocket;
use Mojo::URL;
use Mojo::UserAgent::Transactor;
use Mojo::Util 'encode';
Expand Down Expand Up @@ -303,6 +304,7 @@ is(($t->peer($tx))[2], 3000, 'right port');

# WebSocket handshake
$tx = $t->websocket('ws://127.0.0.1:3000/echo');
ok !$tx->is_websocket, 'not a WebSocket';
is $tx->req->url->to_abs, 'http://127.0.0.1:3000/echo', 'right URL';
is $tx->req->method, 'GET', 'right method';
is $tx->req->headers->connection, 'Upgrade', 'right "Connection" value';
Expand All @@ -312,6 +314,10 @@ ok $tx->req->headers->sec_websocket_protocol,
ok $tx->req->headers->sec_websocket_version,
'has "Sec-WebSocket-Version" value';
is $tx->req->headers->upgrade, 'websocket', 'right "Upgrade" value';
is $t->upgrade($tx), undef, 'not upgraded';
Mojo::Transaction::WebSocket->new(handshake => $tx)->server_handshake;
$tx = $t->upgrade($tx);
ok $tx->is_websocket, 'is a WebSocket';

# WebSocket handshake with header
$tx = $t->websocket('wss://127.0.0.1:3000/echo' => {Expect => 'foo'});
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket.t
Expand Up @@ -196,7 +196,7 @@ $ua->websocket(
}
);
$loop->start;
ok !$ws, 'not a websocket';
ok !$ws, 'not a WebSocket';
is $code, 426, 'right code';
ok $body =~ /^(\d+)failed!$/, 'right content';
is $1, 15, 'right timeout';
Expand Down

0 comments on commit 34de6f4

Please sign in to comment.