Plack-Middleware-Deflater-0.14000755000765000024 014620211657 16356 5ustar00miyagawastaff000000000000README100644000765000024 526314620211657 17325 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14NAME Plack::Middleware::Deflater - Compress response body with Gzip or Deflate SYNOPSIS use Plack::Builder; builder { enable sub { my $app = shift; sub { my $env = shift; my $ua = $env->{HTTP_USER_AGENT} || ''; # Netscape has some problem $env->{"psgix.compress-only-text/html"} = 1 if $ua =~ m!^Mozilla/4!; # Netscape 4.06-4.08 have some more problems $env->{"psgix.no-compress"} = 1 if $ua =~ m!^Mozilla/4\.0[678]!; # MSIE (7|8) masquerades as Netscape, but it is fine if ( $ua =~ m!\bMSIE (?:7|8)! ) { $env->{"psgix.no-compress"} = 0; $env->{"psgix.compress-only-text/html"} = 0; } $app->($env); } }; enable "Deflater", content_type => ['text/css','text/html','text/javascript','application/javascript'], vary_user_agent => 1; sub { [200,['Content-Type','text/html'],["OK"]] } }; DESCRIPTION Plack::Middleware::Deflater is a middleware to encode your response body in gzip or deflate, based on Accept-Encoding HTTP request header. It would save the bandwidth a little bit but should increase the Plack server load, so ideally you should handle this on the frontend reverse proxy servers. This middleware removes 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. 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. ENVIRONMENT VALUE psgix.no-compress Do not apply deflater psgix.compress-only-text/html Apply deflater only if content_type is "text/html" plack.skip-deflater Skip all Deflater features Compare psgix.no-compress with plack.skip-deflater If no-compress is true, PM::Deflater skips gzip or deflate. But adds Vary: Accept-Encoding and Vary: User-Agent header. skip-deflater forces to skip all PM::Deflater feature, doesn't allow to add Vary header. LICENSE This software is licensed under the same terms as Perl itself. AUTHOR Tatsuhiko Miyagawa SEE ALSO Plack, http://httpd.apache.org/docs/2.2/en/mod/mod_deflate.html Changes100644000765000024 304314620211657 17732 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14Revision history for Perl extension Plack::Middleware::Deflater 0.14 2024-05-12 12:08:29 PDT - remove .bak file #18 (skaji) 0.13 2024-05-10 14:50:50 PDT - Do not clobber existing Content-Encoding from the inner app #17 (plicease) 0.12 Thu Sep 12 11:31:07 JST 2013 - add plack.skip-deflater flag for skip all Deflater features 0.11 Tue Aug 6 23:24:07 JST 2013 - fix bug around crc calculation and footer generation. Thanks syohex-san 0.10 Fri Jul 19 10:42:33 JST 2013 - replace IO::Compress with PM::Deflater::Encoder for performance improvement 0.09 Mon May 24 14:24:04 JST 2013 - buffering a first chunk. It contains only the gzip header 0.08 Mon Jun 18 23:40:04 JST 2012 - do compress->close instead of autoflush to write gzip footer 0.07 Mon Jun 11 17:13:20 JST 2012 - added perlcriticrc, no code changes 0.06 Wed May 16 18:24:27 JST 2012 - Support Streaming, Thank you ranguard. 0.05 Wed Jun 22 23:28:32 JST 2011 - No code changes - Added Test::Requires to build_requires - Fixed pod 0.04 Mon May 16 12:58:30 JST 2011 - Added content_type and vary_user_agent options (kazeburo) - Added psgix.no-compress and psgix.compress-only-text/html enviroment value like Apache mod_deflate (kazeburo) 0.03 Fri Apr 2 16:30:32 PDT 2010 - Fixed unused variables warnings (obra) 0.02 Sun Nov 29 13:37:29 JST 2009 - initial CPAN release, separated from Plack core dist 0.01 Sun Nov 29 13:31:48 2009 - original version LICENSE100644000765000024 4367214620211657 17500 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14This software is copyright (c) 2024 by Tatsuhiko Miyagawa. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2024 by Tatsuhiko Miyagawa. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2024 by Tatsuhiko Miyagawa. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End cpanfile100644000765000024 31614620211657 20123 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14requires 'Compress::Zlib'; requires 'Plack'; requires 'perl', '5.008001'; on build => sub { requires 'ExtUtils::MakeMaker', '6.59'; requires 'Test::More', '0.96'; requires 'Test::Requires'; }; dist.ini100644000765000024 1114620211657 20033 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14[@Milla] t000755000765000024 014620211657 16542 5ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14furl.t100644000765000024 147614620211657 20047 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More; use Test::Requires qw(Furl Test::TCP); use Plack::Builder; use Plack::Loader; my $app = builder { enable 'Deflater', content_type => 'text/plain'; sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } }; test_tcp( client => sub { my ($port, $server_pid) = @_; for my $encoding (qw/gzip deflate/) { my $furl = Furl->new( headers => ['Accept-Encoding',$encoding] ); my $res = $furl->get("http://127.0.0.1:$port/"); is $res->content, 'Hello World'; is $res->content_encoding, $encoding; } }, server => sub { my $port = shift; Plack::Loader->load('HTTP::Server::PSGI',host=>'127.0.0.1',port => $port)->run($app); exit; }, ); done_testing; vary.t100644000765000024 640514620211657 20055 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More; use Test::Requires qw(IO::Handle::Util); use IO::Handle::Util qw(:io_from); use HTTP::Request::Common; use Plack::Builder; use Plack::Test; use Plack::Middleware::Deflater; $Plack::Test::Impl = "Server"; ## content_type nomatch, no vary sets. my $app = builder { enable 'Deflater', content_type => 'text/html', vary_user_agent => 1;; sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } }; test_psgi app => $app, client => sub { my $cb = shift; my $req = HTTP::Request->new(GET => "http://localhost/"); my $res = $cb->($req); is $res->decoded_content, 'Hello World'; isnt $res->content_encoding, 'gzip'; unlike $res->header('Vary') || '', qr/Accept-Encoding/; unlike $res->header('Vary') || '', qr/User-Agent/; }; ## app2 my $app2 = builder { enable sub { my $cb = shift; sub { my $env = shift; $env->{HTTP_ACCEPT_ENCODING} =~ s/(gzip|deflate)//gi if $env->{HTTP_USER_AGENT} =~ m!^Mozilla/4! and $env->{HTTP_USER_AGENT} !~ m!\bMSIE\s(7|8)!; $cb->($env); } }; enable 'Deflater', content_type => 'text/plain', vary_user_agent => 1; sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } }; ## ua:lwp test_psgi app => $app2, 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'; like $res->header('Vary'), qr/Accept-Encoding/; like $res->header('Vary'), qr/User-Agent/; }; ## ua:ie6 not gziped, vary:ua vary:ac will be added test_psgi app => $app2, client => sub { my $cb = shift; my $req = HTTP::Request->new(GET => "http://localhost/"); $req->accept_decodable; $req->user_agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.0; Trident/4.0)"); my $res = $cb->($req); is $res->decoded_content, 'Hello World'; isnt $res->content_encoding, 'gzip'; like $res->header('Vary'), qr/Accept-Encoding/; like $res->header('Vary'), qr/User-Agent/; }; ## ua:ie7 gziped test_psgi app => $app2, client => sub { my $cb = shift; my $req = HTTP::Request->new(GET => "http://localhost/"); $req->accept_decodable; $req->user_agent("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0)"); my $res = $cb->($req); is $res->decoded_content, 'Hello World'; is $res->content_encoding, 'gzip'; like $res->header('Vary'), qr/Accept-Encoding/; like $res->header('Vary'), qr/User-Agent/; }; ## ua:ie9 gziped test_psgi app => $app2, client => sub { my $cb = shift; my $req = HTTP::Request->new(GET => "http://localhost/"); $req->accept_decodable; $req->user_agent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); my $res = $cb->($req); is $res->decoded_content, 'Hello World'; is $res->content_encoding, 'gzip'; like $res->header('Vary'), qr/Accept-Encoding/; like $res->header('Vary'), qr/User-Agent/; }; done_testing; xt000755000765000024 014620211657 16732 5ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14pod.t100644000765000024 20114620211657 20012 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/xtuse Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Build.PL100644000765000024 27714620211657 17721 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14# This Build.PL for Plack-Middleware-Deflater was generated by Dist::Zilla::Plugin::ModuleBuildTiny 0.015. use strict; use warnings; use 5.008001; use Module::Build::Tiny 0.034; Build_PL(); META.yml100644000765000024 252414620211657 17713 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14--- abstract: 'Compress response body with Gzip or Deflate' author: - 'Tatsuhiko Miyagawa' build_requires: ExtUtils::MakeMaker: '6.59' Test::More: '0.96' Test::Requires: '0' configure_requires: Module::Build::Tiny: '0.034' dynamic_config: 0 generated_by: 'Dist::Milla version v1.0.22, Dist::Zilla version 6.025, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Plack-Middleware-Deflater no_index: directory: - eg - examples - inc - share - t - xt requires: Compress::Zlib: '0' Plack: '0' perl: '5.008001' resources: bugtracker: https://github.com/miyagawa/Plack-Middleware-Deflater/issues homepage: https://github.com/miyagawa/Plack-Middleware-Deflater repository: https://github.com/miyagawa/Plack-Middleware-Deflater.git version: '0.14' x_contributors: - 'Graham Ollis ' - 'Jesse Vincent ' - 'leo user ' - 'Masahiro Nagano ' - 'Masahiro Nagano ' - 'Shoichi Kaji ' - 'Tatsuhiko Miyagawa ' x_generated_by_perl: v5.34.1 x_serialization_backend: 'YAML::Tiny version 1.73' x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later' x_static_install: 0 MANIFEST100644000765000024 63114620211657 17550 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.025. Build.PL Changes LICENSE MANIFEST META.json META.yml README cpanfile dist.ini lib/Plack/Middleware/Deflater.pm t/00_compile.t t/author-pod-syntax.t t/content_type.t t/deflater.t t/delayed.t t/furl.t t/no-clobber.t t/streaming.t t/vary.t t/xno-compress.t t/xskip-deflater.t xt/perlcritic.t xt/perlcriticrc xt/pod.t xt/synopsis.t META.json100644000765000024 425714620211657 20070 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14{ "abstract" : "Compress response body with Gzip or Deflate", "author" : [ "Tatsuhiko Miyagawa" ], "dynamic_config" : 0, "generated_by" : "Dist::Milla version v1.0.22, Dist::Zilla version 6.025, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Plack-Middleware-Deflater", "no_index" : { "directory" : [ "eg", "examples", "inc", "share", "t", "xt" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "6.59", "Test::More" : "0.96", "Test::Requires" : "0" } }, "configure" : { "requires" : { "Module::Build::Tiny" : "0.034" } }, "develop" : { "requires" : { "Dist::Milla" : "v1.0.22", "Test::Pod" : "1.41" } }, "runtime" : { "requires" : { "Compress::Zlib" : "0", "Plack" : "0", "perl" : "5.008001" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/miyagawa/Plack-Middleware-Deflater/issues" }, "homepage" : "https://github.com/miyagawa/Plack-Middleware-Deflater", "repository" : { "type" : "git", "url" : "https://github.com/miyagawa/Plack-Middleware-Deflater.git", "web" : "https://github.com/miyagawa/Plack-Middleware-Deflater" } }, "version" : "0.14", "x_contributors" : [ "Graham Ollis ", "Jesse Vincent ", "leo user ", "Masahiro Nagano ", "Masahiro Nagano ", "Shoichi Kaji ", "Tatsuhiko Miyagawa " ], "x_generated_by_perl" : "v5.34.1", "x_serialization_backend" : "Cpanel::JSON::XS version 4.27", "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later", "x_static_install" : 0 } delayed.t100644000765000024 264114620211657 20501 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use warnings; use FindBin; use Test::More; use HTTP::Request::Common; use Plack::Test; use Plack::Builder; my $app = builder { enable sub { my $cb = shift; sub { my $env = shift; $env->{HTTP_ACCEPT_ENCODING} =~ s/(gzip|deflate)//gi if $env->{HTTP_USER_AGENT} =~ m!^Mozilla/4! and $env->{HTTP_USER_AGENT} !~ m!\bMSIE\s(7|8)!; $cb->($env); } }; enable 'Deflater', content_type => 'text/plain', vary_user_agent => 1; # Non streaming # sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } # delayed sub { my $env = shift; return sub { my $r = shift; $r->([ '200', [ 'Content-Type' => 'text/plain' ], ["Hello World"]]); }; }; }; my @impl = qw(MockHTTP Server); sub flip_backend { $Plack::Test::Impl = shift @impl } test_psgi app => $app, client => sub { my $cb = shift; my $req = HTTP::Request->new( GET => "http://localhost/" ); $req->accept_decodable; $req->user_agent( "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0)"); my $res = $cb->($req); is $res->decoded_content, 'Hello World'; is $res->content_encoding, 'gzip'; like $res->header('Vary'), qr/Accept-Encoding/; like $res->header('Vary'), qr/User-Agent/; } while flip_backend; done_testing; deflater.t100644000765000024 223214620211657 20654 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More; use Test::Requires qw(IO::Handle::Util); use IO::Handle::Util qw(:io_from); use HTTP::Request::Common; use Plack::Test; use Plack::Middleware::Deflater; $Plack::Test::Impl = "Server"; my @app = ( sub { [ 200, [], [ 'Hello World' ] ] }, sub { [ 200, [], [ 'Hello ', 'World' ] ] }, sub { [ 200, [], [ 'Hello ', '', 'World' ] ] }, sub { [ 200, [], io_from_array [ 'Hello World' ] ] }, sub { [ 200, [], io_from_array [ 'Hello', ' World' ] ] }, sub { [ 200, [], io_from_array [ 'Hello', '', ' World' ] ] }, ); my $app = sub { (shift @app)->(@_) }; for my $encode (qw/gzip deflate/) { test_psgi app => Plack::Middleware::Deflater->wrap($app), client => sub { my $cb = shift; no warnings; local *HTTP::Request::decodable = sub { wantarray ? ($encode) : $encode }; for (0..$#app) { my $req = GET "http://localhost/"; $req->accept_decodable; my $res = $cb->($req); is $res->decoded_content, 'Hello World'; is $res->content_encoding, $encode; like $res->header('Vary'), qr/Accept-Encoding/ } }; } done_testing; streaming.t100644000765000024 376514620211657 21073 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use warnings; use FindBin; use Test::More tests => 4; use HTTP::Request::Common; use Plack::Test; use Plack::Builder; use Test::Requires { 'AnyEvent' => 5.34, 'Plack::Test::AnyEvent' => 0.03 }; my $app = builder { enable sub { my $cb = shift; sub { my $env = shift; $env->{HTTP_ACCEPT_ENCODING} =~ s/(gzip|deflate)//gi if $env->{HTTP_USER_AGENT} =~ m!^Mozilla/4! and $env->{HTTP_USER_AGENT} !~ m!\bMSIE\s(7|8)!; $cb->($env); } }; enable 'Deflater', content_type => 'text/plain', vary_user_agent => 1; # Non streaming # sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } # streaming sub { my $env = shift; return sub { my $r = shift; my $w = $r->([ '200', [ 'Content-Type' => 'text/plain' ]]); my $timer; my $i = 0; my @message = qw/Hello World/; $timer = AnyEvent->timer( after => 1, interval => 1, cb => sub { $w->write($message[$i]. "x" x 1024 . "\n"); $i++; if ( $i == 2 ) { $w->close; undef $timer; } } ); }; }; }; local $Plack::Test::Impl = 'AnyEvent'; test_psgi app => $app, client => sub { my $cb = shift; my $req = HTTP::Request->new( GET => "http://localhost/" ); $req->accept_decodable; $req->user_agent( "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0)"); my $res = $cb->($req); $res->recv; like $res->header('Vary'), qr/Accept-Encoding/; like $res->header('Vary'), qr/User-Agent/; is $res->content_encoding, 'gzip'; is $res->decoded_content, "Hello" . "x" x 1024 . "\nWorld" . "x" x 1024 . "\n"; }; done_testing; synopsis.t100644000765000024 16014620211657 21123 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/xtuse Test::More; eval "use Test::Synopsis"; plan skip_all => "Test::Synopsis required" if $@; all_synopsis_ok(); 00_compile.t100644000765000024 12714620211657 20776 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More tests => 1; BEGIN { use_ok 'Plack::Middleware::Deflater' } no-clobber.t100644000765000024 176414620211657 21121 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More; use HTTP::Request::Common; use Plack::Test; use Plack::Middleware::Deflater; $Plack::Test::Impl = "Server"; my $app = sub { [ 200, ['Content-Encoding' => 'foo'], [ 'Hello World' ] ] }; test_psgi app => Plack::Middleware::Deflater->wrap($app), client => sub { my $cb = shift; my $req = GET "http://localhost/", 'Accept-Encoding' => 'gzip', 'Accept-Encoding' => 'foo'; my $res = $cb->($req); is $res->content, 'Hello World'; is $res->header('Content-Encoding'), 'foo'; like $res->header('Vary'), qr/Accept-Encoding/ }; $app = sub { [ 200, ['Content-Encoding' => 'identity'], [ 'Hello World' ] ] }; test_psgi app => Plack::Middleware::Deflater->wrap($app), client => sub { my $cb = shift; my $req = GET "http://localhost/", 'Accept-Encoding' => 'gzip', 'Accept-Encoding' => 'foo'; my $res = $cb->($req); is $res->decoded_content, 'Hello World'; is $res->header('Content-Encoding'), 'gzip'; like $res->header('Vary'), qr/Accept-Encoding/ }; done_testing; perlcritic.t100644000765000024 33314620211657 21376 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/xtuse strict; use Test::More; eval { require Test::Perl::Critic; Test::Perl::Critic->import( -profile => 'xt/perlcriticrc'); }; plan skip_all => "Test::Perl::Critic is not installed." if $@; all_critic_ok("lib"); perlcriticrc100644000765000024 6414620211657 21442 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/xt[TestingAndDebugging::ProhibitNoStrict] allow=refs content_type.t100644000765000024 315314620211657 21604 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More; use Test::Requires qw(IO::Handle::Util); use IO::Handle::Util qw(:io_from); use HTTP::Request::Common; use Plack::Builder; use Plack::Test; use Plack::Middleware::Deflater; $Plack::Test::Impl = "Server"; my $app = builder { enable 'Deflater', content_type => 'text/plain'; sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } }; test_psgi app => $app, 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 $app2 = builder { enable 'Deflater', content_type => ['text/plain','text/html']; sub { [200, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ "Hello World" ]] } }; test_psgi app => $app2, 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 $app3 = builder { enable 'Deflater', content_type => ['text/plain','text/html']; sub { [200, [ 'Content-Type' => 'image/jpeg' ], [ "Hello World" ]] } }; test_psgi app => $app3, 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; xno-compress.t100644000765000024 335114620211657 21526 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More; use Test::Requires qw(IO::Handle::Util); use IO::Handle::Util qw(:io_from); use HTTP::Request::Common; use Plack::Builder; use Plack::Test; use Plack::Middleware::Deflater; $Plack::Test::Impl = "Server"; ## content_type nomatch, no vary sets. my $app = builder { enable sub { my $cb = shift; sub { my $env = shift; $env->{"psgix.no-compress"} = 1; $cb->($env); } }; enable 'Deflater', content_type => 'text/html', vary_user_agent => 1; sub { [200, [ 'Content-Type' => 'text/html' ], [ "Hello World" ]] } }; test_psgi app => $app, client => sub { my $cb = shift; my $req = HTTP::Request->new(GET => "http://localhost/"); my $res = $cb->($req); is $res->decoded_content, 'Hello World'; isnt $res->content_encoding, 'gzip'; like $res->header('Vary') || '', qr/Accept-Encoding/; like $res->header('Vary') || '', qr/User-Agent/; }; my $app2 = builder { enable sub { my $cb = shift; sub { my $env = shift; $env->{"psgix.compress-only-text/html"} = 1; $cb->($env); } }; enable 'Deflater', content_type => 'text/plain', vary_user_agent => 1; sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } }; test_psgi app => $app2, client => sub { my $cb = shift; my $req = HTTP::Request->new(GET => "http://localhost/"); my $res = $cb->($req); is $res->decoded_content, 'Hello World'; isnt $res->content_encoding, 'gzip'; like $res->header('Vary') || '', qr/Accept-Encoding/; like $res->header('Vary') || '', qr/User-Agent/; }; done_testing; xskip-deflater.t100644000765000024 156514620211657 22020 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/tuse strict; use Test::More; use Test::Requires qw(IO::Handle::Util); use IO::Handle::Util qw(:io_from); use HTTP::Request::Common; use Plack::Builder; use Plack::Test; use Plack::Middleware::Deflater; $Plack::Test::Impl = "Server"; my $app = builder { enable sub { my $cb = shift; sub { my $env = shift; $env->{"plack.skip-deflater"} = 1; $cb->($env); } }; enable 'Deflater', content_type => 'text/plain', vary_user_agent => 1; sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] } }; test_psgi app => $app, client => sub { my $cb = shift; my $req = HTTP::Request->new(GET => "http://localhost/"); my $res = $cb->($req); is $res->content, 'Hello World'; ok !$res->content_encoding; ok !$res->header('Vary'); }; done_testing; author-pod-syntax.t100644000765000024 45414620211657 22460 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/t#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { print qq{1..0 # SKIP these tests are for testing by the author\n}; exit } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok(); Middleware000755000765000024 014620211657 22154 5ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/lib/PlackDeflater.pm100644000765000024 1732414620211657 24427 0ustar00miyagawastaff000000000000Plack-Middleware-Deflater-0.14/lib/Plack/Middlewarepackage Plack::Middleware::Deflater; use strict; use 5.008001; our $VERSION = '0.14'; use parent qw(Plack::Middleware); use Plack::Util::Accessor qw( content_type vary_user_agent); use Plack::Util; sub prepare_app { my $self = shift; if (my $match_cts = $self->content_type) { $match_cts = [$match_cts] if !ref $match_cts; $self->content_type($match_cts); } } sub call { my($self, $env) = @_; my $res = $self->app->($env); $self->response_cb( $res, sub { my $res = shift; # can't operate on Content-Ranges return if $env->{HTTP_CONTENT_RANGE}; return if $env->{"plack.skip-deflater"}; my $h = Plack::Util::headers($res->[1]); my $content_type = $h->get('Content-Type') || ''; $content_type =~ s/(;.*)$//; if (my $match_cts = $self->content_type) { my $match = 0; for my $match_ct (@{$match_cts}) { if ($content_type eq $match_ct) { $match++; last; } } return unless $match; } if (Plack::Util::status_with_no_entity_body($res->[0]) or $h->exists('Cache-Control') && $h->get('Cache-Control') =~ /\bno-transform\b/) { return; } my @vary = split /\s*,\s*/, ($h->get('Vary') || ''); push @vary, 'Accept-Encoding'; push @vary, 'User-Agent' if $self->vary_user_agent; $h->set('Vary' => join(",", @vary)); # Do not clobber already existing encoding return if $h->exists('Content-Encoding') && $h->get('Content-Encoding') ne 'identity'; # some browsers might have problems, so set no-compress return if $env->{"psgix.no-compress"}; # Some browsers might have problems with content types # other than text/html, so set compress-only-text/html if ($env->{"psgix.compress-only-text/html"}) { return if $content_type ne 'text/html'; } # TODO check quality my $encoding = 'identity'; if (defined $env->{HTTP_ACCEPT_ENCODING}) { for my $enc (qw(gzip deflate identity)) { if ($env->{HTTP_ACCEPT_ENCODING} =~ /\b$enc\b/) { $encoding = $enc; last; } } } my $encoder; if ($encoding eq 'gzip' || $encoding eq 'deflate') { $encoder = Plack::Middleware::Deflater::Encoder->new($encoding); } if ($encoder) { $h->set('Content-Encoding' => $encoding); $h->remove('Content-Length'); # normal response if ($res->[2] && ref($res->[2]) && ref($res->[2]) eq 'ARRAY') { my $buf = ''; foreach (@{ $res->[2] }) { $buf .= $encoder->print($_) if defined $_; } $buf .= $encoder->close(); $res->[2] = [$buf]; return; } # delayed or stream return sub { $encoder->print(shift); }; } } ); } 1; package Plack::Middleware::Deflater::Encoder; use strict; use warnings; use Compress::Zlib; use constant GZIP_MAGIC => 0x1f8b; sub new { my $class = shift; my $encoding = shift; my($encoder, $status) = $encoding eq 'gzip' ? deflateInit(-WindowBits => -MAX_WBITS()) : deflateInit(-WindowBits => MAX_WBITS()); die 'Cannot create a deflation stream' if $status != Z_OK; bless { header => 0, closed => 0, encoding => $encoding, encoder => $encoder, crc => crc32(undef), length => 0, }, $class; } sub print : method { my $self = shift; return if $self->{closed}; my $chunk = shift; if (!defined $chunk) { my($buf, $status) = $self->{encoder}->flush(); die "deflate failed: $status" if ($status != Z_OK); if (!$self->{header} && $self->{encoding} eq 'gzip') { $buf = pack("nccVcc", GZIP_MAGIC, Z_DEFLATED, 0, time(), 0, $Compress::Raw::Zlib::gzip_os_code) . $buf; } $buf .= pack("LL", $self->{crc}, $self->{length}) if $self->{encoding} eq 'gzip'; $self->{closed} = 1; return $buf; } my($buf, $status) = $self->{encoder}->deflate($chunk); die "deflate failed: $status" if ($status != Z_OK); $self->{length} += length $chunk; $self->{crc} = crc32($chunk, $self->{crc}); if (length $buf) { if (!$self->{header} && $self->{encoding} eq 'gzip') { $buf = pack("nccVcc", GZIP_MAGIC, Z_DEFLATED, 0, time(), 0, $Compress::Raw::Zlib::gzip_os_code) . $buf; } $self->{header} = 1; return $buf; } return ''; } sub close : method { $_[0]->print(undef); } sub closed { $_[0]->{closed}; } 1; __END__ =head1 NAME Plack::Middleware::Deflater - Compress response body with Gzip or Deflate =head1 SYNOPSIS use Plack::Builder; builder { enable sub { my $app = shift; sub { my $env = shift; my $ua = $env->{HTTP_USER_AGENT} || ''; # Netscape has some problem $env->{"psgix.compress-only-text/html"} = 1 if $ua =~ m!^Mozilla/4!; # Netscape 4.06-4.08 have some more problems $env->{"psgix.no-compress"} = 1 if $ua =~ m!^Mozilla/4\.0[678]!; # MSIE (7|8) masquerades as Netscape, but it is fine if ( $ua =~ m!\bMSIE (?:7|8)! ) { $env->{"psgix.no-compress"} = 0; $env->{"psgix.compress-only-text/html"} = 0; } $app->($env); } }; enable "Deflater", content_type => ['text/css','text/html','text/javascript','application/javascript'], vary_user_agent => 1; sub { [200,['Content-Type','text/html'],["OK"]] } }; =head1 DESCRIPTION Plack::Middleware::Deflater is a middleware to encode your response body in gzip or deflate, based on C HTTP request header. It would save the bandwidth a little bit but should increase the Plack server load, so ideally you should handle this on the frontend reverse proxy servers. This middleware removes C 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 ENVIRONMENT VALUE =over 4 =item psgix.no-compress Do not apply deflater =item psgix.compress-only-text/html Apply deflater only if content_type is "text/html" =item plack.skip-deflater Skip all Deflater features =back =head2 Compare psgix.no-compress with plack.skip-deflater If no-compress is true, PM::Deflater skips gzip or deflate. But adds Vary: Accept-Encoding and Vary: User-Agent header. skip-deflater forces to skip all PM::Deflater feature, doesn't allow to add Vary header. =head1 LICENSE This software is licensed under the same terms as Perl itself. =head1 AUTHOR Tatsuhiko Miyagawa =head1 SEE ALSO L, L =cut