rack 2.1.0 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rack might be problematic. Click here for more details.

Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +126 -6
  3. data/CONTRIBUTING.md +136 -0
  4. data/README.rdoc +83 -39
  5. data/Rakefile +14 -7
  6. data/{SPEC → SPEC.rdoc} +26 -1
  7. data/lib/rack.rb +7 -16
  8. data/lib/rack/auth/abstract/request.rb +0 -2
  9. data/lib/rack/auth/basic.rb +3 -3
  10. data/lib/rack/auth/digest/md5.rb +4 -4
  11. data/lib/rack/auth/digest/request.rb +3 -3
  12. data/lib/rack/body_proxy.rb +13 -9
  13. data/lib/rack/builder.rb +78 -8
  14. data/lib/rack/cascade.rb +23 -8
  15. data/lib/rack/chunked.rb +48 -23
  16. data/lib/rack/common_logger.rb +25 -18
  17. data/lib/rack/conditional_get.rb +18 -16
  18. data/lib/rack/content_length.rb +6 -7
  19. data/lib/rack/content_type.rb +3 -4
  20. data/lib/rack/deflater.rb +49 -35
  21. data/lib/rack/directory.rb +77 -60
  22. data/lib/rack/etag.rb +2 -3
  23. data/lib/rack/events.rb +15 -18
  24. data/lib/rack/file.rb +1 -2
  25. data/lib/rack/files.rb +97 -57
  26. data/lib/rack/handler/cgi.rb +1 -4
  27. data/lib/rack/handler/fastcgi.rb +1 -3
  28. data/lib/rack/handler/lsws.rb +1 -3
  29. data/lib/rack/handler/scgi.rb +1 -3
  30. data/lib/rack/handler/thin.rb +1 -3
  31. data/lib/rack/handler/webrick.rb +12 -5
  32. data/lib/rack/head.rb +0 -2
  33. data/lib/rack/lint.rb +57 -14
  34. data/lib/rack/lobster.rb +3 -5
  35. data/lib/rack/lock.rb +0 -1
  36. data/lib/rack/mock.rb +22 -4
  37. data/lib/rack/multipart.rb +1 -1
  38. data/lib/rack/multipart/generator.rb +11 -6
  39. data/lib/rack/multipart/parser.rb +10 -18
  40. data/lib/rack/multipart/uploaded_file.rb +13 -7
  41. data/lib/rack/query_parser.rb +7 -8
  42. data/lib/rack/recursive.rb +1 -1
  43. data/lib/rack/reloader.rb +1 -3
  44. data/lib/rack/request.rb +182 -76
  45. data/lib/rack/response.rb +62 -19
  46. data/lib/rack/rewindable_input.rb +0 -1
  47. data/lib/rack/runtime.rb +3 -3
  48. data/lib/rack/sendfile.rb +0 -3
  49. data/lib/rack/server.rb +9 -10
  50. data/lib/rack/session/abstract/id.rb +23 -28
  51. data/lib/rack/session/cookie.rb +1 -3
  52. data/lib/rack/session/pool.rb +1 -1
  53. data/lib/rack/show_exceptions.rb +6 -8
  54. data/lib/rack/show_status.rb +5 -7
  55. data/lib/rack/static.rb +13 -6
  56. data/lib/rack/tempfile_reaper.rb +0 -2
  57. data/lib/rack/urlmap.rb +1 -4
  58. data/lib/rack/utils.rb +58 -54
  59. data/lib/rack/version.rb +29 -0
  60. data/rack.gemspec +31 -29
  61. metadata +11 -12
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bundler/gem_tasks"
3
4
  require "rake/testtask"
4
5
 
5
6
  desc "Run all the tests"
@@ -20,7 +21,7 @@ end
20
21
  desc "Make an archive as .tar.gz"
21
22
  task dist: %w[chmod changelog spec rdoc] do
22
23
  sh "git archive --format=tar --prefix=#{release}/ HEAD^{tree} >#{release}.tar"
23
- sh "pax -waf #{release}.tar -s ':^:#{release}/:' SPEC ChangeLog doc rack.gemspec"
24
+ sh "pax -waf #{release}.tar -s ':^:#{release}/:' SPEC.rdoc ChangeLog doc rack.gemspec"
24
25
  sh "gzip -f -9 #{release}.tar"
25
26
  end
26
27
 
@@ -38,7 +39,7 @@ task officialrelease_really: %w[spec dist gem] do
38
39
  end
39
40
 
40
41
  def release
41
- "rack-" + File.read('lib/rack.rb')[/RELEASE += +([\"\'])([\d][\w\.]+)\1/, 2]
42
+ "rack-" + File.read('lib/rack/version.rb')[/RELEASE += +([\"\'])([\d][\w\.]+)\1/, 2]
42
43
  end
43
44
 
44
45
  desc "Make binaries executable"
@@ -71,13 +72,13 @@ file "ChangeLog" => '.git/index' do
71
72
  end
72
73
 
73
74
  desc "Generate Rack Specification"
74
- task spec: "SPEC"
75
+ task spec: "SPEC.rdoc"
75
76
 
76
77
  file 'lib/rack/lint.rb'
77
- file "SPEC" => 'lib/rack/lint.rb' do
78
- File.open("SPEC", "wb") { |file|
78
+ file "SPEC.rdoc" => 'lib/rack/lint.rb' do
79
+ File.open("SPEC.rdoc", "wb") { |file|
79
80
  IO.foreach("lib/rack/lint.rb") { |line|
80
- if line =~ /## (.*)/
81
+ if line =~ /^\s*## ?(.*)/
81
82
  file.puts $1
82
83
  end
83
84
  }
@@ -91,6 +92,12 @@ Rake::TestTask.new("test:regular") do |t|
91
92
  t.verbose = true
92
93
  end
93
94
 
95
+ desc "Run tests with coverage"
96
+ task "test_cov" do
97
+ ENV['COVERAGE'] = '1'
98
+ Rake::Task['test:regular'].invoke
99
+ end
100
+
94
101
  desc "Run all the fast + platform agnostic tests"
95
102
  task test: %w[spec test:regular]
96
103
 
@@ -107,7 +114,7 @@ desc "Generate RDoc documentation"
107
114
  task rdoc: %w[changelog spec] do
108
115
  sh(*%w{rdoc --line-numbers --main README.rdoc
109
116
  --title 'Rack\ Documentation' --charset utf-8 -U -o doc} +
110
- %w{README.rdoc KNOWN-ISSUES SPEC ChangeLog} +
117
+ %w{README.rdoc KNOWN-ISSUES SPEC.rdoc ChangeLog} +
111
118
  `git ls-files lib/\*\*/\*.rb`.strip.split)
112
119
  cp "contrib/rdoc.css", "doc/rdoc.css"
113
120
  end
@@ -1,5 +1,6 @@
1
1
  This specification aims to formalize the Rack protocol. You
2
2
  can (and should) use Rack::Lint to enforce it.
3
+
3
4
  When you develop middleware, be sure to add a Lint before and
4
5
  after to catch all mistakes.
5
6
  = Rack applications
@@ -11,9 +12,10 @@ The *status*,
11
12
  the *headers*,
12
13
  and the *body*.
13
14
  == The Environment
14
- The environment must be an instance of Hash that includes
15
+ The environment must be an unfrozen instance of Hash that includes
15
16
  CGI-like headers. The application is free to modify the
16
17
  environment.
18
+
17
19
  The environment is required to include these variables
18
20
  (adopted from PEP333), except when they'd be empty, but see
19
21
  below.
@@ -104,6 +106,7 @@ be implemented by the server.
104
106
  fetch(key, default = nil) (aliased as []);
105
107
  delete(key);
106
108
  clear;
109
+ to_hash (returning unfrozen Hash instance);
107
110
  <tt>rack.logger</tt>:: A common object interface for logging messages.
108
111
  The object must implement:
109
112
  info(message, &block)
@@ -118,10 +121,13 @@ environment, too. The keys must contain at least one dot,
118
121
  and should be prefixed uniquely. The prefix <tt>rack.</tt>
119
122
  is reserved for use with the Rack core distribution and other
120
123
  accepted specifications and must not be used otherwise.
124
+
121
125
  The environment must not contain the keys
122
126
  <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
123
127
  (use the versions without <tt>HTTP_</tt>).
124
128
  The CGI keys (named without a period) must have String values.
129
+ If the string values for CGI keys contain non-ASCII characters,
130
+ they should use ASCII-8BIT encoding.
125
131
  There are the following restrictions:
126
132
  * <tt>rack.version</tt> must be an array of Integers.
127
133
  * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
@@ -137,6 +143,7 @@ There are the following restrictions:
137
143
  <tt>SCRIPT_NAME</tt> is empty.
138
144
  <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
139
145
  === The Input Stream
146
+
140
147
  The input stream is an IO-like object which contains the raw HTTP
141
148
  POST data.
142
149
  When applicable, its external encoding must be "ASCII-8BIT" and it
@@ -146,14 +153,19 @@ The input stream must respond to +gets+, +each+, +read+ and +rewind+.
146
153
  or +nil+ on EOF.
147
154
  * +read+ behaves like IO#read.
148
155
  Its signature is <tt>read([length, [buffer]])</tt>.
156
+
149
157
  If given, +length+ must be a non-negative Integer (>= 0) or +nil+,
150
158
  and +buffer+ must be a String and may not be nil.
159
+
151
160
  If +length+ is given and not nil, then this method reads at most
152
161
  +length+ bytes from the input stream.
162
+
153
163
  If +length+ is not given or nil, then this method reads
154
164
  all data until EOF.
165
+
155
166
  When EOF is reached, this method returns nil if +length+ is given
156
167
  and not nil, or "" if +length+ is not given or is nil.
168
+
157
169
  If +buffer+ is given, then the read data will be placed
158
170
  into +buffer+ instead of a newly created String object.
159
171
  * +each+ must be called without arguments and only yield Strings.
@@ -175,16 +187,20 @@ The error stream must respond to +puts+, +write+ and +flush+.
175
187
  If rack.hijack? is true then rack.hijack must respond to #call.
176
188
  rack.hijack must return the io that will also be assigned (or is
177
189
  already present, in rack.hijack_io.
190
+
178
191
  rack.hijack_io must respond to:
179
192
  <tt>read, write, read_nonblock, write_nonblock, flush, close,
180
193
  close_read, close_write, closed?</tt>
194
+
181
195
  The semantics of these IO methods must be a best effort match to
182
196
  those of a normal ruby IO or Socket object, using standard
183
197
  arguments and raising standard exceptions. Servers are encouraged
184
198
  to simply pass on real IO objects, although it is recognized that
185
199
  this approach is not directly compatible with SPDY and HTTP 2.0.
200
+
186
201
  IO provided in rack.hijack_io should preference the
187
202
  IO::WaitReadable and IO::WaitWritable APIs wherever supported.
203
+
188
204
  There is a deliberate lack of full specification around
189
205
  rack.hijack_io, as semantics will change from server to server.
190
206
  Users are encouraged to utilize this API with a knowledge of their
@@ -192,7 +208,9 @@ server choice, and servers may extend the functionality of
192
208
  hijack_io to provide additional features to users. The purpose of
193
209
  rack.hijack is for Rack to "get out of the way", as such, Rack only
194
210
  provides the minimum of specification and support.
211
+
195
212
  If rack.hijack? is false, then rack.hijack should not be set.
213
+
196
214
  If rack.hijack? is false, then rack.hijack_io should not be set.
197
215
  ==== Response (after headers)
198
216
  It is also possible to hijack a response after the status and headers
@@ -201,6 +219,7 @@ In order to do this, an application may set the special header
201
219
  <tt>rack.hijack</tt> to an object that responds to <tt>call</tt>
202
220
  accepting an argument that conforms to the <tt>rack.hijack_io</tt>
203
221
  protocol.
222
+
204
223
  After the headers have been sent, and this hijack callback has been
205
224
  called, the application is now responsible for the remaining lifecycle
206
225
  of the IO. The application is also responsible for maintaining HTTP
@@ -209,8 +228,10 @@ applications will have wanted to specify the header Connection:close in
209
228
  HTTP/1.1, and not Connection:keep-alive, as there is no protocol for
210
229
  returning hijacked sockets to the web server. For that purpose, use the
211
230
  body streaming API instead (progressively yielding strings via each).
231
+
212
232
  Servers must ignore the <tt>body</tt> part of the response tuple when
213
233
  the <tt>rack.hijack</tt> response API is in use.
234
+
214
235
  The special response header <tt>rack.hijack</tt> must only be set
215
236
  if the request env has <tt>rack.hijack?</tt> <tt>true</tt>.
216
237
  ==== Conventions
@@ -244,16 +265,20 @@ There must not be a Content-Length header when the
244
265
  === The Body
245
266
  The Body must respond to +each+
246
267
  and must only yield String values.
268
+
247
269
  The Body itself should not be an instance of String, as this will
248
270
  break in Ruby 1.9.
271
+
249
272
  If the Body responds to +close+, it will be called after iteration. If
250
273
  the body is replaced by a middleware after action, the original body
251
274
  must be closed first, if it responds to close.
275
+
252
276
  If the Body responds to +to_path+, it must return a String
253
277
  identifying the location of a file whose contents are identical
254
278
  to that produced by calling +each+; this may be used by the
255
279
  server as an alternative, possibly more efficient way to
256
280
  transport the response.
281
+
257
282
  The Body commonly is an Array of Strings, the application
258
283
  instance itself, or a File-like object.
259
284
  == Thanks
@@ -11,23 +11,11 @@
11
11
  # All modules meant for use in your application are <tt>autoload</tt>ed here,
12
12
  # so it should be enough just to <tt>require 'rack'</tt> in your code.
13
13
 
14
- module Rack
15
- # The Rack protocol version number implemented.
16
- VERSION = [1, 3]
17
-
18
- # Return the Rack protocol version as a dotted string.
19
- def self.version
20
- VERSION.join(".")
21
- end
22
-
23
- RELEASE = "2.1.0"
24
-
25
- # Return the Rack release as a dotted string.
26
- def self.release
27
- RELEASE
28
- end
14
+ require_relative 'rack/version'
29
15
 
16
+ module Rack
30
17
  HTTP_HOST = 'HTTP_HOST'
18
+ HTTP_PORT = 'HTTP_PORT'
31
19
  HTTP_VERSION = 'HTTP_VERSION'
32
20
  HTTPS = 'HTTPS'
33
21
  PATH_INFO = 'PATH_INFO'
@@ -37,9 +25,9 @@ module Rack
37
25
  QUERY_STRING = 'QUERY_STRING'
38
26
  SERVER_PROTOCOL = 'SERVER_PROTOCOL'
39
27
  SERVER_NAME = 'SERVER_NAME'
40
- SERVER_ADDR = 'SERVER_ADDR'
41
28
  SERVER_PORT = 'SERVER_PORT'
42
29
  CACHE_CONTROL = 'Cache-Control'
30
+ EXPIRES = 'Expires'
43
31
  CONTENT_LENGTH = 'Content-Length'
44
32
  CONTENT_TYPE = 'Content-Type'
45
33
  SET_COOKIE = 'Set-Cookie'
@@ -98,6 +86,7 @@ module Rack
98
86
  autoload :ContentLength, "rack/content_length"
99
87
  autoload :ContentType, "rack/content_type"
100
88
  autoload :ETag, "rack/etag"
89
+ autoload :Events, "rack/events"
101
90
  autoload :File, "rack/file"
102
91
  autoload :Files, "rack/files"
103
92
  autoload :Deflater, "rack/deflater"
@@ -108,11 +97,13 @@ module Rack
108
97
  autoload :Lint, "rack/lint"
109
98
  autoload :Lock, "rack/lock"
110
99
  autoload :Logger, "rack/logger"
100
+ autoload :MediaType, "rack/media_type"
111
101
  autoload :MethodOverride, "rack/method_override"
112
102
  autoload :Mime, "rack/mime"
113
103
  autoload :NullLogger, "rack/null_logger"
114
104
  autoload :Recursive, "rack/recursive"
115
105
  autoload :Reloader, "rack/reloader"
106
+ autoload :RewindableInput, "rack/rewindable_input"
116
107
  autoload :Runtime, "rack/runtime"
117
108
  autoload :Sendfile, "rack/sendfile"
118
109
  autoload :Server, "rack/server"
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rack/request'
4
-
5
3
  module Rack
6
4
  module Auth
7
5
  class AbstractRequest
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rack/auth/abstract/handler'
4
- require 'rack/auth/abstract/request'
3
+ require_relative 'abstract/handler'
4
+ require_relative 'abstract/request'
5
5
  require 'base64'
6
6
 
7
7
  module Rack
@@ -44,7 +44,7 @@ module Rack
44
44
 
45
45
  class Request < Auth::AbstractRequest
46
46
  def basic?
47
- "basic" == scheme
47
+ "basic" == scheme && credentials.length == 2
48
48
  end
49
49
 
50
50
  def credentials
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rack/auth/abstract/handler'
4
- require 'rack/auth/digest/request'
5
- require 'rack/auth/digest/params'
6
- require 'rack/auth/digest/nonce'
3
+ require_relative '../abstract/handler'
4
+ require_relative 'request'
5
+ require_relative 'params'
6
+ require_relative 'nonce'
7
7
  require 'digest/md5'
8
8
 
9
9
  module Rack
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rack/auth/abstract/request'
4
- require 'rack/auth/digest/params'
5
- require 'rack/auth/digest/nonce'
3
+ require_relative '../abstract/request'
4
+ require_relative 'params'
5
+ require_relative 'nonce'
6
6
 
7
7
  module Rack
8
8
  module Auth
@@ -1,17 +1,25 @@
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
- def respond_to?(method_name, include_all = false)
16
+ # Return whether the wrapped body responds to the method.
17
+ def respond_to_missing?(method_name, include_all = false)
12
18
  super or @body.respond_to?(method_name, include_all)
13
19
  end
14
20
 
21
+ # If not already closed, close the wrapped body and
22
+ # then call the block the proxy was initialized with.
15
23
  def close
16
24
  return if @closed
17
25
  @closed = true
@@ -22,20 +30,16 @@ module Rack
22
30
  end
23
31
  end
24
32
 
33
+ # Whether the proxy is closed. The proxy starts as not closed,
34
+ # and becomes closed on the first call to close.
25
35
  def closed?
26
36
  @closed
27
37
  end
28
38
 
29
- # N.B. This method is a special case to address the bug described by #434.
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
-
39
+ # Delegate missing methods to the wrapped body.
37
40
  def method_missing(method_name, *args, &block)
38
41
  @body.__send__(method_name, *args, &block)
39
42
  end
43
+ ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
40
44
  end
41
45
  end
@@ -35,6 +35,32 @@ module Rack
35
35
  # https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom
36
36
  UTF_8_BOM = '\xef\xbb\xbf'
37
37
 
38
+ # Parse the given config file to get a Rack application.
39
+ #
40
+ # If the config file ends in +.ru+, it is treated as a
41
+ # rackup file and the contents will be treated as if
42
+ # specified inside a Rack::Builder block, using the given
43
+ # options.
44
+ #
45
+ # If the config file does not end in +.ru+, it is
46
+ # required and Rack will use the basename of the file
47
+ # to guess which constant will be the Rack application to run.
48
+ # The options given will be ignored in this case.
49
+ #
50
+ # Examples:
51
+ #
52
+ # Rack::Builder.parse_file('config.ru')
53
+ # # Rack application built using Rack::Builder.new
54
+ #
55
+ # Rack::Builder.parse_file('app.rb')
56
+ # # requires app.rb, which can be anywhere in Ruby's
57
+ # # load path. After requiring, assumes App constant
58
+ # # contains Rack application
59
+ #
60
+ # Rack::Builder.parse_file('./my_app.rb')
61
+ # # requires ./my_app.rb, which should be in the
62
+ # # process's current directory. After requiring,
63
+ # # assumes MyApp constant contains Rack application
38
64
  def self.parse_file(config, opts = Server::Options.new)
39
65
  if config.end_with?('.ru')
40
66
  return self.load_file(config, opts)
@@ -45,6 +71,25 @@ module Rack
45
71
  end
46
72
  end
47
73
 
74
+ # Load the given file as a rackup file, treating the
75
+ # contents as if specified inside a Rack::Builder block.
76
+ #
77
+ # Treats the first comment at the beginning of a line
78
+ # that starts with a backslash as options similar to
79
+ # options passed on a rackup command line.
80
+ #
81
+ # Ignores content in the file after +__END__+, so that
82
+ # use of +__END__+ will not result in a syntax error.
83
+ #
84
+ # Example config.ru file:
85
+ #
86
+ # $ cat config.ru
87
+ #
88
+ # #\ -p 9393
89
+ #
90
+ # use Rack::ContentLength
91
+ # require './app.rb'
92
+ # run App
48
93
  def self.load_file(path, opts = Server::Options.new)
49
94
  options = {}
50
95
 
@@ -52,6 +97,7 @@ module Rack
52
97
  cfgfile.slice!(/\A#{UTF_8_BOM}/) if cfgfile.encoding == Encoding::UTF_8
53
98
 
54
99
  if cfgfile[/^#\\(.*)/] && opts
100
+ warn "Parsing options from the first comment line is deprecated!"
55
101
  options = opts.parse! $1.split(/\s+/)
56
102
  end
57
103
 
@@ -61,16 +107,26 @@ module Rack
61
107
  return app, options
62
108
  end
63
109
 
110
+ # Evaluate the given +builder_script+ string in the context of
111
+ # a Rack::Builder block, returning a Rack application.
64
112
  def self.new_from_string(builder_script, file = "(rackup)")
65
- eval "Rack::Builder.new {\n" + builder_script + "\n}.to_app",
66
- TOPLEVEL_BINDING, file, 0
113
+ # We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.
114
+ # We cannot use instance_eval(String) as that would resolve constants differently.
115
+ binding, builder = TOPLEVEL_BINDING.eval('Rack::Builder.new.instance_eval { [binding, self] }')
116
+ eval builder_script, binding, file
117
+ builder.to_app
67
118
  end
68
119
 
120
+ # Initialize a new Rack::Builder instance. +default_app+ specifies the
121
+ # default application if +run+ is not called later. If a block
122
+ # is given, it is evaluted in the context of the instance.
69
123
  def initialize(default_app = nil, &block)
70
124
  @use, @map, @run, @warmup, @freeze_app = [], nil, default_app, nil, false
71
125
  instance_eval(&block) if block_given?
72
126
  end
73
127
 
128
+ # Create a new Rack::Builder instance and return the Rack application
129
+ # generated from it.
74
130
  def self.app(default_app = nil, &block)
75
131
  self.new(default_app, &block).to_app
76
132
  end
@@ -101,6 +157,7 @@ module Rack
101
157
  end
102
158
  @use << proc { |app| middleware.new(app, *args, &block) }
103
159
  end
160
+ ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
104
161
 
105
162
  # Takes an argument that is an object that responds to #call and returns a Rack response.
106
163
  # The simplest form of this is a lambda object:
@@ -120,7 +177,8 @@ module Rack
120
177
  @run = app
121
178
  end
122
179
 
123
- # Takes a lambda or block that is used to warm-up the application.
180
+ # Takes a lambda or block that is used to warm-up the application. This block is called
181
+ # before the Rack application is returned by to_app.
124
182
  #
125
183
  # warmup do |app|
126
184
  # client = Rack::MockRequest.new(app)
@@ -133,25 +191,31 @@ module Rack
133
191
  @warmup = prc || block
134
192
  end
135
193
 
136
- # Creates a route within the application.
194
+ # Creates a route within the application. Routes under the mapped path will be sent to
195
+ # the Rack application specified by run inside the block. Other requests will be sent to the
196
+ # default application specified by run outside the block.
137
197
  #
138
198
  # Rack::Builder.app do
139
- # map '/' do
199
+ # map '/heartbeat' do
140
200
  # run Heartbeat
141
201
  # end
202
+ # run App
142
203
  # end
143
204
  #
144
- # The +use+ method can also be used here to specify middleware to run under a specific path:
205
+ # The +use+ method can also be used inside the block to specify middleware to run under a specific path:
145
206
  #
146
207
  # Rack::Builder.app do
147
- # map '/' do
208
+ # map '/heartbeat' do
148
209
  # use Middleware
149
210
  # run Heartbeat
150
211
  # end
212
+ # run App
151
213
  # end
152
214
  #
153
- # This example includes a piece of middleware which will run before requests hit +Heartbeat+.
215
+ # This example includes a piece of middleware which will run before +/heartbeat+ requests hit +Heartbeat+.
154
216
  #
217
+ # Note that providing a +path+ of +/+ will ignore any default application given in a +run+ statement
218
+ # outside the block.
155
219
  def map(path, &block)
156
220
  @map ||= {}
157
221
  @map[path] = block
@@ -163,6 +227,7 @@ module Rack
163
227
  @freeze_app = true
164
228
  end
165
229
 
230
+ # Return the Rack application generated by this instance.
166
231
  def to_app
167
232
  app = @map ? generate_map(@run, @map) : @run
168
233
  fail "missing run or map statement" unless app
@@ -172,12 +237,17 @@ module Rack
172
237
  app
173
238
  end
174
239
 
240
+ # Call the Rack application generated by this builder instance. Note that
241
+ # this rebuilds the Rack application and runs the warmup code (if any)
242
+ # every time it is called, so it should not be used if performance is important.
175
243
  def call(env)
176
244
  to_app.call(env)
177
245
  end
178
246
 
179
247
  private
180
248
 
249
+ # Generate a URLMap instance by generating new Rack applications for each
250
+ # map block in this instance.
181
251
  def generate_map(default_app, mapping)
182
252
  mapped = default_app ? { '/' => default_app } : {}
183
253
  mapping.each { |r, b| mapped[r] = self.class.new(default_app, &b).to_app }