gzip_filter 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/COPYING ADDED
@@ -0,0 +1,55 @@
1
+ This GzipFilter GemPlugin is copyrighted free software by Eden Li
2
+ (eden.li@gmail.com). You can redistribute it and/or modify it under
3
+ either the terms of the GPL or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise make them
13
+ Freely Available, such as by posting said modifications to Usenet or an
14
+ equivalent medium, or by allowing the author to include your
15
+ modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict with
21
+ standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent) on where
30
+ to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of the
33
+ software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under this terms.
43
+
44
+ 5. The scripts and library files supplied as input to or produced as
45
+ output from the software do not automatically fall under the
46
+ copyright of the software, but belong to whomever generated them,
47
+ and may be sold commercially, and may be aggregated with this
48
+ software.
49
+
50
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
51
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
52
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53
+ PURPOSE.
54
+
55
+
data/LICENSE ADDED
@@ -0,0 +1,55 @@
1
+ This GzipFilter GemPlugin is copyrighted free software by Eden Li
2
+ (eden.li@gmail.com). You can redistribute it and/or modify it under
3
+ either the terms of the GPL or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise make them
13
+ Freely Available, such as by posting said modifications to Usenet or an
14
+ equivalent medium, or by allowing the author to include your
15
+ modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict with
21
+ standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent) on where
30
+ to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of the
33
+ software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under this terms.
43
+
44
+ 5. The scripts and library files supplied as input to or produced as
45
+ output from the software do not automatically fall under the
46
+ copyright of the software, but belong to whomever generated them,
47
+ and may be sold commercially, and may be aggregated with this
48
+ software.
49
+
50
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
51
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
52
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53
+ PURPOSE.
54
+
55
+
data/README ADDED
@@ -0,0 +1,30 @@
1
+ = Gzip Filter GemPlugin
2
+
3
+ This GemPlugin hands back HTTP responses compressed using gzip if the
4
+ client requests it. It's been tested in IE6, Mozilla Firefox 1.5 and 2.0.
5
+
6
+ == Install, config, run
7
+ $ sudo gem install gzip_filter
8
+ $ echo 'uri "/", :handler => plugin("/handlers/gzipfilter")' >> config/mongrel.conf
9
+ $ mongrel_rails -S config/mongrel.conf
10
+
11
+ == Using with mongrel_cluster
12
+ Add the following line to your config/mongrel_cluster.yml file:
13
+
14
+ config_script: config/mongrel.conf
15
+
16
+ == ChangeLog
17
+
18
+ 2006/10/25 -- 0.2
19
+ * Added :gzip_condition => proc{|request|} to GzipFilter's constructor.
20
+ You can now write some Ruby code to gzip responses based on what was
21
+ given to you in the request.
22
+
23
+ Example (in mongrel.conf):
24
+ # Gzip only for non IE6 requests
25
+ uri "/", :handler => GzipFilter.new(:gzip_condition => proc {|request|
26
+ request.params['HTTP_USER_AGENT'] !~ /MSIE\s+6\.0/i
27
+ })
28
+
29
+ 2006/10/23 -- 0.1
30
+ * Initial release
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'tools/rakehelp'
7
+ require 'fileutils'
8
+ include FileUtils
9
+
10
+ setup_tests
11
+ setup_clean ["pkg", "lib/*.bundle", "*.gem", ".config"]
12
+
13
+ setup_rdoc ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc']
14
+
15
+ desc "Does a full compile, test run"
16
+ task :default => [:test, :package]
17
+
18
+ version="0.2"
19
+ name="gzip_filter"
20
+
21
+ setup_gem(name, version) do |spec|
22
+ spec.summary = "The GzipFilter GemPlugin"
23
+ spec.description = "Compresses your Mongrel's HTTP responses using gzip."
24
+ spec.author="Eden Li (eden.li@gmail.com)"
25
+ spec.add_dependency('mongrel', '>= 0.3.13.0')
26
+ spec.add_dependency('gem_plugin', '>= 0.2')
27
+ spec.files += Dir.glob("resources/**/*")
28
+ end
29
+
30
+
31
+ task :install => [:test, :package] do
32
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
33
+ end
34
+
35
+ task :uninstall => [:clean] do
36
+ sh %{sudo gem uninstall #{name}}
37
+ end
38
+
@@ -0,0 +1,70 @@
1
+ # Copyright (c) 2006 Eden Li (eden.li@gmail.com)
2
+ # You can redistribute it and/or modify it under terms listed in the LICENSE
3
+ # file accompanying this package.
4
+ #
5
+ require 'mongrel'
6
+ require 'gem_plugin'
7
+ require 'zlib'
8
+
9
+ # The GzipFilter compresses an HTTP response using Ruby's Zlib::GzipWriter.
10
+ # It only does this if the client sends an Accept-Encoding header containing
11
+ # the gzip string.
12
+ #
13
+ # You can tell mongrel to use this filter by placing the following line in
14
+ # a config file and passing that file to mongrel using the -S option.
15
+ #
16
+ # Example mongrel.conf:
17
+ # uri "/", :handler => GzipFilter.new
18
+ #
19
+ # Another example using the gzip_condition:
20
+ # uri "/", :handler => GzipFilter.new(:gzip_condition => proc {|request|
21
+ # request.params['HTTP_USER_AGENT'] !~ /MSIE\s+6\.0/i
22
+ # })
23
+ #
24
+ # Example:
25
+ #
26
+ # $ echo 'uri "/", :handler => GzipFilter.new' >> config/mongrel.conf
27
+ # $ mongrel_rails -S config/mongrel.conf
28
+ #
29
+ class GzipFilter < GemPlugin::Plugin "/handlers"
30
+ include Mongrel::HttpHandlerPlugin
31
+
32
+ # You can pass in a conditional function that will be used to determine
33
+ # if the response should be gzipped or not. It'll get passed the
34
+ # request object for validation. This is called in addition to checks
35
+ # on the Accept-Encoding header.
36
+ #
37
+ # GzipFilter.new(:gzip_condition => proc {|request|
38
+ # request.params['HTTP_USER_AGENT'] !~ /MSIE\s+6\.0/i
39
+ # })
40
+ #
41
+ def initialize(opts = {})
42
+ @conditions = opts[:gzip_condition]
43
+ end
44
+
45
+ def process(request, response)
46
+ if should_gzip(request, response)
47
+ response.header["Content-Encoding"] = "gzip"
48
+ response.body = gzip_stream(response.body)
49
+ end
50
+ end
51
+
52
+ private
53
+ def should_gzip(request, response)
54
+ (@conditions ? @conditions.call(request) : true) &&
55
+ !response.body_sent &&
56
+ request.params["HTTP_ACCEPT_ENCODING"] &&
57
+ request.params["HTTP_ACCEPT_ENCODING"].downcase.include?("gzip")
58
+ end
59
+
60
+ def gzip_stream(stream, close = true)
61
+ z = Zlib::GzipWriter.new(gzipped = StringIO.new)
62
+ stream.rewind
63
+ z.write(stream.read)
64
+ z.finish
65
+ stream.close if close
66
+
67
+ gzipped
68
+ end
69
+ end
70
+
@@ -0,0 +1,91 @@
1
+ require 'test/unit'
2
+ require 'net/http'
3
+ require 'mongrel'
4
+ require 'zlib'
5
+ require 'lib/gzip_filter/init'
6
+
7
+ class SimpleHandler < Mongrel::HttpHandler
8
+ CONTENT = "<html><head><title></title></head><body>lkjlkj</body></html>"
9
+ def process(request, response)
10
+ response.start do |head, out|
11
+ head["Content-Type"] = "text/html"
12
+ out << CONTENT
13
+ end
14
+ end
15
+ end
16
+
17
+ class GzipFilterTest < Test::Unit::TestCase
18
+ def setup
19
+ @mongrel = Mongrel::HttpServer.new("127.0.0.1", 121212)
20
+ @mongrel.register("/", SimpleHandler.new)
21
+ @mongrel.register("/", GzipFilter.new)
22
+
23
+ @mongrel.register("/more/ie/goodness", SimpleHandler.new)
24
+ @mongrel.register("/more/ie/goodness",
25
+ GzipFilter.new(:gzip_condition => proc { |r|
26
+ r.params["HTTP_USER_AGENT"] !~ /msie\s+6\.0/i
27
+ })
28
+ )
29
+ @mongrel.run
30
+ end
31
+
32
+ def teardown
33
+ @mongrel.stop if @mongrel
34
+ end
35
+
36
+ def test_ungzipped
37
+ assert_content(get("http://localhost:121212"))
38
+ end
39
+
40
+ def test_gzipped
41
+ response = get("http://localhost:121212/", {"Accept-Encoding" => "gzip"})
42
+ assert_content(response, :gzip)
43
+
44
+ response = get("http://localhost:121212/", {"Accept-Encoding" => "GZip"})
45
+ assert_content(response, :gzip)
46
+ end
47
+
48
+ def test_deflate
49
+ response = get("http://localhost:121212/", {"Accept-Encoding" => "deflate"})
50
+ assert_content(response)
51
+ end
52
+
53
+ def test_both
54
+ response = get("http://localhost:121212/", {"Accept-Encoding" => "deflate,gzip"})
55
+ assert_content(response, :gzip)
56
+ end
57
+
58
+ def test_condition
59
+ response = get("http://localhost:121212/more/ie/goodness",
60
+ {"User-Agent" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)",
61
+ "Accept-Encoding" => "deflate,gzip"})
62
+ assert_content(response)
63
+
64
+ response = get("http://localhost:121212/more/ie/goodness",
65
+ {"User-Agent" => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0",
66
+ "Accept-Encoding" => "deflate,gzip"})
67
+ assert_content(response, :gzip)
68
+ end
69
+
70
+ private
71
+ def assert_content(response, encoding = nil)
72
+ if encoding == :gzip
73
+ assert response.header['Content-Encoding'].include?('gzip')
74
+ text = Zlib::GzipReader.new(StringIO.new(response.body)).read
75
+ assert_equal SimpleHandler::CONTENT, text
76
+ else
77
+ assert !response.header['Content-Encoding'] ||
78
+ !response.header['Content-Encoding'].include?('gzip')
79
+ assert_equal SimpleHandler::CONTENT, response.body
80
+ end
81
+ end
82
+
83
+ def get(url, headers = {})
84
+ url = URI.parse(url)
85
+ response = nil
86
+ Net::HTTP.start(url.host, url.port) do |h|
87
+ response = h.get(url.path != '' ? url.path : '/', headers)
88
+ end
89
+ response
90
+ end
91
+ end
@@ -0,0 +1,106 @@
1
+
2
+ def make(makedir)
3
+ Dir.chdir(makedir) do
4
+ sh(PLATFORM =~ /win32/ ? 'nmake' : 'make')
5
+ end
6
+ end
7
+
8
+
9
+ def extconf(dir)
10
+ Dir.chdir(dir) do ruby "extconf.rb" end
11
+ end
12
+
13
+
14
+ def setup_tests
15
+ Rake::TestTask.new do |t|
16
+ t.libs << "test"
17
+ t.test_files = FileList['test/test*.rb']
18
+ t.verbose = true
19
+ end
20
+ end
21
+
22
+
23
+ def setup_clean otherfiles
24
+ files = ['build/*', '**/*.o', '**/*.so', '**/*.a', 'lib/*-*', '**/*.log'] + otherfiles
25
+ CLEAN.include(files)
26
+ end
27
+
28
+
29
+ def setup_rdoc files
30
+ Rake::RDocTask.new do |rdoc|
31
+ rdoc.rdoc_dir = 'doc/rdoc'
32
+ rdoc.options << '--line-numbers'
33
+ rdoc.rdoc_files.add(files)
34
+ end
35
+ end
36
+
37
+
38
+ def setup_extension(dir, extension)
39
+ ext = "ext/#{dir}"
40
+ ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
41
+ ext_files = FileList[
42
+ "#{ext}/*.c",
43
+ "#{ext}/*.h",
44
+ "#{ext}/extconf.rb",
45
+ "#{ext}/Makefile",
46
+ "lib"
47
+ ]
48
+
49
+ task "lib" do
50
+ directory "lib"
51
+ end
52
+
53
+ desc "Builds just the #{extension} extension"
54
+ task extension.to_sym => ["#{ext}/Makefile", ext_so ]
55
+
56
+ file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
57
+ extconf "#{ext}"
58
+ end
59
+
60
+ file ext_so => ext_files do
61
+ make "#{ext}"
62
+ cp ext_so, "lib"
63
+ end
64
+ end
65
+
66
+
67
+ def base_gem_spec(pkg_name, pkg_version)
68
+ pkg_version = pkg_version
69
+ pkg_name = pkg_name
70
+ pkg_file_name = "#{pkg_name}-#{pkg_version}"
71
+ Gem::Specification.new do |s|
72
+ s.name = pkg_name
73
+ s.version = pkg_version
74
+ s.platform = Gem::Platform::RUBY
75
+ s.has_rdoc = true
76
+ s.extra_rdoc_files = [ "README" ]
77
+
78
+ s.files = %w(COPYING LICENSE README Rakefile) +
79
+ Dir.glob("{bin,doc/rdoc,test,lib}/**/*") +
80
+ Dir.glob("ext/**/*.{h,c,rb}") +
81
+ Dir.glob("examples/**/*.rb") +
82
+ Dir.glob("tools/*.rb")
83
+
84
+ s.require_path = "lib"
85
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
86
+ s.bindir = "bin"
87
+ end
88
+ end
89
+
90
+ def setup_gem(pkg_name, pkg_version)
91
+ spec = base_gem_spec(pkg_name, pkg_version)
92
+ yield spec if block_given?
93
+
94
+ Rake::GemPackageTask.new(spec) do |p|
95
+ p.gem_spec = spec
96
+ # win32 chokes on this
97
+ p.need_tar = true if RUBY_PLATFORM !~ /mswin/
98
+ end
99
+ end
100
+
101
+ def setup_win32_gem(pkg_name, pkg_version)
102
+ spec = base_gem_spec(pkg_name, pkg_version)
103
+ yield spec if block_given?
104
+
105
+ Gem::Builder.new(spec).build
106
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: gzip_filter
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.2"
7
+ date: 2006-10-25 00:00:00 +08:00
8
+ summary: The GzipFilter GemPlugin
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage:
13
+ rubyforge_project:
14
+ description: Compresses your Mongrel's HTTP responses using gzip.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Eden Li (eden.li@gmail.com)
30
+ files:
31
+ - COPYING
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - test/test_gzip_filter.rb
36
+ - lib/gzip_filter
37
+ - lib/gzip_filter/init.rb
38
+ - tools/rakehelp.rb
39
+ test_files: []
40
+
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies:
52
+ - !ruby/object:Gem::Dependency
53
+ name: mongrel
54
+ version_requirement:
55
+ version_requirements: !ruby/object:Gem::Version::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.3.13.0
60
+ version:
61
+ - !ruby/object:Gem::Dependency
62
+ name: gem_plugin
63
+ version_requirement:
64
+ version_requirements: !ruby/object:Gem::Version::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0.2"
69
+ version: