Skip to content

Commit

Permalink
Text::Globやめて、documentアップデート
Browse files Browse the repository at this point in the history
  • Loading branch information
Masahiro Nagano committed Jan 28, 2011
1 parent 7f56ebf commit 60fa174
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 48 deletions.
1 change: 0 additions & 1 deletion Makefile.PL
Expand Up @@ -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');
Expand Down
25 changes: 24 additions & 1 deletion README
Expand Up @@ -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
Expand All @@ -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.

Expand Down
41 changes: 32 additions & 9 deletions lib/Plack/Middleware/Deflater.pm
Expand Up @@ -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);
}
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
38 changes: 3 additions & 35 deletions t/content_type.t
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
4 changes: 2 additions & 2 deletions t/vary.t
Expand Up @@ -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
Expand All @@ -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" ]] }
};

Expand Down

0 comments on commit 60fa174

Please sign in to comment.