Skip to content

Commit

Permalink
added experimental charset method to Mojo::Content
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 11, 2011
1 parent 1ac1b53 commit 8a14a72
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.27 2011-11-11 00:00:00
- Added EXPERIMENTAL charset method to Mojo::Content.
- Improved documentation.

2.26 2011-11-10 00:00:00
Expand Down
12 changes: 12 additions & 0 deletions lib/Mojo/Content.pm
Expand Up @@ -70,6 +70,11 @@ sub build_headers {
return $headers;
}

sub charset {
(shift->headers->content_type || '') =~ /charset="?([^"\s;]+)"?/i;
return $1;
}

sub clone {
my $self = shift;
return if $self->is_dynamic;
Expand Down Expand Up @@ -513,6 +518,13 @@ Render whole body.
Render all headers.
=head2 C<charset>
my $charset = $content->charset;
Detect content charset.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<clone>
my $clone = $content->clone;
Expand Down
28 changes: 8 additions & 20 deletions lib/Mojo/Message.pm
Expand Up @@ -70,11 +70,10 @@ sub body_params {

# Charset
my $params = Mojo::Parameters->new;
my $type = $self->headers->content_type || '';
$params->charset($self->default_charset);
$type =~ /charset="?(\S+)"?/ and $params->charset($1);
$params->charset($self->content->charset || $self->default_charset);

# "x-application-urlencoded" and "application/x-www-form-urlencoded"
my $type = $self->headers->content_type || '';
if ($type =~ m#(?:x-application|application/x-www-form)-urlencoded#i) {
$params->parse($self->content->asset->slurp);
}
Expand Down Expand Up @@ -180,18 +179,11 @@ sub cookie {

sub dom {
my $self = shift;

# Parse
return if $self->is_multipart;
my $charset;
($self->headers->content_type || '') =~ /charset="?([^"\s;]+)"?/
and $charset = $1;
my $dom = $self->dom_class->new->charset($charset)->parse($self->body);

# Find right away
return $dom->find(@_) if @_;

return $dom;
my $dom =
$self->dom_class->new->charset($self->content->charset)
->parse($self->body);
return @_ ? $dom->find(@_) : $dom;
}

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

# Walk the tree
my @parts;
Expand All @@ -503,9 +493,7 @@ sub _parse_formdata {
}

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

# "Content-Disposition"
my $disposition = $part->headers->content_disposition;
Expand Down
6 changes: 1 addition & 5 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -142,12 +142,8 @@ sub run {
$url = encode 'UTF-8', $url;
warn qq/Problem loading URL "$url". ($message)\n/ if $message && !$code;

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

# Select
$charset //= $tx->res->content->charset;
$self->_select($buffer, $charset, $selector) if $selector;
}

Expand Down
8 changes: 1 addition & 7 deletions lib/Test/Mojo.pm
Expand Up @@ -332,14 +332,8 @@ sub websocket_ok {

sub _get_content {
my ($self, $tx) = @_;

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

# Content
my $content = $tx->res->body;
my $charset = $tx->res->content->charset;
return $charset ? decode($charset, $content) : $content;
}

Expand Down

0 comments on commit 8a14a72

Please sign in to comment.