Skip to content

Commit

Permalink
fix a few small CGI bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 24, 2016
1 parent b96cea3 commit 26efcb9
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 194 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,10 +1,11 @@

6.48 2016-02-23
6.48 2016-02-24
- Added files function to Mojo::Util.
- Updated jQuery to version 2.2.1.
- Improved url_for performance significantly.
- Fixed bug where the results of the list_files method in Mojo::Home would
include directories.
- Fixed a few small CGI bugs.

6.47 2016-02-19
- Deprecated Mojo::Log::is_debug, Mojo::Log::is_error, Mojo::Log::is_info and
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Asset/Memory.pm
Expand Up @@ -12,7 +12,7 @@ sub add_chunk {
my ($self, $chunk) = @_;

# Upgrade if necessary
$self->{content} .= $chunk // '';
$self->{content} .= $chunk;
return $self if !$self->auto_upgrade || $self->size <= $self->max_memory_size;
my $file = Mojo::Asset::File->new;
return $file->add_chunk($self->emit(upgrade => $file)->slurp);
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Date.pm
Expand Up @@ -46,7 +46,6 @@ sub parse {
# Invalid
else { return $self->epoch(undef) }

# Prevent crash
my $epoch = eval { timegm $s, $m, $h, $day, $month, $year };
return $self->epoch(
(defined $epoch && ($epoch += $offset) >= 0) ? $epoch : undef);
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Headers.pm
Expand Up @@ -86,10 +86,10 @@ sub names {
}

sub parse {
my $self = shift;
my ($self, $chunk) = @_;

$self->{state} = 'headers';
$self->{buffer} .= shift // '';
$self->{buffer} .= $chunk;
my $headers = $self->{cache} ||= [];
my $size = $self->max_line_size;
my $lines = $self->max_lines;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -142,10 +142,10 @@ sub json {
}

sub parse {
my $self = shift;
my ($self, $chunk) = @_;

return $self if $self->{error};
$self->{raw_size} += length(my $chunk = shift // '');
$self->{raw_size} += length $chunk;
$self->{buffer} .= $chunk;

# Start-line
Expand Down
35 changes: 13 additions & 22 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -107,17 +107,16 @@ sub params {

sub parse {
my $self = shift;
my ($env, $chunk) = ref $_[0] ? (shift, '') : (undef, shift);

# Parse CGI environment
my $env = @_ > 1 ? {@_} : ref $_[0] eq 'HASH' ? $_[0] : undef;
$self->env($env)->_parse_env($env) if $env;

# Parse normal message
my @args = $env ? () : @_;
if (($self->{state} // '') ne 'cgi') { $self->SUPER::parse(@args) }
if (($self->{state} // '') ne 'cgi') { $self->SUPER::parse($chunk) }

# Parse CGI content
else { $self->content($self->content->parse_body(@args))->SUPER::parse }
else { $self->content($self->content->parse_body($chunk))->SUPER::parse('') }

# Check if we can fix things that require all headers
return $self unless $self->is_finished;
Expand All @@ -129,12 +128,11 @@ sub parse {
if (!$base->host && (my $host = $headers->host)) { $base->authority($host) }

# Basic authentication
my $auth = _parse_basic_auth($headers->authorization);
$base->userinfo($auth) if $auth;
if (my $basic = _basic($headers->authorization)) { $base->userinfo($basic) }

# Basic proxy authentication
my $proxy_auth = _parse_basic_auth($headers->proxy_authorization);
$self->proxy(Mojo::URL->new->userinfo($proxy_auth)) if $proxy_auth;
my $basic = _basic($headers->proxy_authorization);
$self->proxy(Mojo::URL->new->userinfo($basic)) if $basic;

# "X-Forwarded-Proto"
$base->scheme('https')
Expand All @@ -148,14 +146,14 @@ 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;
}
sub _basic { $_[0] && $_[0] =~ /Basic (.+)$/ ? b64_decode $1 : undef }

sub _parse_env {
my ($self, $env) = @_;

# Bypass normal message parser
$self->{state} = 'cgi';

# Extract headers
my $headers = $self->headers;
my $url = $self->url;
Expand All @@ -167,11 +165,8 @@ sub _parse_env {
$headers->header($name => $value);

# Host/Port
if ($name eq 'HOST') {
my ($host, $port) = ($value, undef);
($host, $port) = ($1, $2) if $host =~ /^([^:]*):?(.*)$/;
$base->host($host)->port($port);
}
$value =~ s/:(\d+)$// ? $base->host($value)->port($1) : $base->host($value)
if $name eq 'HOST';
}

# Content-Type is a special case on some servers
Expand Down Expand Up @@ -209,9 +204,6 @@ sub _parse_env {
$buffer =~ s!^/!!;
$path->parse($buffer);
}

# Bypass normal message parser
$self->{state} = 'cgi';
}

sub _start_line {
Expand Down Expand Up @@ -439,8 +431,7 @@ default.
=head2 parse
$req = $req->parse('GET /foo/bar HTTP/1.1');
$req = $req->parse(REQUEST_METHOD => 'GET');
$req = $req->parse({REQUEST_METHOD => 'GET'});
$req = $req->parse({PATH_INFO => '/'});
Parse HTTP request chunks or environment hash.
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/Parameters.pm
Expand Up @@ -119,11 +119,9 @@ sub parse {

sub remove {
my ($self, $name) = @_;

my $pairs = $self->pairs;
my $i = 0;
$pairs->[$i] eq $name ? splice @$pairs, $i, 2 : ($i += 2) while $i < @$pairs;

return $self;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -145,7 +145,7 @@ sub send {
sub server_read {
my ($self, $chunk) = @_;

$self->{read} .= $chunk // '';
$self->{read} .= $chunk;
my $max = $self->max_websocket_size;
while (my $frame = Mojo::WebSocket::parse_frame(\$self->{read}, $max)) {
$self->finish(1009) and last unless ref $frame;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/URL.pm
Expand Up @@ -21,7 +21,7 @@ sub authority {
$self->userinfo(_decode(url_unescape $1)) if $authority =~ s/^([^\@]+)\@//;

# Port
$authority =~ s/:(\d+)$// and $self->port($1);
$self->port($1) if $authority =~ s/:(\d+)$//;

# Host
my $host = url_unescape $authority;
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -290,8 +290,7 @@ sub signed_cookie {
sub stash { Mojo::Util::_stash(stash => @_) }

sub url_for {
my $self = shift;
my $target = shift // '';
my ($self, $target) = (shift, shift // '');

# Absolute URL
return $target if Scalar::Util::blessed $target && $target->isa('Mojo::URL');
Expand Down

1 comment on commit 26efcb9

@yuki-kimoto
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What CGI bug is fixed by this change?

Please sign in to comment.