Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 11, 2012
1 parent 80930fe commit b5e9967
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.0 2012-06-10
3.0 2012-06-11
- Code name "Rainbow", this is a major release.
- Renamed Mojo::CookieJar to Mojo::UserAgent::CookieJar.
- Improved message parser performance slightly.
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojo/Message.pm
Expand Up @@ -99,9 +99,9 @@ sub body_size { shift->content->body_size }
# It cost 80 million dollars to make.
# How do you sleep at night?
# On top of a pile of money, with many beautiful women."
sub build_body { shift->_build('body') }
sub build_headers { shift->_build('header') }
sub build_start_line { shift->_build('start_line') }
sub build_body { shift->_build('get_body_chunk') }
sub build_headers { shift->_build('get_header_chunk') }
sub build_start_line { shift->_build('get_start_line_chunk') }

sub cookie {
my ($self, $name) = @_;
Expand Down Expand Up @@ -310,10 +310,9 @@ sub write { shift->content->write(@_) }
sub write_chunk { shift->content->write_chunk(@_) }

sub _build {
my ($self, $part) = @_;
my ($self, $method) = @_;

# Build part from chunks
my $method = "get_${part}_chunk";
my $buffer = '';
my $offset = 0;
while (1) {
Expand Down
57 changes: 19 additions & 38 deletions lib/Mojo/Server/CGI.pm
Expand Up @@ -30,65 +30,46 @@ sub run {
# Response start line
STDOUT->autoflush(1);
binmode STDOUT;
my $res = $tx->res;
my $offset = 0;
while ($self->nph) {
my $chunk = $res->get_start_line_chunk($offset);

# No start line yet, try again
sleep 1 and next unless defined $chunk;

# End of start line
last unless length $chunk;

# Start line
return unless STDOUT->opened;
print STDOUT $chunk;
$offset += length $chunk;
}
my $res = $tx->res;
return if $self->nph && !_write($res, 'get_start_line_chunk');

# Response headers
$res->fix_headers;
my $code = $res->code || 404;
my $message = $res->message || $res->default_message;
$res->headers->status("$code $message") unless $self->nph;
$offset = 0;
while (1) {
my $chunk = $res->get_header_chunk($offset);
return unless _write($res, 'get_header_chunk');

# No headers yet, try again
sleep 1 and next unless defined $chunk;
# Response body
return unless _write($res, 'get_body_chunk');

# End of headers
last unless length $chunk;
# Finish transaction
$tx->server_close;

# Headers
return unless STDOUT->opened;
print STDOUT $chunk;
$offset += length $chunk;
}
return $res->code;
}

# Response body
$offset = 0;
sub _write {
my ($res, $method) = @_;

# Write chunks to STDOUT
my $offset = 0;
while (1) {
my $chunk = $res->get_body_chunk($offset);
my $chunk = $res->$method($offset);

# No content yet, try again
# No chunk yet, try again
sleep 1 and next unless defined $chunk;

# End of content
# End of part
last unless length $chunk;

# Content
# Part
return unless STDOUT->opened;
print STDOUT $chunk;
$offset += length $chunk;
}

# Finish transaction
$tx->server_close;

return $res->code;
return 1;
}

1;
Expand Down

0 comments on commit b5e9967

Please sign in to comment.