Skip to content

Commit

Permalink
simplified transaction state management
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 12, 2013
1 parent 9830d7a commit 517b6f4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
26 changes: 9 additions & 17 deletions lib/Mojo/Transaction.pm
Expand Up @@ -36,11 +36,7 @@ sub is_finished { (shift->{state} // '') eq 'finished' }

sub is_websocket {undef}

sub is_writing {
return 1 unless my $state = shift->{state};
return !!grep { $_ eq $state }
qw(write write_start_line write_headers write_body);
}
sub is_writing { (shift->{state} // 'write') eq 'write' }

sub remote_address {
my $self = shift;
Expand All @@ -61,24 +57,20 @@ sub remote_address {
return $self->{remote_address};
}

sub resume {
my $self = shift;
if (($self->{state} // '') eq 'paused') { $self->{state} = 'write_body' }
elsif (!$self->is_writing) { $self->{state} = 'write' }
return $self->emit('resume');
}

sub server_close {
my $self = shift;
$self->{state} = 'finished';
return $self->emit('finish');
}
sub resume { shift->_state(qw(write resume)) }
sub server_close { shift->_state(qw(finished finish)) }

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

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

sub _state {
my ($self, $state, $event) = @_;
$self->{state} = $state;
return $self->emit($event);
}

1;

=head1 NAME
Expand Down
27 changes: 15 additions & 12 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -74,7 +74,7 @@ sub _body {
}

# Finished
$self->{state} = $finish ? 'finished' : 'read_response'
$self->{state} = $finish ? 'finished' : 'read'
if $self->{write} <= 0 || (defined $buffer && !length $buffer);

return defined $buffer ? $buffer : '';
Expand All @@ -98,7 +98,7 @@ sub _headers {

# Body
else {
$self->{state} = 'write_body';
$self->{http_state} = 'body';
$self->{write} = $msg->content->is_dynamic ? 1 : $msg->body_size;
}
}
Expand All @@ -117,9 +117,9 @@ sub _start_line {

# Switch to headers
if ($self->{write} <= 0) {
$self->{state} = 'write_headers';
$self->{write} = $msg->header_size;
$self->{offset} = 0;
$self->{http_state} = 'headers';
$self->{write} = $msg->header_size;
$self->{offset} = 0;
}

return $buffer;
Expand All @@ -128,31 +128,34 @@ sub _start_line {
sub _write {
my ($self, $server) = @_;

# Client starts writing right away
$self->{state} ||= 'write' unless $server;
return '' unless $self->{state} eq 'write';

# Nothing written yet
$self->{$_} ||= 0 for qw(offset write);
my $msg = $server ? $self->res : $self->req;
if ($server ? ($self->{state} eq 'write') : !$self->{state}) {
unless ($self->{http_state}) {

# Connection header
my $headers = $msg->headers;
$headers->connection($self->keep_alive ? 'keep-alive' : 'close')
unless $headers->connection;

# Switch to start line
$self->{state} = 'write_start_line';
$self->{write} = $msg->start_line_size;
$self->{http_state} = 'start_line';
$self->{write} = $msg->start_line_size;
}

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

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

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

return $chunk;
}
Expand Down

0 comments on commit 517b6f4

Please sign in to comment.