Skip to content

Commit 3a6f948

Browse files
committed
Allow decoding of application/javascript.
This media type has a charset parameter as per RFC4329, so can be treated in the same way that XML is.
1 parent 421246d commit 3a6f948

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ The following methods are available:
104104
- $mess->decoded\_content( %options )
105105

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

111111
The following options can be specified.
112112

lib/HTTP/Headers.pm

+12
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,13 @@ sub content_is_json {
412412
return $ct eq 'application/json' || $ct eq 'text/json' || $ct =~ /\+json$/;
413413
}
414414

415+
sub content_is_javascript {
416+
my $ct = shift->content_type;
417+
# text/javascript is obsolete in RFC4329 but still used.
418+
# No issue including it as well.
419+
return $ct eq 'application/javascript' || $ct eq 'text/javascript';
420+
}
421+
415422
sub referer {
416423
my $self = shift;
417424
if (@_ && $_[0] =~ /#/) {
@@ -749,6 +756,11 @@ content is XML. This method can't be used to set Content-Type.
749756
Returns TRUE if the Content-Type header field indicate that the
750757
content is JSON. This method can't be used to set Content-Type.
751758
759+
=item $h->content_is_javascript
760+
761+
Returns TRUE if the Content-Type header field indicate that the
762+
content is JavaScript. This method can't be used to set Content-Type.
763+
752764
=item $h->content_encoding
753765
754766
The Content-Encoding header field is used as a modifier to the

lib/HTTP/Message.pm

+4-4
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ sub decoded_content
351351
}
352352
}
353353

354-
if ($self->content_is_text || (my $is_xml = $self->content_is_xml) || $self->content_is_json) {
354+
if ($self->content_is_text || (my $is_xml = $self->content_is_xml) || $self->content_is_json || $self->content_is_javascript) {
355355
my $charset = lc(
356356
$opt{charset} ||
357357
$self->content_type_charset ||
@@ -879,9 +879,9 @@ for details about how charset is determined.
879879
=item $mess->decoded_content( %options )
880880
881881
Returns the content with any C<Content-Encoding> undone and for textual content
882-
(text/*, XML, or JSON) the raw content encoded to Perl's Unicode strings. If
883-
the C<Content-Encoding> or C<charset> of the message is unknown this method
884-
will fail by returning C<undef>.
882+
(text/*, XML, JSON, or JavaScript) the raw content encoded to Perl's Unicode
883+
strings. If the C<Content-Encoding> or C<charset> of the message is unknown
884+
this method will fail by returning C<undef>.
885885
886886
The following options can be specified.
887887

0 commit comments

Comments
 (0)