Skip to content

Commit

Permalink
a few more content event examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 20, 2011
1 parent 404ef41 commit 2e0c6ec
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,7 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.04 2011-10-20 00:00:00
- Fixed typos.
- Improved documentation.

2.03 2011-10-20 00:00:00
- Deprecated all is_done methods in favor of is_finished.
Expand Down
8 changes: 7 additions & 1 deletion lib/Mojo/Content/MultiPart.pm
Expand Up @@ -47,7 +47,7 @@ sub build_boundary {
my $headers = $self->headers;
my $type = $headers->content_type || '';
my $boundary;
$type =~ /boundary=\"?([^\s\"]+)\"?/i and $boundary = $1;
$type =~ /boundary="?([^\s"]+)"?/i and $boundary = $1;
return $boundary if $boundary;

# Generate and check boundary
Expand Down Expand Up @@ -264,6 +264,12 @@ emit the following new ones.
Emitted when a new L<Mojo::Content::Single> part starts.
Note that this event is EXPERIMENTAL and might change without warning!
$multi->on(part => sub {
my ($multi, $single) = @_;
return unless $single->headers->content_disposition =~ /name="([^"]+)"/;
say "Field: $1";
});
=head1 ATTRIBUTES
L<Mojo::Content::MultiPart> inherits all attributes from L<Mojo::Content>
Expand Down
6 changes: 6 additions & 0 deletions lib/Mojo/Content/Single.pm
Expand Up @@ -110,6 +110,12 @@ emit the following new ones.
Emitted when content gets upgraded to a L<Mojo::Content::MultiPart> object.
Note that this event is EXPERIMENTAL and might change without warning!
$single->on(upgrade => sub {
my ($single, $multi) = @_;
return unless $multi->headers->content_type =~ /multipart\/([^;]+)/i;
say "Multipart: $1";
});
=head1 ATTRIBUTES
L<Mojo::Content::Single> inherits all attributes from L<Mojo::Content> and
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/JSON.pm
Expand Up @@ -302,7 +302,7 @@ sub _encode_string {

# Escape string
$string
=~ s/([\x00-\x1F\x7F\x{2028}\x{2029}\\\"\/\b\f\n\r\t])/$REVERSE{$1}/gs;
=~ s/([\x00-\x1F\x7F\x{2028}\x{2029}\\"\/\b\f\n\r\t])/$REVERSE{$1}/gs;

# Stringify
return "\"$string\"";
Expand Down
12 changes: 6 additions & 6 deletions lib/Mojo/Message.pm
Expand Up @@ -71,7 +71,7 @@ sub body_params {
my $params = Mojo::Parameters->new;
my $type = $self->headers->content_type || '';
$params->charset($self->default_charset);
$type =~ /charset=\"?(\S+)\"?/ and $params->charset($1);
$type =~ /charset="?(\S+)"?/ and $params->charset($1);

# "x-application-urlencoded" and "application/x-www-form-urlencoded"
if ($type =~ /(?:x-application|application\/x-www-form)-urlencoded/i) {
Expand Down Expand Up @@ -183,7 +183,7 @@ sub dom {
# Parse
return if $self->is_multipart;
my $charset;
($self->headers->content_type || '') =~ /charset=\"?([^\"\s;]+)\"?/
($self->headers->content_type || '') =~ /charset="?([^"\s;]+)"?/
and $charset = $1;
my $dom = $self->dom_class->new(charset => $charset)->parse($self->body);

Expand Down Expand Up @@ -493,7 +493,7 @@ sub _parse_formdata {
my $content = $self->content;
return \@formdata unless $content->is_multipart;
my $default = $self->default_charset;
($self->headers->content_type || '') =~ /charset=\"?(\S+)\"?/
($self->headers->content_type || '') =~ /charset="?(\S+)"?/
and $default = $1;

# Walk the tree
Expand All @@ -509,14 +509,14 @@ sub _parse_formdata {

# Charset
my $charset = $default;
($part->headers->content_type || '') =~ /charset=\"?(\S+)\"?/
($part->headers->content_type || '') =~ /charset="?(\S+)"?/
and $charset = $1;

# "Content-Disposition"
my $disposition = $part->headers->content_disposition;
next unless $disposition;
my ($name) = $disposition =~ /\ name="?([^\";]+)"?/;
my ($filename) = $disposition =~ /\ filename="?([^\"]*)"?/;
my ($name) = $disposition =~ /\ name="?([^";]+)"?/;
my ($filename) = $disposition =~ /\ filename="?([^"]*)"?/;
my $value = $part;

# Unescape
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/Util.pm
Expand Up @@ -547,7 +547,7 @@ sub qp_encode { $_[0] = MIME::QuotedPrint::encode_qp($_[0]) }
sub quote {

# Escape and quote
$_[0] =~ s/([\"\\])/\\$1/g;
$_[0] =~ s/(["\\])/\\$1/g;
$_[0] = '"' . $_[0] . '"';
}

Expand All @@ -573,14 +573,14 @@ sub trim {
sub unquote {

# Not quoted
return unless $_[0] =~ /^\".*\"$/g;
return unless $_[0] =~ /^".*"$/g;

# Unquote
for ($_[0]) {
s/^\"//g;
s/\"$//g;
s/^"//g;
s/"$//g;
s/\\\\/\\/g;
s/\\\"/\"/g;
s/\\"/"/g;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -143,7 +143,7 @@ sub run {
warn qq/Problem loading URL "$url". ($message)\n/ if $message && !$code;

# Charset
($tx->res->headers->content_type || '') =~ /charset=\"?([^\"\s;]+)\"?/
($tx->res->headers->content_type || '') =~ /charset="?([^"\s;]+)"?/
and $charset = $1
unless defined $charset;

Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -338,7 +338,7 @@ sub _get_content {

# Charset
my $charset;
($tx->res->headers->content_type || '') =~ /charset=\"?([^"\s]+)\"?/
($tx->res->headers->content_type || '') =~ /charset="?([^"\s]+)"?/
and $charset = $1;

# Content
Expand Down

0 comments on commit 2e0c6ec

Please sign in to comment.