rack 2.1.0 → 2.2.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.
Potentially problematic release.
This version of rack might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +126 -6
- data/CONTRIBUTING.md +136 -0
- data/README.rdoc +83 -39
- data/Rakefile +14 -7
- data/{SPEC → SPEC.rdoc} +26 -1
- data/lib/rack.rb +7 -16
- data/lib/rack/auth/abstract/request.rb +0 -2
- data/lib/rack/auth/basic.rb +3 -3
- data/lib/rack/auth/digest/md5.rb +4 -4
- data/lib/rack/auth/digest/request.rb +3 -3
- data/lib/rack/body_proxy.rb +13 -9
- data/lib/rack/builder.rb +78 -8
- data/lib/rack/cascade.rb +23 -8
- data/lib/rack/chunked.rb +48 -23
- data/lib/rack/common_logger.rb +25 -18
- data/lib/rack/conditional_get.rb +18 -16
- data/lib/rack/content_length.rb +6 -7
- data/lib/rack/content_type.rb +3 -4
- data/lib/rack/deflater.rb +49 -35
- data/lib/rack/directory.rb +77 -60
- data/lib/rack/etag.rb +2 -3
- data/lib/rack/events.rb +15 -18
- data/lib/rack/file.rb +1 -2
- data/lib/rack/files.rb +97 -57
- data/lib/rack/handler/cgi.rb +1 -4
- data/lib/rack/handler/fastcgi.rb +1 -3
- data/lib/rack/handler/lsws.rb +1 -3
- data/lib/rack/handler/scgi.rb +1 -3
- data/lib/rack/handler/thin.rb +1 -3
- data/lib/rack/handler/webrick.rb +12 -5
- data/lib/rack/head.rb +0 -2
- data/lib/rack/lint.rb +57 -14
- data/lib/rack/lobster.rb +3 -5
- data/lib/rack/lock.rb +0 -1
- data/lib/rack/mock.rb +22 -4
- data/lib/rack/multipart.rb +1 -1
- data/lib/rack/multipart/generator.rb +11 -6
- data/lib/rack/multipart/parser.rb +10 -18
- data/lib/rack/multipart/uploaded_file.rb +13 -7
- data/lib/rack/query_parser.rb +7 -8
- data/lib/rack/recursive.rb +1 -1
- data/lib/rack/reloader.rb +1 -3
- data/lib/rack/request.rb +182 -76
- data/lib/rack/response.rb +62 -19
- data/lib/rack/rewindable_input.rb +0 -1
- data/lib/rack/runtime.rb +3 -3
- data/lib/rack/sendfile.rb +0 -3
- data/lib/rack/server.rb +9 -10
- data/lib/rack/session/abstract/id.rb +23 -28
- data/lib/rack/session/cookie.rb +1 -3
- data/lib/rack/session/pool.rb +1 -1
- data/lib/rack/show_exceptions.rb +6 -8
- data/lib/rack/show_status.rb +5 -7
- data/lib/rack/static.rb +13 -6
- data/lib/rack/tempfile_reaper.rb +0 -2
- data/lib/rack/urlmap.rb +1 -4
- data/lib/rack/utils.rb +58 -54
- data/lib/rack/version.rb +29 -0
- data/rack.gemspec +31 -29
- metadata +11 -12
data/lib/rack/cascade.rb
CHANGED
@@ -2,25 +2,37 @@
|
|
2
2
|
|
3
3
|
module Rack
|
4
4
|
# Rack::Cascade tries a request on several apps, and returns the
|
5
|
-
# first response that is not 404 or 405 (or in a list of
|
6
|
-
# status codes).
|
5
|
+
# first response that is not 404 or 405 (or in a list of configured
|
6
|
+
# status codes). If all applications tried return one of the configured
|
7
|
+
# status codes, return the last response.
|
7
8
|
|
8
9
|
class Cascade
|
10
|
+
# deprecated, no longer used
|
9
11
|
NotFound = [404, { CONTENT_TYPE => "text/plain" }, []]
|
10
12
|
|
13
|
+
# An array of applications to try in order.
|
11
14
|
attr_reader :apps
|
12
15
|
|
13
|
-
|
16
|
+
# Set the apps to send requests to, and what statuses result in
|
17
|
+
# cascading. Arguments:
|
18
|
+
#
|
19
|
+
# apps: An enumerable of rack applications.
|
20
|
+
# cascade_for: The statuses to use cascading for. If a response is received
|
21
|
+
# from an app, the next app is tried.
|
22
|
+
def initialize(apps, cascade_for = [404, 405])
|
14
23
|
@apps = []
|
15
24
|
apps.each { |app| add app }
|
16
25
|
|
17
|
-
@
|
18
|
-
[*
|
26
|
+
@cascade_for = {}
|
27
|
+
[*cascade_for].each { |status| @cascade_for[status] = true }
|
19
28
|
end
|
20
29
|
|
30
|
+
# Call each app in order. If the responses uses a status that requires
|
31
|
+
# cascading, try the next app. If all responses require cascading,
|
32
|
+
# return the response from the last app.
|
21
33
|
def call(env)
|
22
|
-
|
23
|
-
|
34
|
+
return [404, { CONTENT_TYPE => "text/plain" }, []] if @apps.empty?
|
35
|
+
result = nil
|
24
36
|
last_body = nil
|
25
37
|
|
26
38
|
@apps.each do |app|
|
@@ -33,17 +45,20 @@ module Rack
|
|
33
45
|
last_body.close if last_body.respond_to? :close
|
34
46
|
|
35
47
|
result = app.call(env)
|
48
|
+
return result unless @cascade_for.include?(result[0].to_i)
|
36
49
|
last_body = result[2]
|
37
|
-
break unless @catch.include?(result[0].to_i)
|
38
50
|
end
|
39
51
|
|
40
52
|
result
|
41
53
|
end
|
42
54
|
|
55
|
+
# Append an app to the list of apps to cascade. This app will
|
56
|
+
# be tried last.
|
43
57
|
def add(app)
|
44
58
|
@apps << app
|
45
59
|
end
|
46
60
|
|
61
|
+
# Whether the given app is one of the apps to cascade to.
|
47
62
|
def include?(app)
|
48
63
|
@apps.include?(app)
|
49
64
|
end
|
data/lib/rack/chunked.rb
CHANGED
@@ -1,53 +1,74 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'rack/utils'
|
4
|
-
|
5
3
|
module Rack
|
6
4
|
|
7
5
|
# Middleware that applies chunked transfer encoding to response bodies
|
8
6
|
# when the response does not include a Content-Length header.
|
7
|
+
#
|
8
|
+
# This supports the Trailer response header to allow the use of trailing
|
9
|
+
# headers in the chunked encoding. However, using this requires you manually
|
10
|
+
# specify a response body that supports a +trailers+ method. Example:
|
11
|
+
#
|
12
|
+
# [200, { 'Trailer' => 'Expires'}, ["Hello", "World"]]
|
13
|
+
# # error raised
|
14
|
+
#
|
15
|
+
# body = ["Hello", "World"]
|
16
|
+
# def body.trailers
|
17
|
+
# { 'Expires' => Time.now.to_s }
|
18
|
+
# end
|
19
|
+
# [200, { 'Trailer' => 'Expires'}, body]
|
20
|
+
# # No exception raised
|
9
21
|
class Chunked
|
10
22
|
include Rack::Utils
|
11
23
|
|
12
|
-
# A body wrapper that emits chunked responses
|
24
|
+
# A body wrapper that emits chunked responses.
|
13
25
|
class Body
|
14
26
|
TERM = "\r\n"
|
15
27
|
TAIL = "0#{TERM}"
|
16
28
|
|
17
|
-
|
18
|
-
|
29
|
+
# Store the response body to be chunked.
|
19
30
|
def initialize(body)
|
20
31
|
@body = body
|
21
32
|
end
|
22
33
|
|
34
|
+
# For each element yielded by the response body, yield
|
35
|
+
# the element in chunked encoding.
|
23
36
|
def each(&block)
|
24
37
|
term = TERM
|
25
38
|
@body.each do |chunk|
|
26
39
|
size = chunk.bytesize
|
27
40
|
next if size == 0
|
28
41
|
|
29
|
-
|
30
|
-
yield [size.to_s(16), term, chunk, term].join
|
42
|
+
yield [size.to_s(16), term, chunk.b, term].join
|
31
43
|
end
|
32
44
|
yield TAIL
|
33
|
-
|
34
|
-
yield
|
45
|
+
yield_trailers(&block)
|
46
|
+
yield term
|
35
47
|
end
|
36
48
|
|
49
|
+
# Close the response body if the response body supports it.
|
37
50
|
def close
|
38
51
|
@body.close if @body.respond_to?(:close)
|
39
52
|
end
|
40
53
|
|
41
54
|
private
|
42
55
|
|
43
|
-
|
56
|
+
# Do nothing as this class does not support trailer headers.
|
57
|
+
def yield_trailers
|
44
58
|
end
|
45
59
|
end
|
46
60
|
|
61
|
+
# A body wrapper that emits chunked responses and also supports
|
62
|
+
# sending Trailer headers. Note that the response body provided to
|
63
|
+
# initialize must have a +trailers+ method that returns a hash
|
64
|
+
# of trailer headers, and the rack response itself should have a
|
65
|
+
# Trailer header listing the headers that the +trailers+ method
|
66
|
+
# will return.
|
47
67
|
class TrailerBody < Body
|
48
68
|
private
|
49
69
|
|
50
|
-
|
70
|
+
# Yield strings for each trailer header.
|
71
|
+
def yield_trailers
|
51
72
|
@body.trailers.each_pair do |k, v|
|
52
73
|
yield "#{k}: #{v}\r\n"
|
53
74
|
end
|
@@ -58,10 +79,11 @@ module Rack
|
|
58
79
|
@app = app
|
59
80
|
end
|
60
81
|
|
61
|
-
#
|
62
|
-
# a version (nor response headers)
|
82
|
+
# Whether the HTTP version supports chunked encoding (HTTP 1.1 does).
|
63
83
|
def chunkable_version?(ver)
|
64
84
|
case ver
|
85
|
+
# pre-HTTP/1.0 (informally "HTTP/0.9") HTTP requests did not have
|
86
|
+
# a version (nor response headers)
|
65
87
|
when 'HTTP/1.0', nil, 'HTTP/0.9'
|
66
88
|
false
|
67
89
|
else
|
@@ -69,24 +91,27 @@ module Rack
|
|
69
91
|
end
|
70
92
|
end
|
71
93
|
|
94
|
+
# If the rack app returns a response that should have a body,
|
95
|
+
# but does not have Content-Length or Transfer-Encoding headers,
|
96
|
+
# modify the response to use chunked Transfer-Encoding.
|
72
97
|
def call(env)
|
73
98
|
status, headers, body = @app.call(env)
|
74
|
-
headers = HeaderHash
|
99
|
+
headers = HeaderHash[headers]
|
100
|
+
|
101
|
+
if chunkable_version?(env[SERVER_PROTOCOL]) &&
|
102
|
+
!STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
|
103
|
+
!headers[CONTENT_LENGTH] &&
|
104
|
+
!headers[TRANSFER_ENCODING]
|
75
105
|
|
76
|
-
if ! chunkable_version?(env[SERVER_PROTOCOL]) ||
|
77
|
-
STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) ||
|
78
|
-
headers[CONTENT_LENGTH] ||
|
79
|
-
headers[TRANSFER_ENCODING]
|
80
|
-
[status, headers, body]
|
81
|
-
else
|
82
|
-
headers.delete(CONTENT_LENGTH)
|
83
106
|
headers[TRANSFER_ENCODING] = 'chunked'
|
84
107
|
if headers['Trailer']
|
85
|
-
|
108
|
+
body = TrailerBody.new(body)
|
86
109
|
else
|
87
|
-
|
110
|
+
body = Body.new(body)
|
88
111
|
end
|
89
112
|
end
|
113
|
+
|
114
|
+
[status, headers, body]
|
90
115
|
end
|
91
116
|
end
|
92
117
|
end
|
data/lib/rack/common_logger.rb
CHANGED
@@ -1,45 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'rack/body_proxy'
|
4
|
-
|
5
3
|
module Rack
|
6
4
|
# Rack::CommonLogger forwards every request to the given +app+, and
|
7
5
|
# logs a line in the
|
8
6
|
# {Apache common log format}[http://httpd.apache.org/docs/1.3/logs.html#common]
|
9
|
-
# to the
|
10
|
-
#
|
11
|
-
# If +logger+ is nil, CommonLogger will fall back +rack.errors+, which is
|
12
|
-
# an instance of Rack::NullLogger.
|
13
|
-
#
|
14
|
-
# +logger+ can be any class, including the standard library Logger, and is
|
15
|
-
# expected to have either +write+ or +<<+ method, which accepts the CommonLogger::FORMAT.
|
16
|
-
# According to the SPEC, the error stream must also respond to +puts+
|
17
|
-
# (which takes a single argument that responds to +to_s+), and +flush+
|
18
|
-
# (which is called without arguments in order to make the error appear for
|
19
|
-
# sure)
|
7
|
+
# to the configured logger.
|
20
8
|
class CommonLogger
|
21
9
|
# Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
|
22
10
|
#
|
23
11
|
# lilith.local - - [07/Aug/2006 23:58:02 -0400] "GET / HTTP/1.1" 500 -
|
24
12
|
#
|
25
13
|
# %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
|
26
|
-
|
14
|
+
#
|
15
|
+
# The actual format is slightly different than the above due to the
|
16
|
+
# separation of SCRIPT_NAME and PATH_INFO, and because the elapsed
|
17
|
+
# time in seconds is included at the end.
|
18
|
+
FORMAT = %{%s - %s [%s] "%s %s%s%s %s" %d %s %0.4f\n}
|
27
19
|
|
20
|
+
# +logger+ can be any object that supports the +write+ or +<<+ methods,
|
21
|
+
# which includes the standard library Logger. These methods are called
|
22
|
+
# with a single string argument, the log message.
|
23
|
+
# If +logger+ is nil, CommonLogger will fall back <tt>env['rack.errors']</tt>.
|
28
24
|
def initialize(app, logger = nil)
|
29
25
|
@app = app
|
30
26
|
@logger = logger
|
31
27
|
end
|
32
28
|
|
29
|
+
# Log all requests in common_log format after a response has been
|
30
|
+
# returned. Note that if the app raises an exception, the request
|
31
|
+
# will not be logged, so if exception handling middleware are used,
|
32
|
+
# they should be loaded after this middleware. Additionally, because
|
33
|
+
# the logging happens after the request body has been fully sent, any
|
34
|
+
# exceptions raised during the sending of the response body will
|
35
|
+
# cause the request not to be logged.
|
33
36
|
def call(env)
|
34
37
|
began_at = Utils.clock_time
|
35
|
-
status,
|
36
|
-
|
37
|
-
body = BodyProxy.new(body) { log(env, status,
|
38
|
-
[status,
|
38
|
+
status, headers, body = @app.call(env)
|
39
|
+
headers = Utils::HeaderHash[headers]
|
40
|
+
body = BodyProxy.new(body) { log(env, status, headers, began_at) }
|
41
|
+
[status, headers, body]
|
39
42
|
end
|
40
43
|
|
41
44
|
private
|
42
45
|
|
46
|
+
# Log the request to the configured logger.
|
43
47
|
def log(env, status, header, began_at)
|
44
48
|
length = extract_content_length(header)
|
45
49
|
|
@@ -48,6 +52,7 @@ module Rack
|
|
48
52
|
env["REMOTE_USER"] || "-",
|
49
53
|
Time.now.strftime("%d/%b/%Y:%H:%M:%S %z"),
|
50
54
|
env[REQUEST_METHOD],
|
55
|
+
env[SCRIPT_NAME],
|
51
56
|
env[PATH_INFO],
|
52
57
|
env[QUERY_STRING].empty? ? "" : "?#{env[QUERY_STRING]}",
|
53
58
|
env[SERVER_PROTOCOL],
|
@@ -65,6 +70,8 @@ module Rack
|
|
65
70
|
end
|
66
71
|
end
|
67
72
|
|
73
|
+
# Attempt to determine the content length for the response to
|
74
|
+
# include it in the logged data.
|
68
75
|
def extract_content_length(headers)
|
69
76
|
value = headers[CONTENT_LENGTH]
|
70
77
|
!value || value.to_s == '0' ? '-' : value
|
data/lib/rack/conditional_get.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'rack/utils'
|
4
|
-
|
5
3
|
module Rack
|
6
4
|
|
7
5
|
# Middleware that enables conditional GET using If-None-Match and
|
@@ -21,11 +19,13 @@ module Rack
|
|
21
19
|
@app = app
|
22
20
|
end
|
23
21
|
|
22
|
+
# Return empty 304 response if the response has not been
|
23
|
+
# modified since the last request.
|
24
24
|
def call(env)
|
25
25
|
case env[REQUEST_METHOD]
|
26
26
|
when "GET", "HEAD"
|
27
27
|
status, headers, body = @app.call(env)
|
28
|
-
headers = Utils::HeaderHash
|
28
|
+
headers = Utils::HeaderHash[headers]
|
29
29
|
if status == 200 && fresh?(env, headers)
|
30
30
|
status = 304
|
31
31
|
headers.delete(CONTENT_TYPE)
|
@@ -43,28 +43,32 @@ module Rack
|
|
43
43
|
|
44
44
|
private
|
45
45
|
|
46
|
+
# Return whether the response has not been modified since the
|
47
|
+
# last request.
|
46
48
|
def fresh?(env, headers)
|
47
|
-
|
48
|
-
none_match
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
success &&= modified_since?(to_rfc2822(modified_since), headers) if modified_since
|
54
|
-
success &&= etag_matches?(none_match, headers) if none_match
|
55
|
-
success
|
49
|
+
# If-None-Match has priority over If-Modified-Since per RFC 7232
|
50
|
+
if none_match = env['HTTP_IF_NONE_MATCH']
|
51
|
+
etag_matches?(none_match, headers)
|
52
|
+
elsif (modified_since = env['HTTP_IF_MODIFIED_SINCE']) && (modified_since = to_rfc2822(modified_since))
|
53
|
+
modified_since?(modified_since, headers)
|
54
|
+
end
|
56
55
|
end
|
57
56
|
|
57
|
+
# Whether the ETag response header matches the If-None-Match request header.
|
58
|
+
# If so, the request has not been modified.
|
58
59
|
def etag_matches?(none_match, headers)
|
59
|
-
|
60
|
+
headers['ETag'] == none_match
|
60
61
|
end
|
61
62
|
|
63
|
+
# Whether the Last-Modified response header matches the If-Modified-Since
|
64
|
+
# request header. If so, the request has not been modified.
|
62
65
|
def modified_since?(modified_since, headers)
|
63
66
|
last_modified = to_rfc2822(headers['Last-Modified']) and
|
64
|
-
modified_since and
|
65
67
|
modified_since >= last_modified
|
66
68
|
end
|
67
69
|
|
70
|
+
# Return a Time object for the given string (which should be in RFC2822
|
71
|
+
# format), or nil if the string cannot be parsed.
|
68
72
|
def to_rfc2822(since)
|
69
73
|
# shortest possible valid date is the obsolete: 1 Nov 97 09:55 A
|
70
74
|
# anything shorter is invalid, this avoids exceptions for common cases
|
@@ -73,8 +77,6 @@ module Rack
|
|
73
77
|
# NOTE: there is no trivial way to write this in a non exception way
|
74
78
|
# _rfc2822 returns a hash but is not that usable
|
75
79
|
Time.rfc2822(since) rescue nil
|
76
|
-
else
|
77
|
-
nil
|
78
80
|
end
|
79
81
|
end
|
80
82
|
end
|
data/lib/rack/content_length.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'rack/utils'
|
4
|
-
require 'rack/body_proxy'
|
5
|
-
|
6
3
|
module Rack
|
7
4
|
|
8
|
-
# Sets the Content-Length header on responses
|
5
|
+
# Sets the Content-Length header on responses that do not specify
|
6
|
+
# a Content-Length or Transfer-Encoding header. Note that this
|
7
|
+
# does not fix responses that have an invalid Content-Length
|
8
|
+
# header specified.
|
9
9
|
class ContentLength
|
10
10
|
include Rack::Utils
|
11
11
|
|
@@ -15,12 +15,11 @@ module Rack
|
|
15
15
|
|
16
16
|
def call(env)
|
17
17
|
status, headers, body = @app.call(env)
|
18
|
-
headers = HeaderHash
|
18
|
+
headers = HeaderHash[headers]
|
19
19
|
|
20
20
|
if !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
|
21
21
|
!headers[CONTENT_LENGTH] &&
|
22
|
-
!headers[TRANSFER_ENCODING]
|
23
|
-
body.respond_to?(:to_ary)
|
22
|
+
!headers[TRANSFER_ENCODING]
|
24
23
|
|
25
24
|
obody = body
|
26
25
|
body, length = [], 0
|
data/lib/rack/content_type.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'rack/utils'
|
4
|
-
|
5
3
|
module Rack
|
6
4
|
|
7
5
|
# Sets the Content-Type header on responses which don't have one.
|
@@ -9,7 +7,8 @@ module Rack
|
|
9
7
|
# Builder Usage:
|
10
8
|
# use Rack::ContentType, "text/plain"
|
11
9
|
#
|
12
|
-
# When no content type argument is provided, "text/html" is
|
10
|
+
# When no content type argument is provided, "text/html" is the
|
11
|
+
# default.
|
13
12
|
class ContentType
|
14
13
|
include Rack::Utils
|
15
14
|
|
@@ -19,7 +18,7 @@ module Rack
|
|
19
18
|
|
20
19
|
def call(env)
|
21
20
|
status, headers, body = @app.call(env)
|
22
|
-
headers = Utils::HeaderHash
|
21
|
+
headers = Utils::HeaderHash[headers]
|
23
22
|
|
24
23
|
unless STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i)
|
25
24
|
headers[CONTENT_TYPE] ||= @content_type
|
data/lib/rack/deflater.rb
CHANGED
@@ -2,48 +2,47 @@
|
|
2
2
|
|
3
3
|
require "zlib"
|
4
4
|
require "time" # for Time.httpdate
|
5
|
-
require 'rack/utils'
|
6
|
-
|
7
|
-
require_relative 'core_ext/regexp'
|
8
5
|
|
9
6
|
module Rack
|
10
|
-
# This middleware enables
|
7
|
+
# This middleware enables content encoding of http responses,
|
8
|
+
# usually for purposes of compression.
|
9
|
+
#
|
10
|
+
# Currently supported encodings:
|
11
11
|
#
|
12
|
-
#
|
12
|
+
# * gzip
|
13
|
+
# * identity (no transformation)
|
13
14
|
#
|
14
|
-
#
|
15
|
-
#
|
15
|
+
# This middleware automatically detects when encoding is supported
|
16
|
+
# and allowed. For example no encoding is made when a cache
|
17
|
+
# directive of 'no-transform' is present, when the response status
|
18
|
+
# code is one that doesn't allow an entity body, or when the body
|
19
|
+
# is empty.
|
16
20
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
# directive of 'no-transform' is present, or when the response status
|
20
|
-
# code is one that doesn't allow an entity body.
|
21
|
+
# Note that despite the name, Deflater does not support the +deflate+
|
22
|
+
# encoding.
|
21
23
|
class Deflater
|
22
|
-
using ::Rack::RegexpExtensions
|
24
|
+
(require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'
|
23
25
|
|
24
|
-
|
25
|
-
# Creates Rack::Deflater middleware.
|
26
|
+
# Creates Rack::Deflater middleware. Options:
|
26
27
|
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# compression and throughput. Defaults to `true'.
|
28
|
+
# :if :: a lambda enabling / disabling deflation based on returned boolean value
|
29
|
+
# (e.g <tt>use Rack::Deflater, :if => lambda { |*, body| sum=0; body.each { |i| sum += i.length }; sum > 512 }</tt>).
|
30
|
+
# However, be aware that calling `body.each` inside the block will break cases where `body.each` is not idempotent,
|
31
|
+
# such as when it is an +IO+ instance.
|
32
|
+
# :include :: a list of content types that should be compressed. By default, all content types are compressed.
|
33
|
+
# :sync :: determines if the stream is going to be flushed after every chunk. Flushing after every chunk reduces
|
34
|
+
# latency for time-sensitive streaming applications, but hurts compression and throughput.
|
35
|
+
# Defaults to +true+.
|
36
36
|
def initialize(app, options = {})
|
37
37
|
@app = app
|
38
|
-
|
39
38
|
@condition = options[:if]
|
40
39
|
@compressible_types = options[:include]
|
41
|
-
@sync = options
|
40
|
+
@sync = options.fetch(:sync, true)
|
42
41
|
end
|
43
42
|
|
44
43
|
def call(env)
|
45
44
|
status, headers, body = @app.call(env)
|
46
|
-
headers = Utils::HeaderHash
|
45
|
+
headers = Utils::HeaderHash[headers]
|
47
46
|
|
48
47
|
unless should_deflate?(env, status, headers, body)
|
49
48
|
return [status, headers, body]
|
@@ -63,7 +62,7 @@ module Rack
|
|
63
62
|
case encoding
|
64
63
|
when "gzip"
|
65
64
|
headers['Content-Encoding'] = "gzip"
|
66
|
-
headers.delete(
|
65
|
+
headers.delete(CONTENT_LENGTH)
|
67
66
|
mtime = headers["Last-Modified"]
|
68
67
|
mtime = Time.httpdate(mtime).to_i if mtime
|
69
68
|
[status, headers, GzipStream.new(body, mtime, @sync)]
|
@@ -72,49 +71,60 @@ module Rack
|
|
72
71
|
when nil
|
73
72
|
message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
|
74
73
|
bp = Rack::BodyProxy.new([message]) { body.close if body.respond_to?(:close) }
|
75
|
-
[406, {
|
74
|
+
[406, { CONTENT_TYPE => "text/plain", CONTENT_LENGTH => message.length.to_s }, bp]
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
78
|
+
# Body class used for gzip encoded responses.
|
79
79
|
class GzipStream
|
80
|
+
# Initialize the gzip stream. Arguments:
|
81
|
+
# body :: Response body to compress with gzip
|
82
|
+
# mtime :: The modification time of the body, used to set the
|
83
|
+
# modification time in the gzip header.
|
84
|
+
# sync :: Whether to flush each gzip chunk as soon as it is ready.
|
80
85
|
def initialize(body, mtime, sync)
|
81
|
-
@sync = sync
|
82
86
|
@body = body
|
83
87
|
@mtime = mtime
|
88
|
+
@sync = sync
|
84
89
|
end
|
85
90
|
|
91
|
+
# Yield gzip compressed strings to the given block.
|
86
92
|
def each(&block)
|
87
93
|
@writer = block
|
88
94
|
gzip = ::Zlib::GzipWriter.new(self)
|
89
95
|
gzip.mtime = @mtime if @mtime
|
90
96
|
@body.each { |part|
|
91
|
-
|
92
|
-
#
|
93
|
-
|
97
|
+
# Skip empty strings, as they would result in no output,
|
98
|
+
# and flushing empty parts would raise Zlib::BufError.
|
99
|
+
next if part.empty?
|
100
|
+
|
101
|
+
gzip.write(part)
|
102
|
+
gzip.flush if @sync
|
94
103
|
}
|
95
104
|
ensure
|
96
105
|
gzip.close
|
97
|
-
@writer = nil
|
98
106
|
end
|
99
107
|
|
108
|
+
# Call the block passed to #each with the the gzipped data.
|
100
109
|
def write(data)
|
101
110
|
@writer.call(data)
|
102
111
|
end
|
103
112
|
|
113
|
+
# Close the original body if possible.
|
104
114
|
def close
|
105
115
|
@body.close if @body.respond_to?(:close)
|
106
|
-
@body = nil
|
107
116
|
end
|
108
117
|
end
|
109
118
|
|
110
119
|
private
|
111
120
|
|
121
|
+
# Whether the body should be compressed.
|
112
122
|
def should_deflate?(env, status, headers, body)
|
113
123
|
# Skip compressing empty entity body responses and responses with
|
114
124
|
# no-transform set.
|
115
125
|
if Utils::STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) ||
|
116
126
|
/\bno-transform\b/.match?(headers['Cache-Control'].to_s) ||
|
117
|
-
|
127
|
+
headers['Content-Encoding']&.!~(/\bidentity\b/)
|
118
128
|
return false
|
119
129
|
end
|
120
130
|
|
@@ -124,6 +134,10 @@ module Rack
|
|
124
134
|
# Skip if @condition lambda is given and evaluates to false
|
125
135
|
return false if @condition && !@condition.call(env, status, headers, body)
|
126
136
|
|
137
|
+
# No point in compressing empty body, also handles usage with
|
138
|
+
# Rack::Sendfile.
|
139
|
+
return false if headers[CONTENT_LENGTH] == '0'
|
140
|
+
|
127
141
|
true
|
128
142
|
end
|
129
143
|
end
|