Skip to content
Browse files

Text::Globやめて、documentアップデート

  • Loading branch information...
1 parent 7f56ebf commit 60fa174fa3e985c4e14b44ef4890f49a65b1ac06 Masahiro Nagano committed
Showing with 61 additions and 48 deletions.
  1. +0 −1 Makefile.PL
  2. +24 −1 README
  3. +32 −9 lib/Plack/Middleware/Deflater.pm
  4. +3 −35 t/content_type.t
  5. +2 −2 t/vary.t
View
1 Makefile.PL
@@ -6,7 +6,6 @@ build_requires 'Test::More';
requires 'Plack';
requires 'IO::Compress::Gzip';
requires 'IO::Compress::Deflate';
-requires 'Text::Glob';
use_test_base;
auto_include_deps;
author_tests('xt');
View
25 README
@@ -3,7 +3,17 @@ NAME
Deflate
SYNOPSIS
- enable "Deflater";
+ enable sub {
+ my $app = shift;
+ sub {
+ my $env = shift;
+ delete $env->{HTTP_ACCEPT_ENCODING} if $env->{HTTP_USER_AGENT} =~ m!^Mozilla/4.0[678]!; #Nescape has some problem
+ $app->($env);
+ }
+ };
+ enable "Deflater"
+ content_type => ['text/css','text/html','text/javascript','application/javascript'],
+ vary_user_agent => 1;
DESCRIPTION
Plack::Middleware::Deflater is a middleware to encode your response body
@@ -16,6 +26,19 @@ DESCRIPTION
which means the server should support HTTP/1.1 chunked response or
downgrade to HTTP/1.0 and closes the connection.
+CONFIGURATIONS
+ content_type
+ content_type => 'text/html',
+ content_type => [ 'text/html', 'text/css', 'text/javascript', 'application/javascript', 'application/x-javascript' ]
+
+ Content-Type header to apply deflater. if content-type is not
+ defined, Deflater will try to deflate all contents.
+
+ vary_user_agent
+ vary_user_agent => 1
+
+ Add "User-Agent" to Vary header.
+
LICENSE
This software is licensed under the same terms as Perl itself.
View
41 lib/Plack/Middleware/Deflater.pm
@@ -8,18 +8,12 @@ use Plack::Util::Accessor qw( content_type vary_user_agent);
use IO::Compress::Deflate;
use IO::Compress::Gzip;
use Plack::Util;
-use Text::Glob qw/glob_to_regex/;
sub prepare_app {
my $self = shift;
if ( my $match_cts = $self->content_type ) {
- my @matches;
$match_cts = [$match_cts] if ! ref $match_cts;
- for my $match_ct ( @{$match_cts} ) {
- my $re = glob_to_regex($match_ct);
- push @matches, $re;
- }
- $self->content_type(\@matches);
+ $self->content_type($match_cts);
}
}
@@ -40,7 +34,7 @@ sub call {
$content_type =~ s/(;.*)$//;
my $match=0;
for my $match_ct ( @{$match_cts} ) {
- if ( $content_type =~ m!^${match_ct}$! ) {
+ if ( $content_type eq $match_ct ) {
$match++;
last;
}
@@ -116,7 +110,17 @@ Plack::Middleware::Deflater - Compress response body with Gzip or Deflate
=head1 SYNOPSIS
- enable "Deflater";
+ enable sub {
+ my $app = shift;
+ sub {
+ my $env = shift;
+ delete $env->{HTTP_ACCEPT_ENCODING} if $env->{HTTP_USER_AGENT} =~ m!^Mozilla/4.0[678]!; #Nescape has some problem
+ $app->($env);
+ }
+ };
+ enable "Deflater"
+ content_type => ['text/css','text/html','text/javascript','application/javascript'],
+ vary_user_agent => 1;
=head1 DESCRIPTION
@@ -130,6 +134,25 @@ This middleware removes C<Content-Length> and streams encoded content,
which means the server should support HTTP/1.1 chunked response or
downgrade to HTTP/1.0 and closes the connection.
+=head1 CONFIGURATIONS
+
+=over 4
+
+=item content_type
+
+ content_type => 'text/html',
+ content_type => [ 'text/html', 'text/css', 'text/javascript', 'application/javascript', 'application/x-javascript' ]
+
+Content-Type header to apply deflater. if content-type is not defined, Deflater will try to deflate all contents.
+
+=item vary_user_agent
+
+ vary_user_agent => 1
+
+Add "User-Agent" to Vary header.
+
+=back
+
=head1 LICENSE
This software is licensed under the same terms as Perl itself.
View
38 t/content_type.t
@@ -10,7 +10,7 @@ $Plack::Test::Impl = "Server";
my $app = builder {
- enable 'Deflater', content_type => 'text/*';
+ enable 'Deflater', content_type => 'text/plain';
sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] }
};
test_psgi
@@ -25,7 +25,7 @@ test_psgi
};
my $app2 = builder {
- enable 'Deflater', content_type => 'text/*';
+ enable 'Deflater', content_type => ['text/plain','text/html'];
sub { [200, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ "Hello World" ]] }
};
test_psgi
@@ -40,7 +40,7 @@ test_psgi
};
my $app3 = builder {
- enable 'Deflater', content_type => 'text/*';
+ enable 'Deflater', content_type => ['text/plain','text/html'];
sub { [200, [ 'Content-Type' => 'image/jpeg' ], [ "Hello World" ]] }
};
test_psgi
@@ -54,36 +54,4 @@ test_psgi
isnt $res->content_encoding, 'gzip';
};
-my $app4 = builder {
- enable 'Deflater', content_type => ['text/*','application/javascript'];
- sub { [200, [ 'Content-Type' => 'application/javascript' ], [ "Hello World" ]] }
-};
-test_psgi
- app => $app4,
- client => sub {
- my $cb = shift;
- my $req = HTTP::Request->new(GET => "http://localhost/");
- $req->accept_decodable;
- my $res = $cb->($req);
- is $res->decoded_content, 'Hello World';
- is $res->content_encoding, 'gzip';
- };
-
-my $app5 = builder {
- enable 'Deflater', content_type => ['text/*','application/javascript'];
- sub { [200, [ 'Content-Type' => 'image/gif' ], [ "Hello World" ]] }
-};
-test_psgi
- app => $app5,
- client => sub {
- my $cb = shift;
- my $req = HTTP::Request->new(GET => "http://localhost/");
- $req->accept_decodable;
- my $res = $cb->($req);
- is $res->decoded_content, 'Hello World';
- isnt $res->content_encoding, 'gzip';
- };
-
-
-
done_testing;
View
4 t/vary.t
@@ -10,7 +10,7 @@ $Plack::Test::Impl = "Server";
my $app = builder {
- enable 'Deflater', content_type => 'image/*', vary_user_agent => 1;;
+ enable 'Deflater', content_type => 'text/html', vary_user_agent => 1;;
sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] }
};
test_psgi
@@ -34,7 +34,7 @@ my $app2 = builder {
$cb->($env);
}
};
- enable 'Deflater', content_type => 'text/*', vary_user_agent => 1;
+ enable 'Deflater', content_type => 'text/plain', vary_user_agent => 1;
sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] }
};

0 comments on commit 60fa174

Please sign in to comment.
Something went wrong with that request. Please try again.