rack 2.2.17 → 3.2.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.

Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +501 -70
  3. data/CONTRIBUTING.md +63 -55
  4. data/MIT-LICENSE +1 -1
  5. data/README.md +376 -0
  6. data/SPEC.rdoc +243 -277
  7. data/lib/rack/auth/abstract/handler.rb +3 -1
  8. data/lib/rack/auth/abstract/request.rb +5 -1
  9. data/lib/rack/auth/basic.rb +1 -3
  10. data/lib/rack/bad_request.rb +8 -0
  11. data/lib/rack/body_proxy.rb +21 -3
  12. data/lib/rack/builder.rb +108 -69
  13. data/lib/rack/cascade.rb +2 -3
  14. data/lib/rack/common_logger.rb +22 -17
  15. data/lib/rack/conditional_get.rb +20 -16
  16. data/lib/rack/constants.rb +68 -0
  17. data/lib/rack/content_length.rb +12 -16
  18. data/lib/rack/content_type.rb +8 -5
  19. data/lib/rack/deflater.rb +40 -26
  20. data/lib/rack/directory.rb +9 -3
  21. data/lib/rack/etag.rb +17 -23
  22. data/lib/rack/events.rb +4 -0
  23. data/lib/rack/files.rb +15 -17
  24. data/lib/rack/head.rb +8 -8
  25. data/lib/rack/headers.rb +238 -0
  26. data/lib/rack/lint.rb +817 -648
  27. data/lib/rack/lock.rb +2 -5
  28. data/lib/rack/media_type.rb +6 -7
  29. data/lib/rack/method_override.rb +5 -1
  30. data/lib/rack/mime.rb +14 -5
  31. data/lib/rack/mock.rb +1 -300
  32. data/lib/rack/mock_request.rb +161 -0
  33. data/lib/rack/mock_response.rb +147 -0
  34. data/lib/rack/multipart/generator.rb +7 -5
  35. data/lib/rack/multipart/parser.rb +241 -95
  36. data/lib/rack/multipart/uploaded_file.rb +45 -4
  37. data/lib/rack/multipart.rb +53 -40
  38. data/lib/rack/null_logger.rb +9 -0
  39. data/lib/rack/query_parser.rb +116 -121
  40. data/lib/rack/recursive.rb +2 -0
  41. data/lib/rack/reloader.rb +0 -2
  42. data/lib/rack/request.rb +269 -141
  43. data/lib/rack/response.rb +151 -66
  44. data/lib/rack/rewindable_input.rb +27 -5
  45. data/lib/rack/runtime.rb +7 -6
  46. data/lib/rack/sendfile.rb +30 -25
  47. data/lib/rack/show_exceptions.rb +25 -6
  48. data/lib/rack/show_status.rb +17 -9
  49. data/lib/rack/static.rb +8 -8
  50. data/lib/rack/tempfile_reaper.rb +15 -4
  51. data/lib/rack/urlmap.rb +3 -1
  52. data/lib/rack/utils.rb +228 -238
  53. data/lib/rack/version.rb +3 -15
  54. data/lib/rack.rb +13 -90
  55. metadata +15 -41
  56. data/README.rdoc +0 -347
  57. data/Rakefile +0 -130
  58. data/bin/rackup +0 -5
  59. data/contrib/rack.png +0 -0
  60. data/contrib/rack.svg +0 -150
  61. data/contrib/rack_logo.svg +0 -164
  62. data/contrib/rdoc.css +0 -412
  63. data/example/lobster.ru +0 -6
  64. data/example/protectedlobster.rb +0 -16
  65. data/example/protectedlobster.ru +0 -10
  66. data/lib/rack/auth/digest/md5.rb +0 -131
  67. data/lib/rack/auth/digest/nonce.rb +0 -53
  68. data/lib/rack/auth/digest/params.rb +0 -54
  69. data/lib/rack/auth/digest/request.rb +0 -43
  70. data/lib/rack/chunked.rb +0 -117
  71. data/lib/rack/core_ext/regexp.rb +0 -14
  72. data/lib/rack/file.rb +0 -7
  73. data/lib/rack/handler/cgi.rb +0 -59
  74. data/lib/rack/handler/fastcgi.rb +0 -100
  75. data/lib/rack/handler/lsws.rb +0 -61
  76. data/lib/rack/handler/scgi.rb +0 -71
  77. data/lib/rack/handler/thin.rb +0 -36
  78. data/lib/rack/handler/webrick.rb +0 -129
  79. data/lib/rack/handler.rb +0 -104
  80. data/lib/rack/lobster.rb +0 -70
  81. data/lib/rack/logger.rb +0 -20
  82. data/lib/rack/server.rb +0 -466
  83. data/lib/rack/session/abstract/id.rb +0 -523
  84. data/lib/rack/session/cookie.rb +0 -203
  85. data/lib/rack/session/memcache.rb +0 -10
  86. data/lib/rack/session/pool.rb +0 -90
  87. data/rack.gemspec +0 -46
data/SPEC.rdoc CHANGED
@@ -1,292 +1,258 @@
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
- = Rack applications
7
- A Rack application is a Ruby object (not a class) that
8
- responds to +call+.
9
- It takes exactly one argument, the *environment*
10
- and returns an Array of exactly three values:
11
- The *status*,
12
- the *headers*,
13
- and the *body*.
14
- == The Environment
15
- The environment must be an unfrozen instance of Hash that includes
16
- CGI-like headers. The application is free to modify the
17
- environment.
18
-
19
- The environment is required to include these variables
20
- (adopted from PEP333), except when they'd be empty, but see
21
- below.
22
- <tt>REQUEST_METHOD</tt>:: The HTTP request method, such as
23
- "GET" or "POST". This cannot ever
24
- be an empty string, and so is
25
- always required.
26
- <tt>SCRIPT_NAME</tt>:: The initial portion of the request
27
- URL's "path" that corresponds to the
28
- application object, so that the
29
- application knows its virtual
30
- "location". This may be an empty
31
- string, if the application corresponds
32
- to the "root" of the server.
33
- <tt>PATH_INFO</tt>:: The remainder of the request URL's
34
- "path", designating the virtual
35
- "location" of the request's target
36
- within the application. This may be an
37
- empty string, if the request URL targets
38
- the application root and does not have a
39
- trailing slash. This value may be
40
- percent-encoded when originating from
41
- a URL.
42
- <tt>QUERY_STRING</tt>:: The portion of the request URL that
43
- follows the <tt>?</tt>, if any. May be
44
- empty, but is always required!
45
- <tt>SERVER_NAME</tt>:: When combined with <tt>SCRIPT_NAME</tt> and
46
- <tt>PATH_INFO</tt>, these variables can be
47
- used to complete the URL. Note, however,
48
- that <tt>HTTP_HOST</tt>, if present,
49
- should be used in preference to
50
- <tt>SERVER_NAME</tt> for reconstructing
51
- the request URL.
52
- <tt>SERVER_NAME</tt> can never be an empty
53
- string, and so is always required.
54
- <tt>SERVER_PORT</tt>:: An optional +Integer+ which is the port the
55
- server is running on. Should be specified if
56
- the server is running on a non-standard port.
57
- <tt>HTTP_</tt> Variables:: Variables corresponding to the
58
- client-supplied HTTP request
59
- headers (i.e., variables whose
60
- names begin with <tt>HTTP_</tt>). The
61
- presence or absence of these
62
- variables should correspond with
63
- the presence or absence of the
64
- appropriate HTTP header in the
65
- request. See
66
- {RFC3875 section 4.1.18}[https://tools.ietf.org/html/rfc3875#section-4.1.18]
67
- for specific behavior.
68
- In addition to this, the Rack environment must include these
69
- Rack-specific variables:
70
- <tt>rack.version</tt>:: The Array representing this version of Rack
71
- See Rack::VERSION, that corresponds to
72
- the version of this SPEC.
73
- <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the
74
- request URL.
75
- <tt>rack.input</tt>:: See below, the input stream.
76
- <tt>rack.errors</tt>:: See below, the error stream.
77
- <tt>rack.multithread</tt>:: true if the application object may be
78
- simultaneously invoked by another thread
79
- in the same process, false otherwise.
80
- <tt>rack.multiprocess</tt>:: true if an equivalent application object
81
- may be simultaneously invoked by another
82
- process, false otherwise.
83
- <tt>rack.run_once</tt>:: true if the server expects
84
- (but does not guarantee!) that the
85
- application will only be invoked this one
86
- time during the life of its containing
87
- process. Normally, this will only be true
88
- for a server based on CGI
89
- (or something similar).
90
- <tt>rack.hijack?</tt>:: present and true if the server supports
91
- connection hijacking. See below, hijacking.
92
- <tt>rack.hijack</tt>:: an object responding to #call that must be
93
- called at least once before using
94
- rack.hijack_io.
95
- It is recommended #call return rack.hijack_io
96
- as well as setting it in env if necessary.
97
- <tt>rack.hijack_io</tt>:: if rack.hijack? is true, and rack.hijack
98
- has received #call, this will contain
99
- an object resembling an IO. See hijacking.
100
- Additional environment specifications have approved to
101
- standardized middleware APIs. None of these are required to
102
- be implemented by the server.
103
- <tt>rack.session</tt>:: A hash like interface for storing
104
- request session data.
105
- The store must implement:
106
- store(key, value) (aliased as []=);
107
- fetch(key, default = nil) (aliased as []);
108
- delete(key);
109
- clear;
110
- to_hash (returning unfrozen Hash instance);
111
- <tt>rack.logger</tt>:: A common object interface for logging messages.
112
- The object must implement:
113
- info(message, &block)
114
- debug(message, &block)
115
- warn(message, &block)
116
- error(message, &block)
117
- fatal(message, &block)
118
- <tt>rack.multipart.buffer_size</tt>:: An Integer hint to the multipart parser as to what chunk size to use for reads and writes.
119
- <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.
120
- The server or the application can store their own data in the
121
- environment, too. The keys must contain at least one dot,
122
- and should be prefixed uniquely. The prefix <tt>rack.</tt>
123
- is reserved for use with the Rack core distribution and other
124
- accepted specifications and must not be used otherwise.
125
-
126
- The <tt>SERVER_PORT</tt> must be an Integer if set.
127
- The <tt>SERVER_NAME</tt> must be a valid authority as defined by RFC7540.
128
- The <tt>HTTP_HOST</tt> must be a valid authority as defined by RFC7540.
129
- The environment must not contain the keys
130
- <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
131
- (use the versions without <tt>HTTP_</tt>).
132
- The CGI keys (named without a period) must have String values.
133
- If the string values for CGI keys contain non-ASCII characters,
134
- they should use ASCII-8BIT encoding.
135
- There are the following restrictions:
136
- * <tt>rack.version</tt> must be an array of Integers.
137
- * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
138
- * There must be a valid input stream in <tt>rack.input</tt>.
139
- * There must be a valid error stream in <tt>rack.errors</tt>.
140
- * There may be a valid hijack stream in <tt>rack.hijack_io</tt>
141
- * The <tt>REQUEST_METHOD</tt> must be a valid token.
142
- * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
143
- * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
144
- * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
145
- * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
146
- set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
147
- <tt>SCRIPT_NAME</tt> is empty.
148
- <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
1
+ = Rack Specification
2
+
3
+ This specification aims to formalize the Rack protocol. You can (and should) use +Rack::Lint+ to enforce it. When you develop middleware, be sure to test with +Rack::Lint+ to catch possible violations of this specification.
4
+
5
+ == The Application
6
+
7
+ A Rack application is a Ruby object that responds to +call+. It takes exactly one argument, the +environment+ (representing an HTTP request) and returns a non-frozen +Array+ of exactly three elements: the +status+, the +headers+, and the +body+ (representing an HTTP response).
8
+
9
+ == The Request Environment
10
+
11
+ Incoming HTTP requests are represented using an environment. The environment must be an unfrozen +Hash+. The Rack application is free to modify the environment, but the modified environment should also comply with this specification. All environment keys must be strings.
12
+
13
+ === CGI Variables
14
+
15
+ The environment is required to include these variables, adopted from {The Common Gateway Interface}[https://datatracker.ietf.org/doc/html/rfc3875] (CGI), except when they'd be empty, but see below.
16
+
17
+ The CGI keys (named without a period) must have +String+ values and are reserved for the Rack specification. If the values for CGI keys contain non-ASCII characters, they should use <tt>ASCII-8BIT</tt> encoding.
18
+
19
+ The server and application can store their own data in the environment, too. The keys must contain at least one dot, and should be prefixed uniquely. The prefix <tt>rack.</tt> is reserved for use with the Rack specification and the classes that ship with Rack.
20
+
21
+ ==== <tt>REQUEST_METHOD</tt>
22
+
23
+ The HTTP request method, such as "GET" or "POST". This cannot ever be an empty string, and so is always required.
24
+
25
+ ==== <tt>SCRIPT_NAME</tt>
26
+
27
+ The initial portion of the request URL's path that corresponds to the application object, so that the application knows its virtual location. This may be an empty string, if the application corresponds to the root of the server. If non-empty, the string must start with <tt>/</tt>, but should not end with <tt>/</tt>.
28
+
29
+ In addition, <tt>SCRIPT_NAME</tt> MUST not be <tt>/</tt>, but instead be empty, and one of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be set, e.g. <tt>PATH_INFO</tt> can be <tt>/</tt> if <tt>SCRIPT_NAME</tt> is empty.
30
+
31
+ ==== <tt>PATH_INFO</tt>
32
+
33
+ The remainder of the request URL's "path", designating the virtual "location" of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash. This value may be percent-encoded when originating from a URL.
34
+
35
+ The <tt>PATH_INFO</tt>, if provided, must be a valid request target or an empty string, as defined by {RFC9110}[https://datatracker.ietf.org/doc/html/rfc9110#target.resource].
36
+ * Only <tt>OPTIONS</tt> requests may have <tt>PATH_INFO</tt> set to <tt>*</tt> (asterisk-form).
37
+ * Only <tt>CONNECT</tt> requests may have <tt>PATH_INFO</tt> set to an authority (authority-form). Note that in HTTP/2+, the authority-form is not a valid request target.
38
+ * <tt>CONNECT</tt> and <tt>OPTIONS</tt> requests must not have <tt>PATH_INFO</tt> set to a URI (absolute-form).
39
+ * Otherwise, <tt>PATH_INFO</tt> must start with a <tt>/</tt> and must not include a fragment part starting with <tt>#</tt> (origin-form).
40
+
41
+ ==== <tt>QUERY_STRING</tt>
42
+
43
+ The portion of the request URL that follows the <tt>?</tt>, if any. May be empty, but is always required!
44
+
45
+ ==== <tt>SERVER_NAME</tt>
46
+
47
+ Must be a valid host, as defined by {RFC3986}[https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2].
48
+
49
+ When combined with <tt>SCRIPT_NAME</tt>, <tt>PATH_INFO</tt>, and <tt>QUERY_STRING</tt>, these variables can be used to reconstruct the original the request URL. Note, however, that <tt>HTTP_HOST</tt>, if present, should be used in preference to <tt>SERVER_NAME</tt> for reconstructing the request URL.
50
+
51
+ ==== <tt>SERVER_PROTOCOL</tt>
52
+
53
+ The HTTP version used for the request. It must match the regular expression <tt>HTTP\/\d(\.\d)?</tt>.
54
+
55
+ ==== <tt>SERVER_PORT</tt>
56
+
57
+ The port the server is running on, if the server is running on a non-standard port. It must consist of digits only.
58
+
59
+ The standard ports are:
60
+ * 80 for HTTP
61
+ * 443 for HTTPS
62
+
63
+ ==== <tt>CONTENT_TYPE</tt>
64
+
65
+ The optional MIME type of the request body, if any.
66
+
67
+ ==== <tt>CONTENT_LENGTH</tt>
68
+
69
+ The length of the request body, if any. It must consist of digits only.
70
+
71
+ ==== <tt>HTTP_HOST</tt>
72
+
73
+ An optional HTTP authority, as defined by {RFC9110}[https://datatracker.ietf.org/doc/html/rfc9110#name-host-and-authority].
74
+
75
+ ==== <tt>HTTP_</tt> Headers
76
+
77
+ Unless specified above, the environment can contain any number of additional headers, each starting with <tt>HTTP_</tt>. The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request, and those headers have no specific interpretation or validation by the Rack specification. However, there are many standard HTTP headers that have a specific meaning in the context of a request; see {RFC3875 section 4.1.18}[https://tools.ietf.org/html/rfc3875#section-4.1.18] for more details.
78
+
79
+ For compatibility with the CGI specifiction, the environment must not contain the keys <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>. Instead, the keys <tt>CONTENT_TYPE</tt> and <tt>CONTENT_LENGTH</tt> must be used.
80
+
81
+ === Rack-Specific Variables
82
+
83
+ In addition to CGI variables, the Rack environment includes Rack-specific variables. These variables are prefixed with <tt>rack.</tt> and are reserved for use by the Rack specification, or by the classes that ship with Rack.
84
+
85
+ ==== <tt>rack.url_scheme</tt>
86
+
87
+ The URL scheme, which must be one of <tt>http</tt>, <tt>https</tt>, <tt>ws</tt> or <tt>wss</tt>. This can never be an empty string, and so is always required. The scheme should be set according to the last hop. For example, if a client makes a request to a reverse proxy over HTTPS, but the connection between the reverse proxy and the server is over plain HTTP, the reverse proxy should set <tt>rack.url_scheme</tt> to <tt>http</tt>.
88
+
89
+ ==== <tt>rack.protocol</tt>
90
+
91
+ An optional +Array+ of +String+ values, containing the protocols advertised by the client in the <tt>upgrade</tt> header (HTTP/1) or the <tt>:protocol</tt> pseudo-header (HTTP/2+).
92
+
93
+ ==== <tt>rack.session</tt>
94
+
95
+ An optional +Hash+-like interface for storing request session data. The store must implement:
96
+ * <tt>store(key, value)</tt> (aliased as <tt>[]=</tt>) to set a value for a key,
97
+ * <tt>fetch(key, default = nil)</tt> (aliased as <tt>[]</tt>) to retrieve a value for a key,
98
+ * <tt>delete(key)</tt> to delete a key,
99
+ * <tt>clear</tt> to clear the session,
100
+ * <tt>to_hash</tt> (optional) to retrieve the session as a Hash.
101
+
102
+ ==== <tt>rack.logger</tt>
103
+
104
+ An optional +Logger+-like interface for logging messages. The logger must implement:
105
+ * <tt>info(message, &block)</tt>,
106
+ * <tt>debug(message, &block)</tt>,
107
+ * <tt>warn(message, &block)</tt>,
108
+ * <tt>error(message, &block)</tt>,
109
+ * <tt>fatal(message, &block)</tt>.
110
+
111
+ ==== <tt>rack.multipart.buffer_size</tt>
112
+
113
+ An optional +Integer+ hint to the multipart parser as to what chunk size to use for reads and writes.
114
+
115
+ ==== <tt>rack.multipart.tempfile_factory</tt>
116
+
117
+ An optional object for constructing temporary files for multipart form data. The factory must implement:
118
+ * <tt>call(filename, content_type)</tt> to create a temporary file for a multipart form field.
119
+ The factory must return an +IO+-like object that responds to <tt><<</tt> and optionally <tt>rewind</tt>.
120
+
121
+ ==== <tt>rack.hijack?</tt>
122
+
123
+ If present and truthy, indicates that the server supports partial hijacking. See the section below on hijacking for more information.
124
+
125
+ ==== <tt>rack.hijack</tt>
126
+
127
+ If present, an object responding to +call+ that is used to perform a full hijack. See the section below on hijacking for more information.
128
+
129
+ ==== <tt>rack.early_hints</tt>
130
+
131
+ If present, an object responding to +call+ that is used to send early hints. See the section below on early hints for more information.
132
+
133
+ ==== <tt>rack.input</tt>
134
+
135
+ If present, the input stream. See the section below on the input stream for more information.
136
+
137
+ ==== <tt>rack.errors</tt>
138
+
139
+ The error stream. See the section below on the error stream for more information.
140
+
141
+ ==== <tt>rack.response_finished</tt>
142
+
143
+ If present, an array of callables that will be run by the server after the response has been processed. The callables are called with <tt>environment, status, headers, error</tt> arguments and should not raise any exceptions. The callables would typically be called after sending the response to the client, but it could also be called if an error occurs while generating the response or sending the response (in that case, the +error+ argument will be a kind of +Exception+). The callables will be called in reverse order.
144
+
149
145
  === The Input Stream
150
146
 
151
- The input stream is an IO-like object which contains the raw HTTP
152
- POST data.
153
- When applicable, its external encoding must be "ASCII-8BIT" and it
154
- must be opened in binary mode, for Ruby 1.9 compatibility.
155
- The input stream must respond to +gets+, +each+, +read+ and +rewind+.
156
- * +gets+ must be called without arguments and return a string,
157
- or +nil+ on EOF.
158
- * +read+ behaves like IO#read.
159
- Its signature is <tt>read([length, [buffer]])</tt>.
160
-
161
- If given, +length+ must be a non-negative Integer (>= 0) or +nil+,
162
- and +buffer+ must be a String and may not be nil.
163
-
164
- If +length+ is given and not nil, then this method reads at most
165
- +length+ bytes from the input stream.
166
-
167
- If +length+ is not given or nil, then this method reads
168
- all data until EOF.
169
-
170
- When EOF is reached, this method returns nil if +length+ is given
171
- and not nil, or "" if +length+ is not given or is nil.
172
-
173
- If +buffer+ is given, then the read data will be placed
174
- into +buffer+ instead of a newly created String object.
175
- * +each+ must be called without arguments and only yield Strings.
176
- * +rewind+ must be called without arguments. It rewinds the input
177
- stream back to the beginning. It must not raise Errno::ESPIPE:
178
- that is, it may not be a pipe or a socket. Therefore, handler
179
- developers must buffer the input data into some rewindable object
180
- if the underlying input stream is not rewindable.
181
- * +close+ must never be called on the input stream.
147
+ The input stream is an +IO+-like object which contains the raw HTTP request data. When applicable, its external encoding must be <tt>ASCII-8BIT</tt> and it must be opened in binary mode. The input stream must respond to +gets+, +each+, and +read+:
148
+ * +gets+ must be called without arguments and return a +String+, or +nil+ on EOF (end-of-file).
149
+ * +read+ behaves like <tt>IO#read</tt>. Its signature is <tt>read([length, [buffer]])</tt>.
150
+ * If given, +length+ must be a non-negative Integer (>= 0) or +nil+, and +buffer+ must be a +String+ and may not be +nil+.
151
+ * If +length+ is given and not +nil+, then this method reads at most +length+ bytes from the input stream.
152
+ * If +length+ is not given or +nil+, then this method reads all data until EOF.
153
+ * When EOF is reached, this method returns +nil+ if +length+ is given and not +nil+, or +""+ if +length+ is not given or is +nil+.
154
+ * If +buffer+ is given, then the read data will be placed into +buffer+ instead of a newly created +String+.
155
+ * +each+ must be called without arguments and only yield +String+ values.
156
+ * +close+ can be called on the input stream to indicate that any remaining input is not needed.
157
+
182
158
  === The Error Stream
183
- The error stream must respond to +puts+, +write+ and +flush+.
159
+
160
+ The error stream must respond to +puts+, +write+ and +flush+:
184
161
  * +puts+ must be called with a single argument that responds to +to_s+.
185
- * +write+ must be called with a single argument that is a String.
186
- * +flush+ must be called without arguments and must be called
187
- in order to make the error appear for sure.
162
+ * +write+ must be called with a single argument that is a +String+.
163
+ * +flush+ must be called without arguments and must be called in order to make the error appear for sure.
188
164
  * +close+ must never be called on the error stream.
165
+
189
166
  === Hijacking
190
- ==== Request (before status)
191
- If rack.hijack? is true then rack.hijack must respond to #call.
192
- rack.hijack must return the io that will also be assigned (or is
193
- already present, in rack.hijack_io.
194
-
195
- rack.hijack_io must respond to:
196
- <tt>read, write, read_nonblock, write_nonblock, flush, close,
197
- close_read, close_write, closed?</tt>
198
-
199
- The semantics of these IO methods must be a best effort match to
200
- those of a normal ruby IO or Socket object, using standard
201
- arguments and raising standard exceptions. Servers are encouraged
202
- to simply pass on real IO objects, although it is recognized that
203
- this approach is not directly compatible with SPDY and HTTP 2.0.
204
-
205
- IO provided in rack.hijack_io should preference the
206
- IO::WaitReadable and IO::WaitWritable APIs wherever supported.
207
-
208
- There is a deliberate lack of full specification around
209
- rack.hijack_io, as semantics will change from server to server.
210
- Users are encouraged to utilize this API with a knowledge of their
211
- server choice, and servers may extend the functionality of
212
- hijack_io to provide additional features to users. The purpose of
213
- rack.hijack is for Rack to "get out of the way", as such, Rack only
214
- provides the minimum of specification and support.
215
-
216
- If rack.hijack? is false, then rack.hijack should not be set.
217
-
218
- If rack.hijack? is false, then rack.hijack_io should not be set.
219
- ==== Response (after headers)
220
- It is also possible to hijack a response after the status and headers
221
- have been sent.
222
- In order to do this, an application may set the special header
223
- <tt>rack.hijack</tt> to an object that responds to <tt>call</tt>
224
- accepting an argument that conforms to the <tt>rack.hijack_io</tt>
225
- protocol.
226
-
227
- After the headers have been sent, and this hijack callback has been
228
- called, the application is now responsible for the remaining lifecycle
229
- of the IO. The application is also responsible for maintaining HTTP
230
- semantics. Of specific note, in almost all cases in the current SPEC,
231
- applications will have wanted to specify the header Connection:close in
232
- HTTP/1.1, and not Connection:keep-alive, as there is no protocol for
233
- returning hijacked sockets to the web server. For that purpose, use the
234
- body streaming API instead (progressively yielding strings via each).
235
-
236
- Servers must ignore the <tt>body</tt> part of the response tuple when
237
- the <tt>rack.hijack</tt> response API is in use.
238
-
239
- The special response header <tt>rack.hijack</tt> must only be set
240
- if the request env has <tt>rack.hijack?</tt> <tt>true</tt>.
241
- ==== Conventions
242
- * Middleware should not use hijack unless it is handling the whole
243
- response.
244
- * Middleware may wrap the IO object for the response pattern.
245
- * Middleware should not wrap the IO object for the request pattern. The
246
- request pattern is intended to provide the hijacker with "raw tcp".
167
+
168
+ The hijacking interfaces provides a means for an application to take control of the HTTP connection. There are two distinct hijack interfaces: full hijacking where the application takes over the raw connection, and partial hijacking where the application takes over just the response body stream. In both cases, the application is responsible for closing the hijacked stream.
169
+
170
+ Full hijacking only works with HTTP/1. Partial hijacking is functionally equivalent to streaming bodies, and is still optionally supported for backwards compatibility with older Rack versions.
171
+
172
+ ==== Full Hijack
173
+
174
+ Full hijack is used to completely take over an HTTP/1 connection. It occurs before any headers are written and causes the server to ignore any response generated by the application. It is intended to be used when applications need access to the raw HTTP/1 connection.
175
+
176
+ If <tt>rack.hijack</tt> is present in +env+, it must respond to +call+ and return an +IO+ object which can be used to read and write to the underlying connection using HTTP/1 semantics and formatting.
177
+
178
+ ==== Partial Hijack
179
+
180
+ Partial hijack is used for bi-directional streaming of the request and response body. It occurs after the status and headers are written by the server and causes the server to ignore the Body of the response. It is intended to be used when applications need bi-directional streaming.
181
+
182
+ If <tt>rack.hijack?</tt> is present in +env+ and truthy, an application may set the special response header <tt>rack.hijack</tt> to an object that responds to +call+, accepting a +stream+ argument.
183
+
184
+ After the response status and headers have been sent, this hijack callback will be called with a +stream+ argument which follows the same interface as outlined in "Streaming Body". Servers must ignore the +body+ part of the response tuple when the <tt>rack.hijack</tt> response header is present. Using an empty +Array+ is recommended.
185
+
186
+ If <tt>rack.hijack?</tt> is not present and truthy, the special response header <tt>rack.hijack</tt> must not be present in the response headers.
187
+
188
+ === Early Hints
189
+
190
+ The application or any middleware may call the <tt>rack.early_hints</tt> with an object which would be valid as the headers of a Rack response.
191
+
192
+ If <tt>rack.early_hints</tt> is present, it must respond to +call+.
193
+ If <tt>rack.early_hints</tt> is called, it must be called with valid Rack response headers.
194
+
247
195
  == The Response
196
+
197
+ Outgoing HTTP responses are generated from the response tuple generated by the application. The response tuple is an +Array+ of three elements, which are: the HTTP status, the headers, and the response body. The Rack application is responsible for ensuring that the response tuple is well-formed and should follow the rules set out in this specification.
198
+
248
199
  === The Status
249
- This is an HTTP status. When parsed as integer (+to_i+), it must be
250
- greater than or equal to 100.
200
+
201
+ This is an HTTP status. It must be an Integer greater than or equal to 100.
202
+
251
203
  === The Headers
252
- The header must respond to +each+, and yield values of key and value.
253
- The header keys must be Strings.
254
- Special headers starting "rack." are for communicating with the
255
- server, and must not be sent back to the client.
256
- The header must not contain a +Status+ key.
257
- The header must conform to RFC7230 token specification, i.e. cannot
258
- contain non-printable ASCII, DQUOTE or "(),/:;<=>?@[\]{}".
259
- The values of the header must be Strings,
260
- consisting of lines (for multiple header values, e.g. multiple
261
- <tt>Set-Cookie</tt> values) separated by "\\n".
262
- The lines must not contain characters below 037.
263
- === The Content-Type
264
- There must not be a <tt>Content-Type</tt>, when the +Status+ is 1xx,
265
- 204 or 304.
266
- === The Content-Length
267
- There must not be a <tt>Content-Length</tt> header when the
268
- +Status+ is 1xx, 204 or 304.
204
+
205
+ The headers must be an unfrozen +Hash+. The header keys must be +String+ values. Special headers starting <tt>rack.</tt> are for communicating with the server, and must not be sent back to the client.
206
+
207
+ * The headers must not contain a <tt>"status"</tt> key.
208
+ * Header keys must conform to {RFC7230}[https://tools.ietf.org/html/rfc7230] token specification, i.e. cannot contain non-printable ASCII, <tt>DQUOTE</tt> or <tt>(),/:;<=>?@[\]{}</tt>.
209
+ * Header keys must not contain uppercase ASCII characters (A-Z).
210
+ * Header values must be either a +String+, or an +Array+ of +String+ values, such that each +String+ must not contain <tt>NUL</tt> (<tt>\0</tt>), <tt>CR</tt> (<tt>\r</tt>), or <tt>LF</tt> (<tt>\n</tt>).
211
+
212
+ ==== The <tt>content-type</tt> Header
213
+
214
+ There must not be a <tt>content-type</tt> header key when the status is <tt>1xx</tt>, <tt>204</tt>, or <tt>304</tt>.
215
+
216
+ ==== The <tt>content-length</tt> Header
217
+
218
+ There must not be a <tt>content-length</tt> header key when the status is <tt>1xx</tt>, <tt>204</tt>, or <tt>304</tt>.
219
+
220
+ ==== The <tt>rack.protocol</tt> Header
221
+
222
+ If the <tt>rack.protocol</tt> header is present, it must be a +String+, and must be one of the values from the <tt>rack.protocol</tt> array from the environment.
223
+
224
+ Setting this value informs the server that it should perform a connection upgrade. In HTTP/1, this is done using the +upgrade+ header. In HTTP/2+, this is done by accepting the request.
225
+
269
226
  === The Body
270
- The Body must respond to +each+
271
- and must only yield String values.
272
227
 
273
- The Body itself should not be an instance of String, as this will
274
- break in Ruby 1.9.
228
+ The Body is typically an +Array+ of +String+ values, an enumerable that yields +String+ values, a +Proc+, or an +IO+-like object.
229
+
230
+ The Body must respond to +each+ or +call+. It may optionally respond to +to_path+ or +to_ary+. A Body that responds to +each+ is considered to be an Enumerable Body. A Body that responds to +call+ is considered to be a Streaming Body.
231
+
232
+ A Body that responds to both +each+ and +call+ must be treated as an Enumerable Body, not a Streaming Body. If it responds to +each+, you must call +each+ and not +call+. If the Body doesn't respond to +each+, then you can assume it responds to +call+.
233
+
234
+ The Body must either be consumed or returned. The Body is consumed by optionally calling either +each+ or +call+. Then, if the Body responds to +close+, it must be called to release any resources associated with the generation of the body. In other words, +close+ must always be called at least once; typically after the web server has sent the response to the client, but also in cases where the Rack application makes internal/virtual requests and discards the response.
235
+
236
+ After calling +close+, the Body is considered closed and should not be consumed again. If the original Body is replaced by a new Body, the new Body must also consume the original Body by calling +close+ if possible.
237
+
238
+ If the Body responds to +to_path+, it must return either +nil+ or a +String+. If a +String+ is returned, it must be a path for the local file system whose contents are identical to that produced by calling +each+; this may be used by the server as an alternative, possibly more efficient way to transport the response. The +to_path+ method does not consume the body.
239
+
240
+ ==== Enumerable Body
275
241
 
276
- If the Body responds to +close+, it will be called after iteration. If
277
- the body is replaced by a middleware after action, the original body
278
- must be closed first, if it responds to close.
242
+ The Enumerable Body must respond to +each+, which must only be called once, must not be called after being closed, and must only yield +String+ values.
279
243
 
280
- If the Body responds to +to_path+, it must return a String
281
- identifying the location of a file whose contents are identical
282
- to that produced by calling +each+; this may be used by the
283
- server as an alternative, possibly more efficient way to
284
- transport the response.
244
+ Middleware must not call +each+ directly on the Body. Instead, middleware can return a new Body that calls +each+ on the original Body, yielding at least once per iteration.
245
+
246
+ If the Body responds to +to_ary+, it must return an +Array+ whose contents are identical to that produced by calling +each+. Middleware may call +to_ary+ directly on the Body and return a new Body in its place. In other words, middleware can only process the Body directly if it responds to +to_ary+. If the Body responds to both +to_ary+ and +close+, its implementation of +to_ary+ must call +close+.
247
+
248
+ ==== Streaming Body
249
+
250
+ The Streaming Body must respond to +call+, which must only be called once, must not be called after being closed, and accept a +stream+ argument.
251
+
252
+ The +stream+ argument must respond to: +read+, +write+, <tt><<</tt>, +flush+, +close+, +close_read+, +close_write+, and +closed?+. The semantics of these +IO+ methods must be a best effort match to those of a normal Ruby +IO+ or +Socket+ object, using standard arguments and raising standard exceptions. Servers may simply pass on real +IO+ objects to the Streaming Body. In some cases (e.g. when using <tt>transfer-encoding</tt> or HTTP/2+), the server may need to provide a wrapper that implements the required methods, in order to provide the correct semantics.
285
253
 
286
- The Body commonly is an Array of Strings, the application
287
- instance itself, or a File-like object.
288
254
  == Thanks
289
- Some parts of this specification are adopted from PEP333: Python
290
- Web Server Gateway Interface
291
- v1.0 (http://www.python.org/dev/peps/pep-0333/). I'd like to thank
292
- everyone involved in that effort.
255
+
256
+ We'd like to thank everyone who has contributed to the Rack project over the years. Your work has made this specification possible. That includes everyone who has contributed code, documentation, bug reports, and feedback. We'd also like to thank the authors of the various web servers, frameworks, and libraries that have implemented the Rack specification. Your work has helped to make the web a better place.
257
+
258
+ Some parts of this specification are adapted from {PEP 333 – Python Web Server Gateway Interface v1.0}[https://peps.python.org/pep-0333/]. We'd like to thank everyone involved in that effort.
@@ -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
- 'WWW-Authenticate' => www_authenticate.to_s },
26
+ 'www-authenticate' => www_authenticate.to_s },
25
27
  []
26
28
  ]
27
29
  end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # XXX: Remove when removing AbstractRequest#request
4
+ require_relative '../../request'
5
+
3
6
  module Rack
4
7
  module Auth
5
8
  class AbstractRequest
@@ -9,6 +12,7 @@ module Rack
9
12
  end
10
13
 
11
14
  def request
15
+ warn "Rack::Auth::AbstractRequest#request is deprecated and will be removed in a future version of rack.", uplevel: 1
12
16
  @request ||= Request.new(@env)
13
17
  end
14
18
 
@@ -25,7 +29,7 @@ module Rack
25
29
  end
26
30
 
27
31
  def scheme
28
- @scheme ||= parts.first && parts.first.downcase
32
+ @scheme ||= parts.first&.downcase
29
33
  end
30
34
 
31
35
  def params
@@ -9,8 +9,6 @@ module Rack
9
9
  #
10
10
  # Initialize with the Rack application that you want protecting,
11
11
  # and a block that checks if a username and password pair are valid.
12
- #
13
- # See also: <tt>example/protectedlobster.rb</tt>
14
12
 
15
13
  class Basic < AbstractHandler
16
14
 
@@ -47,7 +45,7 @@ module Rack
47
45
  end
48
46
 
49
47
  def credentials
50
- @credentials ||= params.unpack("m").first.split(':', 2)
48
+ @credentials ||= params.unpack1('m').split(':', 2)
51
49
  end
52
50
 
53
51
  def username