rack-brotli 0.0.0.1.ENOTAG
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +19 -0
- data/README.md +35 -0
- data/lib/rack/brotli/deflater.rb +110 -0
- data/lib/rack/rack-brotli.rb +12 -0
- metadata +173 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7d9b8c8a82c1721978996d011aa30e6beff3c11b
|
4
|
+
data.tar.gz: ee682fe690ebd3b7b7585dca14f16b801da1da74
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e57f60b8f3aa5cd79b7577ee1e870e0ddb8042358222a609e8c2141c8baae50711c387c7a6b4867d2f88c1d24ec4a96a5f254719482ba4f54b8d87b0a98fa71b
|
7
|
+
data.tar.gz: b141f4f16448b10c3d8a27284367574df4e129a3c373b3987872a62f94ce64737a62e5ca81b8da3f6223a18ae25545d522b79174294312ffd59ca662b76852c9
|
data/COPYING
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright (c) 2008 The Committers
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
of this software and associated documentation files (the "Software"), to
|
6
|
+
deal in the Software without restriction, including without limitation the
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
8
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
9
|
+
furnished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
12
|
+
all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
17
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Rack::Brotli [![Build Status](https://travis-ci.org/marcotc/rack-brotli.svg?branch=master)](https://travis-ci.org/marcotc/rack-brotli)
|
2
|
+
|
3
|
+
`Rack::Brotli` compresses `Rack` responses using [Google's Brotli](https://github.com/google/brotli) compression algorithm.
|
4
|
+
|
5
|
+
Brotli generally compresses better than `gzip` for the same CPU cost and is supported by [Chrome, Firefox, IE and Opera](http://caniuse.com/#feat=brotli).
|
6
|
+
|
7
|
+
### Use
|
8
|
+
|
9
|
+
Gems are available too:
|
10
|
+
|
11
|
+
gem install rack-brotli
|
12
|
+
|
13
|
+
Requiring `'rack/brotli'` will autoload `Rack::Brotli` module. The following example shows what a simple rackup
|
14
|
+
(`config.ru`) file might look like:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'rack'
|
18
|
+
require 'rack/brotli'
|
19
|
+
|
20
|
+
use Rack::Brotli
|
21
|
+
|
22
|
+
run theapp
|
23
|
+
```
|
24
|
+
|
25
|
+
### Testing
|
26
|
+
|
27
|
+
To run the entire test suite, run
|
28
|
+
|
29
|
+
rake test
|
30
|
+
|
31
|
+
### Links
|
32
|
+
|
33
|
+
* rack-brotli on GitHub:: <http://github.com/marcotc/rack-brotli>
|
34
|
+
* Rack:: <http://rack.rubyforge.org/>
|
35
|
+
* Rack On GitHub:: <http://github.com/rack/rack>
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require "brotli"
|
2
|
+
require 'rack/utils'
|
3
|
+
|
4
|
+
module Rack::Brotli
|
5
|
+
# This middleware enables compression of http responses.
|
6
|
+
#
|
7
|
+
# Currently supported compression algorithms:
|
8
|
+
#
|
9
|
+
# * br
|
10
|
+
#
|
11
|
+
# The middleware automatically detects when compression is supported
|
12
|
+
# and allowed. For example no transformation is made when a cache
|
13
|
+
# directive of 'no-transform' is present, or when the response status
|
14
|
+
# code is one that doesn't allow an entity body.
|
15
|
+
class Deflater
|
16
|
+
##
|
17
|
+
# Creates Rack::Brotli middleware.
|
18
|
+
#
|
19
|
+
# [app] rack app instance
|
20
|
+
# [options] hash of deflater options, i.e.
|
21
|
+
# 'if' - a lambda enabling / disabling deflation based on returned boolean value
|
22
|
+
# e.g use Rack::Brotli, :if => lambda { |env, status, headers, body| body.map(&:bytesize).reduce(0, :+) > 512 }
|
23
|
+
# 'include' - a list of content types that should be compressed
|
24
|
+
# 'deflater' - Brotli compression options
|
25
|
+
def initialize(app, options = {})
|
26
|
+
@app = app
|
27
|
+
|
28
|
+
@condition = options[:if]
|
29
|
+
@compressible_types = options[:include]
|
30
|
+
@deflater_options = options[:deflater]
|
31
|
+
end
|
32
|
+
|
33
|
+
def call(env)
|
34
|
+
status, headers, body = @app.call(env)
|
35
|
+
headers = Rack::Utils::HeaderHash.new(headers)
|
36
|
+
|
37
|
+
unless should_deflate?(env, status, headers, body)
|
38
|
+
return [status, headers, body]
|
39
|
+
end
|
40
|
+
|
41
|
+
request = Rack::Request.new(env)
|
42
|
+
|
43
|
+
encoding = Rack::Utils.select_best_encoding(%w(br),
|
44
|
+
request.accept_encoding)
|
45
|
+
|
46
|
+
return [status, headers, body] unless encoding
|
47
|
+
|
48
|
+
# Set the Vary HTTP header.
|
49
|
+
vary = headers["Vary"].to_s.split(",").map(&:strip)
|
50
|
+
unless vary.include?("*") || vary.include?("Accept-Encoding")
|
51
|
+
headers["Vary"] = vary.push("Accept-Encoding").join(",")
|
52
|
+
end
|
53
|
+
|
54
|
+
case encoding
|
55
|
+
when "br"
|
56
|
+
headers['Content-Encoding'] = "br"
|
57
|
+
headers.delete(Rack::CONTENT_LENGTH)
|
58
|
+
[status, headers, BrotliStream.new(body, @deflater_options)]
|
59
|
+
when nil
|
60
|
+
message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
|
61
|
+
bp = Rack::BodyProxy.new([message]) { body.close if body.respond_to?(:close) }
|
62
|
+
[406, {Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => message.length.to_s}, bp]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class BrotliStream
|
67
|
+
include Rack::Utils
|
68
|
+
|
69
|
+
def initialize(body, options)
|
70
|
+
@body = body
|
71
|
+
@options = options
|
72
|
+
end
|
73
|
+
|
74
|
+
def each(&block)
|
75
|
+
@writer = block
|
76
|
+
buffer = ''
|
77
|
+
@body.each { |part|
|
78
|
+
buffer << part
|
79
|
+
}
|
80
|
+
yield ::Brotli.deflate(buffer, @options)
|
81
|
+
ensure
|
82
|
+
@writer = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
def close
|
86
|
+
@body.close if @body.respond_to?(:close)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def should_deflate?(env, status, headers, body)
|
93
|
+
# Skip compressing empty entity body responses and responses with
|
94
|
+
# no-transform set.
|
95
|
+
if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
|
96
|
+
headers[Rack::CACHE_CONTROL].to_s =~ /\bno-transform\b/ ||
|
97
|
+
(headers['Content-Encoding'] && headers['Content-Encoding'] !~ /\bidentity\b/)
|
98
|
+
return false
|
99
|
+
end
|
100
|
+
|
101
|
+
# Skip if @compressible_types are given and does not include request's content type
|
102
|
+
return false if @compressible_types && !(headers.has_key?(Rack::CONTENT_TYPE) && @compressible_types.include?(headers[Rack::CONTENT_TYPE][/[^;]*/]))
|
103
|
+
|
104
|
+
# Skip if @condition lambda is given and evaluates to false
|
105
|
+
return false if @condition && !@condition.call(env, status, headers, body)
|
106
|
+
|
107
|
+
true
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-brotli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0.1.ENOTAG
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marco Costa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: git-version-bump
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.15'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: brotli
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.1.7
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.7
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: github-release
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.1'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.6'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.6'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.4'
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 10.4.2
|
107
|
+
type: :development
|
108
|
+
prerelease: false
|
109
|
+
version_requirements: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - "~>"
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '10.4'
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 10.4.2
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: rdoc
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '3.12'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '3.12'
|
131
|
+
description: Rack::Brotli enables Google's Brotli compression on HTTP responses
|
132
|
+
email: marco@marcotc.com
|
133
|
+
executables: []
|
134
|
+
extensions: []
|
135
|
+
extra_rdoc_files:
|
136
|
+
- README.md
|
137
|
+
- COPYING
|
138
|
+
files:
|
139
|
+
- COPYING
|
140
|
+
- README.md
|
141
|
+
- lib/rack/brotli/deflater.rb
|
142
|
+
- lib/rack/rack-brotli.rb
|
143
|
+
homepage: http://github.com/marcotc/rack-brotli/
|
144
|
+
licenses:
|
145
|
+
- MIT
|
146
|
+
metadata: {}
|
147
|
+
post_install_message:
|
148
|
+
rdoc_options:
|
149
|
+
- "--line-numbers"
|
150
|
+
- "--inline-source"
|
151
|
+
- "--title"
|
152
|
+
- rack-brotli
|
153
|
+
- "--main"
|
154
|
+
- README
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 1.3.1
|
167
|
+
requirements: []
|
168
|
+
rubyforge_project:
|
169
|
+
rubygems_version: 2.4.5
|
170
|
+
signing_key:
|
171
|
+
specification_version: 2
|
172
|
+
summary: Brotli compression for Rack responses
|
173
|
+
test_files: []
|