Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow decoding of application/json and application/javascript. #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ The following methods are available:
- $mess->decoded\_content( %options )

Returns the content with any `Content-Encoding` undone and for textual content
the raw content encoded to Perl's Unicode strings. If the `Content-Encoding`
or `charset` of the message is unknown this method will fail by returning
`undef`.
(text/*, XML, JSON, or JavaScript) the raw content encoded to Perl's Unicode
strings. If the `Content-Encoding` or `charset` of the message is unknown this
method will fail by returning `undef`.

The following options can be specified.

Expand Down
24 changes: 24 additions & 0 deletions lib/HTTP/Headers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,20 @@ sub content_is_xml {
return 0;
}

sub content_is_json {
my $ct = shift->content_type;
# text/json is not standard but still used by various servers.
# No issue including it as well.
return $ct eq 'application/json' || $ct eq 'text/json' || $ct =~ /\+json$/;
}

sub content_is_javascript {
my $ct = shift->content_type;
# text/javascript is obsolete in RFC4329 but still used.
# No issue including it as well.
return $ct eq 'application/javascript' || $ct eq 'text/javascript';
}

sub referer {
my $self = shift;
if (@_ && $_[0] =~ /#/) {
Expand Down Expand Up @@ -737,6 +751,16 @@ content is XHTML. This method can't be used to set Content-Type.
Returns TRUE if the Content-Type header field indicate that the
content is XML. This method can't be used to set Content-Type.

=item $h->content_is_json

Returns TRUE if the Content-Type header field indicate that the
content is JSON. This method can't be used to set Content-Type.

=item $h->content_is_javascript

Returns TRUE if the Content-Type header field indicate that the
content is JavaScript. This method can't be used to set Content-Type.

=item $h->content_encoding

The Content-Encoding header field is used as a modifier to the
Expand Down
8 changes: 4 additions & 4 deletions lib/HTTP/Message.pm
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ sub decoded_content
}
}

if ($self->content_is_text || (my $is_xml = $self->content_is_xml)) {
if ($self->content_is_text || (my $is_xml = $self->content_is_xml) || $self->content_is_json || $self->content_is_javascript) {
my $charset = lc(
$opt{charset} ||
$self->content_type_charset ||
Expand Down Expand Up @@ -879,9 +879,9 @@ for details about how charset is determined.
=item $mess->decoded_content( %options )

Returns the content with any C<Content-Encoding> undone and for textual content
the raw content encoded to Perl's Unicode strings. If the C<Content-Encoding>
or C<charset> of the message is unknown this method will fail by returning
C<undef>.
(text/*, XML, JSON, or JavaScript) the raw content encoded to Perl's Unicode
strings. If the C<Content-Encoding> or C<charset> of the message is unknown
this method will fail by returning C<undef>.

The following options can be specified.

Expand Down