Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved message parser performance slightly
  • Loading branch information
kraih committed Sep 30, 2011
1 parent 51046a7 commit 4410778
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Changes
@@ -1,8 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.0 2011-09-30 00:00:00
2.0 2011-10-01 00:00:00
- Code name "Leaf Fluttering In Wind", this is a major release.
- Improved Mojo::Headers parser performance slightly.
- Improved message parser performance slightly.
- Fixed small formatting bug in Mojo::Headers.

1.99 2011-09-29 00:00:00
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -50,13 +50,13 @@ sub parse {
}

# Field
if (my @match = $name =~ m/$FIELD_RE/o) {
if (my @match = $name =~ $FIELD_RE) {

# Underscore
(my $id = lc $match[0]) =~ tr/-/_/;

# Flag
$cookies[-1]->$id($id =~ m/$FLAG_RE/o ? 1 : $value);
$cookies[-1]->$id($id =~ $FLAG_RE ? 1 : $value);
}
}
}
Expand Down
44 changes: 17 additions & 27 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -343,35 +343,25 @@ sub _parse_start_line {

# Ignore any leading empty lines
my $line = get_line $self->{buffer};
while ((defined $line) && ($line =~ m/^\s*$/)) {
$line = get_line $self->{buffer};
}
$line = get_line $self->{buffer}
while ((defined $line) && ($line =~ m/^\s*$/));
return unless defined $line;

# We have a (hopefully) full request line
if (defined $line) {
if ($line =~ m/$START_LINE_RE/o) {
$self->method($1);
my $url = $self->url;
$self->method eq 'CONNECT'
? $url->authority($2)
: $url->parse($2);

# HTTP 0.9 is identified by the missing version
if (defined $3) {
$self->version($3);
$self->{state} = 'content';
}
else {
$self->version('0.9');
$self->{state} = 'done';

# HTTP 0.9 has no headers or body and does not support
# pipelining
$self->{buffer} = '';
}
}
else { $self->error('Bad request start line.', 400) }
}
return $self->error('Bad request start line.', 400)
unless $line =~ $START_LINE_RE;
$self->method($1);
my $url = $self->url;
$1 eq 'CONNECT'
? $url->authority($2)
: $url->parse($2);

# HTTP 0.9 is identified by the missing version
$self->{state} = 'content';
return $self->version($3) if defined $3;
$self->version('0.9');
$self->{state} = 'done';
$self->{buffer} = '';
}

1;
Expand Down
39 changes: 11 additions & 28 deletions lib/Mojo/Message/Response.pm
Expand Up @@ -139,39 +139,22 @@ sub _build_start_line {
sub _parse_start_line {
my $self = shift;

# HTTP 0.9 responses have no start line
return $self->{state} = 'content' if $self->version eq '0.9';

# Try to detect HTTP 0.9
if ($self->{buffer} =~ /^\s*(\S+\s*)/) {
my $string = $1;

# HTTP 0.9 will most likely not start with "HTTP/"
my $match = "\/PTTH";
substr $match, 0, 5 - length $string, '' if length $string < 5;
$match = reverse $match;

# Detected!
if ($string !~ /^\s*$match/) {
$self->version('0.9');
$self->{state} = 'content';
$self->content->relaxed(1);
return 1;
}
$self->{state} = 'content';
if ($self->{buffer} !~ /^\s*HTTP\//) {
$self->version('0.9');
return $self->content->relaxed(1);
}

# We have a full HTTP 1.0+ response line
my $line = get_line $self->{buffer};
if (defined $line) {
if ($line =~ m/$START_LINE_RE/o) {
$self->version($1);
$self->code($2);
$self->message($3);
$self->{state} = 'content';
$self->content->auto_relax(1);
}
else { $self->error('Bad response start line.') }
}
return unless defined $line;
return $self->error('Bad response start line.')
unless $line =~ $START_LINE_RE;
$self->version($1);
$self->code($2);
$self->message($3);
$self->content->auto_relax(1);
}

1;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -475,7 +475,7 @@ sub punycode_encode {
my $len = length $_[0];

# Remove non basic characters
$output =~ s/[^\x00-\x7f]+//ogs;
$output =~ s/[^\x00-\x7f]+//gs;

# Non basic characters in input
my $h = my $b = length $output;
Expand Down

0 comments on commit 4410778

Please sign in to comment.