Skip to content

Commit aa01099

Browse files
committed
Decline processing if we're unable to determine the Content-Type.
1 parent 865be17 commit aa01099

File tree

8 files changed

+69
-26
lines changed

8 files changed

+69
-26
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Revision history for Perl extension Apache2::Filter::Minifier::JavaScript.
22

3+
1.04_02 ???
4+
- decline processing if unable to determine Content-Type of document
5+
36
1.04_01 Tue Oct 16 00:59 PDT 2007
47
- added timing info to the debug logs
58
- use JavaScript::Minifier::XS if available; its -MUCH- faster

MANIFEST

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ MANIFEST.SKIP
66
META.yml
77
README.txt
88
lib/Apache2/Filter/Minifier/JavaScript.pm
9-
t/charset.t
109
t/content-length.t
10+
t/content-type.t
1111
t/decline.t
1212
t/dynamic.t
1313
t/mime-types.t
@@ -25,6 +25,7 @@ t/htdocs/minified-xs.txt
2525
t/MY/slurp.pm
2626
t/MY/CharsetHandler.pm
2727
t/MY/JSHandler.pm
28+
t/MY/NoCTypeHandler.pm
2829
t/MY/PlainHandler.pm
2930
t/MY/UpperCase.pm
3031
t/perl-bin/js.pl

META.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: Apache2-Filter-Minifier-JavaScript
3-
version: 1.04_01
3+
version: 1.04_02
44
author:
55
- Graham TerMarsch ([email protected])
66
abstract: JS minifying output filter
@@ -18,7 +18,7 @@ recommends:
1818
provides:
1919
Apache2::Filter::Minifier::JavaScript:
2020
file: lib/Apache2/Filter/Minifier/JavaScript.pm
21-
version: 1.04_01
21+
version: 1.04_02
2222
generated_by: Module::Build version 0.2805
2323
meta-spec:
2424
url: http://module-build.sourceforge.net/META-spec-v1.2.html

lib/Apache2/Filter/Minifier/JavaScript.pm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ eval { require JavaScript::Minifier::XS; };
1919

2020
###############################################################################
2121
# Version number.
22-
our $VERSION = '1.04_01';
22+
our $VERSION = '1.04_02';
2323

2424
###############################################################################
2525
# MIME-Types we're willing to minify.
@@ -47,8 +47,11 @@ sub handler {
4747
);
4848

4949
# determine Content-Type of document
50-
my $ctype = $r->content_type;
51-
$ctype =~ s{;.*}{};
50+
my ($ctype) = ($r->content_type =~ /^(.+?)(?:;.*)?$/);
51+
unless ($ctype) {
52+
$log->info( "unable to determine content type; skipping : URL ", $r->uri );
53+
return Apache2::Const::DECLINED;
54+
}
5255

5356
# only process JS documents
5457
unless (exists $types{$ctype}) {

t/MY/NoCTypeHandler.pm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package MY::NoCTypeHandler;
2+
3+
use strict;
4+
use warnings;
5+
use Apache2::RequestIO qw();
6+
use Apache2::RequestRec qw();
7+
use Apache2::RequestUtil qw();
8+
use Apache2::Const -compile => qw(OK);
9+
use File::Spec::Functions qw(catfile);
10+
use MY::slurp;
11+
12+
sub handler {
13+
my $r = shift;
14+
$r->print( slurp(catfile($r->document_root,'test.txt')) );
15+
return Apache2::Const::OK;
16+
}
17+
18+
1;

t/charset.t

Lines changed: 0 additions & 19 deletions
This file was deleted.

t/conf/extra.conf.in

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ Alias /raw @DocumentRoot@
2727
PerlAddVar JsMimeType text/json
2828
</Location>
2929

30-
<Location /charset>
30+
<Location /content-type/charset>
3131
SetHandler modperl
3232
PerlResponseHandler MY::CharsetHandler
3333
PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
3434
</Location>
3535

36+
<Location /content-type/missing>
37+
SetHandler modperl
38+
PerlResponseHandler MY::NoCTypeHandler
39+
PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
40+
</Location>
41+
3642
<Location /decline_uc>
3743
SetHandler modperl
3844
PerlResponseHandler MY::PlainHandler

t/content-type.t

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use strict;
2+
use warnings FATAL => 'all';
3+
use Apache::Test;
4+
use Apache::TestRequest;
5+
use Apache::TestUtil qw(t_cmp);
6+
use lib 't';
7+
use MY::slurp;
8+
9+
# Test "Content-Type" headers
10+
plan tests => 3, need_lwp;
11+
12+
# "Content-Type" with additional attributes (e.g. "charset")
13+
charset_minified: {
14+
my $body = GET_BODY '/content-type/charset';
15+
my $min = slurp( 't/htdocs/minified.txt' );
16+
chomp($min);
17+
18+
ok( t_cmp($body, $min) );
19+
}
20+
21+
# Missing "Content-Type" header; should decline processing and we get the
22+
# un-minified version. Apache, however, -will- set a default "Content-Type"
23+
# into the response.
24+
content_type_missing: {
25+
my $res = GET '/content-type/missing';
26+
my $body = $res->content;
27+
my $orig = slurp( 't/htdocs/test.js' );
28+
29+
ok( $res->content_type !~ m{text/javascript} );
30+
ok( t_cmp($body, $orig) );
31+
}

0 commit comments

Comments
 (0)