Skip to content

Commit

Permalink
merged request and upgrade events in Mojo::Transaction::HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 19, 2011
1 parent e4c7dd7 commit 6ceca27
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 50 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.02 2011-10-20 00:00:00
- Merged request and upgrade events in Mojo::Transaction::HTTP.
- Improved Mojolicious::Controller attribute default values.
- Improved documentation.

Expand Down
10 changes: 0 additions & 10 deletions lib/Mojo.pm
Expand Up @@ -5,7 +5,6 @@ use Carp 'croak';
use Mojo::Home;
use Mojo::Log;
use Mojo::Transaction::HTTP;
use Mojo::Transaction::WebSocket;
use Scalar::Util 'weaken';

has home => sub { Mojo::Home->new };
Expand Down Expand Up @@ -40,8 +39,6 @@ sub build_tx { Mojo::Transaction::HTTP->new }
# "D’oh."
sub handler { croak 'Method "handler" not implemented in subclass' }

sub upgrade_tx { Mojo::Transaction::WebSocket->new(handshake => pop) }

1;
__END__
Expand Down Expand Up @@ -143,13 +140,6 @@ L<Mojo::Transaction::HTTP> or L<Mojo::Transaction::WebSocket> object.
my ($self, $tx) = @_;
}
=head2 C<upgrade_tx>
my $ws = $app->upgrade_tx(tx);
Upgrade transaction, defaults to building a L<Mojo::Transaction::WebSocket>
object.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
8 changes: 0 additions & 8 deletions lib/Mojo/Server.pm
Expand Up @@ -23,8 +23,6 @@ sub new {

sub build_tx { shift->app->build_tx }

sub upgrade_tx { shift->app->upgrade_tx(shift)->server_handshake }

sub load_app {
my ($self, $file) = @_;

Expand Down Expand Up @@ -159,12 +157,6 @@ Note that this method is EXPERIMENTAL and might change without warning!
Start server.
=head2 C<upgrade_tx>
my $upgraded = $server->upgrade_tx(tx);
Let application upgrade transaction.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
21 changes: 11 additions & 10 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -110,18 +110,19 @@ sub _build_tx {
weaken $self;
$tx->on(
request => sub {
my $tx = pop;
my ($tx, $ws) = @_;

# WebSocket
if ($ws) {
$self->{connections}->{$id}->{websocket} = $ws->server_handshake;
$self->emit(request => $ws);
}

# HTTP
$self->emit(request => $tx);
$tx->on(resume => sub { $self->_write($id) });
}
);

# Upgrade
$tx->on(
upgrade => sub {
return unless $_[1]->req->headers->upgrade eq 'websocket';
$self->{connections}->{$id}->{websocket} = $_[1] =
$self->upgrade_tx($_[1]);
# Resume
$tx->on(resume => sub { $self->_write($id) });
}
);

Expand Down
36 changes: 14 additions & 22 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojo::Transaction';

use Mojo::Message::Request;
use Mojo::Message::Response;
use Mojo::Transaction::WebSocket;

has req => sub { Mojo::Message::Request->new };
has res => sub { Mojo::Message::Response->new };
Expand Down Expand Up @@ -148,15 +149,6 @@ EOF
shift->on(request => shift);
}

# DEPRECATED in Smiling Face With Sunglasses!
sub on_upgrade {
warn <<EOF;
Mojo::Transaction::HTTP->on_upgrade is DEPRECATED in favor of using
Mojo::Transaction::HTTP->on!!!
EOF
shift->on(upgrade => shift);
}

sub server_leftovers {
my $self = shift;

Expand Down Expand Up @@ -185,9 +177,15 @@ sub server_read {

# EOF
elsif ((length $chunk == 0) || ($req->is_done && !$self->{handled}++)) {
my $upgraded = $self;
$self->emit(upgrade => $upgraded) if $req->headers->upgrade;
$self->emit(request => $upgraded);

# WebSocket
if (($req->headers->upgrade || '') eq 'websocket') {
$self->emit(
request => Mojo::Transaction::WebSocket->new(handshake => $self));
}

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

# Expect 100 Continue
Expand Down Expand Up @@ -342,18 +340,12 @@ can emit the following new ones.
=head2 C<request>
$tx->on(request => sub {
my ($tx, $upgraded) = @_;
});
Emitted when a request is ready and needs to be handled.
=head2 C<upgrade>
$tx->on(upgrade => sub {
my ($tx, $upgraded) = @_;
my ($tx, $ws) = @_;
});
Emitted when the transaction can be upgraded.
Emitted when a request is ready and needs to be handled, an optional
L<Mojo::Transaction::WebSocket> object will be passed for WebSocket handshake
requests.
=head1 ATTRIBUTES
Expand Down

0 comments on commit 6ceca27

Please sign in to comment.