Skip to content

Commit

Permalink
the upgrade event in Mojo::Transaction::HTTP does not actually serve …
Browse files Browse the repository at this point in the history
…a purpose
  • Loading branch information
kraih committed Jan 9, 2016
1 parent 3916e24 commit 219f612
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,8 +1,10 @@

6.40 2016-01-09
- Removed upgrade event from Mojo::Transaction::HTTP.
- Replaced deprecated proxy method in Mojo::Message::Request with an
attribute.
- Added SNI support to all built-in web servers. (bpmedley, sri)
- Added next attribute to Mojo::Transaction::HTTP.
- Updated jQuery to version 2.2.0.

6.39 2016-01-03
Expand Down
25 changes: 15 additions & 10 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojo::Server';

use Carp 'croak';
use Mojo::IOLoop;
use Mojo::Transaction::WebSocket;
use Mojo::URL;
use Mojo::Util 'term_escape';
use Scalar::Util 'weaken';
Expand Down Expand Up @@ -76,19 +77,22 @@ sub _build_tx {
$tx->remote_address($handle->peerhost)->remote_port($handle->peerport);
$tx->req->url->base->scheme('https') if $c->{tls};

# Handle upgrades and requests
weaken $self;
$tx->on(
upgrade => sub {
my ($tx, $ws) = @_;
$ws->server_handshake;
$self->{connections}{$id}{ws} = $ws;
}
);
$tx->on(
request => sub {
my $tx = shift;
$self->emit(request => $self->{connections}{$id}{ws} || $tx);

# WebSocket
if ($tx->req->is_handshake) {
my $ws = Mojo::Transaction::WebSocket->new(handshake => $tx);
$ws->server_handshake;
$self->emit(request => $ws);
$tx->next($ws->handshake(undef));
}

# HTTP
else { $self->emit(request => $tx) }

$tx->on(resume => sub { $self->_write($id) });
}
);
Expand Down Expand Up @@ -118,7 +122,8 @@ sub _finish {
$tx->server_close;

# Upgrade connection to WebSocket
if (my $ws = $c->{tx} = delete $c->{ws}) {
if (my $ws = $c->{tx} = $tx->next) {
$c->{tx} = $ws->handshake($tx->next(undef));

# Successful upgrade
if ($ws->res->code == 101) {
Expand Down
32 changes: 10 additions & 22 deletions lib/Mojo/Transaction/HTTP.pm
@@ -1,9 +1,7 @@
package Mojo::Transaction::HTTP;
use Mojo::Base 'Mojo::Transaction';

use Mojo::Transaction::WebSocket;

has 'previous';
has [qw(previous next)];

sub client_read {
my ($self, $chunk) = @_;
Expand Down Expand Up @@ -59,10 +57,7 @@ sub server_read {
$self->{state} ||= 'read';

# Generate response
return unless $req->is_finished && !$self->{handled}++;
$self->emit(upgrade => Mojo::Transaction::WebSocket->new(handshake => $self))
if $req->is_handshake;
$self->emit('request');
$self->emit('request') if $req->is_finished && !$self->{handled}++;
}

sub server_write { shift->_write(1) }
Expand Down Expand Up @@ -229,21 +224,6 @@ Emitted for unexpected C<1xx> responses that will be ignored.
$tx->res->on(finish => sub { say 'Follow-up response is finished.' });
});
=head2 upgrade
$tx->on(upgrade => sub {
my ($tx, $ws) = @_;
...
});
Emitted when transaction gets upgraded to a L<Mojo::Transaction::WebSocket>
object.
$tx->on(upgrade => sub {
my ($tx, $ws) = @_;
$ws->res->headers->header('X-Bender' => 'Bite my shiny metal ass!');
});
=head1 ATTRIBUTES
L<Mojo::Transaction::HTTP> inherits all attributes from L<Mojo::Transaction>
Expand All @@ -261,6 +241,14 @@ L<Mojo::Transaction::HTTP> object.
say $tx->previous->previous->req->url->path;
say $tx->previous->req->url->path;
=head2 next
my $next = $tx->next;
$tx = $tx->next(Mojo::Transaction::WebSocket->new);
Follow-up transaction for connections that will get upgraded, usually a
L<Mojo::Transaction::WebSocket> object.
=head1 METHODS
L<Mojo::Transaction::HTTP> inherits all methods from L<Mojo::Transaction> and
Expand Down

0 comments on commit 219f612

Please sign in to comment.