Skip to content

Commit

Permalink
improve Mojo::UserAgent to complain about unsupported protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 23, 2017
1 parent 00625bc commit bd8685e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

7.27 2017-02-22
7.27 2017-02-23
- Added support for UNIX domain sockets. (sri, salva)
- Improved Mojo::UserAgent to complain about unsupported protocols.
- Fixed a bug in Mojo::URL where invalid host strings could be generated.
- Fixed blib handling in Mojo::Home.

Expand Down
11 changes: 9 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -291,9 +291,16 @@ sub _reuse {
sub _start {
my ($self, $loop, $tx, $cb) = @_;

# Application server
# Check protocol (WebSocket handshakes are HTTP)
my $url = $tx->req->url;
unless ($url->is_abs) {
if (my $proto = $url->protocol) {
$tx->res->error({message => "Unsupported protocol: $proto"})
and return $loop->next_tick(sub { $self->$cb($tx) })
unless grep { $proto eq $_ } qw(http http+unix https);
}

# Application server
else {
my $base
= $loop == $self->ioloop ? $self->server->url : $self->server->nb_url;
$url->scheme($base->scheme)->host($base->host)->port($base->port);
Expand Down
13 changes: 7 additions & 6 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -143,13 +143,14 @@ sub websocket {
my $tx = $self->tx(GET => @_);
my $req = $tx->req;
$req->headers->sec_websocket_protocol(join ', ', @$sub) if @$sub;
my $url = $req->url;
my $proto = $url->protocol;
$url->scheme(
$proto eq 'ws+unix' ? 'http+unix' : $proto eq 'wss' ? 'https' : 'http')
if $proto;

# Handshake
# Handshake protocol
my $url = $req->url;
my $proto = $url->protocol // '';
if ($proto eq 'ws') { $url->scheme('http') }
elsif ($proto eq 'wss') { $url->scheme('https') }
elsif ($proto eq 'ws+unix') { $url->scheme('http+unix') }

return client_handshake $tx;
}

Expand Down
7 changes: 7 additions & 0 deletions t/mojo/user_agent.t
Expand Up @@ -175,6 +175,13 @@ ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->max_message_size, 0, 'right value';
is $tx->res->body, 'works!', 'right content';

# Unsupported protocol
$tx = $ua->get('htttp://example.com');
ok !$tx->success, 'not successful';
is $tx->error->{message}, 'Unsupported protocol: htttp', 'right error';
eval { $tx->result };
like $@, qr/Unsupported protocol: htttp/, 'right error';

# Shortcuts for common request methods
is $ua->delete('/method')->res->body, 'DELETE', 'right content';
is $ua->get('/method')->res->body, 'GET', 'right content';
Expand Down
12 changes: 12 additions & 0 deletions t/mojo/websocket.t
Expand Up @@ -380,6 +380,18 @@ $ua->websocket(
Mojo::IOLoop->start;
is $status, 1006, 'right status';

# Unsupported protocol
my $error;
$ua->websocket(
'wsss://example.com' => sub {
my ($ua, $tx) = @_;
$error = $tx->error;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
is $error->{message}, 'Unsupported protocol: wsss', 'right error';

# 16-bit length
$result = undef;
$ua->websocket(
Expand Down

0 comments on commit bd8685e

Please sign in to comment.