rack 3.0.15 → 3.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +368 -6
  3. data/CONTRIBUTING.md +11 -9
  4. data/README.md +103 -28
  5. data/SPEC.rdoc +206 -288
  6. data/lib/rack/auth/abstract/request.rb +2 -0
  7. data/lib/rack/auth/basic.rb +1 -2
  8. data/lib/rack/bad_request.rb +8 -0
  9. data/lib/rack/builder.rb +29 -10
  10. data/lib/rack/cascade.rb +0 -3
  11. data/lib/rack/conditional_get.rb +4 -3
  12. data/lib/rack/constants.rb +4 -0
  13. data/lib/rack/directory.rb +6 -3
  14. data/lib/rack/events.rb +21 -6
  15. data/lib/rack/files.rb +1 -1
  16. data/lib/rack/head.rb +2 -3
  17. data/lib/rack/headers.rb +86 -2
  18. data/lib/rack/lint.rb +482 -425
  19. data/lib/rack/media_type.rb +14 -10
  20. data/lib/rack/mime.rb +6 -5
  21. data/lib/rack/mock_request.rb +10 -15
  22. data/lib/rack/mock_response.rb +50 -20
  23. data/lib/rack/multipart/parser.rb +255 -76
  24. data/lib/rack/multipart/uploaded_file.rb +42 -5
  25. data/lib/rack/multipart.rb +34 -1
  26. data/lib/rack/query_parser.rb +86 -78
  27. data/lib/rack/request.rb +78 -65
  28. data/lib/rack/response.rb +28 -20
  29. data/lib/rack/rewindable_input.rb +4 -1
  30. data/lib/rack/sendfile.rb +51 -21
  31. data/lib/rack/show_exceptions.rb +10 -4
  32. data/lib/rack/show_status.rb +0 -2
  33. data/lib/rack/static.rb +7 -3
  34. data/lib/rack/utils.rb +175 -119
  35. data/lib/rack/version.rb +3 -20
  36. data/lib/rack.rb +1 -4
  37. metadata +6 -12
  38. data/lib/rack/auth/digest/md5.rb +0 -1
  39. data/lib/rack/auth/digest/nonce.rb +0 -1
  40. data/lib/rack/auth/digest/params.rb +0 -1
  41. data/lib/rack/auth/digest/request.rb +0 -1
  42. data/lib/rack/auth/digest.rb +0 -256
  43. data/lib/rack/chunked.rb +0 -120
  44. data/lib/rack/file.rb +0 -9
  45. data/lib/rack/logger.rb +0 -22
data/SPEC.rdoc CHANGED
@@ -1,340 +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
-
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.
1
+ = Rack Specification
141
2
 
142
- === The Input Stream
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>
143
86
 
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>.
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>.
153
88
 
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.
89
+ ==== <tt>rack.protocol</tt>
156
90
 
157
- If +length+ is given and not nil, then this method reads at most
158
- +length+ bytes from the input stream.
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+).
159
92
 
160
- If +length+ is not given or nil, then this method reads
161
- all data until EOF.
93
+ ==== <tt>rack.session</tt>
162
94
 
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.
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.
165
101
 
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.
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
+
145
+ === The Input Stream
146
+
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.
171
157
 
172
158
  === The Error Stream
173
159
 
174
- The error stream must respond to +puts+, +write+ and +flush+.
160
+ The error stream must respond to +puts+, +write+ and +flush+:
175
161
  * +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.
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.
179
164
  * +close+ must never be called on the error stream.
180
165
 
181
166
  === Hijacking
182
167
 
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.
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.
189
169
 
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.
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.
193
171
 
194
172
  ==== Full Hijack
195
173
 
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.
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.
202
175
 
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.
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.
207
177
 
208
178
  ==== Partial Hijack
209
179
 
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.
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.
213
187
 
214
- It is intended to be used when applications need bi-directional
215
- streaming.
188
+ === Early Hints
216
189
 
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.
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.
221
191
 
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.
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.
228
194
 
229
- The special response header +rack.hijack+ must only be set
230
- if the request +env+ has a truthy +rack.hijack?+.
231
195
  == The Response
232
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
+
233
199
  === The Status
234
200
 
235
- This is an HTTP status. It must be an Integer greater than or equal to
236
- 100.
201
+ This is an HTTP status. It must be an Integer greater than or equal to 100.
237
202
 
238
203
  === The Headers
239
204
 
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.
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
251
217
 
252
- === The content-type
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>.
253
219
 
254
- There must not be a <tt>content-type</tt> header key when the +Status+ is 1xx,
255
- 204, or 304.
220
+ ==== The <tt>rack.protocol</tt> Header
256
221
 
257
- === The content-length
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.
258
223
 
259
- There must not be a <tt>content-length</tt> header key when the
260
- +Status+ is 1xx, 204, or 304.
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.
261
225
 
262
226
  === The Body
263
227
 
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.
228
+ The Body is typically an +Array+ of +String+ values, an enumerable that yields +String+ values, a +Proc+, or an +IO+-like object.
299
229
 
300
- ==== Enumerable Body
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.
301
231
 
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.
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+.
306
233
 
307
- The Body itself should not be an instance of String, as this will
308
- break in Ruby 1.9.
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.
309
235
 
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.
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.
313
237
 
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+.
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.
321
239
 
322
- ==== Streaming Body
240
+ ==== Enumerable Body
323
241
 
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.
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.
328
243
 
329
- The +stream+ argument must implement:
330
- <tt>read, write, <<, flush, close, close_read, close_write, closed?</tt>
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.
331
245
 
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.
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.
337
253
 
338
254
  == 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.
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,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # XXX: Remove when removing AbstractRequest#request
3
4
  require_relative '../../request'
4
5
 
5
6
  module Rack
@@ -11,6 +12,7 @@ module Rack
11
12
  end
12
13
 
13
14
  def request
15
+ warn "Rack::Auth::AbstractRequest#request is deprecated and will be removed in a future version of rack.", uplevel: 1
14
16
  @request ||= Request.new(@env)
15
17
  end
16
18
 
@@ -2,7 +2,6 @@
2
2
 
3
3
  require_relative 'abstract/handler'
4
4
  require_relative 'abstract/request'
5
- require 'base64'
6
5
 
7
6
  module Rack
8
7
  module Auth
@@ -46,7 +45,7 @@ module Rack
46
45
  end
47
46
 
48
47
  def credentials
49
- @credentials ||= Base64.decode64(params).split(':', 2)
48
+ @credentials ||= params.unpack1('m').split(':', 2)
50
49
  end
51
50
 
52
51
  def username
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rack
4
+ # Represents a 400 Bad Request error when input data fails to meet the
5
+ # requirements.
6
+ module BadRequest
7
+ end
8
+ end