rack 2.1.0 → 3.1.0
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 +377 -16
- data/CONTRIBUTING.md +144 -0
- data/MIT-LICENSE +1 -1
- data/README.md +328 -0
- data/SPEC.rdoc +365 -0
- data/lib/rack/auth/abstract/handler.rb +3 -1
- data/lib/rack/auth/abstract/request.rb +2 -2
- data/lib/rack/auth/basic.rb +4 -7
- data/lib/rack/bad_request.rb +8 -0
- data/lib/rack/body_proxy.rb +34 -12
- data/lib/rack/builder.rb +162 -59
- data/lib/rack/cascade.rb +24 -10
- data/lib/rack/common_logger.rb +43 -28
- data/lib/rack/conditional_get.rb +30 -25
- data/lib/rack/constants.rb +66 -0
- data/lib/rack/content_length.rb +10 -16
- data/lib/rack/content_type.rb +9 -7
- data/lib/rack/deflater.rb +78 -50
- data/lib/rack/directory.rb +86 -63
- data/lib/rack/etag.rb +14 -22
- data/lib/rack/events.rb +18 -17
- data/lib/rack/files.rb +99 -61
- data/lib/rack/head.rb +8 -9
- data/lib/rack/headers.rb +238 -0
- data/lib/rack/lint.rb +868 -642
- data/lib/rack/lock.rb +2 -6
- data/lib/rack/logger.rb +3 -0
- data/lib/rack/media_type.rb +9 -4
- data/lib/rack/method_override.rb +6 -2
- data/lib/rack/mime.rb +14 -5
- data/lib/rack/mock.rb +1 -253
- data/lib/rack/mock_request.rb +171 -0
- data/lib/rack/mock_response.rb +124 -0
- data/lib/rack/multipart/generator.rb +15 -8
- data/lib/rack/multipart/parser.rb +238 -107
- data/lib/rack/multipart/uploaded_file.rb +17 -7
- data/lib/rack/multipart.rb +54 -42
- data/lib/rack/null_logger.rb +9 -0
- data/lib/rack/query_parser.rb +87 -105
- data/lib/rack/recursive.rb +3 -1
- data/lib/rack/reloader.rb +0 -4
- data/lib/rack/request.rb +366 -135
- data/lib/rack/response.rb +186 -68
- data/lib/rack/rewindable_input.rb +24 -6
- data/lib/rack/runtime.rb +8 -7
- data/lib/rack/sendfile.rb +29 -27
- data/lib/rack/show_exceptions.rb +27 -12
- data/lib/rack/show_status.rb +21 -13
- data/lib/rack/static.rb +19 -12
- data/lib/rack/tempfile_reaper.rb +14 -5
- data/lib/rack/urlmap.rb +5 -6
- data/lib/rack/utils.rb +274 -260
- data/lib/rack/version.rb +21 -0
- data/lib/rack.rb +18 -103
- metadata +25 -52
- data/README.rdoc +0 -262
- data/Rakefile +0 -123
- data/SPEC +0 -263
- data/bin/rackup +0 -5
- data/contrib/rack.png +0 -0
- data/contrib/rack.svg +0 -150
- data/contrib/rack_logo.svg +0 -164
- data/contrib/rdoc.css +0 -412
- data/example/lobster.ru +0 -6
- data/example/protectedlobster.rb +0 -16
- data/example/protectedlobster.ru +0 -10
- data/lib/rack/auth/digest/md5.rb +0 -131
- data/lib/rack/auth/digest/nonce.rb +0 -54
- data/lib/rack/auth/digest/params.rb +0 -54
- data/lib/rack/auth/digest/request.rb +0 -43
- data/lib/rack/chunked.rb +0 -92
- data/lib/rack/core_ext/regexp.rb +0 -14
- data/lib/rack/file.rb +0 -8
- data/lib/rack/handler/cgi.rb +0 -62
- data/lib/rack/handler/fastcgi.rb +0 -102
- data/lib/rack/handler/lsws.rb +0 -63
- data/lib/rack/handler/scgi.rb +0 -73
- data/lib/rack/handler/thin.rb +0 -38
- data/lib/rack/handler/webrick.rb +0 -122
- data/lib/rack/handler.rb +0 -104
- data/lib/rack/lobster.rb +0 -72
- data/lib/rack/server.rb +0 -467
- data/lib/rack/session/abstract/id.rb +0 -528
- data/lib/rack/session/cookie.rb +0 -205
- data/lib/rack/session/memcache.rb +0 -10
- data/lib/rack/session/pool.rb +0 -85
- data/rack.gemspec +0 -44
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative '../../constants'
|
4
|
+
|
3
5
|
module Rack
|
4
6
|
module Auth
|
5
7
|
# Rack::Auth::AbstractHandler implements common authentication functionality.
|
@@ -21,7 +23,7 @@ module Rack
|
|
21
23
|
return [ 401,
|
22
24
|
{ CONTENT_TYPE => 'text/plain',
|
23
25
|
CONTENT_LENGTH => '0',
|
24
|
-
'
|
26
|
+
'www-authenticate' => www_authenticate.to_s },
|
25
27
|
[]
|
26
28
|
]
|
27
29
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative '../../request'
|
4
4
|
|
5
5
|
module Rack
|
6
6
|
module Auth
|
@@ -27,7 +27,7 @@ module Rack
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def scheme
|
30
|
-
@scheme ||= parts.first
|
30
|
+
@scheme ||= parts.first&.downcase
|
31
31
|
end
|
32
32
|
|
33
33
|
def params
|
data/lib/rack/auth/basic.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require 'base64'
|
3
|
+
require_relative 'abstract/handler'
|
4
|
+
require_relative 'abstract/request'
|
6
5
|
|
7
6
|
module Rack
|
8
7
|
module Auth
|
@@ -10,8 +9,6 @@ module Rack
|
|
10
9
|
#
|
11
10
|
# Initialize with the Rack application that you want protecting,
|
12
11
|
# and a block that checks if a username and password pair are valid.
|
13
|
-
#
|
14
|
-
# See also: <tt>example/protectedlobster.rb</tt>
|
15
12
|
|
16
13
|
class Basic < AbstractHandler
|
17
14
|
|
@@ -44,11 +41,11 @@ module Rack
|
|
44
41
|
|
45
42
|
class Request < Auth::AbstractRequest
|
46
43
|
def basic?
|
47
|
-
"basic" == scheme
|
44
|
+
"basic" == scheme && credentials.length == 2
|
48
45
|
end
|
49
46
|
|
50
47
|
def credentials
|
51
|
-
@credentials ||=
|
48
|
+
@credentials ||= params.unpack1('m').split(':', 2)
|
52
49
|
end
|
53
50
|
|
54
51
|
def username
|
data/lib/rack/body_proxy.rb
CHANGED
@@ -1,41 +1,63 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Rack
|
4
|
+
# Proxy for response bodies allowing calling a block when
|
5
|
+
# the response body is closed (after the response has been fully
|
6
|
+
# sent to the client).
|
4
7
|
class BodyProxy
|
8
|
+
# Set the response body to wrap, and the block to call when the
|
9
|
+
# response has been fully sent.
|
5
10
|
def initialize(body, &block)
|
6
11
|
@body = body
|
7
12
|
@block = block
|
8
13
|
@closed = false
|
9
14
|
end
|
10
15
|
|
11
|
-
|
12
|
-
|
16
|
+
# Return whether the wrapped body responds to the method.
|
17
|
+
def respond_to_missing?(method_name, include_all = false)
|
18
|
+
case method_name
|
19
|
+
when :to_str
|
20
|
+
false
|
21
|
+
else
|
22
|
+
super or @body.respond_to?(method_name, include_all)
|
23
|
+
end
|
13
24
|
end
|
14
25
|
|
26
|
+
# If not already closed, close the wrapped body and
|
27
|
+
# then call the block the proxy was initialized with.
|
15
28
|
def close
|
16
29
|
return if @closed
|
17
30
|
@closed = true
|
18
31
|
begin
|
19
|
-
@body.close if @body.respond_to?
|
32
|
+
@body.close if @body.respond_to?(:close)
|
20
33
|
ensure
|
21
34
|
@block.call
|
22
35
|
end
|
23
36
|
end
|
24
37
|
|
38
|
+
# Whether the proxy is closed. The proxy starts as not closed,
|
39
|
+
# and becomes closed on the first call to close.
|
25
40
|
def closed?
|
26
41
|
@closed
|
27
42
|
end
|
28
43
|
|
29
|
-
#
|
30
|
-
# We are applying this special case for #each only. Future bugs of this
|
31
|
-
# class will be handled by requesting users to patch their ruby
|
32
|
-
# implementation, to save adding too many methods in this class.
|
33
|
-
def each
|
34
|
-
@body.each { |body| yield body }
|
35
|
-
end
|
36
|
-
|
44
|
+
# Delegate missing methods to the wrapped body.
|
37
45
|
def method_missing(method_name, *args, &block)
|
38
|
-
|
46
|
+
case method_name
|
47
|
+
when :to_str
|
48
|
+
super
|
49
|
+
when :to_ary
|
50
|
+
begin
|
51
|
+
@body.__send__(method_name, *args, &block)
|
52
|
+
ensure
|
53
|
+
close
|
54
|
+
end
|
55
|
+
else
|
56
|
+
@body.__send__(method_name, *args, &block)
|
57
|
+
end
|
39
58
|
end
|
59
|
+
# :nocov:
|
60
|
+
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
|
61
|
+
# :nocov:
|
40
62
|
end
|
41
63
|
end
|
data/lib/rack/builder.rb
CHANGED
@@ -1,76 +1,138 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'urlmap'
|
4
|
+
|
5
|
+
module Rack; end
|
6
|
+
Rack::BUILDER_TOPLEVEL_BINDING = ->(builder){builder.instance_eval{binding}}
|
7
|
+
|
3
8
|
module Rack
|
4
|
-
# Rack::Builder
|
5
|
-
# applications.
|
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.
|
6
13
|
#
|
7
14
|
# Example:
|
8
15
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# run Rack::Lobster.new
|
16
|
-
# end
|
17
|
-
# end
|
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
|
18
22
|
#
|
19
|
-
#
|
23
|
+
# run app
|
20
24
|
#
|
21
25
|
# Or
|
22
26
|
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
+
# app = Rack::Builder.app do
|
28
|
+
# use Rack::CommonLogger
|
29
|
+
# run lambda { |env| [200, {'content-type' => 'text/plain'}, ['OK']] }
|
30
|
+
# end
|
27
31
|
#
|
28
|
-
#
|
32
|
+
# run app
|
29
33
|
#
|
30
34
|
# +use+ adds middleware to the stack, +run+ dispatches to an application.
|
31
35
|
# You can use +map+ to construct a Rack::URLMap in a convenient way.
|
32
|
-
|
33
36
|
class Builder
|
34
37
|
|
35
38
|
# https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom
|
36
39
|
UTF_8_BOM = '\xef\xbb\xbf'
|
37
40
|
|
38
|
-
|
39
|
-
|
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)
|
41
68
|
else
|
42
|
-
require
|
43
|
-
|
44
|
-
return app, {}
|
69
|
+
require path
|
70
|
+
return Object.const_get(::File.basename(path, '.rb').split('_').map(&:capitalize).join(''))
|
45
71
|
end
|
46
72
|
end
|
47
73
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
53
90
|
|
54
|
-
if
|
55
|
-
options
|
91
|
+
if config[/^#\\(.*)/]
|
92
|
+
fail "Parsing options from the first comment line is no longer supported: #{path}"
|
56
93
|
end
|
57
94
|
|
58
|
-
|
59
|
-
app = new_from_string cfgfile, path
|
95
|
+
config.sub!(/^__END__\n.*\Z/m, '')
|
60
96
|
|
61
|
-
return
|
97
|
+
return new_from_string(config, path, **options)
|
62
98
|
end
|
63
99
|
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
67
111
|
end
|
68
112
|
|
69
|
-
|
70
|
-
|
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
|
+
|
71
124
|
instance_eval(&block) if block_given?
|
72
125
|
end
|
73
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.
|
74
136
|
def self.app(default_app = nil, &block)
|
75
137
|
self.new(default_app, &block).to_app
|
76
138
|
end
|
@@ -89,7 +151,7 @@ module Rack
|
|
89
151
|
# end
|
90
152
|
#
|
91
153
|
# use Middleware
|
92
|
-
# run lambda { |env| [200, { "
|
154
|
+
# run lambda { |env| [200, { "content-type" => "text/plain" }, ["OK"]] }
|
93
155
|
#
|
94
156
|
# All requests through to this application will first be processed by the middleware class.
|
95
157
|
# The +call+ method in this example sets an additional environment key which then can be
|
@@ -101,26 +163,41 @@ module Rack
|
|
101
163
|
end
|
102
164
|
@use << proc { |app| middleware.new(app, *args, &block) }
|
103
165
|
end
|
166
|
+
# :nocov:
|
167
|
+
ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
|
168
|
+
# :nocov:
|
104
169
|
|
105
|
-
# Takes
|
106
|
-
#
|
170
|
+
# Takes a block or argument that is an object that responds to #call and
|
171
|
+
# returns a Rack response.
|
172
|
+
#
|
173
|
+
# You can use a block:
|
174
|
+
#
|
175
|
+
# run do |env|
|
176
|
+
# [200, { "content-type" => "text/plain" }, ["Hello World!"]]
|
177
|
+
# end
|
178
|
+
#
|
179
|
+
# You can also provide a lambda:
|
107
180
|
#
|
108
|
-
# run lambda { |env| [200, { "
|
181
|
+
# run lambda { |env| [200, { "content-type" => "text/plain" }, ["OK"]] }
|
109
182
|
#
|
110
|
-
#
|
183
|
+
# You can also provide a class instance:
|
111
184
|
#
|
112
185
|
# class Heartbeat
|
113
|
-
# def
|
114
|
-
# [200, { "
|
186
|
+
# def call(env)
|
187
|
+
# [200, { "content-type" => "text/plain" }, ["OK"]]
|
115
188
|
# end
|
116
189
|
# end
|
117
190
|
#
|
118
|
-
# run Heartbeat
|
119
|
-
|
120
|
-
|
191
|
+
# run Heartbeat.new
|
192
|
+
#
|
193
|
+
def run(app = nil, &block)
|
194
|
+
raise ArgumentError, "Both app and block given!" if app && block_given?
|
195
|
+
|
196
|
+
@run = app || block
|
121
197
|
end
|
122
198
|
|
123
|
-
# Takes a lambda or block that is used to warm-up the application.
|
199
|
+
# Takes a lambda or block that is used to warm-up the application. This block is called
|
200
|
+
# before the Rack application is returned by to_app.
|
124
201
|
#
|
125
202
|
# warmup do |app|
|
126
203
|
# client = Rack::MockRequest.new(app)
|
@@ -133,25 +210,45 @@ module Rack
|
|
133
210
|
@warmup = prc || block
|
134
211
|
end
|
135
212
|
|
136
|
-
# Creates a route within the application.
|
213
|
+
# Creates a route within the application. Routes under the mapped path will be sent to
|
214
|
+
# the Rack application specified by run inside the block. Other requests will be sent to the
|
215
|
+
# default application specified by run outside the block.
|
216
|
+
#
|
217
|
+
# class App
|
218
|
+
# def call(env)
|
219
|
+
# [200, {'content-type' => 'text/plain'}, ["Hello World"]]
|
220
|
+
# end
|
221
|
+
# end
|
137
222
|
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
223
|
+
# class Heartbeat
|
224
|
+
# def call(env)
|
225
|
+
# [200, { "content-type" => "text/plain" }, ["OK"]]
|
141
226
|
# end
|
142
227
|
# end
|
143
228
|
#
|
144
|
-
#
|
229
|
+
# app = Rack::Builder.app do
|
230
|
+
# map '/heartbeat' do
|
231
|
+
# run Heartbeat.new
|
232
|
+
# end
|
233
|
+
# run App.new
|
234
|
+
# end
|
235
|
+
#
|
236
|
+
# run app
|
237
|
+
#
|
238
|
+
# The +use+ method can also be used inside the block to specify middleware to run under a specific path:
|
145
239
|
#
|
146
|
-
# Rack::Builder.app do
|
147
|
-
# map '/' do
|
240
|
+
# app = Rack::Builder.app do
|
241
|
+
# map '/heartbeat' do
|
148
242
|
# use Middleware
|
149
|
-
# run Heartbeat
|
243
|
+
# run Heartbeat.new
|
150
244
|
# end
|
245
|
+
# run App.new
|
151
246
|
# end
|
152
247
|
#
|
153
|
-
# This example includes a piece of middleware which will run before requests hit +Heartbeat+.
|
248
|
+
# This example includes a piece of middleware which will run before +/heartbeat+ requests hit +Heartbeat+.
|
154
249
|
#
|
250
|
+
# Note that providing a +path+ of +/+ will ignore any default application given in a +run+ statement
|
251
|
+
# outside the block.
|
155
252
|
def map(path, &block)
|
156
253
|
@map ||= {}
|
157
254
|
@map[path] = block
|
@@ -163,6 +260,7 @@ module Rack
|
|
163
260
|
@freeze_app = true
|
164
261
|
end
|
165
262
|
|
263
|
+
# Return the Rack application generated by this instance.
|
166
264
|
def to_app
|
167
265
|
app = @map ? generate_map(@run, @map) : @run
|
168
266
|
fail "missing run or map statement" unless app
|
@@ -172,12 +270,17 @@ module Rack
|
|
172
270
|
app
|
173
271
|
end
|
174
272
|
|
273
|
+
# Call the Rack application generated by this builder instance. Note that
|
274
|
+
# this rebuilds the Rack application and runs the warmup code (if any)
|
275
|
+
# every time it is called, so it should not be used if performance is important.
|
175
276
|
def call(env)
|
176
277
|
to_app.call(env)
|
177
278
|
end
|
178
279
|
|
179
280
|
private
|
180
281
|
|
282
|
+
# Generate a URLMap instance by generating new Rack applications for each
|
283
|
+
# map block in this instance.
|
181
284
|
def generate_map(default_app, mapping)
|
182
285
|
mapped = default_app ? { '/' => default_app } : {}
|
183
286
|
mapping.each { |r, b| mapped[r] = self.class.new(default_app, &b).to_app }
|
data/lib/rack/cascade.rb
CHANGED
@@ -1,26 +1,37 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'constants'
|
4
|
+
|
3
5
|
module Rack
|
4
6
|
# 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).
|
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.
|
7
10
|
|
8
11
|
class Cascade
|
9
|
-
|
10
|
-
|
12
|
+
# An array of applications to try in order.
|
11
13
|
attr_reader :apps
|
12
14
|
|
13
|
-
|
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])
|
14
22
|
@apps = []
|
15
23
|
apps.each { |app| add app }
|
16
24
|
|
17
|
-
@
|
18
|
-
[*
|
25
|
+
@cascade_for = {}
|
26
|
+
[*cascade_for].each { |status| @cascade_for[status] = true }
|
19
27
|
end
|
20
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.
|
21
32
|
def call(env)
|
22
|
-
|
23
|
-
|
33
|
+
return [404, { CONTENT_TYPE => "text/plain" }, []] if @apps.empty?
|
34
|
+
result = nil
|
24
35
|
last_body = nil
|
25
36
|
|
26
37
|
@apps.each do |app|
|
@@ -33,17 +44,20 @@ module Rack
|
|
33
44
|
last_body.close if last_body.respond_to? :close
|
34
45
|
|
35
46
|
result = app.call(env)
|
47
|
+
return result unless @cascade_for.include?(result[0].to_i)
|
36
48
|
last_body = result[2]
|
37
|
-
break unless @catch.include?(result[0].to_i)
|
38
49
|
end
|
39
50
|
|
40
51
|
result
|
41
52
|
end
|
42
53
|
|
54
|
+
# Append an app to the list of apps to cascade. This app will
|
55
|
+
# be tried last.
|
43
56
|
def add(app)
|
44
57
|
@apps << app
|
45
58
|
end
|
46
59
|
|
60
|
+
# Whether the given app is one of the apps to cascade to.
|
47
61
|
def include?(app)
|
48
62
|
@apps.include?(app)
|
49
63
|
end
|
data/lib/rack/common_logger.rb
CHANGED
@@ -1,61 +1,74 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative 'constants'
|
4
|
+
require_relative 'utils'
|
5
|
+
require_relative 'body_proxy'
|
6
|
+
require_relative 'request'
|
4
7
|
|
5
8
|
module Rack
|
6
9
|
# Rack::CommonLogger forwards every request to the given +app+, and
|
7
10
|
# logs a line in the
|
8
11
|
# {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)
|
12
|
+
# to the configured logger.
|
20
13
|
class CommonLogger
|
21
14
|
# Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
|
22
15
|
#
|
23
16
|
# lilith.local - - [07/Aug/2006 23:58:02 -0400] "GET / HTTP/1.1" 500 -
|
24
17
|
#
|
25
18
|
# %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
|
26
|
-
|
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\n}
|
27
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>.
|
28
29
|
def initialize(app, logger = nil)
|
29
30
|
@app = app
|
30
31
|
@logger = logger
|
31
32
|
end
|
32
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.
|
33
41
|
def call(env)
|
34
42
|
began_at = Utils.clock_time
|
35
|
-
status,
|
36
|
-
|
37
|
-
|
38
|
-
|
43
|
+
status, headers, body = response = @app.call(env)
|
44
|
+
|
45
|
+
response[2] = BodyProxy.new(body) { log(env, status, headers, began_at) }
|
46
|
+
response
|
39
47
|
end
|
40
48
|
|
41
49
|
private
|
42
50
|
|
43
|
-
|
44
|
-
|
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)
|
45
55
|
|
46
|
-
msg = FORMAT
|
47
|
-
|
48
|
-
|
56
|
+
msg = sprintf(FORMAT,
|
57
|
+
request.ip || "-",
|
58
|
+
request.get_header("REMOTE_USER") || "-",
|
49
59
|
Time.now.strftime("%d/%b/%Y:%H:%M:%S %z"),
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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),
|
54
65
|
status.to_s[0..3],
|
55
66
|
length,
|
56
|
-
Utils.clock_time - began_at
|
67
|
+
Utils.clock_time - began_at)
|
68
|
+
|
69
|
+
msg.gsub!(/[^[:print:]\n]/) { |c| sprintf("\\x%x", c.ord) }
|
57
70
|
|
58
|
-
logger = @logger ||
|
71
|
+
logger = @logger || request.get_header(RACK_ERRORS)
|
59
72
|
# Standard library logger doesn't support write but it supports << which actually
|
60
73
|
# calls to write on the log device without formatting
|
61
74
|
if logger.respond_to?(:write)
|
@@ -65,6 +78,8 @@ module Rack
|
|
65
78
|
end
|
66
79
|
end
|
67
80
|
|
81
|
+
# Attempt to determine the content length for the response to
|
82
|
+
# include it in the logged data.
|
68
83
|
def extract_content_length(headers)
|
69
84
|
value = headers[CONTENT_LENGTH]
|
70
85
|
!value || value.to_s == '0' ? '-' : value
|