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.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/nano-sharp-hub.gemspec +12 -0
  3. data/rack-3.2.6/CHANGELOG.md +1346 -0
  4. data/rack-3.2.6/CONTRIBUTING.md +144 -0
  5. data/rack-3.2.6/MIT-LICENSE +20 -0
  6. data/rack-3.2.6/README.md +384 -0
  7. data/rack-3.2.6/SPEC.rdoc +258 -0
  8. data/rack-3.2.6/lib/rack/auth/abstract/handler.rb +41 -0
  9. data/rack-3.2.6/lib/rack/auth/abstract/request.rb +51 -0
  10. data/rack-3.2.6/lib/rack/auth/basic.rb +58 -0
  11. data/rack-3.2.6/lib/rack/bad_request.rb +8 -0
  12. data/rack-3.2.6/lib/rack/body_proxy.rb +63 -0
  13. data/rack-3.2.6/lib/rack/builder.rb +296 -0
  14. data/rack-3.2.6/lib/rack/cascade.rb +67 -0
  15. data/rack-3.2.6/lib/rack/common_logger.rb +89 -0
  16. data/rack-3.2.6/lib/rack/conditional_get.rb +87 -0
  17. data/rack-3.2.6/lib/rack/config.rb +22 -0
  18. data/rack-3.2.6/lib/rack/constants.rb +68 -0
  19. data/rack-3.2.6/lib/rack/content_length.rb +34 -0
  20. data/rack-3.2.6/lib/rack/content_type.rb +33 -0
  21. data/rack-3.2.6/lib/rack/deflater.rb +158 -0
  22. data/rack-3.2.6/lib/rack/directory.rb +208 -0
  23. data/rack-3.2.6/lib/rack/etag.rb +71 -0
  24. data/rack-3.2.6/lib/rack/events.rb +172 -0
  25. data/rack-3.2.6/lib/rack/files.rb +216 -0
  26. data/rack-3.2.6/lib/rack/head.rb +25 -0
  27. data/rack-3.2.6/lib/rack/headers.rb +238 -0
  28. data/rack-3.2.6/lib/rack/lint.rb +964 -0
  29. data/rack-3.2.6/lib/rack/lock.rb +29 -0
  30. data/rack-3.2.6/lib/rack/media_type.rb +52 -0
  31. data/rack-3.2.6/lib/rack/method_override.rb +56 -0
  32. data/rack-3.2.6/lib/rack/mime.rb +694 -0
  33. data/rack-3.2.6/lib/rack/mock.rb +3 -0
  34. data/rack-3.2.6/lib/rack/mock_request.rb +161 -0
  35. data/rack-3.2.6/lib/rack/mock_response.rb +156 -0
  36. data/rack-3.2.6/lib/rack/multipart/generator.rb +99 -0
  37. data/rack-3.2.6/lib/rack/multipart/parser.rb +621 -0
  38. data/rack-3.2.6/lib/rack/multipart/uploaded_file.rb +82 -0
  39. data/rack-3.2.6/lib/rack/multipart.rb +77 -0
  40. data/rack-3.2.6/lib/rack/null_logger.rb +48 -0
  41. data/rack-3.2.6/lib/rack/query_parser.rb +261 -0
  42. data/rack-3.2.6/lib/rack/recursive.rb +66 -0
  43. data/rack-3.2.6/lib/rack/reloader.rb +112 -0
  44. data/rack-3.2.6/lib/rack/request.rb +790 -0
  45. data/rack-3.2.6/lib/rack/response.rb +403 -0
  46. data/rack-3.2.6/lib/rack/rewindable_input.rb +116 -0
  47. data/rack-3.2.6/lib/rack/runtime.rb +35 -0
  48. data/rack-3.2.6/lib/rack/sendfile.rb +197 -0
  49. data/rack-3.2.6/lib/rack/show_exceptions.rb +409 -0
  50. data/rack-3.2.6/lib/rack/show_status.rb +121 -0
  51. data/rack-3.2.6/lib/rack/static.rb +192 -0
  52. data/rack-3.2.6/lib/rack/tempfile_reaper.rb +33 -0
  53. data/rack-3.2.6/lib/rack/urlmap.rb +99 -0
  54. data/rack-3.2.6/lib/rack/utils.rb +714 -0
  55. data/rack-3.2.6/lib/rack/version.rb +17 -0
  56. data/rack-3.2.6/lib/rack.rb +64 -0
  57. metadata +96 -0
@@ -0,0 +1,296 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'urlmap'
4
+
5
+ module Rack; end
6
+ Rack::BUILDER_TOPLEVEL_BINDING = ->(builder){builder.instance_eval{binding}}
7
+
8
+ module Rack
9
+ # Rack::Builder provides a domain-specific language (DSL) to construct Rack
10
+ # applications. It is primarily used to parse +config.ru+ files which
11
+ # instantiate several middleware and a final application which are hosted
12
+ # by a Rack-compatible web server.
13
+ #
14
+ # Example:
15
+ #
16
+ # app = Rack::Builder.new do
17
+ # use Rack::CommonLogger
18
+ # map "/ok" do
19
+ # run lambda { |env| [200, {'content-type' => 'text/plain'}, ['OK']] }
20
+ # end
21
+ # end
22
+ #
23
+ # run app
24
+ #
25
+ # Or
26
+ #
27
+ # app = Rack::Builder.app do
28
+ # use Rack::CommonLogger
29
+ # run lambda { |env| [200, {'content-type' => 'text/plain'}, ['OK']] }
30
+ # end
31
+ #
32
+ # run app
33
+ #
34
+ # +use+ adds middleware to the stack, +run+ dispatches to an application.
35
+ # You can use +map+ to construct a Rack::URLMap in a convenient way.
36
+ class Builder
37
+
38
+ # https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom
39
+ UTF_8_BOM = '\xef\xbb\xbf'
40
+
41
+ # Parse the given config file to get a Rack application.
42
+ #
43
+ # If the config file ends in +.ru+, it is treated as a
44
+ # rackup file and the contents will be treated as if
45
+ # specified inside a Rack::Builder block.
46
+ #
47
+ # If the config file does not end in +.ru+, it is
48
+ # required and Rack will use the basename of the file
49
+ # to guess which constant will be the Rack application to run.
50
+ #
51
+ # Examples:
52
+ #
53
+ # Rack::Builder.parse_file('config.ru')
54
+ # # Rack application built using Rack::Builder.new
55
+ #
56
+ # Rack::Builder.parse_file('app.rb')
57
+ # # requires app.rb, which can be anywhere in Ruby's
58
+ # # load path. After requiring, assumes App constant
59
+ # # is a Rack application
60
+ #
61
+ # Rack::Builder.parse_file('./my_app.rb')
62
+ # # requires ./my_app.rb, which should be in the
63
+ # # process's current directory. After requiring,
64
+ # # assumes MyApp constant is a Rack application
65
+ def self.parse_file(path, **options)
66
+ if path.end_with?('.ru')
67
+ return self.load_file(path, **options)
68
+ else
69
+ require path
70
+ return Object.const_get(::File.basename(path, '.rb').split('_').map(&:capitalize).join(''))
71
+ end
72
+ end
73
+
74
+ # Load the given file as a rackup file, treating the
75
+ # contents as if specified inside a Rack::Builder block.
76
+ #
77
+ # Ignores content in the file after +__END__+, so that
78
+ # use of +__END__+ will not result in a syntax error.
79
+ #
80
+ # Example config.ru file:
81
+ #
82
+ # $ cat config.ru
83
+ #
84
+ # use Rack::ContentLength
85
+ # require './app.rb'
86
+ # run App
87
+ def self.load_file(path, **options)
88
+ config = ::File.read(path)
89
+ config.slice!(/\A#{UTF_8_BOM}/) if config.encoding == Encoding::UTF_8
90
+
91
+ if config[/^#\\(.*)/]
92
+ fail "Parsing options from the first comment line is no longer supported: #{path}"
93
+ end
94
+
95
+ config.sub!(/^__END__\n.*\Z/m, '')
96
+
97
+ return new_from_string(config, path, **options)
98
+ end
99
+
100
+ # Evaluate the given +builder_script+ string in the context of
101
+ # a Rack::Builder block, returning a Rack application.
102
+ def self.new_from_string(builder_script, path = "(rackup)", **options)
103
+ builder = self.new(**options)
104
+
105
+ # We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.
106
+ # We cannot use instance_eval(String) as that would resolve constants differently.
107
+ binding = BUILDER_TOPLEVEL_BINDING.call(builder)
108
+ eval(builder_script, binding, path)
109
+
110
+ return builder.to_app
111
+ end
112
+
113
+ # Initialize a new Rack::Builder instance. +default_app+ specifies the
114
+ # default application if +run+ is not called later. If a block
115
+ # is given, it is evaluated in the context of the instance.
116
+ def initialize(default_app = nil, **options, &block)
117
+ @use = []
118
+ @map = nil
119
+ @run = default_app
120
+ @warmup = nil
121
+ @freeze_app = false
122
+ @options = options
123
+
124
+ instance_eval(&block) if block_given?
125
+ end
126
+
127
+ # Any options provided to the Rack::Builder instance at initialization.
128
+ # These options can be server-specific. Some general options are:
129
+ #
130
+ # * +:isolation+: One of +process+, +thread+ or +fiber+. The execution
131
+ # isolation model to use.
132
+ attr :options
133
+
134
+ # Create a new Rack::Builder instance and return the Rack application
135
+ # generated from it.
136
+ def self.app(default_app = nil, &block)
137
+ self.new(default_app, &block).to_app
138
+ end
139
+
140
+ # Specifies middleware to use in a stack.
141
+ #
142
+ # class Middleware
143
+ # def initialize(app)
144
+ # @app = app
145
+ # end
146
+ #
147
+ # def call(env)
148
+ # env["rack.some_header"] = "setting an example"
149
+ # @app.call(env)
150
+ # end
151
+ # end
152
+ #
153
+ # use Middleware
154
+ # run lambda { |env| [200, { "content-type" => "text/plain" }, ["OK"]] }
155
+ #
156
+ # All requests through to this application will first be processed by the middleware class.
157
+ # The +call+ method in this example sets an additional environment key which then can be
158
+ # referenced in the application if required.
159
+ def use(middleware, *args, &block)
160
+ if @map
161
+ mapping, @map = @map, nil
162
+ @use << proc { |app| generate_map(app, mapping) }
163
+ end
164
+ @use << proc { |app| middleware.new(app, *args, &block) }
165
+
166
+ nil
167
+ end
168
+ # :nocov:
169
+ ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
170
+ # :nocov:
171
+
172
+ # Takes a block or argument that is an object that responds to #call and
173
+ # returns a Rack response.
174
+ #
175
+ # You can use a block:
176
+ #
177
+ # run do |env|
178
+ # [200, { "content-type" => "text/plain" }, ["Hello World!"]]
179
+ # end
180
+ #
181
+ # You can also provide a lambda:
182
+ #
183
+ # run lambda { |env| [200, { "content-type" => "text/plain" }, ["OK"]] }
184
+ #
185
+ # You can also provide a class instance:
186
+ #
187
+ # class Heartbeat
188
+ # def call(env)
189
+ # [200, { "content-type" => "text/plain" }, ["OK"]]
190
+ # end
191
+ # end
192
+ #
193
+ # run Heartbeat.new
194
+ #
195
+ def run(app = nil, &block)
196
+ raise ArgumentError, "Both app and block given!" if app && block_given?
197
+
198
+ @run = app || block
199
+
200
+ nil
201
+ end
202
+
203
+ # Takes a lambda or block that is used to warm-up the application. This block is called
204
+ # before the Rack application is returned by to_app.
205
+ #
206
+ # warmup do |app|
207
+ # client = Rack::MockRequest.new(app)
208
+ # client.get('/')
209
+ # end
210
+ #
211
+ # use SomeMiddleware
212
+ # run MyApp
213
+ def warmup(prc = nil, &block)
214
+ @warmup = prc || block
215
+ end
216
+
217
+ # Creates a route within the application. Routes under the mapped path will be sent to
218
+ # the Rack application specified by run inside the block. Other requests will be sent to the
219
+ # default application specified by run outside the block.
220
+ #
221
+ # class App
222
+ # def call(env)
223
+ # [200, {'content-type' => 'text/plain'}, ["Hello World"]]
224
+ # end
225
+ # end
226
+ #
227
+ # class Heartbeat
228
+ # def call(env)
229
+ # [200, { "content-type" => "text/plain" }, ["OK"]]
230
+ # end
231
+ # end
232
+ #
233
+ # app = Rack::Builder.app do
234
+ # map '/heartbeat' do
235
+ # run Heartbeat.new
236
+ # end
237
+ # run App.new
238
+ # end
239
+ #
240
+ # run app
241
+ #
242
+ # The +use+ method can also be used inside the block to specify middleware to run under a specific path:
243
+ #
244
+ # app = Rack::Builder.app do
245
+ # map '/heartbeat' do
246
+ # use Middleware
247
+ # run Heartbeat.new
248
+ # end
249
+ # run App.new
250
+ # end
251
+ #
252
+ # This example includes a piece of middleware which will run before +/heartbeat+ requests hit +Heartbeat+.
253
+ #
254
+ # Note that providing a +path+ of +/+ will ignore any default application given in a +run+ statement
255
+ # outside the block.
256
+ def map(path, &block)
257
+ @map ||= {}
258
+ @map[path] = block
259
+
260
+ nil
261
+ end
262
+
263
+ # Freeze the app (set using run) and all middleware instances when building the application
264
+ # in to_app.
265
+ def freeze_app
266
+ @freeze_app = true
267
+ end
268
+
269
+ # Return the Rack application generated by this instance.
270
+ def to_app
271
+ app = @map ? generate_map(@run, @map) : @run
272
+ fail "missing run or map statement" unless app
273
+ app.freeze if @freeze_app
274
+ app = @use.reverse.inject(app) { |a, e| e[a].tap { |x| x.freeze if @freeze_app } }
275
+ @warmup.call(app) if @warmup
276
+ app
277
+ end
278
+
279
+ # Call the Rack application generated by this builder instance. Note that
280
+ # this rebuilds the Rack application and runs the warmup code (if any)
281
+ # every time it is called, so it should not be used if performance is important.
282
+ def call(env)
283
+ to_app.call(env)
284
+ end
285
+
286
+ private
287
+
288
+ # Generate a URLMap instance by generating new Rack applications for each
289
+ # map block in this instance.
290
+ def generate_map(default_app, mapping)
291
+ mapped = default_app ? { '/' => default_app } : {}
292
+ mapping.each { |r, b| mapped[r] = self.class.new(default_app, &b).to_app }
293
+ URLMap.new(mapped)
294
+ end
295
+ end
296
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'constants'
4
+
5
+ module Rack
6
+ # Rack::Cascade tries a request on several apps, and returns the
7
+ # first response that is not 404 or 405 (or in a list of configured
8
+ # status codes). If all applications tried return one of the configured
9
+ # status codes, return the last response.
10
+
11
+ class Cascade
12
+ # An array of applications to try in order.
13
+ attr_reader :apps
14
+
15
+ # Set the apps to send requests to, and what statuses result in
16
+ # cascading. Arguments:
17
+ #
18
+ # apps: An enumerable of rack applications.
19
+ # cascade_for: The statuses to use cascading for. If a response is received
20
+ # from an app, the next app is tried.
21
+ def initialize(apps, cascade_for = [404, 405])
22
+ @apps = []
23
+ apps.each { |app| add app }
24
+
25
+ @cascade_for = {}
26
+ [*cascade_for].each { |status| @cascade_for[status] = true }
27
+ end
28
+
29
+ # Call each app in order. If the responses uses a status that requires
30
+ # cascading, try the next app. If all responses require cascading,
31
+ # return the response from the last app.
32
+ def call(env)
33
+ return [404, { CONTENT_TYPE => "text/plain" }, []] if @apps.empty?
34
+ result = nil
35
+ last_body = nil
36
+
37
+ @apps.each do |app|
38
+ # The SPEC says that the body must be closed after it has been iterated
39
+ # by the server, or if it is replaced by a middleware action. Cascade
40
+ # replaces the body each time a cascade happens. It is assumed that nil
41
+ # does not respond to close, otherwise the previous application body
42
+ # will be closed. The final application body will not be closed, as it
43
+ # will be passed to the server as a result.
44
+ last_body.close if last_body.respond_to? :close
45
+
46
+ result = app.call(env)
47
+ return result unless @cascade_for.include?(result[0].to_i)
48
+ last_body = result[2]
49
+ end
50
+
51
+ result
52
+ end
53
+
54
+ # Append an app to the list of apps to cascade. This app will
55
+ # be tried last.
56
+ def add(app)
57
+ @apps << app
58
+ end
59
+
60
+ # Whether the given app is one of the apps to cascade to.
61
+ def include?(app)
62
+ @apps.include?(app)
63
+ end
64
+
65
+ alias_method :<<, :add
66
+ end
67
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'constants'
4
+ require_relative 'utils'
5
+ require_relative 'body_proxy'
6
+ require_relative 'request'
7
+
8
+ module Rack
9
+ # Rack::CommonLogger forwards every request to the given +app+, and
10
+ # logs a line in the
11
+ # {Apache common log format}[http://httpd.apache.org/docs/1.3/logs.html#common]
12
+ # to the configured logger.
13
+ class CommonLogger
14
+ # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
15
+ #
16
+ # lilith.local - - [07/Aug/2006 23:58:02 -0400] "GET / HTTP/1.1" 500 -
17
+ #
18
+ # %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
19
+ #
20
+ # The actual format is slightly different than the above due to the
21
+ # separation of SCRIPT_NAME and PATH_INFO, and because the elapsed
22
+ # time in seconds is included at the end.
23
+ FORMAT = %{%s - %s [%s] "%s %s%s%s %s" %d %s %0.4f }
24
+
25
+ # +logger+ can be any object that supports the +write+ or +<<+ methods,
26
+ # which includes the standard library Logger. These methods are called
27
+ # with a single string argument, the log message.
28
+ # If +logger+ is nil, CommonLogger will fall back <tt>env['rack.errors']</tt>.
29
+ def initialize(app, logger = nil)
30
+ @app = app
31
+ @logger = logger
32
+ end
33
+
34
+ # Log all requests in common_log format after a response has been
35
+ # returned. Note that if the app raises an exception, the request
36
+ # will not be logged, so if exception handling middleware are used,
37
+ # they should be loaded after this middleware. Additionally, because
38
+ # the logging happens after the request body has been fully sent, any
39
+ # exceptions raised during the sending of the response body will
40
+ # cause the request not to be logged.
41
+ def call(env)
42
+ began_at = Utils.clock_time
43
+ status, headers, body = response = @app.call(env)
44
+
45
+ response[2] = BodyProxy.new(body) { log(env, status, headers, began_at) }
46
+ response
47
+ end
48
+
49
+ private
50
+
51
+ # Log the request to the configured logger.
52
+ def log(env, status, response_headers, began_at)
53
+ request = Rack::Request.new(env)
54
+ length = extract_content_length(response_headers)
55
+
56
+ msg = sprintf(FORMAT,
57
+ request.ip || "-",
58
+ request.get_header("REMOTE_USER") || "-",
59
+ Time.now.strftime("%d/%b/%Y:%H:%M:%S %z"),
60
+ request.request_method,
61
+ request.script_name,
62
+ request.path_info,
63
+ request.query_string.empty? ? "" : "?#{request.query_string}",
64
+ request.get_header(SERVER_PROTOCOL),
65
+ status.to_s[0..3],
66
+ length,
67
+ Utils.clock_time - began_at)
68
+
69
+ msg.gsub!(/[^[:print:]]/) { |c| sprintf("\\x%x", c.ord) }
70
+ msg[-1] = "\n"
71
+
72
+ logger = @logger || request.get_header(RACK_ERRORS)
73
+ # Standard library logger doesn't support write but it supports << which actually
74
+ # calls to write on the log device without formatting
75
+ if logger.respond_to?(:write)
76
+ logger.write(msg)
77
+ else
78
+ logger << msg
79
+ end
80
+ end
81
+
82
+ # Attempt to determine the content length for the response to
83
+ # include it in the logged data.
84
+ def extract_content_length(headers)
85
+ value = headers[CONTENT_LENGTH]
86
+ !value || value.to_s == '0' ? '-' : value
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'constants'
4
+ require_relative 'utils'
5
+ require_relative 'body_proxy'
6
+
7
+ module Rack
8
+
9
+ # Middleware that enables conditional GET using if-none-match and
10
+ # if-modified-since. The application should set either or both of the
11
+ # last-modified or etag response headers according to RFC 2616. When
12
+ # either of the conditions is met, the response body is set to be zero
13
+ # length and the response status is set to 304 Not Modified.
14
+ #
15
+ # Applications that defer response body generation until the body's each
16
+ # message is received will avoid response body generation completely when
17
+ # a conditional GET matches.
18
+ #
19
+ # Adapted from Michael Klishin's Merb implementation:
20
+ # https://github.com/wycats/merb/blob/master/merb-core/lib/merb-core/rack/middleware/conditional_get.rb
21
+ class ConditionalGet
22
+ def initialize(app)
23
+ @app = app
24
+ end
25
+
26
+ # Return empty 304 response if the response has not been
27
+ # modified since the last request.
28
+ def call(env)
29
+ case env[REQUEST_METHOD]
30
+ when "GET", "HEAD"
31
+ status, headers, body = response = @app.call(env)
32
+
33
+ if status == 200 && fresh?(env, headers)
34
+ response[0] = 304
35
+ headers.delete(CONTENT_TYPE)
36
+ headers.delete(CONTENT_LENGTH)
37
+
38
+ # We are done with the body:
39
+ body.close if body.respond_to?(:close)
40
+ response[2] = []
41
+ end
42
+ response
43
+ else
44
+ @app.call(env)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ # Return whether the response has not been modified since the
51
+ # last request.
52
+ def fresh?(env, headers)
53
+ # if-none-match has priority over if-modified-since per RFC 7232
54
+ if none_match = env['HTTP_IF_NONE_MATCH']
55
+ etag_matches?(none_match, headers)
56
+ elsif (modified_since = env['HTTP_IF_MODIFIED_SINCE']) && (modified_since = to_rfc2822(modified_since))
57
+ modified_since?(modified_since, headers)
58
+ end
59
+ end
60
+
61
+ # Whether the etag response header matches the if-none-match request header.
62
+ # If so, the request has not been modified.
63
+ def etag_matches?(none_match, headers)
64
+ headers[ETAG] == none_match
65
+ end
66
+
67
+ # Whether the last-modified response header matches the if-modified-since
68
+ # request header. If so, the request has not been modified.
69
+ def modified_since?(modified_since, headers)
70
+ last_modified = to_rfc2822(headers['last-modified']) and
71
+ modified_since >= last_modified
72
+ end
73
+
74
+ # Return a Time object for the given string (which should be in RFC2822
75
+ # format), or nil if the string cannot be parsed.
76
+ def to_rfc2822(since)
77
+ # shortest possible valid date is the obsolete: 1 Nov 97 09:55 A
78
+ # anything shorter is invalid, this avoids exceptions for common cases
79
+ # most common being the empty string
80
+ if since && since.length >= 16
81
+ # NOTE: there is no trivial way to write this in a non exception way
82
+ # _rfc2822 returns a hash but is not that usable
83
+ Time.rfc2822(since) rescue nil
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rack
4
+ # Rack::Config modifies the environment using the block given during
5
+ # initialization.
6
+ #
7
+ # Example:
8
+ # use Rack::Config do |env|
9
+ # env['my-key'] = 'some-value'
10
+ # end
11
+ class Config
12
+ def initialize(app, &block)
13
+ @app = app
14
+ @block = block
15
+ end
16
+
17
+ def call(env)
18
+ @block.call(env)
19
+ @app.call(env)
20
+ end
21
+ end
22
+ end
@@ -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