Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
merge client_close and server_close
  • Loading branch information
kraih committed Jan 27, 2016
1 parent 5108bab commit 034ece4
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 61 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

6.43 2016-01-27
- Removed client_close and server_close methods from Mojo::Transaction.
- Added closed method to Mojo::Transaction.
- Added parse_message method to Mojo::Transaction::WebSocket.
- Improved a few examples to avoid timing attacks.

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/CGI.pm
Expand Up @@ -38,7 +38,7 @@ sub run {
return undef unless $tx->is_empty || _write($res, 'get_body_chunk');

# Finish transaction
$tx->server_close;
$tx->closed;

return $res->code;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -111,7 +111,7 @@ sub _close {
my ($self, $id) = @_;

# Finish gracefully
if (my $tx = $self->{connections}{$id}{tx}) { $tx->server_close }
if (my $tx = $self->{connections}{$id}{tx}) { $tx->closed }

delete $self->{connections}{$id};
}
Expand All @@ -125,7 +125,7 @@ sub _finish {
return $self->_remove($id) if $tx->is_websocket;

# Finish transaction
delete($c->{tx})->server_close;
delete($c->{tx})->closed;

# Upgrade connection to WebSocket
if (my $ws = delete $c->{next}) {
Expand All @@ -138,7 +138,7 @@ sub _finish {
}

# Failed upgrade
else { $ws->server_close }
else { $ws->closed }
}

# Close connection if necessary
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/PSGI.pm
Expand Up @@ -44,7 +44,7 @@ package Mojo::Server::PSGI::_IO;
use Mojo::Base -base;

# Finish transaction
sub close { shift->{tx}->server_close }
sub close { shift->{tx}->closed }

sub getline {
my $self = shift;
Expand Down
26 changes: 8 additions & 18 deletions lib/Mojo/Transaction.pm
Expand Up @@ -10,11 +10,11 @@ has [
has req => sub { Mojo::Message::Request->new };
has res => sub { Mojo::Message::Response->new };

sub client_close { shift->server_close }

sub client_read { croak 'Method "client_read" not implemented by subclass' }
sub client_write { croak 'Method "client_write" not implemented by subclass' }

sub closed { shift->completed->emit('finish') }

sub completed { ++$_[0]{completed} and return $_[0] }

sub connection {
Expand All @@ -41,8 +41,6 @@ sub remote_address {
: $self->original_remote_address;
}

sub server_close { shift->completed->emit('finish') }

sub server_read { croak 'Method "server_read" not implemented by subclass' }
sub server_write { croak 'Method "server_write" not implemented by subclass' }

Expand Down Expand Up @@ -152,13 +150,6 @@ HTTP response, defaults to a L<Mojo::Message::Response> object.
L<Mojo::Transaction> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 client_close
$tx->client_close;
Transaction closed client-side, used to implement user agents such as
L<Mojo::UserAgent>.
=head2 client_read
$tx->client_read($bytes);
Expand All @@ -173,6 +164,12 @@ Meant to be overloaded in a subclass.
Write data client-side, used to implement user agents such as
L<Mojo::UserAgent>. Meant to be overloaded in a subclass.
=head2 closed
$tx = $tx->closed;
All transaction data has been sent.
=head2 completed
$tx = $tx->completed;
Expand Down Expand Up @@ -223,13 +220,6 @@ Same as L</"original_remote_address"> or the last value of the
C<X-Forwarded-For> header if L</"req"> has been performed through a reverse
proxy.
=head2 server_close
$tx->server_close;
Transaction closed server-side, used to implement web servers such as
L<Mojo::Server::Daemon>.
=head2 server_read
$tx->server_read($bytes);
Expand Down
25 changes: 0 additions & 25 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -3,23 +3,6 @@ use Mojo::Base 'Mojo::Transaction';

has 'previous';

sub client_close {
my ($self, $close) = @_;

# Premature connection close
my $res = $self->res->finish;
if ($close && !$res->code && !$res->error) {
$res->error({message => 'Premature connection close'});
}

# 4xx/5xx
elsif ($res->is_status_class(400) || $res->is_status_class(500)) {
$res->error({message => $res->message, code => $res->code});
}

$self->server_close;
}

sub client_read {
my ($self, $chunk) = @_;

Expand Down Expand Up @@ -268,14 +251,6 @@ L<Mojo::Transaction::HTTP> object.
L<Mojo::Transaction::HTTP> inherits all methods from L<Mojo::Transaction> and
implements the following new ones.
=head2 client_close
$tx->client_close;
$tx->client_close(1);
Transaction closed client-side, no actual connection close is assumed by
default, used to implement user agents such as L<Mojo::UserAgent>.
=head2 client_read
$tx->client_read($bytes);
Expand Down
23 changes: 11 additions & 12 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -48,6 +48,11 @@ sub build_message {
sub client_read { shift->server_read(@_) }
sub client_write { shift->server_write(@_) }

sub closed {
my $self = shift->completed;
return $self->emit(finish => $self->{close} ? (@{$self->{close}}) : 1006);
}

sub connection { shift->handshake->connection }

sub finish {
Expand Down Expand Up @@ -137,11 +142,6 @@ sub send {
return $self->emit('resume');
}

sub server_close {
my $self = shift->completed;
return $self->emit(finish => $self->{close} ? (@{$self->{close}}) : 1006);
}

sub server_read {
my ($self, $chunk) = @_;

Expand Down Expand Up @@ -397,6 +397,12 @@ Read data client-side, used to implement user agents such as L<Mojo::UserAgent>.
Write data client-side, used to implement user agents such as
L<Mojo::UserAgent>.
=head2 closed
$tx = $tx->closed;
All transaction data has been sent.
=head2 connection
my $id = $ws->connection;
Expand Down Expand Up @@ -493,13 +499,6 @@ will be invoked once all data has been written.
use Mojo::WebSocket 'WS_PING';
$ws->send([1, 0, 0, 0, WS_PING, 'Hello World!']);
=head2 server_close
$ws->server_close;
Transaction closed server-side, used to implement web servers such as
L<Mojo::Server::Daemon>.
=head2 server_read
$ws->server_read($data);
Expand Down
10 changes: 9 additions & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -217,7 +217,15 @@ sub _finish {
return unless my $c = $self->{connections}{$id};
$c->{ioloop}->remove($c->{timeout}) if $c->{timeout};
return $self->_reuse($id, $close) unless my $old = $c->{tx};
$old->client_close($close);

# Premature connection close or 4xx/5xx
my $res = $old->closed->res->finish;
if ($close && !$res->code && !$res->error) {
$res->error({message => 'Premature connection close'});
}
elsif ($res->is_status_class(400) || $res->is_status_class(500)) {
$res->error({message => $res->message, code => $res->code});
}

# Always remove connection for WebSockets
return $self->_remove($id) if $old->is_websocket;
Expand Down

0 comments on commit 034ece4

Please sign in to comment.