Skip to content

Commit

Permalink
fixed more progress event bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 3, 2012
1 parent fe517f8 commit 5682c12
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.73 2012-04-04
2.73 2012-04-03
- Improved documentation.
- Improved tests.
- Fixed multiple progress event bugs in Mojo::Message.
Expand Down
14 changes: 5 additions & 9 deletions lib/Mojo/Cookie.pm
Expand Up @@ -10,11 +10,6 @@ use Mojo::Util 'unquote';

has [qw/name value/];

my $COOKIE_SEPARATOR_RE = qr/^\s*\,\s*/;
my $NAME_RE = qr/^\s*([^\=\;\,]+)\s*\=?\s*/;
my $SEPARATOR_RE = qr/^\s*\;\s*/;
my $VALUE_RE = qr/^("(?:\\\\|\\"|[^"])+"|[^\;\,]+)\s*/;

# "My Homer is not a communist.
# He may be a liar, a pig, an idiot, a communist,
# but he is not a porn star."
Expand All @@ -29,22 +24,23 @@ sub _tokenize {
while ($string) {

# Name
if ($string =~ s/$NAME_RE//) {
if ($string =~ s/^\s*([^\=\;\,]+)\s*\=?\s*//) {
my $name = $1;

# "expires" is a special case, thank you Netscape...
$string =~ s/^([^\;\,]+\,?[^\;\,]+)/"$1"/ if $name =~ /^expires$/i;

# Value
my $value;
$value = unquote $1 if $string =~ s/$VALUE_RE//;
$value = unquote $1
if $string =~ s/^("(?:\\\\|\\"|[^"])+"|[^\;\,]+)\s*//;

# Token
push @token, [$name, $value];

# Separator
$string =~ s/$SEPARATOR_RE//;
if ($string =~ s/$COOKIE_SEPARATOR_RE//) {
$string =~ s/^\s*\;\s*//;
if ($string =~ s/^\s*\,\s*//) {
push @tree, [@token];
@token = ();
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -21,7 +21,6 @@ my $CLASS_ID_RE = qr/
(?:\#((?:\\\#|[^\.\#])+)) # ID
)
/x;
my $ELEMENT_RE = qr/^((?:\\\.|\\\#|[^\.\#])+)/;
my $PSEUDO_CLASS_RE = qr/(?:\:([\w\-]+)(?:\(((?:\([^\)]+\)|[^\)])+)\))?)/;
my $TOKEN_RE = qr/
(\s*,\s*)? # Separator
Expand Down Expand Up @@ -142,7 +141,7 @@ sub _compile {

# Element
my $tag = '*';
$element =~ s/$ELEMENT_RE// and $tag = $self->_unescape($1);
$element =~ s/^((?:\\\.|\\\#|[^\.\#])+)// and $tag = $self->_unescape($1);

# Tag
push @$selector, ['tag', $tag];
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -23,7 +23,6 @@ my $ATTR_RE = qr/
\s*
/x;
my $END_RE = qr#^\s*/\s*(.+)\s*#;
my $START_RE = qr#([^\s/]+)([\s\S]*)#;
my $TOKEN_RE = qr/
([^<]*) # Text
(?:
Expand Down Expand Up @@ -118,7 +117,7 @@ sub parse {
if ($tag =~ $END_RE) { $self->_end($cs ? $1 : lc($1), \$current) }

# Start
elsif ($tag =~ $START_RE) {
elsif ($tag =~ qr#([^\s/]+)([\s\S]*)#) {
my ($start, $attr) = ($cs ? $1 : lc($1), $2);

# Attributes
Expand Down
32 changes: 15 additions & 17 deletions lib/Mojo/Message.pm
Expand Up @@ -30,13 +30,11 @@ sub at_least_version {
my ($search_major, $search_minor) = split /\./, $version;
my ($current_major, $current_minor) = split /\./, $self->version;

# Version is equal or newer
# Major version is newer
return 1 if $search_major < $current_major;
return 1
if $search_major == $current_major && $search_minor <= $current_minor;

# Version is older
return;
# Minor version is newer or equal
return $search_major == $current_major && $search_minor <= $current_minor;
}

sub body {
Expand Down Expand Up @@ -163,11 +161,11 @@ sub error {

sub fix_headers {
my $self = shift;
return $self
if $self->{fix}++ || !$self->at_least_version('1.0') || $self->is_chunked;

# Content-Length header or connection close is required in HTTP 1.0
# unless the chunked transfer encoding is used
return $self
if $self->{fix}++ || !$self->at_least_version('1.0') || $self->is_chunked;
my $headers = $self->headers;
$self->is_dynamic
? $headers->connection('close')
Expand All @@ -178,13 +176,13 @@ sub fix_headers {
}

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

# Progress
$self->emit(progress => 'body', @_);
$self->emit(progress => 'body', $offset);

# Chunk
my $chunk = $self->content->get_body_chunk(@_);
my $chunk = $self->content->get_body_chunk($offset);
return $chunk if !defined $chunk || length $chunk;

# Finish
Expand All @@ -195,20 +193,20 @@ sub get_body_chunk {
}

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

# Progress
$self->emit(progress => 'headers', @_);
$self->emit(progress => 'headers', $offset);

# HTTP 0.9 has no headers
return '' if $self->version eq '0.9';

return $self->fix_headers->content->get_header_chunk(@_);
return $self->fix_headers->content->get_header_chunk($offset);
}

sub get_start_line_chunk {
my ($self, $offset) = @_;
$self->emit(progress => 'start_line', @_);
$self->emit(progress => 'start_line', $offset);
return substr $self->{start_line_buffer} //= $self->_build_start_line,
$offset, CHUNK_SIZE;
}
Expand Down Expand Up @@ -248,7 +246,7 @@ sub start_line_size { length shift->build_start_line }

sub to_string {
my $self = shift;
$self->build_start_line . $self->build_headers . $self->build_body;
return $self->build_start_line . $self->build_headers . $self->build_body;
}

sub upload {
Expand Down Expand Up @@ -403,8 +401,6 @@ sub _parse {
return $self;
}

sub _parse_start_line { }

sub _parse_formdata {
my $self = shift;

Expand Down Expand Up @@ -456,6 +452,8 @@ sub _parse_formdata {
return \@formdata;
}

sub _parse_start_line { }

1;
__END__
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -18,7 +18,6 @@ my $START_LINE_RE = qr|
(?:\s+HTTP/(\d+\.\d+))? # Version
$
|x;
my $HOST_RE = qr/^([^\:]*)\:?(.*)$/;

sub clone {
my $self = shift;
Expand Down Expand Up @@ -235,7 +234,7 @@ sub _parse_env {
if ($name eq 'HOST') {
my $host = $value;
my $port;
($host, $port) = ($1, $2) if $host =~ $HOST_RE;
($host, $port) = ($1, $2) if $host =~ /^([^\:]*)\:?(.*)$/;
$base->host($host)->port($port);
}
}
Expand Down
4 changes: 1 addition & 3 deletions lib/Mojo/Message/Response.pm
Expand Up @@ -7,8 +7,6 @@ use Mojo::Util 'get_line';

has [qw/code message/];

my $START_LINE_RE = qr|^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$|;

# Umarked codes are from RFC 2616
my %MESSAGES = (
100 => 'Continue',
Expand Down Expand Up @@ -134,7 +132,7 @@ sub _parse_start_line {
# We have a full HTTP 1.0+ response line
return unless defined(my $line = get_line \$self->{buffer});
return $self->error('Bad response start line.')
unless $line =~ $START_LINE_RE;
unless $line =~ qr|^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$|;
$self->version($1)->code($2)->message($3);
$self->content->auto_relax(1);
$self->{state} = 'content';
Expand Down
22 changes: 17 additions & 5 deletions t/mojo/request.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 976;
use Test::More tests => 980;

# "When will I learn?
# The answer to life's problems aren't at the bottom of a bottle,
Expand Down Expand Up @@ -1194,24 +1194,36 @@ ok $req->is_finished, 'request is finished';

# Build HTTP 1.1 request parts with progress
$req = Mojo::Message::Request->new;
($finished, $progress) = undef;
my $state;
$finished = undef;
$progress = 0;
$req->on(finish => sub { $finished = shift->is_finished });
$req->on(progress => sub { $progress++ });
$req->on(
progress => sub {
my ($req, $part, $offset) = @_;
$state = $part;
$progress += $offset;
}
);
$req->method('get');
$req->url->parse('http://127.0.0.1/foo/bar');
$req->headers->expect('100-continue');
$req->body("Hello World!\n");
ok !$state, 'no state';
ok !$progress, 'no progress';
ok !$finished, 'not finished';
ok $req->build_start_line, 'built start line';
is $state, 'start_line', 'made progress on start_line';
ok $progress, 'made progress';
$progress = undef;
$progress = 0;
ok !$finished, 'not finished';
ok $req->build_headers, 'built headers';
is $state, 'headers', 'made progress on headers';
ok $progress, 'made progress';
$progress = undef;
$progress = 0;
ok !$finished, 'not finished';
ok $req->build_body, 'built body';
is $state, 'body', 'made progress on headers';
ok $progress, 'made progress';
ok $finished, 'finished';

Expand Down
21 changes: 16 additions & 5 deletions t/mojo/response.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 344;
use Test::More tests => 348;

# "Quick Smithers. Bring the mind eraser device!
# You mean the revolver, sir?
Expand Down Expand Up @@ -414,24 +414,35 @@ is $res->body, "Hello World!\n", 'right content';

# Build HTTP 1.1 response parts with progress
$res = Mojo::Message::Response->new;
my ($finished, $progress);
my ($finished, $state);
my $progress = 0;
$res->on(finish => sub { $finished = shift->is_finished });
$res->on(progress => sub { $progress++ });
$res->on(
progress => sub {
my ($res, $part, $offset) = @_;
$state = $part;
$progress += $offset;
}
);
$res->code(200);
$res->headers->connection('keep-alive');
$res->headers->date('Sun, 17 Aug 2008 16:27:35 GMT');
$res->body("Hello World!\n");
ok !$state, 'no state';
ok !$progress, 'no progress';
ok !$finished, 'not finished';
ok $res->build_start_line, 'built start line';
is $state, 'start_line', 'made progress on start_line';
ok $progress, 'made progress';
$progress = undef;
$progress = 0;
ok !$finished, 'not finished';
ok $res->build_headers, 'built headers';
is $state, 'headers', 'made progress on headers';
ok $progress, 'made progress';
$progress = undef;
$progress = 0;
ok !$finished, 'not finished';
ok $res->build_body, 'built body';
is $state, 'body', 'made progress on headers';
ok $progress, 'made progress';
ok $finished, 'finished';

Expand Down

0 comments on commit 5682c12

Please sign in to comment.