Skip to content

Commit

Permalink
added is_handshake method to Mojo::Message::Request
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 26, 2014
1 parent d196623 commit c0b0587
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
4.85 2014-02-26
- Added next_tick method to Mojo::IOLoop and Mojo::Reactor.
- Added host_port and path_query methods to Mojo::URL.
- Added is_handshake method to Mojo::Message::Request.
- Improved Mojo::Reactor::EV responsiveness.
- Fixed IDNA support for CONNECT requests.
- Fixed "0" value bug in Mojo::Message::Request.
Expand Down
13 changes: 10 additions & 3 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -107,9 +107,8 @@ sub get_start_line_chunk {

# Proxy
elsif ($self->proxy) {
$url = $url->clone->userinfo(undef);
my $upgrade = lc($self->headers->upgrade // '');
$path = $url unless $upgrade eq 'websocket' || $url->protocol eq 'https';
$path = $url->clone->userinfo(undef)
unless $self->is_handshake || $url->protocol eq 'https';
}

$self->{start_buffer} = "$method $path HTTP/@{[$self->version]}\x0d\x0a";
Expand All @@ -119,6 +118,8 @@ sub get_start_line_chunk {
return substr $self->{start_buffer}, $offset, 131072;
}

sub is_handshake { lc($_[0]->headers->upgrade // '') eq 'websocket' }

sub is_secure {
my $url = shift->url;
return ($url->protocol || $url->base->protocol) eq 'https';
Expand Down Expand Up @@ -362,6 +363,12 @@ Make sure request has all required headers.
Get a chunk of request line data starting from a specific position.
=head2 is_handshake
my $bool = $req->is_handshake;
Check C<Upgrade> header for C<websocket> value.
=head2 is_secure
my $bool = $req->is_secure;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -60,7 +60,7 @@ sub server_read {
# Generate response
return unless $req->is_finished && !$self->{handled}++;
$self->emit(upgrade => Mojo::Transaction::WebSocket->new(handshake => $self))
if lc($req->headers->upgrade // '') eq 'websocket';
if $req->is_handshake;
$self->emit('request');
}

Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -34,7 +34,7 @@ sub endpoint {

# Proxy for normal HTTP requests
return $self->_proxy($tx, $proto, $host, $port)
if $proto eq 'http' && lc($req->headers->upgrade // '') ne 'websocket';
if $proto eq 'http' && !$req->is_handshake;

return $proto, $host, $port;
}
Expand All @@ -56,8 +56,7 @@ sub proxy_connect {

# WebSocket and/or HTTPS
my $url = $req->url;
my $upgrade = lc($req->headers->upgrade // '');
return undef unless $upgrade eq 'websocket' || $url->protocol eq 'https';
return undef unless $req->is_handshake || $url->protocol eq 'https';

# CONNECT request
my $new = $self->tx(CONNECT => $url->clone->userinfo(undef));
Expand Down Expand Up @@ -128,7 +127,7 @@ sub tx {
sub upgrade {
my ($self, $tx) = @_;
my $code = $tx->res->code // '';
return undef unless $tx->req->headers->upgrade && $code eq '101';
return undef unless $tx->req->is_handshake && $code eq '101';
my $ws = Mojo::Transaction::WebSocket->new(handshake => $tx, masked => 1);
return $ws->client_challenge ? $ws : undef;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -315,7 +315,7 @@ sub _request_ok {
local $Test::Builder::Level = $Test::Builder::Level + 1;

# Establish WebSocket connection
if (lc($tx->req->headers->upgrade // '') eq 'websocket') {
if ($tx->req->is_handshake) {
$self->{messages} = [];
$self->{finished} = undef;
$self->ua->start(
Expand Down
16 changes: 9 additions & 7 deletions t/mojo/request.t
Expand Up @@ -230,10 +230,11 @@ $req->parse("Connection: Upgrade\x0d\x0a");
$req->parse("Sec-WebSocket-Key: abcdef=\x0d\x0a");
$req->parse("Sec-WebSocket-Protocol: sample\x0d\x0a");
$req->parse("Upgrade: websocket\x0d\x0a\x0d\x0a");
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
is $req->url, '/demo', 'right URL';
ok $req->is_finished, 'request is finished';
ok $req->is_handshake, 'request is WebSocket handshake';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
is $req->url, '/demo', 'right URL';
is $req->headers->host, 'example.com', 'right "Host" value';
is $req->headers->connection, 'Upgrade', 'right "Connection" value';
is $req->headers->sec_websocket_protocol, 'sample',
Expand All @@ -249,9 +250,10 @@ $req->parse("GET /foo/bar/baz.html HTTP/1.0\x0d\x0a");
$req->parse("Content-Type: text/plain;charset=UTF-8\x0d\x0a");
$req->parse("Content-Length: 0\x0d\x0a\x0d\x0a");
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
is $req->version, '1.0', 'right version';
is $req->url, '/foo/bar/baz.html', 'right URL';
ok !$req->is_handshake, 'request is not a WebSocket handshake';
is $req->method, 'GET', 'right method';
is $req->version, '1.0', 'right version';
is $req->url, '/foo/bar/baz.html', 'right URL';
is $req->headers->content_type, 'text/plain;charset=UTF-8',
'right "Content-Type" value';
is $req->headers->content_length, 0, 'right "Content-Length" value';
Expand Down

0 comments on commit c0b0587

Please sign in to comment.