Skip to content

Commit

Permalink
move client_ methods too
Browse files Browse the repository at this point in the history
  • Loading branch information
jberger committed Nov 29, 2015
1 parent 766c209 commit 4f8ff71
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 38 deletions.
8 changes: 6 additions & 2 deletions lib/Mojo/Channel/HTTP.pm
Expand Up @@ -2,11 +2,15 @@ package Mojo::Channel::HTTP;

use Mojo::Base 'Mojo::Channel';

has tx => sub { Mojo::Transaction->new }
has tx => sub { Mojo::Transaction->new };

sub incoming { die 'meant to be overloaded by subclass' }

sub is_server { die 'meant to be overloaded by subclass' }
sub is_finished { (shift->{state} // '') eq 'finished' }

sub is_server { undef }

sub is_writing { (shift->{state} // 'write') eq 'write' }

sub outgoing { die 'meant to be overloaded by subclass' }

Expand Down
20 changes: 17 additions & 3 deletions lib/Mojo/Channel/HTTP/Client.pm
Expand Up @@ -4,11 +4,25 @@ use Mojo::Base 'Mojo::Channel::HTTP';

sub incoming { shift->tx->res }

sub is_server { undef }

sub outgoing { shift->tx->req }

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

# Skip body for HEAD request
my $res = $self->tx->res;
$res->content->skip_body(1) if uc $self->tx->req->method eq 'HEAD';
return unless $res->parse($chunk)->is_finished;

# Unexpected 1xx response
return $self->{state} = 'finished'
if !$res->is_status_class(100) || $res->headers->upgrade;
$self->tx->_handle_unexpected;
return if (my $leftovers = $res->content->leftovers) eq '';
$self->read($leftovers);
}

sub write {
my $self = shift;

# Client starts writing right away
Expand Down
14 changes: 14 additions & 0 deletions lib/Mojo/Channel/HTTP/Server.pm
Expand Up @@ -8,5 +8,19 @@ sub is_server { 1 }

sub outgoing { shift->tx->res }

sub read {
my ($self, $chunk) = @_;
my $tx = $self->tx;

# Parse request
my $req = $tx->req;
$req->parse($chunk) unless $req->error;
$self->{state} ||= 'read';

# Generate response
return unless $req->is_finished && !$self->{handled}++;
$tx->_announce_request;
}

1;

35 changes: 27 additions & 8 deletions lib/Mojo/Transaction.pm
Expand Up @@ -2,10 +2,13 @@ package Mojo::Transaction;
use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Mojo::Channel::HTTP::Client;
use Mojo::Channel::HTTP::Server;
use Mojo::Message::Request;
use Mojo::Message::Response;
use Scalar::Util;

has channel => sub { Mojo::Channel::HTTP->new };
has 'channel'; #sub { Mojo::Channel::HTTP->new };
has [
qw(kept_alive local_address local_port original_remote_address remote_port)];
has req => sub { Mojo::Message::Request->new };
Expand All @@ -28,8 +31,8 @@ sub client_close {
return $self->server_close;
}

sub client_read { shift->channel->read(@_) }
sub client_write { shift->channel->write(@_) }
sub client_read { shift->_channel->read(@_) }
sub client_write { shift->_channel->write(@_) }

sub connection {
my $self = shift;
Expand All @@ -39,11 +42,11 @@ sub connection {

sub error { $_[0]->req->error || $_[0]->res->error }

sub is_finished { (shift->{state} // '') eq 'finished' }
sub is_finished { (shift->channel || return)->is_finished }

sub is_websocket {undef}

sub is_writing { (shift->{state} // 'write') eq 'write' }
sub is_writing { (shift->channel || return)->is_writing }

sub remote_address {
my $self = shift;
Expand All @@ -60,14 +63,30 @@ sub remote_address {
sub resume { shift->_state(qw(write resume)) }
sub server_close { shift->_state(qw(finished finish)) }

sub server_read { shift->channel->read(@_) }
sub server_write { shift->channel->write(@_) }
sub server_read { shift->_channel->read(@_) }
sub server_write { shift->_channel->write(@_) }

sub success { $_[0]->error ? undef : $_[0]->res }

sub _channel {
my ($self, $server) = @_;
if (my $channel = $self->channel) {
return $channel if $server && $channel->isa('Mojo::Channel::HTTP::Server');
return $channel if !$server && $channel->isa('Mojo::Channel::HTTP::Client');
die 'channel mismatch!'
}
my $class = $server ? 'Mojo::Channel::HTTP::Server' : 'Mojo::Channel::HTTP::Client';
my $channel = $class->new(tx => $self);
Scalar::Util::weaken($channel->{tx});
return $channel;
}

sub _state {
my ($self, $state, $event) = @_;
$self->{state} = $state;
#TODO encapsulation!!!!!
if (my $channel = $self->channel) {
$channel->{state} = $state;
}
return $self->emit($event);
}

Expand Down
34 changes: 9 additions & 25 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -5,22 +5,6 @@ use Mojo::Transaction::WebSocket;

has 'previous';

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

# Skip body for HEAD request
my $res = $self->res;
$res->content->skip_body(1) if uc $self->req->method eq 'HEAD';
return unless $res->parse($chunk)->is_finished;

# Unexpected 1xx response
return $self->{state} = 'finished'
if !$res->is_status_class(100) || $res->headers->upgrade;
$self->res($res->new)->emit(unexpected => $res);
return if (my $leftovers = $res->content->leftovers) eq '';
$self->client_read($leftovers);
}

sub is_empty { !!(uc $_[0]->req->method eq 'HEAD' || $_[0]->res->is_empty) }

sub keep_alive {
Expand Down Expand Up @@ -48,21 +32,21 @@ sub redirects {
return \@redirects;
}

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

# Parse request
#TODO encapsulation!!!!
sub _announce_request {
my $self = shift;
my $req = $self->req;
$req->parse($chunk) unless $req->error;
$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');
}

sub _handle_unexpected {
my $self = shift;
my $res = $self->res;
$self->res($res->new)->emit(unexpected => $res);
}

1;

=encoding utf8
Expand Down

0 comments on commit 4f8ff71

Please sign in to comment.