homura-runtime 0.3.3 → 0.3.5
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 +4 -4
- data/CHANGELOG.md +24 -0
- data/docs/ARCHITECTURE.md +2 -2
- data/lib/homura/runtime/ai.rb +31 -8
- data/lib/homura/runtime/async_registry.rb +18 -0
- data/lib/homura/runtime/durable_object.rb +43 -1
- data/lib/homura/runtime/queue.rb +12 -17
- data/lib/homura/runtime/version.rb +1 -1
- data/lib/homura/runtime.rb +103 -50
- data/vendor/rack/auth/abstract/handler.rb +41 -0
- data/vendor/rack/auth/abstract/request.rb +51 -0
- data/vendor/rack/auth/basic.rb +58 -0
- data/vendor/rack/bad_request.rb +8 -0
- data/vendor/rack/body_proxy.rb +63 -0
- data/vendor/rack/builder.rb +315 -0
- data/vendor/rack/cascade.rb +67 -0
- data/vendor/rack/common_logger.rb +94 -0
- data/vendor/rack/conditional_get.rb +87 -0
- data/vendor/rack/config.rb +22 -0
- data/vendor/rack/constants.rb +68 -0
- data/vendor/rack/content_length.rb +34 -0
- data/vendor/rack/content_type.rb +33 -0
- data/vendor/rack/deflater.rb +159 -0
- data/vendor/rack/directory.rb +210 -0
- data/vendor/rack/etag.rb +71 -0
- data/vendor/rack/events.rb +172 -0
- data/vendor/rack/files.rb +224 -0
- data/vendor/rack/head.rb +25 -0
- data/vendor/rack/headers.rb +238 -0
- data/vendor/rack/lint.rb +1000 -0
- data/vendor/rack/lock.rb +29 -0
- data/vendor/rack/media_type.rb +42 -0
- data/vendor/rack/method_override.rb +56 -0
- data/vendor/rack/mime.rb +694 -0
- data/vendor/rack/mock.rb +3 -0
- data/vendor/rack/mock_request.rb +161 -0
- data/vendor/rack/mock_response.rb +147 -0
- data/vendor/rack/multipart/generator.rb +99 -0
- data/vendor/rack/multipart/parser.rb +586 -0
- data/vendor/rack/multipart/uploaded_file.rb +82 -0
- data/vendor/rack/multipart.rb +77 -0
- data/vendor/rack/null_logger.rb +48 -0
- data/vendor/rack/protection/authenticity_token.rb +256 -0
- data/vendor/rack/protection/base.rb +140 -0
- data/vendor/rack/protection/content_security_policy.rb +80 -0
- data/vendor/rack/protection/cookie_tossing.rb +77 -0
- data/vendor/rack/protection/escaped_params.rb +93 -0
- data/vendor/rack/protection/form_token.rb +25 -0
- data/vendor/rack/protection/frame_options.rb +39 -0
- data/vendor/rack/protection/http_origin.rb +43 -0
- data/vendor/rack/protection/ip_spoofing.rb +27 -0
- data/vendor/rack/protection/json_csrf.rb +60 -0
- data/vendor/rack/protection/path_traversal.rb +45 -0
- data/vendor/rack/protection/referrer_policy.rb +27 -0
- data/vendor/rack/protection/remote_referrer.rb +22 -0
- data/vendor/rack/protection/remote_token.rb +24 -0
- data/vendor/rack/protection/session_hijacking.rb +37 -0
- data/vendor/rack/protection/strict_transport.rb +41 -0
- data/vendor/rack/protection/version.rb +7 -0
- data/vendor/rack/protection/xss_header.rb +27 -0
- data/vendor/rack/protection.rb +58 -0
- data/vendor/rack/query_parser.rb +261 -0
- data/vendor/rack/recursive.rb +66 -0
- data/vendor/rack/reloader.rb +112 -0
- data/vendor/rack/request.rb +818 -0
- data/vendor/rack/response.rb +403 -0
- data/vendor/rack/rewindable_input.rb +116 -0
- data/vendor/rack/runtime.rb +35 -0
- data/vendor/rack/sendfile.rb +197 -0
- data/vendor/rack/session/abstract/id.rb +533 -0
- data/vendor/rack/session/constants.rb +13 -0
- data/vendor/rack/session/cookie.rb +292 -0
- data/vendor/rack/session/encryptor.rb +415 -0
- data/vendor/rack/session/pool.rb +76 -0
- data/vendor/rack/session/version.rb +10 -0
- data/vendor/rack/session.rb +12 -0
- data/vendor/rack/show_exceptions.rb +433 -0
- data/vendor/rack/show_status.rb +121 -0
- data/vendor/rack/static.rb +188 -0
- data/vendor/rack/tempfile_reaper.rb +44 -0
- data/vendor/rack/urlmap.rb +99 -0
- data/vendor/rack/utils.rb +631 -0
- data/vendor/rack/version.rb +17 -0
- data/vendor/rack.rb +66 -0
- metadata +76 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rack
|
|
4
|
+
# Request env keys
|
|
5
|
+
HTTP_HOST = 'HTTP_HOST'
|
|
6
|
+
HTTP_PORT = 'HTTP_PORT'
|
|
7
|
+
HTTPS = 'HTTPS'
|
|
8
|
+
PATH_INFO = 'PATH_INFO'
|
|
9
|
+
REQUEST_METHOD = 'REQUEST_METHOD'
|
|
10
|
+
REQUEST_PATH = 'REQUEST_PATH'
|
|
11
|
+
SCRIPT_NAME = 'SCRIPT_NAME'
|
|
12
|
+
QUERY_STRING = 'QUERY_STRING'
|
|
13
|
+
SERVER_PROTOCOL = 'SERVER_PROTOCOL'
|
|
14
|
+
SERVER_NAME = 'SERVER_NAME'
|
|
15
|
+
SERVER_PORT = 'SERVER_PORT'
|
|
16
|
+
HTTP_COOKIE = 'HTTP_COOKIE'
|
|
17
|
+
|
|
18
|
+
# Response Header Keys
|
|
19
|
+
CACHE_CONTROL = 'cache-control'
|
|
20
|
+
CONTENT_LENGTH = 'content-length'
|
|
21
|
+
CONTENT_TYPE = 'content-type'
|
|
22
|
+
ETAG = 'etag'
|
|
23
|
+
EXPIRES = 'expires'
|
|
24
|
+
SET_COOKIE = 'set-cookie'
|
|
25
|
+
TRANSFER_ENCODING = 'transfer-encoding'
|
|
26
|
+
|
|
27
|
+
# HTTP method verbs
|
|
28
|
+
GET = 'GET'
|
|
29
|
+
POST = 'POST'
|
|
30
|
+
PUT = 'PUT'
|
|
31
|
+
PATCH = 'PATCH'
|
|
32
|
+
DELETE = 'DELETE'
|
|
33
|
+
HEAD = 'HEAD'
|
|
34
|
+
OPTIONS = 'OPTIONS'
|
|
35
|
+
CONNECT = 'CONNECT'
|
|
36
|
+
LINK = 'LINK'
|
|
37
|
+
UNLINK = 'UNLINK'
|
|
38
|
+
TRACE = 'TRACE'
|
|
39
|
+
|
|
40
|
+
# Rack environment variables
|
|
41
|
+
RACK_VERSION = 'rack.version'
|
|
42
|
+
RACK_TEMPFILES = 'rack.tempfiles'
|
|
43
|
+
RACK_EARLY_HINTS = 'rack.early_hints'
|
|
44
|
+
RACK_ERRORS = 'rack.errors'
|
|
45
|
+
RACK_LOGGER = 'rack.logger'
|
|
46
|
+
RACK_INPUT = 'rack.input'
|
|
47
|
+
RACK_SESSION = 'rack.session'
|
|
48
|
+
RACK_SESSION_OPTIONS = 'rack.session.options'
|
|
49
|
+
RACK_SHOWSTATUS_DETAIL = 'rack.showstatus.detail'
|
|
50
|
+
RACK_URL_SCHEME = 'rack.url_scheme'
|
|
51
|
+
RACK_HIJACK = 'rack.hijack'
|
|
52
|
+
RACK_IS_HIJACK = 'rack.hijack?'
|
|
53
|
+
RACK_RECURSIVE_INCLUDE = 'rack.recursive.include'
|
|
54
|
+
RACK_MULTIPART_BUFFER_SIZE = 'rack.multipart.buffer_size'
|
|
55
|
+
RACK_MULTIPART_TEMPFILE_FACTORY = 'rack.multipart.tempfile_factory'
|
|
56
|
+
RACK_RESPONSE_FINISHED = 'rack.response_finished'
|
|
57
|
+
RACK_PROTOCOL = 'rack.protocol'
|
|
58
|
+
RACK_REQUEST_FORM_INPUT = 'rack.request.form_input'
|
|
59
|
+
RACK_REQUEST_FORM_HASH = 'rack.request.form_hash'
|
|
60
|
+
RACK_REQUEST_FORM_PAIRS = 'rack.request.form_pairs'
|
|
61
|
+
RACK_REQUEST_FORM_VARS = 'rack.request.form_vars'
|
|
62
|
+
RACK_REQUEST_FORM_ERROR = 'rack.request.form_error'
|
|
63
|
+
RACK_REQUEST_COOKIE_HASH = 'rack.request.cookie_hash'
|
|
64
|
+
RACK_REQUEST_COOKIE_STRING = 'rack.request.cookie_string'
|
|
65
|
+
RACK_REQUEST_QUERY_HASH = 'rack.request.query_hash'
|
|
66
|
+
RACK_REQUEST_QUERY_STRING = 'rack.request.query_string'
|
|
67
|
+
RACK_METHODOVERRIDE_ORIGINAL_METHOD = 'rack.methodoverride.original_method'
|
|
68
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'constants'
|
|
4
|
+
require_relative 'utils'
|
|
5
|
+
|
|
6
|
+
module Rack
|
|
7
|
+
|
|
8
|
+
# Sets the content-length header on responses that do not specify
|
|
9
|
+
# a content-length or transfer-encoding header. Note that this
|
|
10
|
+
# does not fix responses that have an invalid content-length
|
|
11
|
+
# header specified.
|
|
12
|
+
class ContentLength
|
|
13
|
+
include Rack::Utils
|
|
14
|
+
|
|
15
|
+
def initialize(app)
|
|
16
|
+
@app = app
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call(env)
|
|
20
|
+
status, headers, body = response = @app.call(env)
|
|
21
|
+
|
|
22
|
+
if !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
|
|
23
|
+
!headers[CONTENT_LENGTH] &&
|
|
24
|
+
!headers[TRANSFER_ENCODING] &&
|
|
25
|
+
body.respond_to?(:to_ary)
|
|
26
|
+
|
|
27
|
+
response[2] = body = body.to_ary
|
|
28
|
+
headers[CONTENT_LENGTH] = body.sum(&:bytesize).to_s
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
response
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'constants'
|
|
4
|
+
require_relative 'utils'
|
|
5
|
+
|
|
6
|
+
module Rack
|
|
7
|
+
|
|
8
|
+
# Sets the content-type header on responses which don't have one.
|
|
9
|
+
#
|
|
10
|
+
# Builder Usage:
|
|
11
|
+
# use Rack::ContentType, "text/plain"
|
|
12
|
+
#
|
|
13
|
+
# When no content type argument is provided, "text/html" is the
|
|
14
|
+
# default.
|
|
15
|
+
class ContentType
|
|
16
|
+
include Rack::Utils
|
|
17
|
+
|
|
18
|
+
def initialize(app, content_type = "text/html")
|
|
19
|
+
@app = app
|
|
20
|
+
@content_type = content_type
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def call(env)
|
|
24
|
+
status, headers, _ = response = @app.call(env)
|
|
25
|
+
|
|
26
|
+
unless STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i)
|
|
27
|
+
headers[CONTENT_TYPE] ||= @content_type
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
response
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
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
|
+
|
|
30
|
+
GZIP_MTIME = RUBY_VERSION >= "2.7" ? 0 : 1
|
|
31
|
+
|
|
32
|
+
# Creates Rack::Deflater middleware. Options:
|
|
33
|
+
#
|
|
34
|
+
# :if :: a lambda enabling / disabling deflation based on returned boolean value
|
|
35
|
+
# (e.g <tt>use Rack::Deflater, :if => lambda { |*, body| sum=0; body.each { |i| sum += i.length }; sum > 512 }</tt>).
|
|
36
|
+
# However, be aware that calling `body.each` inside the block will break cases where `body.each` is not idempotent,
|
|
37
|
+
# such as when it is an +IO+ instance.
|
|
38
|
+
# :include :: a list of content types that should be compressed. By default, all content types are compressed.
|
|
39
|
+
# :sync :: determines if the stream is going to be flushed after every chunk. Flushing after every chunk reduces
|
|
40
|
+
# latency for time-sensitive streaming applications, but hurts compression and throughput.
|
|
41
|
+
# Defaults to +true+.
|
|
42
|
+
def initialize(app, options = {})
|
|
43
|
+
@app = app
|
|
44
|
+
@condition = options[:if]
|
|
45
|
+
@compressible_types = options[:include]
|
|
46
|
+
@sync = options.fetch(:sync, true)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def call(env)
|
|
50
|
+
status, headers, body = response = @app.call(env)
|
|
51
|
+
|
|
52
|
+
unless should_deflate?(env, status, headers, body)
|
|
53
|
+
return response
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
request = Request.new(env)
|
|
57
|
+
|
|
58
|
+
encoding = Utils.select_best_encoding(%w(gzip identity),
|
|
59
|
+
request.accept_encoding)
|
|
60
|
+
|
|
61
|
+
# Set the Vary HTTP header.
|
|
62
|
+
vary = headers["vary"].to_s.split(",").map(&:strip)
|
|
63
|
+
unless vary.include?("*") || vary.any?{|v| v.downcase == 'accept-encoding'}
|
|
64
|
+
headers["vary"] = vary.push("Accept-Encoding").join(",")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
case encoding
|
|
68
|
+
when "gzip"
|
|
69
|
+
headers['content-encoding'] = "gzip"
|
|
70
|
+
headers.delete(CONTENT_LENGTH)
|
|
71
|
+
response[2] = GzipStream.new(body, GZIP_MTIME, @sync)
|
|
72
|
+
response
|
|
73
|
+
when "identity"
|
|
74
|
+
response
|
|
75
|
+
else # when nil
|
|
76
|
+
# Only possible encoding values here are 'gzip', 'identity', and nil
|
|
77
|
+
message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
|
|
78
|
+
bp = Rack::BodyProxy.new([message]) { body.close if body.respond_to?(:close) }
|
|
79
|
+
[406, { CONTENT_TYPE => "text/plain", CONTENT_LENGTH => message.length.to_s }, bp]
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Body class used for gzip encoded responses.
|
|
84
|
+
class GzipStream
|
|
85
|
+
|
|
86
|
+
BUFFER_LENGTH = 128 * 1_024
|
|
87
|
+
|
|
88
|
+
# Initialize the gzip stream. Arguments:
|
|
89
|
+
# body :: Response body to compress with gzip
|
|
90
|
+
# mtime :: The modification time of the body, used to set the
|
|
91
|
+
# modification time in the gzip header.
|
|
92
|
+
# sync :: Whether to flush each gzip chunk as soon as it is ready.
|
|
93
|
+
def initialize(body, mtime, sync)
|
|
94
|
+
@body = body
|
|
95
|
+
@mtime = mtime
|
|
96
|
+
@sync = sync
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Yield gzip compressed strings to the given block.
|
|
100
|
+
def each(&block)
|
|
101
|
+
@writer = block
|
|
102
|
+
gzip = ::Zlib::GzipWriter.new(self)
|
|
103
|
+
gzip.mtime = @mtime if @mtime
|
|
104
|
+
# @body.each is equivalent to @body.gets (slow)
|
|
105
|
+
if @body.is_a? ::File # XXX: Should probably be ::IO
|
|
106
|
+
while part = @body.read(BUFFER_LENGTH)
|
|
107
|
+
gzip.write(part)
|
|
108
|
+
gzip.flush if @sync
|
|
109
|
+
end
|
|
110
|
+
else
|
|
111
|
+
@body.each { |part|
|
|
112
|
+
# Skip empty strings, as they would result in no output,
|
|
113
|
+
# and flushing empty parts would raise Zlib::BufError.
|
|
114
|
+
next if part.empty?
|
|
115
|
+
gzip.write(part)
|
|
116
|
+
gzip.flush if @sync
|
|
117
|
+
}
|
|
118
|
+
end
|
|
119
|
+
ensure
|
|
120
|
+
gzip.finish
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Call the block passed to #each with the gzipped data.
|
|
124
|
+
def write(data)
|
|
125
|
+
@writer.call(data)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Close the original body if possible.
|
|
129
|
+
def close
|
|
130
|
+
@body.close if @body.respond_to?(:close)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
private
|
|
135
|
+
|
|
136
|
+
# Whether the body should be compressed.
|
|
137
|
+
def should_deflate?(env, status, headers, body)
|
|
138
|
+
# Skip compressing empty entity body responses and responses with
|
|
139
|
+
# no-transform set.
|
|
140
|
+
if Utils::STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) ||
|
|
141
|
+
/\bno-transform\b/.match?(headers[CACHE_CONTROL].to_s) ||
|
|
142
|
+
headers['content-encoding']&.!~(/\bidentity\b/)
|
|
143
|
+
return false
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Skip if @compressible_types are given and does not include request's content type
|
|
147
|
+
return false if @compressible_types && !(headers.has_key?(CONTENT_TYPE) && @compressible_types.include?(headers[CONTENT_TYPE][/[^;]*/]))
|
|
148
|
+
|
|
149
|
+
# Skip if @condition lambda is given and evaluates to false
|
|
150
|
+
return false if @condition && !@condition.call(env, status, headers, body)
|
|
151
|
+
|
|
152
|
+
# No point in compressing empty body, also handles usage with
|
|
153
|
+
# Rack::Sendfile.
|
|
154
|
+
return false if headers[CONTENT_LENGTH] == '0'
|
|
155
|
+
|
|
156
|
+
true
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,210 @@
|
|
|
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
|
+
# Be aware that just like the default behavior of most webservers, Rack::Directory
|
|
20
|
+
# will follow symbolic links encountered under the root. If a symlink points to
|
|
21
|
+
# a location outside of the root, that target will still be served as part of
|
|
22
|
+
# the response.
|
|
23
|
+
|
|
24
|
+
class Directory
|
|
25
|
+
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"
|
|
26
|
+
DIR_PAGE_HEADER = <<-PAGE
|
|
27
|
+
<html><head>
|
|
28
|
+
<title>%s</title>
|
|
29
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
30
|
+
<style type='text/css'>
|
|
31
|
+
table { width:100%%; }
|
|
32
|
+
.name { text-align:left; }
|
|
33
|
+
.size, .mtime { text-align:right; }
|
|
34
|
+
.type { width:11em; }
|
|
35
|
+
.mtime { width:15em; }
|
|
36
|
+
</style>
|
|
37
|
+
</head><body>
|
|
38
|
+
<h1>%s</h1>
|
|
39
|
+
<hr />
|
|
40
|
+
<table>
|
|
41
|
+
<tr>
|
|
42
|
+
<th class='name'>Name</th>
|
|
43
|
+
<th class='size'>Size</th>
|
|
44
|
+
<th class='type'>Type</th>
|
|
45
|
+
<th class='mtime'>Last Modified</th>
|
|
46
|
+
</tr>
|
|
47
|
+
PAGE
|
|
48
|
+
DIR_PAGE_FOOTER = <<-PAGE
|
|
49
|
+
</table>
|
|
50
|
+
<hr />
|
|
51
|
+
</body></html>
|
|
52
|
+
PAGE
|
|
53
|
+
|
|
54
|
+
# Body class for directory entries, showing an index page with links
|
|
55
|
+
# to each file.
|
|
56
|
+
class DirectoryBody < Struct.new(:root, :path, :files)
|
|
57
|
+
# Yield strings for each part of the directory entry
|
|
58
|
+
def each
|
|
59
|
+
show_path = Utils.escape_html(path.sub(/^#{root}/, ''))
|
|
60
|
+
yield(DIR_PAGE_HEADER % [ show_path, show_path ])
|
|
61
|
+
|
|
62
|
+
unless path.chomp('/') == root
|
|
63
|
+
yield(DIR_FILE % DIR_FILE_escape(files.call('..')))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
Dir.foreach(path) do |basename|
|
|
67
|
+
next if basename.start_with?('.')
|
|
68
|
+
next unless f = files.call(basename)
|
|
69
|
+
yield(DIR_FILE % DIR_FILE_escape(f))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
yield(DIR_PAGE_FOOTER)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
# Escape each element in the array of html strings.
|
|
78
|
+
def DIR_FILE_escape(htmls)
|
|
79
|
+
htmls.map { |e| Utils.escape_html(e) }
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# The root of the directory hierarchy. Only requests for files and
|
|
84
|
+
# directories inside of the root directory are supported.
|
|
85
|
+
attr_reader :root
|
|
86
|
+
|
|
87
|
+
# Set the root directory and application for serving files.
|
|
88
|
+
def initialize(root, app = nil)
|
|
89
|
+
@root = ::File.expand_path(root)
|
|
90
|
+
@app = app || Files.new(@root)
|
|
91
|
+
@head = Head.new(method(:get))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def call(env)
|
|
95
|
+
# strip body if this is a HEAD call
|
|
96
|
+
@head.call env
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Internals of request handling. Similar to call but does
|
|
100
|
+
# not remove body for HEAD requests.
|
|
101
|
+
def get(env)
|
|
102
|
+
script_name = env[SCRIPT_NAME]
|
|
103
|
+
path_info = Utils.unescape_path(env[PATH_INFO])
|
|
104
|
+
|
|
105
|
+
if client_error_response = check_bad_request(path_info) || check_forbidden(path_info)
|
|
106
|
+
client_error_response
|
|
107
|
+
else
|
|
108
|
+
path = ::File.join(@root, path_info)
|
|
109
|
+
list_path(env, path, path_info, script_name)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Rack response to use for requests with invalid paths, or nil if path is valid.
|
|
114
|
+
def check_bad_request(path_info)
|
|
115
|
+
return if Utils.valid_path?(path_info)
|
|
116
|
+
|
|
117
|
+
body = "Bad Request\n"
|
|
118
|
+
[400, { CONTENT_TYPE => "text/plain",
|
|
119
|
+
CONTENT_LENGTH => body.bytesize.to_s,
|
|
120
|
+
"x-cascade" => "pass" }, [body]]
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Rack response to use for requests with paths outside the root, or nil if path is inside the root.
|
|
124
|
+
def check_forbidden(path_info)
|
|
125
|
+
return unless path_info.include? ".."
|
|
126
|
+
return if ::File.expand_path(::File.join(@root, path_info)).start_with?(@root)
|
|
127
|
+
|
|
128
|
+
body = "Forbidden\n"
|
|
129
|
+
[403, { CONTENT_TYPE => "text/plain",
|
|
130
|
+
CONTENT_LENGTH => body.bytesize.to_s,
|
|
131
|
+
"x-cascade" => "pass" }, [body]]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Rack response to use for directories under the root.
|
|
135
|
+
def list_directory(path_info, path, script_name)
|
|
136
|
+
url_head = (script_name.split('/') + path_info.split('/')).map do |part|
|
|
137
|
+
Utils.escape_path part
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Globbing not safe as path could contain glob metacharacters
|
|
141
|
+
body = DirectoryBody.new(@root, path, ->(basename) do
|
|
142
|
+
stat = stat(::File.join(path, basename))
|
|
143
|
+
next unless stat
|
|
144
|
+
|
|
145
|
+
url = ::File.join(*url_head + [Utils.escape_path(basename)])
|
|
146
|
+
mtime = stat.mtime.httpdate
|
|
147
|
+
if stat.directory?
|
|
148
|
+
type = 'directory'
|
|
149
|
+
size = '-'
|
|
150
|
+
url << '/'
|
|
151
|
+
if basename == '..'
|
|
152
|
+
basename = 'Parent Directory'
|
|
153
|
+
else
|
|
154
|
+
basename << '/'
|
|
155
|
+
end
|
|
156
|
+
else
|
|
157
|
+
type = Mime.mime_type(::File.extname(basename))
|
|
158
|
+
size = filesize_format(stat.size)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
[ url, basename, size, type, mtime ]
|
|
162
|
+
end)
|
|
163
|
+
|
|
164
|
+
[ 200, { CONTENT_TYPE => 'text/html; charset=utf-8' }, body ]
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# File::Stat for the given path, but return nil for missing/bad entries.
|
|
168
|
+
def stat(path)
|
|
169
|
+
::File.stat(path)
|
|
170
|
+
rescue Errno::ENOENT, Errno::ELOOP
|
|
171
|
+
return nil
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Rack response to use for files and directories under the root.
|
|
175
|
+
# Unreadable and non-file, non-directory entries will get a 404 response.
|
|
176
|
+
def list_path(env, path, path_info, script_name)
|
|
177
|
+
if (stat = stat(path)) && stat.readable?
|
|
178
|
+
return @app.call(env) if stat.file?
|
|
179
|
+
return list_directory(path_info, path, script_name) if stat.directory?
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
entity_not_found(path_info)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Rack response to use for unreadable and non-file, non-directory entries.
|
|
186
|
+
def entity_not_found(path_info)
|
|
187
|
+
body = "Entity not found: #{path_info}\n"
|
|
188
|
+
[404, { CONTENT_TYPE => "text/plain",
|
|
189
|
+
CONTENT_LENGTH => body.bytesize.to_s,
|
|
190
|
+
"x-cascade" => "pass" }, [body]]
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Stolen from Ramaze
|
|
194
|
+
FILESIZE_FORMAT = [
|
|
195
|
+
['%.1fT', 1 << 40],
|
|
196
|
+
['%.1fG', 1 << 30],
|
|
197
|
+
['%.1fM', 1 << 20],
|
|
198
|
+
['%.1fK', 1 << 10],
|
|
199
|
+
]
|
|
200
|
+
|
|
201
|
+
# Provide human readable file sizes
|
|
202
|
+
def filesize_format(int)
|
|
203
|
+
FILESIZE_FORMAT.each do |format, size|
|
|
204
|
+
return format % (int.to_f / size) if int >= size
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
"#{int}B"
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
data/vendor/rack/etag.rb
ADDED
|
@@ -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
|