Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
note: tests fail
- Loading branch information
Showing
8 changed files
with
113 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,99 @@ | ||
package Mojo::Channel::HTTP; | ||
use Mojo::Base 'Mojo::Channel'; | ||
|
||
sub is_server { 0 } | ||
|
||
sub write { | ||
my $self = shift; | ||
my $server = $self->is_server; | ||
my $tx = $self->{tx}; | ||
|
||
# Client starts writing right away | ||
$tx->{state} ||= 'write' unless $server; | ||
return '' unless $tx->{state} eq 'write'; | ||
|
||
# Nothing written yet | ||
$self->{$_} ||= 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}; | ||
|
||
# Start-line | ||
my $chunk = ''; | ||
$chunk .= $self->_start_line($msg) if $self->{http_state} eq 'start_line'; | ||
|
||
# Headers | ||
$chunk .= $self->_headers($msg, $server) if $self->{http_state} eq 'headers'; | ||
|
||
# Body | ||
$chunk .= $self->_body($msg, $server) if $self->{http_state} eq 'body'; | ||
|
||
return $chunk; | ||
} | ||
|
||
sub _body { | ||
my ($self, $msg, $finish) = @_; | ||
my $tx = $self->{tx}; | ||
|
||
# Prepare body chunk | ||
my $buffer = $msg->get_body_chunk($self->{offset}); | ||
my $written = defined $buffer ? length $buffer : 0; | ||
$self->{write} = $msg->content->is_dynamic ? 1 : ($self->{write} - $written); | ||
$self->{offset} += $written; | ||
if (defined $buffer) { delete $self->{delay} } | ||
|
||
# Delayed | ||
elsif (delete $self->{delay}) { $tx->{state} = 'paused' } | ||
else { $self->{delay} = 1 } | ||
|
||
# Finished | ||
$tx->{state} = $finish ? 'finished' : 'read' | ||
if $self->{write} <= 0 || defined $buffer && $buffer eq ''; | ||
|
||
return defined $buffer ? $buffer : ''; | ||
} | ||
|
||
sub _headers { | ||
my ($self, $msg, $head) = @_; | ||
my $tx = $self->{tx}; | ||
|
||
# Prepare header chunk | ||
my $buffer = $msg->get_header_chunk($self->{offset}); | ||
my $written = defined $buffer ? length $buffer : 0; | ||
$self->{write} -= $written; | ||
$self->{offset} += $written; | ||
|
||
# Switch to body | ||
if ($self->{write} <= 0) { | ||
$self->{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; | ||
} | ||
} | ||
|
||
return $buffer; | ||
} | ||
|
||
sub _start_line { | ||
my ($self, $msg) = @_; | ||
|
||
# Prepare start-line chunk | ||
my $buffer = $msg->get_start_line_chunk($self->{offset}); | ||
my $written = defined $buffer ? length $buffer : 0; | ||
$self->{write} -= $written; | ||
$self->{offset} += $written; | ||
|
||
# Switch to headers | ||
@$self{qw(http_state write offset)} = ('headers', $msg->header_size, 0) | ||
if $self->{write} <= 0; | ||
|
||
return $buffer; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters