rack 2.0.9.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (201) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +808 -0
  3. data/CONTRIBUTING.md +142 -0
  4. data/{COPYING → MIT-LICENSE} +4 -2
  5. data/README.md +293 -0
  6. data/SPEC.rdoc +340 -0
  7. data/lib/rack/auth/abstract/handler.rb +6 -2
  8. data/lib/rack/auth/abstract/request.rb +4 -2
  9. data/lib/rack/auth/basic.rb +7 -4
  10. data/lib/rack/auth/digest/md5.rb +1 -129
  11. data/lib/rack/auth/digest/nonce.rb +1 -51
  12. data/lib/rack/auth/digest/params.rb +1 -52
  13. data/lib/rack/auth/digest/request.rb +1 -41
  14. data/lib/rack/auth/digest.rb +256 -0
  15. data/lib/rack/body_proxy.rb +18 -15
  16. data/lib/rack/builder.rb +151 -40
  17. data/lib/rack/cascade.rb +30 -12
  18. data/lib/rack/chunked.rb +74 -23
  19. data/lib/rack/common_logger.rb +49 -36
  20. data/lib/rack/conditional_get.rb +33 -26
  21. data/lib/rack/config.rb +2 -0
  22. data/lib/rack/constants.rb +63 -0
  23. data/lib/rack/content_length.rb +13 -16
  24. data/lib/rack/content_type.rb +12 -8
  25. data/lib/rack/deflater.rb +84 -45
  26. data/lib/rack/directory.rb +90 -64
  27. data/lib/rack/etag.rb +17 -23
  28. data/lib/rack/events.rb +23 -20
  29. data/lib/rack/file.rb +5 -172
  30. data/lib/rack/files.rb +216 -0
  31. data/lib/rack/head.rb +10 -9
  32. data/lib/rack/headers.rb +154 -0
  33. data/lib/rack/lint.rb +786 -645
  34. data/lib/rack/lock.rb +4 -6
  35. data/lib/rack/logger.rb +4 -0
  36. data/lib/rack/media_type.rb +10 -5
  37. data/lib/rack/method_override.rb +8 -2
  38. data/lib/rack/mime.rb +17 -1
  39. data/lib/rack/mock.rb +2 -195
  40. data/lib/rack/mock_request.rb +166 -0
  41. data/lib/rack/mock_response.rb +126 -0
  42. data/lib/rack/multipart/generator.rb +21 -15
  43. data/lib/rack/multipart/parser.rb +161 -118
  44. data/lib/rack/multipart/uploaded_file.rb +19 -7
  45. data/lib/rack/multipart.rb +23 -41
  46. data/lib/rack/null_logger.rb +11 -0
  47. data/lib/rack/query_parser.rb +126 -65
  48. data/lib/rack/recursive.rb +9 -5
  49. data/lib/rack/reloader.rb +6 -4
  50. data/lib/rack/request.rb +331 -74
  51. data/lib/rack/response.rb +223 -70
  52. data/lib/rack/rewindable_input.rb +28 -8
  53. data/lib/rack/runtime.rb +11 -8
  54. data/lib/rack/sendfile.rb +42 -33
  55. data/lib/rack/show_exceptions.rb +35 -18
  56. data/lib/rack/show_status.rb +25 -15
  57. data/lib/rack/static.rb +30 -18
  58. data/lib/rack/tempfile_reaper.rb +16 -5
  59. data/lib/rack/urlmap.rb +14 -6
  60. data/lib/rack/utils.rb +268 -260
  61. data/lib/rack/version.rb +34 -0
  62. data/lib/rack.rb +15 -92
  63. metadata +44 -207
  64. data/HISTORY.md +0 -520
  65. data/README.rdoc +0 -316
  66. data/Rakefile +0 -116
  67. data/SPEC +0 -263
  68. data/bin/rackup +0 -4
  69. data/contrib/rack.png +0 -0
  70. data/contrib/rack.svg +0 -150
  71. data/contrib/rack_logo.svg +0 -164
  72. data/contrib/rdoc.css +0 -412
  73. data/example/lobster.ru +0 -4
  74. data/example/protectedlobster.rb +0 -14
  75. data/example/protectedlobster.ru +0 -8
  76. data/lib/rack/handler/cgi.rb +0 -60
  77. data/lib/rack/handler/fastcgi.rb +0 -100
  78. data/lib/rack/handler/lsws.rb +0 -61
  79. data/lib/rack/handler/scgi.rb +0 -70
  80. data/lib/rack/handler/thin.rb +0 -36
  81. data/lib/rack/handler/webrick.rb +0 -120
  82. data/lib/rack/handler.rb +0 -99
  83. data/lib/rack/lobster.rb +0 -70
  84. data/lib/rack/server.rb +0 -395
  85. data/lib/rack/session/abstract/id.rb +0 -510
  86. data/lib/rack/session/cookie.rb +0 -204
  87. data/lib/rack/session/memcache.rb +0 -99
  88. data/lib/rack/session/pool.rb +0 -83
  89. data/rack.gemspec +0 -34
  90. data/test/builder/an_underscore_app.rb +0 -5
  91. data/test/builder/anything.rb +0 -5
  92. data/test/builder/comment.ru +0 -4
  93. data/test/builder/end.ru +0 -5
  94. data/test/builder/line.ru +0 -1
  95. data/test/builder/options.ru +0 -2
  96. data/test/cgi/assets/folder/test.js +0 -1
  97. data/test/cgi/assets/fonts/font.eot +0 -1
  98. data/test/cgi/assets/images/image.png +0 -1
  99. data/test/cgi/assets/index.html +0 -1
  100. data/test/cgi/assets/javascripts/app.js +0 -1
  101. data/test/cgi/assets/stylesheets/app.css +0 -1
  102. data/test/cgi/lighttpd.conf +0 -26
  103. data/test/cgi/rackup_stub.rb +0 -6
  104. data/test/cgi/sample_rackup.ru +0 -5
  105. data/test/cgi/test +0 -9
  106. data/test/cgi/test+directory/test+file +0 -1
  107. data/test/cgi/test.fcgi +0 -9
  108. data/test/cgi/test.gz +0 -0
  109. data/test/cgi/test.ru +0 -5
  110. data/test/gemloader.rb +0 -10
  111. data/test/helper.rb +0 -34
  112. data/test/multipart/bad_robots +0 -259
  113. data/test/multipart/binary +0 -0
  114. data/test/multipart/content_type_and_no_filename +0 -6
  115. data/test/multipart/empty +0 -10
  116. data/test/multipart/fail_16384_nofile +0 -814
  117. data/test/multipart/file1.txt +0 -1
  118. data/test/multipart/filename_and_modification_param +0 -7
  119. data/test/multipart/filename_and_no_name +0 -6
  120. data/test/multipart/filename_with_encoded_words +0 -7
  121. data/test/multipart/filename_with_escaped_quotes +0 -6
  122. data/test/multipart/filename_with_escaped_quotes_and_modification_param +0 -7
  123. data/test/multipart/filename_with_null_byte +0 -7
  124. data/test/multipart/filename_with_percent_escaped_quotes +0 -6
  125. data/test/multipart/filename_with_single_quote +0 -7
  126. data/test/multipart/filename_with_unescaped_percentages +0 -6
  127. data/test/multipart/filename_with_unescaped_percentages2 +0 -6
  128. data/test/multipart/filename_with_unescaped_percentages3 +0 -6
  129. data/test/multipart/filename_with_unescaped_quotes +0 -6
  130. data/test/multipart/ie +0 -6
  131. data/test/multipart/invalid_character +0 -6
  132. data/test/multipart/mixed_files +0 -21
  133. data/test/multipart/nested +0 -10
  134. data/test/multipart/none +0 -9
  135. data/test/multipart/quoted +0 -15
  136. data/test/multipart/rack-logo.png +0 -0
  137. data/test/multipart/semicolon +0 -6
  138. data/test/multipart/text +0 -15
  139. data/test/multipart/three_files_three_fields +0 -31
  140. data/test/multipart/unity3d_wwwform +0 -11
  141. data/test/multipart/webkit +0 -32
  142. data/test/rackup/config.ru +0 -31
  143. data/test/registering_handler/rack/handler/registering_myself.rb +0 -8
  144. data/test/spec_auth_basic.rb +0 -89
  145. data/test/spec_auth_digest.rb +0 -260
  146. data/test/spec_body_proxy.rb +0 -85
  147. data/test/spec_builder.rb +0 -233
  148. data/test/spec_cascade.rb +0 -63
  149. data/test/spec_cgi.rb +0 -84
  150. data/test/spec_chunked.rb +0 -103
  151. data/test/spec_common_logger.rb +0 -107
  152. data/test/spec_conditional_get.rb +0 -103
  153. data/test/spec_config.rb +0 -23
  154. data/test/spec_content_length.rb +0 -86
  155. data/test/spec_content_type.rb +0 -46
  156. data/test/spec_deflater.rb +0 -375
  157. data/test/spec_directory.rb +0 -148
  158. data/test/spec_etag.rb +0 -108
  159. data/test/spec_events.rb +0 -133
  160. data/test/spec_fastcgi.rb +0 -85
  161. data/test/spec_file.rb +0 -264
  162. data/test/spec_handler.rb +0 -57
  163. data/test/spec_head.rb +0 -46
  164. data/test/spec_lint.rb +0 -520
  165. data/test/spec_lobster.rb +0 -59
  166. data/test/spec_lock.rb +0 -204
  167. data/test/spec_logger.rb +0 -24
  168. data/test/spec_media_type.rb +0 -42
  169. data/test/spec_method_override.rb +0 -110
  170. data/test/spec_mime.rb +0 -51
  171. data/test/spec_mock.rb +0 -359
  172. data/test/spec_multipart.rb +0 -721
  173. data/test/spec_null_logger.rb +0 -21
  174. data/test/spec_recursive.rb +0 -75
  175. data/test/spec_request.rb +0 -1423
  176. data/test/spec_response.rb +0 -528
  177. data/test/spec_rewindable_input.rb +0 -128
  178. data/test/spec_runtime.rb +0 -50
  179. data/test/spec_sendfile.rb +0 -125
  180. data/test/spec_server.rb +0 -193
  181. data/test/spec_session_abstract_id.rb +0 -31
  182. data/test/spec_session_abstract_session_hash.rb +0 -45
  183. data/test/spec_session_cookie.rb +0 -442
  184. data/test/spec_session_memcache.rb +0 -357
  185. data/test/spec_session_persisted_secure_secure_session_hash.rb +0 -73
  186. data/test/spec_session_pool.rb +0 -247
  187. data/test/spec_show_exceptions.rb +0 -93
  188. data/test/spec_show_status.rb +0 -104
  189. data/test/spec_static.rb +0 -184
  190. data/test/spec_tempfile_reaper.rb +0 -64
  191. data/test/spec_thin.rb +0 -96
  192. data/test/spec_urlmap.rb +0 -237
  193. data/test/spec_utils.rb +0 -742
  194. data/test/spec_version.rb +0 -11
  195. data/test/spec_webrick.rb +0 -206
  196. data/test/static/another/index.html +0 -1
  197. data/test/static/foo.html +0 -1
  198. data/test/static/index.html +0 -1
  199. data/test/testrequest.rb +0 -78
  200. data/test/unregistered_handler/rack/handler/unregistered.rb +0 -7
  201. data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +0 -7
data/SPEC.rdoc ADDED
@@ -0,0 +1,340 @@
1
+ This specification aims to formalize the Rack protocol. You
2
+ can (and should) use Rack::Lint to enforce it.
3
+
4
+ When you develop middleware, be sure to add a Lint before and
5
+ after to catch all mistakes.
6
+
7
+ = Rack applications
8
+
9
+ A Rack application is a Ruby object (not a class) that
10
+ responds to +call+.
11
+ It takes exactly one argument, the *environment*
12
+ and returns a non-frozen Array of exactly three values:
13
+ The *status*,
14
+ the *headers*,
15
+ and the *body*.
16
+
17
+ == The Environment
18
+
19
+ The environment must be an unfrozen instance of Hash that includes
20
+ CGI-like headers. The Rack application is free to modify the
21
+ environment.
22
+
23
+ The environment is required to include these variables
24
+ (adopted from {PEP 333}[https://peps.python.org/pep-0333/]), except when they'd be empty, but see
25
+ below.
26
+ <tt>REQUEST_METHOD</tt>:: The HTTP request method, such as
27
+ "GET" or "POST". This cannot ever
28
+ be an empty string, and so is
29
+ always required.
30
+ <tt>SCRIPT_NAME</tt>:: The initial portion of the request
31
+ URL's "path" that corresponds to the
32
+ application object, so that the
33
+ application knows its virtual
34
+ "location". This may be an empty
35
+ string, if the application corresponds
36
+ to the "root" of the server.
37
+ <tt>PATH_INFO</tt>:: The remainder of the request URL's
38
+ "path", designating the virtual
39
+ "location" of the request's target
40
+ within the application. This may be an
41
+ empty string, if the request URL targets
42
+ the application root and does not have a
43
+ trailing slash. This value may be
44
+ percent-encoded when originating from
45
+ a URL.
46
+ <tt>QUERY_STRING</tt>:: The portion of the request URL that
47
+ follows the <tt>?</tt>, if any. May be
48
+ empty, but is always required!
49
+ <tt>SERVER_NAME</tt>:: When combined with <tt>SCRIPT_NAME</tt> and
50
+ <tt>PATH_INFO</tt>, these variables can be
51
+ used to complete the URL. Note, however,
52
+ that <tt>HTTP_HOST</tt>, if present,
53
+ should be used in preference to
54
+ <tt>SERVER_NAME</tt> for reconstructing
55
+ the request URL.
56
+ <tt>SERVER_NAME</tt> can never be an empty
57
+ string, and so is always required.
58
+ <tt>SERVER_PORT</tt>:: An optional +Integer+ which is the port the
59
+ server is running on. Should be specified if
60
+ the server is running on a non-standard port.
61
+ <tt>SERVER_PROTOCOL</tt>:: A string representing the HTTP version used
62
+ for the request.
63
+ <tt>HTTP_</tt> Variables:: Variables corresponding to the
64
+ client-supplied HTTP request
65
+ headers (i.e., variables whose
66
+ names begin with <tt>HTTP_</tt>). The
67
+ presence or absence of these
68
+ variables should correspond with
69
+ the presence or absence of the
70
+ appropriate HTTP header in the
71
+ request. See
72
+ {RFC3875 section 4.1.18}[https://tools.ietf.org/html/rfc3875#section-4.1.18]
73
+ for specific behavior.
74
+ In addition to this, the Rack environment must include these
75
+ Rack-specific variables:
76
+ <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the
77
+ request URL.
78
+ <tt>rack.input</tt>:: See below, the input stream.
79
+ <tt>rack.errors</tt>:: See below, the error stream.
80
+ <tt>rack.hijack?</tt>:: See below, if present and true, indicates
81
+ that the server supports partial hijacking.
82
+ <tt>rack.hijack</tt>:: See below, if present, an object responding
83
+ to +call+ that is used to perform a full
84
+ hijack.
85
+ Additional environment specifications have approved to
86
+ standardized middleware APIs. None of these are required to
87
+ be implemented by the server.
88
+ <tt>rack.session</tt>:: A hash-like interface for storing
89
+ request session data.
90
+ The store must implement:
91
+ store(key, value) (aliased as []=);
92
+ fetch(key, default = nil) (aliased as []);
93
+ delete(key);
94
+ clear;
95
+ to_hash (returning unfrozen Hash instance);
96
+ <tt>rack.logger</tt>:: A common object interface for logging messages.
97
+ The object must implement:
98
+ info(message, &block)
99
+ debug(message, &block)
100
+ warn(message, &block)
101
+ error(message, &block)
102
+ fatal(message, &block)
103
+ <tt>rack.multipart.buffer_size</tt>:: An Integer hint to the multipart parser as to what chunk size to use for reads and writes.
104
+ <tt>rack.multipart.tempfile_factory</tt>:: An object responding to #call with two arguments, the filename and content_type given for the multipart form field, and returning an IO-like object that responds to #<< and optionally #rewind. This factory will be used to instantiate the tempfile for each multipart form file upload field, rather than the default class of Tempfile.
105
+ The server or the application can store their own data in the
106
+ environment, too. The keys must contain at least one dot,
107
+ and should be prefixed uniquely. The prefix <tt>rack.</tt>
108
+ is reserved for use with the Rack core distribution and other
109
+ accepted specifications and must not be used otherwise.
110
+
111
+ The <tt>SERVER_PORT</tt> must be an Integer if set.
112
+ The <tt>SERVER_NAME</tt> must be a valid authority as defined by RFC7540.
113
+ The <tt>HTTP_HOST</tt> must be a valid authority as defined by RFC7540.
114
+ The <tt>SERVER_PROTOCOL</tt> must match the regexp <tt>HTTP/\d(\.\d)?</tt>.
115
+ If the <tt>HTTP_VERSION</tt> is present, it must equal the <tt>SERVER_PROTOCOL</tt>.
116
+ The environment must not contain the keys
117
+ <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
118
+ (use the versions without <tt>HTTP_</tt>).
119
+ The CGI keys (named without a period) must have String values.
120
+ If the string values for CGI keys contain non-ASCII characters,
121
+ they should use ASCII-8BIT encoding.
122
+ There are the following restrictions:
123
+ * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
124
+ * There must be a valid input stream in <tt>rack.input</tt>.
125
+ * There must be a valid error stream in <tt>rack.errors</tt>.
126
+ * There may be a valid hijack callback in <tt>rack.hijack</tt>
127
+ * The <tt>REQUEST_METHOD</tt> must be a valid token.
128
+ * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
129
+ * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
130
+ * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
131
+ * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
132
+ set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
133
+ <tt>SCRIPT_NAME</tt> is empty.
134
+ <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
135
+ <tt>rack.response_finished</tt>:: An array of callables run by the server after the response has been
136
+ processed. This would typically be invoked after sending the response to the client, but it could also be
137
+ invoked if an error occurs while generating the response or sending the response; in that case, the error
138
+ argument will be a subclass of +Exception+.
139
+ The callables are invoked with +env, status, headers, error+ arguments and should not raise any
140
+ exceptions. They should be invoked in reverse order of registration.
141
+
142
+ === The Input Stream
143
+
144
+ The input stream is an IO-like object which contains the raw HTTP
145
+ POST data.
146
+ When applicable, its external encoding must be "ASCII-8BIT" and it
147
+ must be opened in binary mode, for Ruby 1.9 compatibility.
148
+ The input stream must respond to +gets+, +each+, and +read+.
149
+ * +gets+ must be called without arguments and return a string,
150
+ or +nil+ on EOF.
151
+ * +read+ behaves like IO#read.
152
+ Its signature is <tt>read([length, [buffer]])</tt>.
153
+
154
+ If given, +length+ must be a non-negative Integer (>= 0) or +nil+,
155
+ and +buffer+ must be a String and may not be nil.
156
+
157
+ If +length+ is given and not nil, then this method reads at most
158
+ +length+ bytes from the input stream.
159
+
160
+ If +length+ is not given or nil, then this method reads
161
+ all data until EOF.
162
+
163
+ When EOF is reached, this method returns nil if +length+ is given
164
+ and not nil, or "" if +length+ is not given or is nil.
165
+
166
+ If +buffer+ is given, then the read data will be placed
167
+ into +buffer+ instead of a newly created String object.
168
+ * +each+ must be called without arguments and only yield Strings.
169
+ * +close+ can be called on the input stream to indicate that the
170
+ any remaining input is not needed.
171
+
172
+ === The Error Stream
173
+
174
+ The error stream must respond to +puts+, +write+ and +flush+.
175
+ * +puts+ must be called with a single argument that responds to +to_s+.
176
+ * +write+ must be called with a single argument that is a String.
177
+ * +flush+ must be called without arguments and must be called
178
+ in order to make the error appear for sure.
179
+ * +close+ must never be called on the error stream.
180
+
181
+ === Hijacking
182
+
183
+ The hijacking interfaces provides a means for an application to take
184
+ control of the HTTP connection. There are two distinct hijack
185
+ interfaces: full hijacking where the application takes over the raw
186
+ connection, and partial hijacking where the application takes over
187
+ just the response body stream. In both cases, the application is
188
+ responsible for closing the hijacked stream.
189
+
190
+ Full hijacking only works with HTTP/1. Partial hijacking is functionally
191
+ equivalent to streaming bodies, and is still optionally supported for
192
+ backwards compatibility with older Rack versions.
193
+
194
+ ==== Full Hijack
195
+
196
+ Full hijack is used to completely take over an HTTP/1 connection. It
197
+ occurs before any headers are written and causes the request to
198
+ ignores any response generated by the application.
199
+
200
+ It is intended to be used when applications need access to raw HTTP/1
201
+ connection.
202
+
203
+ If +rack.hijack+ is present in +env+, it must respond to +call+
204
+ and return an +IO+ instance which can be used to read and write
205
+ to the underlying connection using HTTP/1 semantics and
206
+ formatting.
207
+
208
+ ==== Partial Hijack
209
+
210
+ Partial hijack is used for bi-directional streaming of the request and
211
+ response body. It occurs after the status and headers are written by
212
+ the server and causes the server to ignore the Body of the response.
213
+
214
+ It is intended to be used when applications need bi-directional
215
+ streaming.
216
+
217
+ If +rack.hijack?+ is present in +env+ and truthy,
218
+ an application may set the special response header +rack.hijack+
219
+ to an object that responds to +call+,
220
+ accepting a +stream+ argument.
221
+
222
+ After the response status and headers have been sent, this hijack
223
+ callback will be invoked with a +stream+ argument which follows the
224
+ same interface as outlined in "Streaming Body". Servers must
225
+ ignore the +body+ part of the response tuple when the
226
+ +rack.hijack+ response header is present. Using an empty +Array+
227
+ instance is recommended.
228
+
229
+ The special response header +rack.hijack+ must only be set
230
+ if the request +env+ has a truthy +rack.hijack?+.
231
+ == The Response
232
+
233
+ === The Status
234
+
235
+ This is an HTTP status. It must be an Integer greater than or equal to
236
+ 100.
237
+
238
+ === The Headers
239
+
240
+ The headers must be a unfrozen Hash.
241
+ The header keys must be Strings.
242
+ Special headers starting "rack." are for communicating with the
243
+ server, and must not be sent back to the client.
244
+ The header must not contain a +Status+ key.
245
+ Header keys must conform to RFC7230 token specification, i.e. cannot
246
+ contain non-printable ASCII, DQUOTE or "(),/:;<=>?@[\]{}".
247
+ Header keys must not contain uppercase ASCII characters (A-Z).
248
+ Header values must be either a String instance,
249
+ or an Array of String instances,
250
+ such that each String instance must not contain characters below 037.
251
+
252
+ === The content-type
253
+
254
+ There must not be a <tt>content-type</tt> header key when the +Status+ is 1xx,
255
+ 204, or 304.
256
+
257
+ === The content-length
258
+
259
+ There must not be a <tt>content-length</tt> header key when the
260
+ +Status+ is 1xx, 204, or 304.
261
+
262
+ === The Body
263
+
264
+ The Body is typically an +Array+ of +String+ instances, an enumerable
265
+ that yields +String+ instances, a +Proc+ instance, or a File-like
266
+ object.
267
+
268
+ The Body must respond to +each+ or +call+. It may optionally respond
269
+ to +to_path+ or +to_ary+. A Body that responds to +each+ is considered
270
+ to be an Enumerable Body. A Body that responds to +call+ is considered
271
+ to be a Streaming Body.
272
+
273
+ A Body that responds to both +each+ and +call+ must be treated as an
274
+ Enumerable Body, not a Streaming Body. If it responds to +each+, you
275
+ must call +each+ and not +call+. If the Body doesn't respond to
276
+ +each+, then you can assume it responds to +call+.
277
+
278
+ The Body must either be consumed or returned. The Body is consumed by
279
+ optionally calling either +each+ or +call+.
280
+ Then, if the Body responds to +close+, it must be called to release
281
+ any resources associated with the generation of the body.
282
+ In other words, +close+ must always be called at least once; typically
283
+ after the web server has sent the response to the client, but also in
284
+ cases where the Rack application makes internal/virtual requests and
285
+ discards the response.
286
+
287
+
288
+ After calling +close+, the Body is considered closed and should not
289
+ be consumed again.
290
+ If the original Body is replaced by a new Body, the new Body must
291
+ also consume the original Body by calling +close+ if possible.
292
+
293
+ If the Body responds to +to_path+, it must return a +String+
294
+ path for the local file system whose contents are identical
295
+ to that produced by calling +each+; this may be used by the
296
+ server as an alternative, possibly more efficient way to
297
+ transport the response. The +to_path+ method does not consume
298
+ the body.
299
+
300
+ ==== Enumerable Body
301
+
302
+ The Enumerable Body must respond to +each+.
303
+ It must only be called once.
304
+ It must not be called after being closed.
305
+ and must only yield String values.
306
+
307
+ The Body itself should not be an instance of String, as this will
308
+ break in Ruby 1.9.
309
+
310
+ Middleware must not call +each+ directly on the Body.
311
+ Instead, middleware can return a new Body that calls +each+ on the
312
+ original Body, yielding at least once per iteration.
313
+
314
+ If the Body responds to +to_ary+, it must return an +Array+ whose
315
+ contents are identical to that produced by calling +each+.
316
+ Middleware may call +to_ary+ directly on the Body and return a new
317
+ Body in its place. In other words, middleware can only process the
318
+ Body directly if it responds to +to_ary+. If the Body responds to both
319
+ +to_ary+ and +close+, its implementation of +to_ary+ must call
320
+ +close+.
321
+
322
+ ==== Streaming Body
323
+
324
+ The Streaming Body must respond to +call+.
325
+ It must only be called once.
326
+ It must not be called after being closed.
327
+ It takes a +stream+ argument.
328
+
329
+ The +stream+ argument must implement:
330
+ <tt>read, write, <<, flush, close, close_read, close_write, closed?</tt>
331
+
332
+ The semantics of these IO methods must be a best effort match to
333
+ those of a normal Ruby IO or Socket object, using standard arguments
334
+ and raising standard exceptions. Servers are encouraged to simply
335
+ pass on real IO objects, although it is recognized that this approach
336
+ is not directly compatible with HTTP/2.
337
+
338
+ == Thanks
339
+ Some parts of this specification are adopted from {PEP 333 – Python Web Server Gateway Interface v1.0}[https://peps.python.org/pep-0333/]
340
+ I'd like to thank everyone involved in that effort.
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../constants'
4
+
1
5
  module Rack
2
6
  module Auth
3
7
  # Rack::Auth::AbstractHandler implements common authentication functionality.
@@ -8,7 +12,7 @@ module Rack
8
12
 
9
13
  attr_accessor :realm
10
14
 
11
- def initialize(app, realm=nil, &authenticator)
15
+ def initialize(app, realm = nil, &authenticator)
12
16
  @app, @realm, @authenticator = app, realm, authenticator
13
17
  end
14
18
 
@@ -19,7 +23,7 @@ module Rack
19
23
  return [ 401,
20
24
  { CONTENT_TYPE => 'text/plain',
21
25
  CONTENT_LENGTH => '0',
22
- 'WWW-Authenticate' => www_authenticate.to_s },
26
+ 'www-authenticate' => www_authenticate.to_s },
23
27
  []
24
28
  ]
25
29
  end
@@ -1,4 +1,6 @@
1
- require 'rack/request'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../request'
2
4
 
3
5
  module Rack
4
6
  module Auth
@@ -25,7 +27,7 @@ module Rack
25
27
  end
26
28
 
27
29
  def scheme
28
- @scheme ||= parts.first && parts.first.downcase
30
+ @scheme ||= parts.first&.downcase
29
31
  end
30
32
 
31
33
  def params
@@ -1,5 +1,8 @@
1
- require 'rack/auth/abstract/handler'
2
- require 'rack/auth/abstract/request'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'abstract/handler'
4
+ require_relative 'abstract/request'
5
+ require 'base64'
3
6
 
4
7
  module Rack
5
8
  module Auth
@@ -41,11 +44,11 @@ module Rack
41
44
 
42
45
  class Request < Auth::AbstractRequest
43
46
  def basic?
44
- "basic" == scheme
47
+ "basic" == scheme && credentials.length == 2
45
48
  end
46
49
 
47
50
  def credentials
48
- @credentials ||= params.unpack("m*").first.split(/:/, 2)
51
+ @credentials ||= Base64.decode64(params).split(':', 2)
49
52
  end
50
53
 
51
54
  def username
@@ -1,129 +1 @@
1
- require 'rack/auth/abstract/handler'
2
- require 'rack/auth/digest/request'
3
- require 'rack/auth/digest/params'
4
- require 'rack/auth/digest/nonce'
5
- require 'digest/md5'
6
-
7
- module Rack
8
- module Auth
9
- module Digest
10
- # Rack::Auth::Digest::MD5 implements the MD5 algorithm version of
11
- # HTTP Digest Authentication, as per RFC 2617.
12
- #
13
- # Initialize with the [Rack] application that you want protecting,
14
- # and a block that looks up a plaintext password for a given username.
15
- #
16
- # +opaque+ needs to be set to a constant base64/hexadecimal string.
17
- #
18
- class MD5 < AbstractHandler
19
-
20
- attr_accessor :opaque
21
-
22
- attr_writer :passwords_hashed
23
-
24
- def initialize(app, realm=nil, opaque=nil, &authenticator)
25
- @passwords_hashed = nil
26
- if opaque.nil? and realm.respond_to? :values_at
27
- realm, opaque, @passwords_hashed = realm.values_at :realm, :opaque, :passwords_hashed
28
- end
29
- super(app, realm, &authenticator)
30
- @opaque = opaque
31
- end
32
-
33
- def passwords_hashed?
34
- !!@passwords_hashed
35
- end
36
-
37
- def call(env)
38
- auth = Request.new(env)
39
-
40
- unless auth.provided?
41
- return unauthorized
42
- end
43
-
44
- if !auth.digest? || !auth.correct_uri? || !valid_qop?(auth)
45
- return bad_request
46
- end
47
-
48
- if valid?(auth)
49
- if auth.nonce.stale?
50
- return unauthorized(challenge(:stale => true))
51
- else
52
- env['REMOTE_USER'] = auth.username
53
-
54
- return @app.call(env)
55
- end
56
- end
57
-
58
- unauthorized
59
- end
60
-
61
-
62
- private
63
-
64
- QOP = 'auth'.freeze
65
-
66
- def params(hash = {})
67
- Params.new do |params|
68
- params['realm'] = realm
69
- params['nonce'] = Nonce.new.to_s
70
- params['opaque'] = H(opaque)
71
- params['qop'] = QOP
72
-
73
- hash.each { |k, v| params[k] = v }
74
- end
75
- end
76
-
77
- def challenge(hash = {})
78
- "Digest #{params(hash)}"
79
- end
80
-
81
- def valid?(auth)
82
- valid_opaque?(auth) && valid_nonce?(auth) && valid_digest?(auth)
83
- end
84
-
85
- def valid_qop?(auth)
86
- QOP == auth.qop
87
- end
88
-
89
- def valid_opaque?(auth)
90
- H(opaque) == auth.opaque
91
- end
92
-
93
- def valid_nonce?(auth)
94
- auth.nonce.valid?
95
- end
96
-
97
- def valid_digest?(auth)
98
- pw = @authenticator.call(auth.username)
99
- pw && Rack::Utils.secure_compare(digest(auth, pw), auth.response)
100
- end
101
-
102
- def md5(data)
103
- ::Digest::MD5.hexdigest(data)
104
- end
105
-
106
- alias :H :md5
107
-
108
- def KD(secret, data)
109
- H([secret, data] * ':')
110
- end
111
-
112
- def A1(auth, password)
113
- [ auth.username, auth.realm, password ] * ':'
114
- end
115
-
116
- def A2(auth)
117
- [ auth.method, auth.uri ] * ':'
118
- end
119
-
120
- def digest(auth, password)
121
- password_hash = passwords_hashed? ? password : H(A1(auth, password))
122
-
123
- KD(password_hash, [ auth.nonce, auth.nc, auth.cnonce, QOP, H(A2(auth)) ] * ':')
124
- end
125
-
126
- end
127
- end
128
- end
129
- end
1
+ require_relative '../digest'
@@ -1,51 +1 @@
1
- require 'digest/md5'
2
-
3
- module Rack
4
- module Auth
5
- module Digest
6
- # Rack::Auth::Digest::Nonce is the default nonce generator for the
7
- # Rack::Auth::Digest::MD5 authentication handler.
8
- #
9
- # +private_key+ needs to set to a constant string.
10
- #
11
- # +time_limit+ can be optionally set to an integer (number of seconds),
12
- # to limit the validity of the generated nonces.
13
-
14
- class Nonce
15
-
16
- class << self
17
- attr_accessor :private_key, :time_limit
18
- end
19
-
20
- def self.parse(string)
21
- new(*string.unpack("m*").first.split(' ', 2))
22
- end
23
-
24
- def initialize(timestamp = Time.now, given_digest = nil)
25
- @timestamp, @given_digest = timestamp.to_i, given_digest
26
- end
27
-
28
- def to_s
29
- [([ @timestamp, digest ] * ' ')].pack("m*").strip
30
- end
31
-
32
- def digest
33
- ::Digest::MD5.hexdigest([ @timestamp, self.class.private_key ] * ':')
34
- end
35
-
36
- def valid?
37
- digest == @given_digest
38
- end
39
-
40
- def stale?
41
- !self.class.time_limit.nil? && (Time.now.to_i - @timestamp) > self.class.time_limit
42
- end
43
-
44
- def fresh?
45
- !stale?
46
- end
47
-
48
- end
49
- end
50
- end
51
- end
1
+ require_relative '../digest'
@@ -1,52 +1 @@
1
- module Rack
2
- module Auth
3
- module Digest
4
- class Params < Hash
5
-
6
- def self.parse(str)
7
- Params[*split_header_value(str).map do |param|
8
- k, v = param.split('=', 2)
9
- [k, dequote(v)]
10
- end.flatten]
11
- end
12
-
13
- def self.dequote(str) # From WEBrick::HTTPUtils
14
- ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
15
- ret.gsub!(/\\(.)/, "\\1")
16
- ret
17
- end
18
-
19
- def self.split_header_value(str)
20
- str.scan(/\w+\=(?:"[^\"]+"|[^,]+)/n)
21
- end
22
-
23
- def initialize
24
- super()
25
-
26
- yield self if block_given?
27
- end
28
-
29
- def [](k)
30
- super k.to_s
31
- end
32
-
33
- def []=(k, v)
34
- super k.to_s, v.to_s
35
- end
36
-
37
- UNQUOTED = ['nc', 'stale']
38
-
39
- def to_s
40
- map do |k, v|
41
- "#{k}=" << (UNQUOTED.include?(k) ? v.to_s : quote(v))
42
- end.join(', ')
43
- end
44
-
45
- def quote(str) # From WEBrick::HTTPUtils
46
- '"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
47
- end
48
-
49
- end
50
- end
51
- end
52
- end
1
+ require_relative '../digest'
@@ -1,41 +1 @@
1
- require 'rack/auth/abstract/request'
2
- require 'rack/auth/digest/params'
3
- require 'rack/auth/digest/nonce'
4
-
5
- module Rack
6
- module Auth
7
- module Digest
8
- class Request < Auth::AbstractRequest
9
- def method
10
- @env[RACK_METHODOVERRIDE_ORIGINAL_METHOD] || @env[REQUEST_METHOD]
11
- end
12
-
13
- def digest?
14
- "digest" == scheme
15
- end
16
-
17
- def correct_uri?
18
- request.fullpath == uri
19
- end
20
-
21
- def nonce
22
- @nonce ||= Nonce.parse(params['nonce'])
23
- end
24
-
25
- def params
26
- @params ||= Params.parse(parts.last)
27
- end
28
-
29
- def respond_to?(sym, *)
30
- super or params.has_key? sym.to_s
31
- end
32
-
33
- def method_missing(sym, *args)
34
- return super unless params.has_key?(key = sym.to_s)
35
- return params[key] if args.size == 0
36
- raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
37
- end
38
- end
39
- end
40
- end
41
- end
1
+ require_relative '../digest'