nano-sharp-hub 0.0.1
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.
- checksums.yaml +7 -0
- data/nano-sharp-hub.gemspec +12 -0
- data/rack-3.2.6/CHANGELOG.md +1346 -0
- data/rack-3.2.6/CONTRIBUTING.md +144 -0
- data/rack-3.2.6/MIT-LICENSE +20 -0
- data/rack-3.2.6/README.md +384 -0
- data/rack-3.2.6/SPEC.rdoc +258 -0
- data/rack-3.2.6/lib/rack/auth/abstract/handler.rb +41 -0
- data/rack-3.2.6/lib/rack/auth/abstract/request.rb +51 -0
- data/rack-3.2.6/lib/rack/auth/basic.rb +58 -0
- data/rack-3.2.6/lib/rack/bad_request.rb +8 -0
- data/rack-3.2.6/lib/rack/body_proxy.rb +63 -0
- data/rack-3.2.6/lib/rack/builder.rb +296 -0
- data/rack-3.2.6/lib/rack/cascade.rb +67 -0
- data/rack-3.2.6/lib/rack/common_logger.rb +89 -0
- data/rack-3.2.6/lib/rack/conditional_get.rb +87 -0
- data/rack-3.2.6/lib/rack/config.rb +22 -0
- data/rack-3.2.6/lib/rack/constants.rb +68 -0
- data/rack-3.2.6/lib/rack/content_length.rb +34 -0
- data/rack-3.2.6/lib/rack/content_type.rb +33 -0
- data/rack-3.2.6/lib/rack/deflater.rb +158 -0
- data/rack-3.2.6/lib/rack/directory.rb +208 -0
- data/rack-3.2.6/lib/rack/etag.rb +71 -0
- data/rack-3.2.6/lib/rack/events.rb +172 -0
- data/rack-3.2.6/lib/rack/files.rb +216 -0
- data/rack-3.2.6/lib/rack/head.rb +25 -0
- data/rack-3.2.6/lib/rack/headers.rb +238 -0
- data/rack-3.2.6/lib/rack/lint.rb +964 -0
- data/rack-3.2.6/lib/rack/lock.rb +29 -0
- data/rack-3.2.6/lib/rack/media_type.rb +52 -0
- data/rack-3.2.6/lib/rack/method_override.rb +56 -0
- data/rack-3.2.6/lib/rack/mime.rb +694 -0
- data/rack-3.2.6/lib/rack/mock.rb +3 -0
- data/rack-3.2.6/lib/rack/mock_request.rb +161 -0
- data/rack-3.2.6/lib/rack/mock_response.rb +156 -0
- data/rack-3.2.6/lib/rack/multipart/generator.rb +99 -0
- data/rack-3.2.6/lib/rack/multipart/parser.rb +621 -0
- data/rack-3.2.6/lib/rack/multipart/uploaded_file.rb +82 -0
- data/rack-3.2.6/lib/rack/multipart.rb +77 -0
- data/rack-3.2.6/lib/rack/null_logger.rb +48 -0
- data/rack-3.2.6/lib/rack/query_parser.rb +261 -0
- data/rack-3.2.6/lib/rack/recursive.rb +66 -0
- data/rack-3.2.6/lib/rack/reloader.rb +112 -0
- data/rack-3.2.6/lib/rack/request.rb +790 -0
- data/rack-3.2.6/lib/rack/response.rb +403 -0
- data/rack-3.2.6/lib/rack/rewindable_input.rb +116 -0
- data/rack-3.2.6/lib/rack/runtime.rb +35 -0
- data/rack-3.2.6/lib/rack/sendfile.rb +197 -0
- data/rack-3.2.6/lib/rack/show_exceptions.rb +409 -0
- data/rack-3.2.6/lib/rack/show_status.rb +121 -0
- data/rack-3.2.6/lib/rack/static.rb +192 -0
- data/rack-3.2.6/lib/rack/tempfile_reaper.rb +33 -0
- data/rack-3.2.6/lib/rack/urlmap.rb +99 -0
- data/rack-3.2.6/lib/rack/utils.rb +714 -0
- data/rack-3.2.6/lib/rack/version.rb +17 -0
- data/rack-3.2.6/lib/rack.rb +64 -0
- metadata +96 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "zlib"
|
|
4
|
+
require "time" # for Time.httpdate
|
|
5
|
+
|
|
6
|
+
require_relative 'constants'
|
|
7
|
+
require_relative 'utils'
|
|
8
|
+
require_relative 'request'
|
|
9
|
+
require_relative 'body_proxy'
|
|
10
|
+
|
|
11
|
+
module Rack
|
|
12
|
+
# This middleware enables content encoding of http responses,
|
|
13
|
+
# usually for purposes of compression.
|
|
14
|
+
#
|
|
15
|
+
# Currently supported encodings:
|
|
16
|
+
#
|
|
17
|
+
# * gzip
|
|
18
|
+
# * identity (no transformation)
|
|
19
|
+
#
|
|
20
|
+
# This middleware automatically detects when encoding is supported
|
|
21
|
+
# and allowed. For example no encoding is made when a cache
|
|
22
|
+
# directive of 'no-transform' is present, when the response status
|
|
23
|
+
# code is one that doesn't allow an entity body, or when the body
|
|
24
|
+
# is empty.
|
|
25
|
+
#
|
|
26
|
+
# Note that despite the name, Deflater does not support the +deflate+
|
|
27
|
+
# encoding.
|
|
28
|
+
class Deflater
|
|
29
|
+
# Creates Rack::Deflater middleware. Options:
|
|
30
|
+
#
|
|
31
|
+
# :if :: a lambda enabling / disabling deflation based on returned boolean value
|
|
32
|
+
# (e.g <tt>use Rack::Deflater, :if => lambda { |*, body| sum=0; body.each { |i| sum += i.length }; sum > 512 }</tt>).
|
|
33
|
+
# However, be aware that calling `body.each` inside the block will break cases where `body.each` is not idempotent,
|
|
34
|
+
# such as when it is an +IO+ instance.
|
|
35
|
+
# :include :: a list of content types that should be compressed. By default, all content types are compressed.
|
|
36
|
+
# :sync :: determines if the stream is going to be flushed after every chunk. Flushing after every chunk reduces
|
|
37
|
+
# latency for time-sensitive streaming applications, but hurts compression and throughput.
|
|
38
|
+
# Defaults to +true+.
|
|
39
|
+
def initialize(app, options = {})
|
|
40
|
+
@app = app
|
|
41
|
+
@condition = options[:if]
|
|
42
|
+
@compressible_types = options[:include]
|
|
43
|
+
@sync = options.fetch(:sync, true)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def call(env)
|
|
47
|
+
status, headers, body = response = @app.call(env)
|
|
48
|
+
|
|
49
|
+
unless should_deflate?(env, status, headers, body)
|
|
50
|
+
return response
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
request = Request.new(env)
|
|
54
|
+
|
|
55
|
+
encoding = Utils.select_best_encoding(%w(gzip identity),
|
|
56
|
+
request.accept_encoding)
|
|
57
|
+
|
|
58
|
+
# Set the Vary HTTP header.
|
|
59
|
+
vary = headers["vary"].to_s.split(",").map(&:strip)
|
|
60
|
+
unless vary.include?("*") || vary.any?{|v| v.downcase == 'accept-encoding'}
|
|
61
|
+
headers["vary"] = vary.push("Accept-Encoding").join(",")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
case encoding
|
|
65
|
+
when "gzip"
|
|
66
|
+
headers['content-encoding'] = "gzip"
|
|
67
|
+
headers.delete(CONTENT_LENGTH)
|
|
68
|
+
mtime = headers["last-modified"]
|
|
69
|
+
mtime = Time.httpdate(mtime).to_i if mtime
|
|
70
|
+
response[2] = GzipStream.new(body, mtime, @sync)
|
|
71
|
+
response
|
|
72
|
+
when "identity"
|
|
73
|
+
response
|
|
74
|
+
else # when nil
|
|
75
|
+
# Only possible encoding values here are 'gzip', 'identity', and nil
|
|
76
|
+
message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
|
|
77
|
+
bp = Rack::BodyProxy.new([message]) { body.close if body.respond_to?(:close) }
|
|
78
|
+
[406, { CONTENT_TYPE => "text/plain", CONTENT_LENGTH => message.length.to_s }, bp]
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Body class used for gzip encoded responses.
|
|
83
|
+
class GzipStream
|
|
84
|
+
|
|
85
|
+
BUFFER_LENGTH = 128 * 1_024
|
|
86
|
+
|
|
87
|
+
# Initialize the gzip stream. Arguments:
|
|
88
|
+
# body :: Response body to compress with gzip
|
|
89
|
+
# mtime :: The modification time of the body, used to set the
|
|
90
|
+
# modification time in the gzip header.
|
|
91
|
+
# sync :: Whether to flush each gzip chunk as soon as it is ready.
|
|
92
|
+
def initialize(body, mtime, sync)
|
|
93
|
+
@body = body
|
|
94
|
+
@mtime = mtime
|
|
95
|
+
@sync = sync
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Yield gzip compressed strings to the given block.
|
|
99
|
+
def each(&block)
|
|
100
|
+
@writer = block
|
|
101
|
+
gzip = ::Zlib::GzipWriter.new(self)
|
|
102
|
+
gzip.mtime = @mtime if @mtime
|
|
103
|
+
# @body.each is equivalent to @body.gets (slow)
|
|
104
|
+
if @body.is_a? ::File # XXX: Should probably be ::IO
|
|
105
|
+
while part = @body.read(BUFFER_LENGTH)
|
|
106
|
+
gzip.write(part)
|
|
107
|
+
gzip.flush if @sync
|
|
108
|
+
end
|
|
109
|
+
else
|
|
110
|
+
@body.each { |part|
|
|
111
|
+
# Skip empty strings, as they would result in no output,
|
|
112
|
+
# and flushing empty parts would raise Zlib::BufError.
|
|
113
|
+
next if part.empty?
|
|
114
|
+
gzip.write(part)
|
|
115
|
+
gzip.flush if @sync
|
|
116
|
+
}
|
|
117
|
+
end
|
|
118
|
+
ensure
|
|
119
|
+
gzip.finish
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Call the block passed to #each with the gzipped data.
|
|
123
|
+
def write(data)
|
|
124
|
+
@writer.call(data)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Close the original body if possible.
|
|
128
|
+
def close
|
|
129
|
+
@body.close if @body.respond_to?(:close)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
private
|
|
134
|
+
|
|
135
|
+
# Whether the body should be compressed.
|
|
136
|
+
def should_deflate?(env, status, headers, body)
|
|
137
|
+
# Skip compressing empty entity body responses and responses with
|
|
138
|
+
# no-transform set.
|
|
139
|
+
if Utils::STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) ||
|
|
140
|
+
/\bno-transform\b/.match?(headers[CACHE_CONTROL].to_s) ||
|
|
141
|
+
headers['content-encoding']&.!~(/\bidentity\b/)
|
|
142
|
+
return false
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Skip if @compressible_types are given and does not include request's content type
|
|
146
|
+
return false if @compressible_types && !(headers.has_key?(CONTENT_TYPE) && @compressible_types.include?(headers[CONTENT_TYPE][/[^;]*/]))
|
|
147
|
+
|
|
148
|
+
# Skip if @condition lambda is given and evaluates to false
|
|
149
|
+
return false if @condition && !@condition.call(env, status, headers, body)
|
|
150
|
+
|
|
151
|
+
# No point in compressing empty body, also handles usage with
|
|
152
|
+
# Rack::Sendfile.
|
|
153
|
+
return false if headers[CONTENT_LENGTH] == '0'
|
|
154
|
+
|
|
155
|
+
true
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
|
|
5
|
+
require_relative 'constants'
|
|
6
|
+
require_relative 'utils'
|
|
7
|
+
require_relative 'head'
|
|
8
|
+
require_relative 'mime'
|
|
9
|
+
require_relative 'files'
|
|
10
|
+
|
|
11
|
+
module Rack
|
|
12
|
+
# Rack::Directory serves entries below the +root+ given, according to the
|
|
13
|
+
# path info of the Rack request. If a directory is found, the file's contents
|
|
14
|
+
# will be presented in an html based index. If a file is found, the env will
|
|
15
|
+
# be passed to the specified +app+.
|
|
16
|
+
#
|
|
17
|
+
# If +app+ is not specified, a Rack::Files of the same +root+ will be used.
|
|
18
|
+
|
|
19
|
+
class Directory
|
|
20
|
+
DIR_FILE = "<tr><td class='name'><a href='./%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>\n"
|
|
21
|
+
DIR_PAGE_HEADER = <<-PAGE
|
|
22
|
+
<html><head>
|
|
23
|
+
<title>%s</title>
|
|
24
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
25
|
+
<style type='text/css'>
|
|
26
|
+
table { width:100%%; }
|
|
27
|
+
.name { text-align:left; }
|
|
28
|
+
.size, .mtime { text-align:right; }
|
|
29
|
+
.type { width:11em; }
|
|
30
|
+
.mtime { width:15em; }
|
|
31
|
+
</style>
|
|
32
|
+
</head><body>
|
|
33
|
+
<h1>%s</h1>
|
|
34
|
+
<hr />
|
|
35
|
+
<table>
|
|
36
|
+
<tr>
|
|
37
|
+
<th class='name'>Name</th>
|
|
38
|
+
<th class='size'>Size</th>
|
|
39
|
+
<th class='type'>Type</th>
|
|
40
|
+
<th class='mtime'>Last Modified</th>
|
|
41
|
+
</tr>
|
|
42
|
+
PAGE
|
|
43
|
+
DIR_PAGE_FOOTER = <<-PAGE
|
|
44
|
+
</table>
|
|
45
|
+
<hr />
|
|
46
|
+
</body></html>
|
|
47
|
+
PAGE
|
|
48
|
+
|
|
49
|
+
# Body class for directory entries, showing an index page with links
|
|
50
|
+
# to each file.
|
|
51
|
+
class DirectoryBody < Struct.new(:root, :path, :files)
|
|
52
|
+
# Yield strings for each part of the directory entry
|
|
53
|
+
def each
|
|
54
|
+
show_path = Utils.escape_html(path.sub(/\A#{Regexp.escape(root)}/, ''))
|
|
55
|
+
yield(DIR_PAGE_HEADER % [ show_path, show_path ])
|
|
56
|
+
|
|
57
|
+
unless path.chomp('/') == root
|
|
58
|
+
yield(DIR_FILE % DIR_FILE_escape(files.call('..')))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
Dir.foreach(path) do |basename|
|
|
62
|
+
next if basename.start_with?('.')
|
|
63
|
+
next unless f = files.call(basename)
|
|
64
|
+
yield(DIR_FILE % DIR_FILE_escape(f))
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
yield(DIR_PAGE_FOOTER)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
# Escape each element in the array of html strings.
|
|
73
|
+
def DIR_FILE_escape(htmls)
|
|
74
|
+
htmls.map { |e| Utils.escape_html(e) }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The root of the directory hierarchy. Only requests for files and
|
|
79
|
+
# directories inside of the root directory are supported.
|
|
80
|
+
attr_reader :root
|
|
81
|
+
|
|
82
|
+
# Set the root directory and application for serving files.
|
|
83
|
+
def initialize(root, app = nil)
|
|
84
|
+
@root = ::File.expand_path(root)
|
|
85
|
+
@root_with_separator = @root.end_with?(::File::SEPARATOR) ? @root : "#{@root}#{::File::SEPARATOR}"
|
|
86
|
+
@app = app || Files.new(@root)
|
|
87
|
+
@head = Head.new(method(:get))
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def call(env)
|
|
91
|
+
# strip body if this is a HEAD call
|
|
92
|
+
@head.call env
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Internals of request handling. Similar to call but does
|
|
96
|
+
# not remove body for HEAD requests.
|
|
97
|
+
def get(env)
|
|
98
|
+
script_name = env[SCRIPT_NAME]
|
|
99
|
+
path_info = Utils.unescape_path(env[PATH_INFO])
|
|
100
|
+
|
|
101
|
+
if client_error_response = check_bad_request(path_info) || check_forbidden(path_info)
|
|
102
|
+
client_error_response
|
|
103
|
+
else
|
|
104
|
+
path = ::File.join(@root, path_info)
|
|
105
|
+
list_path(env, path, path_info, script_name)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Rack response to use for requests with invalid paths, or nil if path is valid.
|
|
110
|
+
def check_bad_request(path_info)
|
|
111
|
+
return if Utils.valid_path?(path_info)
|
|
112
|
+
|
|
113
|
+
body = "Bad Request\n"
|
|
114
|
+
[400, { CONTENT_TYPE => "text/plain",
|
|
115
|
+
CONTENT_LENGTH => body.bytesize.to_s,
|
|
116
|
+
"x-cascade" => "pass" }, [body]]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Rack response to use for requests with paths outside the root, or nil if path is inside the root.
|
|
120
|
+
def check_forbidden(path_info)
|
|
121
|
+
return unless path_info.include? ".."
|
|
122
|
+
|
|
123
|
+
expanded_path = ::File.expand_path(::File.join(@root, path_info))
|
|
124
|
+
return if expanded_path == @root || expanded_path.start_with?(@root_with_separator)
|
|
125
|
+
|
|
126
|
+
body = "Forbidden\n"
|
|
127
|
+
[403, { CONTENT_TYPE => "text/plain",
|
|
128
|
+
CONTENT_LENGTH => body.bytesize.to_s,
|
|
129
|
+
"x-cascade" => "pass" }, [body]]
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Rack response to use for directories under the root.
|
|
133
|
+
def list_directory(path_info, path, script_name)
|
|
134
|
+
url_head = (script_name.split('/') + path_info.split('/')).map do |part|
|
|
135
|
+
Utils.escape_path part
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Globbing not safe as path could contain glob metacharacters
|
|
139
|
+
body = DirectoryBody.new(@root, path, ->(basename) do
|
|
140
|
+
stat = stat(::File.join(path, basename))
|
|
141
|
+
next unless stat
|
|
142
|
+
|
|
143
|
+
url = ::File.join(*url_head + [Utils.escape_path(basename)])
|
|
144
|
+
mtime = stat.mtime.httpdate
|
|
145
|
+
if stat.directory?
|
|
146
|
+
type = 'directory'
|
|
147
|
+
size = '-'
|
|
148
|
+
url << '/'
|
|
149
|
+
if basename == '..'
|
|
150
|
+
basename = 'Parent Directory'
|
|
151
|
+
else
|
|
152
|
+
basename << '/'
|
|
153
|
+
end
|
|
154
|
+
else
|
|
155
|
+
type = Mime.mime_type(::File.extname(basename))
|
|
156
|
+
size = filesize_format(stat.size)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
[ url, basename, size, type, mtime ]
|
|
160
|
+
end)
|
|
161
|
+
|
|
162
|
+
[ 200, { CONTENT_TYPE => 'text/html; charset=utf-8' }, body ]
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# File::Stat for the given path, but return nil for missing/bad entries.
|
|
166
|
+
def stat(path)
|
|
167
|
+
::File.stat(path)
|
|
168
|
+
rescue Errno::ENOENT, Errno::ELOOP
|
|
169
|
+
return nil
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Rack response to use for files and directories under the root.
|
|
173
|
+
# Unreadable and non-file, non-directory entries will get a 404 response.
|
|
174
|
+
def list_path(env, path, path_info, script_name)
|
|
175
|
+
if (stat = stat(path)) && stat.readable?
|
|
176
|
+
return @app.call(env) if stat.file?
|
|
177
|
+
return list_directory(path_info, path, script_name) if stat.directory?
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
entity_not_found(path_info)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Rack response to use for unreadable and non-file, non-directory entries.
|
|
184
|
+
def entity_not_found(path_info)
|
|
185
|
+
body = "Entity not found: #{path_info}\n"
|
|
186
|
+
[404, { CONTENT_TYPE => "text/plain",
|
|
187
|
+
CONTENT_LENGTH => body.bytesize.to_s,
|
|
188
|
+
"x-cascade" => "pass" }, [body]]
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Stolen from Ramaze
|
|
192
|
+
FILESIZE_FORMAT = [
|
|
193
|
+
['%.1fT', 1 << 40],
|
|
194
|
+
['%.1fG', 1 << 30],
|
|
195
|
+
['%.1fM', 1 << 20],
|
|
196
|
+
['%.1fK', 1 << 10],
|
|
197
|
+
]
|
|
198
|
+
|
|
199
|
+
# Provide human readable file sizes
|
|
200
|
+
def filesize_format(int)
|
|
201
|
+
FILESIZE_FORMAT.each do |format, size|
|
|
202
|
+
return format % (int.to_f / size) if int >= size
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
"#{int}B"
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'digest/sha2'
|
|
4
|
+
|
|
5
|
+
require_relative 'constants'
|
|
6
|
+
require_relative 'utils'
|
|
7
|
+
|
|
8
|
+
module Rack
|
|
9
|
+
# Automatically sets the etag header on all String bodies.
|
|
10
|
+
#
|
|
11
|
+
# The etag header is skipped if etag or last-modified headers are sent or if
|
|
12
|
+
# a sendfile body (body.responds_to :to_path) is given (since such cases
|
|
13
|
+
# should be handled by apache/nginx).
|
|
14
|
+
#
|
|
15
|
+
# On initialization, you can pass two parameters: a cache-control directive
|
|
16
|
+
# used when etag is absent and a directive when it is present. The first
|
|
17
|
+
# defaults to nil, while the second defaults to "max-age=0, private, must-revalidate"
|
|
18
|
+
class ETag
|
|
19
|
+
ETAG_STRING = Rack::ETAG
|
|
20
|
+
DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate"
|
|
21
|
+
|
|
22
|
+
def initialize(app, no_cache_control = nil, cache_control = DEFAULT_CACHE_CONTROL)
|
|
23
|
+
@app = app
|
|
24
|
+
@cache_control = cache_control
|
|
25
|
+
@no_cache_control = no_cache_control
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call(env)
|
|
29
|
+
status, headers, body = response = @app.call(env)
|
|
30
|
+
|
|
31
|
+
if etag_status?(status) && body.respond_to?(:to_ary) && !skip_caching?(headers)
|
|
32
|
+
body = body.to_ary
|
|
33
|
+
digest = digest_body(body)
|
|
34
|
+
headers[ETAG_STRING] = %(W/"#{digest}") if digest
|
|
35
|
+
|
|
36
|
+
# Body was modified, so we need to re-assign it:
|
|
37
|
+
response[2] = body
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
unless headers[CACHE_CONTROL]
|
|
41
|
+
if digest
|
|
42
|
+
headers[CACHE_CONTROL] = @cache_control if @cache_control
|
|
43
|
+
else
|
|
44
|
+
headers[CACHE_CONTROL] = @no_cache_control if @no_cache_control
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
response
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def etag_status?(status)
|
|
54
|
+
status == 200 || status == 201
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def skip_caching?(headers)
|
|
58
|
+
headers.key?(ETAG_STRING) || headers.key?('last-modified')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def digest_body(body)
|
|
62
|
+
digest = nil
|
|
63
|
+
|
|
64
|
+
body.each do |part|
|
|
65
|
+
(digest ||= Digest::SHA256.new) << part unless part.empty?
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
digest && digest.hexdigest.byteslice(0,32)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'body_proxy'
|
|
4
|
+
require_relative 'request'
|
|
5
|
+
require_relative 'response'
|
|
6
|
+
|
|
7
|
+
module Rack
|
|
8
|
+
### This middleware provides hooks to certain places in the request /
|
|
9
|
+
# response lifecycle. This is so that middleware that don't need to filter
|
|
10
|
+
# the response data can safely leave it alone and not have to send messages
|
|
11
|
+
# down the traditional "rack stack".
|
|
12
|
+
#
|
|
13
|
+
# The events are:
|
|
14
|
+
#
|
|
15
|
+
# * on_start(request, response)
|
|
16
|
+
#
|
|
17
|
+
# This event is sent at the start of the request, before the next
|
|
18
|
+
# middleware in the chain is called. This method is called with a request
|
|
19
|
+
# object, and a response object. Right now, the response object is always
|
|
20
|
+
# nil, but in the future it may actually be a real response object.
|
|
21
|
+
#
|
|
22
|
+
# * on_commit(request, response)
|
|
23
|
+
#
|
|
24
|
+
# The response has been committed. The application has returned, but the
|
|
25
|
+
# response has not been sent to the webserver yet. This method is always
|
|
26
|
+
# called with a request object and the response object. The response
|
|
27
|
+
# object is constructed from the rack triple that the application returned.
|
|
28
|
+
# Changes may still be made to the response object at this point.
|
|
29
|
+
#
|
|
30
|
+
# * on_send(request, response)
|
|
31
|
+
#
|
|
32
|
+
# The webserver has started iterating over the response body, or has called
|
|
33
|
+
# the streaming body, and presumably has started sending data over the
|
|
34
|
+
# wire. This method is always called with a request object and the response
|
|
35
|
+
# object. The response object is constructed from the rack triple that the
|
|
36
|
+
# application returned. Changes SHOULD NOT be made to the response object
|
|
37
|
+
# as the webserver has already started sending data. Any mutations will
|
|
38
|
+
# likely result in an exception.
|
|
39
|
+
#
|
|
40
|
+
# * on_finish(request, response)
|
|
41
|
+
#
|
|
42
|
+
# The webserver has closed the response, and all data has been written to
|
|
43
|
+
# the response socket. The request and response object should both be
|
|
44
|
+
# read-only at this point. The body MAY NOT be available on the response
|
|
45
|
+
# object as it may have been flushed to the socket.
|
|
46
|
+
#
|
|
47
|
+
# * on_error(request, response, error)
|
|
48
|
+
#
|
|
49
|
+
# An exception has occurred in the application or an `on_commit` event.
|
|
50
|
+
# This method will get the request, the response (if available) and the
|
|
51
|
+
# exception that was raised.
|
|
52
|
+
#
|
|
53
|
+
# ## Order
|
|
54
|
+
#
|
|
55
|
+
# `on_start` is called on the handlers in the order that they were passed to
|
|
56
|
+
# the constructor. `on_commit`, on_send`, `on_finish`, and `on_error` are
|
|
57
|
+
# called in the reverse order. `on_finish` handlers are called inside an
|
|
58
|
+
# `ensure` block, so they are guaranteed to be called even if something
|
|
59
|
+
# raises an exception. If something raises an exception in a `on_finish`
|
|
60
|
+
# method, then nothing is guaranteed.
|
|
61
|
+
|
|
62
|
+
class Events
|
|
63
|
+
module Abstract
|
|
64
|
+
def on_start(req, res)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def on_commit(req, res)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def on_send(req, res)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def on_finish(req, res)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def on_error(req, res, e)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
class EventedBodyProxy < Rack::BodyProxy # :nodoc:
|
|
81
|
+
attr_reader :request, :response
|
|
82
|
+
|
|
83
|
+
def initialize(body, request, response, handlers, &block)
|
|
84
|
+
super(body, &block)
|
|
85
|
+
@request = request
|
|
86
|
+
@response = response
|
|
87
|
+
@handlers = handlers
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def each
|
|
91
|
+
@handlers.reverse_each { |handler| handler.on_send request, response }
|
|
92
|
+
super
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def call(stream)
|
|
96
|
+
@handlers.reverse_each { |handler| handler.on_send request, response }
|
|
97
|
+
super
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def respond_to?(method_name, include_all = false)
|
|
101
|
+
case method_name
|
|
102
|
+
when :each, :call
|
|
103
|
+
@body.respond_to?(method_name, include_all)
|
|
104
|
+
else
|
|
105
|
+
super
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
class BufferedResponse < Rack::Response::Raw # :nodoc:
|
|
111
|
+
attr_reader :body
|
|
112
|
+
|
|
113
|
+
def initialize(status, headers, body)
|
|
114
|
+
super(status, headers)
|
|
115
|
+
@body = body
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def to_a; [status, headers, body]; end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def initialize(app, handlers)
|
|
122
|
+
@app = app
|
|
123
|
+
@handlers = handlers
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def call(env)
|
|
127
|
+
request = make_request env
|
|
128
|
+
on_start request, nil
|
|
129
|
+
|
|
130
|
+
begin
|
|
131
|
+
status, headers, body = @app.call request.env
|
|
132
|
+
response = make_response status, headers, body
|
|
133
|
+
on_commit request, response
|
|
134
|
+
rescue StandardError => e
|
|
135
|
+
on_error request, response, e
|
|
136
|
+
on_finish request, response
|
|
137
|
+
raise
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
body = EventedBodyProxy.new(body, request, response, @handlers) do
|
|
141
|
+
on_finish request, response
|
|
142
|
+
end
|
|
143
|
+
[response.status, response.headers, body]
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
private
|
|
147
|
+
|
|
148
|
+
def on_error(request, response, e)
|
|
149
|
+
@handlers.reverse_each { |handler| handler.on_error request, response, e }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def on_commit(request, response)
|
|
153
|
+
@handlers.reverse_each { |handler| handler.on_commit request, response }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def on_start(request, response)
|
|
157
|
+
@handlers.each { |handler| handler.on_start request, nil }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def on_finish(request, response)
|
|
161
|
+
@handlers.reverse_each { |handler| handler.on_finish request, response }
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def make_request(env)
|
|
165
|
+
Rack::Request.new env
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def make_response(status, headers, body)
|
|
169
|
+
BufferedResponse.new status, headers, body
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|