Skip to content

Commit

Permalink
added experimental boundary method to Mojo::Content
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 10, 2011
1 parent cc67f0d commit a0b3338
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 27 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -6,6 +6,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added EXPERIMENTAL on_body attribute to Mojo::Content.
- Added EXPERIMENTAL contains method to Mojo::Path.
- Added EXPERIMENTAL auto_upgrade attribute to Mojo::Content::Single.
- Added EXPERIMENTAL boundary method to Mojo::Content.
- Improved message parser performance slightly.
- Improved Mojo::IOLoop to die if started twice.
- Improved setuidgid in Mojo::Server::Daemon.
Expand Down
26 changes: 17 additions & 9 deletions lib/Mojo/Content.pm
Expand Up @@ -16,6 +16,13 @@ sub body_contains {

sub body_size { croak 'Method "body_size" not implemented by subclass' }

sub boundary {
return
unless (shift->headers->content_type || '')
=~ /multipart.*boundary=\"*([a-zA-Z0-9\'\(\)\,\.\:\?\-\_\+\/]+)/i;
return $1;
}

# "Operator! Give me the number for 911!"
sub build_body {
my $self = shift;
Expand Down Expand Up @@ -131,13 +138,7 @@ sub is_dynamic {
return;
}

sub is_multipart {
my $self = shift;
my $type = $self->headers->content_type || '';
$type =~ /multipart.*boundary=\"*([a-zA-Z0-9\'\(\)\,\.\:\?\-\_\+\/]+)/i
and return $1;
return;
}
sub is_multipart {undef}

sub is_parsing_body {
return 1 if (shift->{state} || '') eq 'body';
Expand Down Expand Up @@ -486,6 +487,13 @@ Check if content contains a specific string.
Content size in bytes.
=head2 C<boundary>
my $boundary = $content->boundary;
Extract multipart boundary from content type header.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<build_body>
my $string = $content->build_body;
Expand Down Expand Up @@ -556,9 +564,9 @@ Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<is_multipart>
my $success = $content->is_multipart;
my $false = $content->is_multipart;
Check if content is multipart.
False.
=head2 C<is_parsing_body>
Expand Down
10 changes: 9 additions & 1 deletion lib/Mojo/Content/MultiPart.pm
Expand Up @@ -128,6 +128,8 @@ sub get_body_chunk {
}
}

sub is_multipart {1}

sub parse {
my $self = shift;

Expand All @@ -148,7 +150,7 @@ sub _parse_multipart {

# Parse
$self->{multi_state} ||= 'multipart_preamble';
my $boundary = $self->is_multipart;
my $boundary = $self->boundary;
while (!$self->is_done) {

# Preamble
Expand Down Expand Up @@ -300,6 +302,12 @@ Note that this method is EXPERIMENTAL and might change without warning!
Get a chunk of content starting from a specfic position.
=head2 C<is_multipart>
my $true = $content->is_multipart;
True.
=head2 C<parse>
$content = $content->parse('Content-Type: multipart/mixed');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/Single.pm
Expand Up @@ -47,7 +47,7 @@ sub parse {
return $self if ($self->{state} || '') eq 'headers' || $self->on_read;

# Content needs to be upgraded to multipart
if ($self->auto_upgrade && $self->is_multipart) {
if ($self->auto_upgrade && defined($self->boundary)) {
return $self if $self->isa('Mojo::Content::MultiPart');
return Mojo::Content::MultiPart->new($self)->parse;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message.pm
Expand Up @@ -772,7 +772,7 @@ Note that this method is EXPERIMENTAL and might change without warning!
my $success = $message->is_multipart;
Check if message content is multipart.
Check if message content is a L<Mojo::Content::MultiPart> object.
=head2 C<json>
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Transaction.pm
Expand Up @@ -29,7 +29,7 @@ sub is_done {
return;
}

sub is_websocket {0}
sub is_websocket {undef}

sub is_writing {
return 1 unless my $state = shift->{state};
Expand Down Expand Up @@ -237,9 +237,9 @@ Check if transaction is done.
=head2 C<is_websocket>
my $success = $tx->is_websocket;
my $false = $tx->is_websocket;
Check if transaction is a WebSocket.
False.
=head2 C<is_writing>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -453,7 +453,7 @@ Finish the WebSocket connection gracefully.
=head2 C<is_websocket>
my $success = $ws->is_websocket;
my $true = $ws->is_websocket;
True.
Expand Down
16 changes: 9 additions & 7 deletions t/mojo/message.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 1231;
use Test::More tests => 1233;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -612,9 +612,10 @@ $req->parse("use strict;\n");
$req->parse("use warnings;\n\n");
$req->parse("print \"Hello World :)\\n\"\n");
$req->parse("\x0d\x0a------------0xKhTmLbOuNdArY--");
ok $req->is_done, 'request is done';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
ok $req->is_done, 'request is done';
is $req->is_multipart, 1, 'multipart content';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
is $req->at_least_version('1.0'), 1, 'at least version 1.0';
is $req->at_least_version('1.2'), undef, 'not version 1.2';
is $req->url, '/foo/bar/baz.html?foo13#23', 'right URL';
Expand Down Expand Up @@ -660,9 +661,10 @@ $req->parse("use strict;\n");
$req->parse("use warnings;\n\n");
$req->parse("print \"Hello World :)\\n\"\n");
$req->parse("\x0d\x0a------------0xKhTmLbOuNdArY--");
ok $req->is_done, 'request is done';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
ok $req->is_done, 'request is done';
is $req->is_multipart, undef, 'no multipart content';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
is $req->at_least_version('1.0'), 1, 'at least version 1.0';
is $req->at_least_version('1.2'), undef, 'not version 1.2';
is $req->url, '/foo/bar/baz.html?foo13#23', 'right URL';
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/websocket.t
Expand Up @@ -191,8 +191,8 @@ $ua->websocket(
}
);
$loop->start;
is $ws, 0, 'not a websocket';
is $code, 426, 'right code';
is $ws, undef, 'not a websocket';
is $code, 426, 'right code';
ok $body =~ /^(\d+)failed!$/, 'right content';
ok $1 < 100, 'right timeout';

Expand Down Expand Up @@ -413,7 +413,7 @@ $ua->websocket(
);
$loop->start;
is $done, 1, 'transaction is done';
is $websocket, 0, 'no websocket';
is $websocket, undef, 'no websocket';
is $code, 500, 'right status';
is $message, 'Internal Server Error', 'right message';

Expand All @@ -429,7 +429,7 @@ $ua->websocket(
}
);
$loop->start;
is $websocket, 0, 'no websocket';
is $websocket, undef, 'no websocket';
is $code, 403, 'right status';
is $message, "i'm a teapot", 'right message';

Expand Down

0 comments on commit a0b3338

Please sign in to comment.