Skip to content

Commit

Permalink
moved state back into transaction
Browse files Browse the repository at this point in the history
fixes previous test failures
  • Loading branch information
jberger committed Jan 9, 2016
1 parent db4377e commit 30496cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
49 changes: 25 additions & 24 deletions lib/Mojo/Channel/HTTP.pm
Expand Up @@ -13,20 +13,20 @@ sub write {
return '' unless $tx->{state} eq 'write';

# Nothing written yet
$self->{$_} ||= 0 for qw(offset write);
$tx->{$_} ||= 0 for qw(offset write);
my $msg = $server ? $tx->res : $tx->req;
@$self{qw(http_state write)} = ('start_line', $msg->start_line_size)
unless $self->{http_state};
@$tx{qw(http_state write)} = ('start_line', $msg->start_line_size)
unless $tx->{http_state};

# Start-line
my $chunk = '';
$chunk .= $self->_start_line($msg) if $self->{http_state} eq 'start_line';
$chunk .= $self->_start_line($msg) if $tx->{http_state} eq 'start_line';

# Headers
$chunk .= $self->_headers($msg, $server) if $self->{http_state} eq 'headers';
$chunk .= $self->_headers($msg, $server) if $tx->{http_state} eq 'headers';

# Body
$chunk .= $self->_body($msg, $server) if $self->{http_state} eq 'body';
$chunk .= $self->_body($msg, $server) if $tx->{http_state} eq 'body';

return $chunk;
}
Expand All @@ -36,19 +36,19 @@ sub _body {
my $tx = $self->{tx};

# Prepare body chunk
my $buffer = $msg->get_body_chunk($self->{offset});
my $buffer = $msg->get_body_chunk($tx->{offset});
my $written = defined $buffer ? length $buffer : 0;
$self->{write} = $msg->content->is_dynamic ? 1 : ($self->{write} - $written);
$self->{offset} += $written;
$tx->{write} = $msg->content->is_dynamic ? 1 : ($tx->{write} - $written);
$tx->{offset} += $written;
if (defined $buffer) { delete $self->{delay} }

# Delayed
elsif (delete $self->{delay}) { $tx->{state} = 'paused' }
else { $self->{delay} = 1 }
elsif (delete $tx->{delay}) { $tx->{state} = 'paused' }
else { $tx->{delay} = 1 }

# Finished
$tx->{state} = $finish ? 'finished' : 'read'
if $self->{write} <= 0 || defined $buffer && $buffer eq '';
if $tx->{write} <= 0 || defined $buffer && $buffer eq '';

return defined $buffer ? $buffer : '';
}
Expand All @@ -58,22 +58,22 @@ sub _headers {
my $tx = $self->{tx};

# Prepare header chunk
my $buffer = $msg->get_header_chunk($self->{offset});
my $buffer = $msg->get_header_chunk($tx->{offset});
my $written = defined $buffer ? length $buffer : 0;
$self->{write} -= $written;
$self->{offset} += $written;
$tx->{write} -= $written;
$tx->{offset} += $written;

# Switch to body
if ($self->{write} <= 0) {
$self->{offset} = 0;
if ($tx->{write} <= 0) {
$tx->{offset} = 0;

# Response without body
if ($head && $tx->is_empty) { $tx->{state} = 'finished' }

# Body
else {
$self->{http_state} = 'body';
$self->{write} = $msg->content->is_dynamic ? 1 : $msg->body_size;
$tx->{http_state} = 'body';
$tx->{write} = $msg->content->is_dynamic ? 1 : $msg->body_size;
}
}

Expand All @@ -82,16 +82,17 @@ sub _headers {

sub _start_line {
my ($self, $msg) = @_;
my $tx = $self->{tx};

# Prepare start-line chunk
my $buffer = $msg->get_start_line_chunk($self->{offset});
my $buffer = $msg->get_start_line_chunk($tx->{offset});
my $written = defined $buffer ? length $buffer : 0;
$self->{write} -= $written;
$self->{offset} += $written;
$tx->{write} -= $written;
$tx->{offset} += $written;

# Switch to headers
@$self{qw(http_state write offset)} = ('headers', $msg->header_size, 0)
if $self->{write} <= 0;
@$tx{qw(http_state write offset)} = ('headers', $msg->header_size, 0)
if $tx->{write} <= 0;

return $buffer;
}
Expand Down
6 changes: 0 additions & 6 deletions t/mojo/transactor.t
Expand Up @@ -919,10 +919,4 @@ is $tx->req->body, '', 'no content';
is $tx->res->code, undef, 'no status';
is $tx->res->headers->location, undef, 'no "Location" value';

# Abstract methods
eval { Mojo::Transaction->client_write };
like $@, qr/Method "client_write" not implemented by subclass/, 'right error';
eval { Mojo::Transaction->server_write };
like $@, qr/Method "server_write" not implemented by subclass/, 'right error';

done_testing();

0 comments on commit 30496cf

Please sign in to comment.