Skip to content

Commit

Permalink
let the text method use default_charset
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 12, 2015
1 parent 45027e8 commit 32be807
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Changes
@@ -1,9 +1,10 @@

5.78 2015-02-11
5.78 2015-02-12
- Replaced expires method in Mojo::Cookie::Response with an attribute.
- Added split_cookie_header function to Mojo::Util.
- Relaxed RFC 822/1123 and RFC 850/1036 handling in Mojo::Date.
- Improved Mojo::Reactor::Poll performance.
- Improved Mojo::Reactor::Poll performance significantly.
- Improved text method in Mojo::Message to use default_charset as well.
- Improved design of built-in templates.
- Fixed bug in Mojo::DOM that made parsing a requirement.
- Fixed warnings in Mojo::URL.
Expand Down
9 changes: 5 additions & 4 deletions lib/Mojo/Message.pm
Expand Up @@ -188,7 +188,7 @@ sub start_line_size { length shift->build_start_line }
sub text {
my $self = shift;
my $body = $self->body;
my $charset = $self->content->charset;
my $charset = $self->content->charset || $self->default_charset;
return $charset ? decode($charset, $body) // $body : $body;
}

Expand Down Expand Up @@ -372,7 +372,8 @@ Message content, defaults to a L<Mojo::Content::Single> object.
my $charset = $msg->default_charset;
$msg = $msg->default_charset('UTF-8');
Default charset used for form-data parsing, defaults to C<UTF-8>.
Default charset used by L</"text"> and for form-data parsing, defaults to
C<UTF-8>.
=head2 max_line_size
Expand Down Expand Up @@ -621,8 +622,8 @@ Size of the start-line in bytes.
my $str = $msg->text;
Retrieve L</"body"> and try to decode it if a charset could be extracted with
L<Mojo::Content/"charset">.
Retrieve L</"body"> and try to decode it with L<Mojo::Content/"charset"> or
L</"default_charset">.
=head2 to_string
Expand Down
12 changes: 6 additions & 6 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -44,11 +44,8 @@ sub run {
# Verbose
weaken $tx;
$tx->res->content->on(
body => sub {
warn $tx->req->$_ for qw(build_start_line build_headers);
warn $tx->res->$_ for qw(build_start_line build_headers);
}
) if $verbose;
body => sub { warn _header($tx->req), _header($tx->res) })
if $verbose;

# Stream content (ignore redirects)
$tx->res->content->unsubscribe('read')->on(
Expand All @@ -73,9 +70,12 @@ sub run {
return _json($buffer, $selector) if $selector eq '' || $selector =~ m!^/!;

# Selector
_select($buffer, $selector, $charset // $tx->res->content->charset, @args);
$charset //= $tx->res->content->charset // $tx->res->default_charset;
_select($buffer, $selector, $charset, @args);
}

sub _header { $_[0]->build_start_line, $_[0]->headers->to_string, "\n\n" }

sub _json {
return unless my $data = j(shift);
return unless defined($data = Mojo::JSON::Pointer->new($data)->get(shift));
Expand Down
9 changes: 8 additions & 1 deletion t/mojo/response.t
Expand Up @@ -975,7 +975,7 @@ ok $res->content->is_dynamic, 'dynamic content';
is $count, length($body), 'right length';
is $full, $body, 'right content';

# Body helper
# Body
$res = Mojo::Message::Response->new;
$res->body('hi there!');
ok !$res->content->asset->is_file, 'stored in memory';
Expand All @@ -991,6 +991,13 @@ is $res->body('hello!')->body, 'hello!', 'right content';
$res->content(Mojo::Content::MultiPart->new);
$res->body('hi!');
is $res->body, 'hi!', 'right content';

# Text
$res = Mojo::Message::Response->new;
my $snowman = encode 'UTF-8', '';
is $res->body($snowman)->text, '', 'right content';
is $res->body($snowman)->dom->text, '', 'right text';
$res = Mojo::Message::Response->new;
my $yatta = encode 'shift_jis', 'やった';
is $res->body($yatta)->text, $yatta, 'right content';
$res->headers->content_type('text/plain;charset=shift_jis');
Expand Down

0 comments on commit 32be807

Please sign in to comment.