Skip to content

Commit

Permalink
start_line_size is now an abstract method
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 16, 2015
1 parent f918a4d commit 04c12ef
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 39 deletions.
16 changes: 9 additions & 7 deletions lib/Mojo/Message.pm
Expand Up @@ -183,7 +183,9 @@ sub parse {
return $self->emit('progress')->content->is_finished ? $self->finish : $self;
}

sub start_line_size { length shift->build_start_line }
sub start_line_size {
croak 'Method "start_line_size" not implemented by subclass';
}

sub text {
my $self = shift;
Expand Down Expand Up @@ -302,6 +304,7 @@ Mojo::Message - HTTP message base class
sub cookies {...}
sub extract_start_line {...}
sub get_start_line_chunk {...}
sub start_line_size {...}
=head1 DESCRIPTION
Expand Down Expand Up @@ -438,19 +441,19 @@ Content size in bytes.
my $bytes = $msg->build_body;
Render whole body.
Render whole body with L</"get_body_chunk">.
=head2 build_headers
my $bytes = $msg->build_headers;
Render all headers.
Render all headers with L</"get_header_chunk">.
=head2 build_start_line
my $bytes = $msg->build_start_line;
Render start-line.
Render start-line with L</"get_start_line_chunk">.
=head2 cookie
Expand Down Expand Up @@ -617,7 +620,7 @@ Parse message chunk.
my $size = $msg->start_line_size;
Size of the start-line in bytes.
Size of the start-line in bytes. Meant to be overloaded in a subclass.
=head2 text
Expand All @@ -630,8 +633,7 @@ L</"default_charset">.
my $str = $msg->to_string;
Render whole message. Note that this method finalizes the message, and that it
might not be possible to render the same message twice.
Render whole message.
=head2 upload
Expand Down
60 changes: 36 additions & 24 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -84,30 +84,7 @@ sub fix_headers {

sub get_start_line_chunk {
my ($self, $offset) = @_;

unless (defined $self->{start_buffer}) {

# Path
my $url = $self->url;
my $path = $url->path_query;
$path = "/$path" unless $path =~ m!^/!;

# CONNECT
my $method = uc $self->method;
if ($method eq 'CONNECT') {
my $port = $url->port || ($url->protocol eq 'https' ? '443' : '80');
$path = $url->ihost . ":$port";
}

# Proxy
elsif ($self->proxy && $url->protocol ne 'https') {
$path = $url->clone->userinfo(undef) unless $self->is_handshake;
}

$self->{start_buffer} = "$method $path HTTP/@{[$self->version]}\x0d\x0a";
}

$self->emit(progress => 'start_line', $offset);
$self->_start_line->emit(progress => 'start_line', $offset);
return substr $self->{start_buffer}, $offset, 131072;
}

Expand Down Expand Up @@ -178,6 +155,8 @@ sub proxy {

sub query_params { shift->url->query }

sub start_line_size { length shift->_start_line->{start_buffer} }

sub _parse_basic_auth {
return undef unless my $header = shift;
return $header =~ /Basic (.+)$/ ? b64_decode $1 : undef;
Expand Down Expand Up @@ -244,6 +223,33 @@ sub _parse_env {
$self->{state} = 'cgi';
}

sub _start_line {
my $self = shift;

return $self if defined $self->{start_buffer};

# Path
my $url = $self->url;
my $path = $url->path_query;
$path = "/$path" unless $path =~ m!^/!;

# CONNECT
my $method = uc $self->method;
if ($method eq 'CONNECT') {
my $port = $url->port || ($url->protocol eq 'https' ? '443' : '80');
$path = $url->ihost . ":$port";
}

# Proxy
elsif ($self->proxy && $url->protocol ne 'https') {
$path = $url->clone->userinfo(undef) unless $self->is_handshake;
}

$self->{start_buffer} = "$method $path HTTP/@{[$self->version]}\x0d\x0a";

return $self;
}

1;

=encoding utf8
Expand Down Expand Up @@ -452,6 +458,12 @@ All C<GET> parameters, usually a L<Mojo::Parameters> object.
# Turn GET parameters to hash and extract value
say $req->query_params->to_hash->{foo};
=head2 start_line_size
my $size = $req->start_line_size;
Size of the request-line in bytes.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
28 changes: 20 additions & 8 deletions lib/Mojo/Message/Response.pm
Expand Up @@ -117,14 +117,7 @@ sub fix_headers {

sub get_start_line_chunk {
my ($self, $offset) = @_;

unless (defined $self->{start_buffer}) {
my $code = $self->code || 404;
my $msg = $self->message || $self->default_message;
$self->{start_buffer} = "HTTP/@{[$self->version]} $code $msg\x0d\x0a";
}

$self->emit(progress => 'start_line', $offset);
$self->_start_line->emit(progress => 'start_line', $offset);
return substr $self->{start_buffer}, $offset, 131072;
}

Expand All @@ -140,6 +133,19 @@ sub is_status_class {
return $code >= $class && $code < ($class + 100);
}

sub start_line_size { length shift->_start_line->{start_buffer} }

sub _start_line {
my $self = shift;

return $self if defined $self->{start_buffer};
my $code = $self->code || 404;
my $msg = $self->message || $self->default_message;
$self->{start_buffer} = "HTTP/@{[$self->version]} $code $msg\x0d\x0a";

return $self;
}

1;

=encoding utf8
Expand Down Expand Up @@ -252,6 +258,12 @@ Check if this is a C<1xx>, C<204> or C<304> response.
Check response status class.
=head2 start_line_size
my $size = $req->start_line_size;
Size of the status-line in bytes.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
3 changes: 3 additions & 0 deletions t/mojo/request.t
Expand Up @@ -2095,5 +2095,8 @@ like $@, qr/Method "extract_start_line" not implemented by subclass/,
eval { Mojo::Message->get_start_line_chunk };
like $@, qr/Method "get_start_line_chunk" not implemented by subclass/,
'right error';
eval { Mojo::Message->start_line_size };
like $@, qr/Method "start_line_size" not implemented by subclass/,
'right error';

done_testing();

0 comments on commit 04c12ef

Please sign in to comment.