Skip to content

Commit

Permalink
Allow decoding of application/javascript.
Browse files Browse the repository at this point in the history
This media type has a charset parameter as per RFC4329, so can
be treated in the same way that XML is.
  • Loading branch information
dracos committed Mar 11, 2018
1 parent 421246d commit 3a6f948
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
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
(text/*, XML, or JSON) 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
12 changes: 12 additions & 0 deletions lib/HTTP/Headers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ sub content_is_json {
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 @@ -749,6 +756,11 @@ content is XML. This method can't be used to set Content-Type.
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) || $self->content_is_json) {
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
(text/*, XML, or JSON) 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

0 comments on commit 3a6f948

Please sign in to comment.