rack 3.1.16 → 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.
data/SPEC.rdoc CHANGED
@@ -1,365 +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
- <tt>rack.protocol</tt>:: An optional +Array+ of +String+, containing
86
- the protocols advertised by the client in
87
- the +upgrade+ header (HTTP/1) or the
88
- +:protocol+ pseudo-header (HTTP/2).
89
- Additional environment specifications have approved to
90
- standardized middleware APIs. None of these are required to
91
- be implemented by the server.
92
- <tt>rack.session</tt>:: A hash-like interface for storing
93
- request session data.
94
- The store must implement:
95
- store(key, value) (aliased as []=);
96
- fetch(key, default = nil) (aliased as []);
97
- delete(key);
98
- clear;
99
- to_hash (returning unfrozen Hash instance);
100
- <tt>rack.logger</tt>:: A common object interface for logging messages.
101
- The object must implement:
102
- info(message, &block)
103
- debug(message, &block)
104
- warn(message, &block)
105
- error(message, &block)
106
- fatal(message, &block)
107
- <tt>rack.multipart.buffer_size</tt>:: An Integer hint to the multipart parser as to what chunk size to use for reads and writes.
108
- <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.
109
- The server or the application can store their own data in the
110
- environment, too. The keys must contain at least one dot,
111
- and should be prefixed uniquely. The prefix <tt>rack.</tt>
112
- is reserved for use with the Rack core distribution and other
113
- accepted specifications and must not be used otherwise.
114
-
115
- The <tt>SERVER_PORT</tt> must be an Integer if set.
116
- The <tt>SERVER_NAME</tt> must be a valid authority as defined by RFC7540.
117
- The <tt>HTTP_HOST</tt> must be a valid authority as defined by RFC7540.
118
- The <tt>SERVER_PROTOCOL</tt> must match the regexp <tt>HTTP/\d(\.\d)?</tt>.
119
- The environment must not contain the keys
120
- <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
121
- (use the versions without <tt>HTTP_</tt>).
122
- The CGI keys (named without a period) must have String values.
123
- If the string values for CGI keys contain non-ASCII characters,
124
- they should use ASCII-8BIT encoding.
125
- There are the following restrictions:
126
- * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
127
- * There may be a valid input stream in <tt>rack.input</tt>.
128
- * There must be a valid error stream in <tt>rack.errors</tt>.
129
- * There may be a valid hijack callback in <tt>rack.hijack</tt>
130
- * There may be a valid early hints callback in <tt>rack.early_hints</tt>
131
- * The <tt>REQUEST_METHOD</tt> must be a valid token.
132
- * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
133
- * The <tt>PATH_INFO</tt>, if provided, must be a valid request target or an empty string.
134
- * Only <tt>OPTIONS</tt> requests may have <tt>PATH_INFO</tt> set to <tt>*</tt> (asterisk-form).
135
- * 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.
136
- * <tt>CONNECT</tt> and <tt>OPTIONS</tt> requests must not have <tt>PATH_INFO</tt> set to a URI (absolute-form).
137
- * Otherwise, <tt>PATH_INFO</tt> must start with a <tt>/</tt> and must not include a fragment part starting with '#' (origin-form).
138
- * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
139
- * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
140
- set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
141
- <tt>SCRIPT_NAME</tt> is empty.
142
- <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
143
- <tt>rack.response_finished</tt>:: An array of callables run by the server after the response has been
144
- processed. This would typically be invoked after sending the response to the client, but it could also be
145
- invoked if an error occurs while generating the response or sending the response; in that case, the error
146
- argument will be a subclass of +Exception+.
147
- The callables are invoked with +env, status, headers, error+ arguments and should not raise any
148
- exceptions. They should be invoked in reverse order of registration.
1
+ = Rack Specification
149
2
 
150
- === 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.
151
80
 
152
- The input stream is an IO-like object which contains the raw HTTP
153
- POST data.
154
- When applicable, its external encoding must be "ASCII-8BIT" and it
155
- must be opened in binary mode.
156
- The input stream must respond to +gets+, +each+, and +read+.
157
- * +gets+ must be called without arguments and return a string,
158
- or +nil+ on EOF.
159
- * +read+ behaves like <tt>IO#read</tt>.
160
- Its signature is <tt>read([length, [buffer]])</tt>.
81
+ === Rack-Specific Variables
161
82
 
162
- If given, +length+ must be a non-negative Integer (>= 0) or +nil+,
163
- and +buffer+ must be a String and may not be nil.
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.
164
84
 
165
- If +length+ is given and not nil, then this method reads at most
166
- +length+ bytes from the input stream.
85
+ ==== <tt>rack.url_scheme</tt>
167
86
 
168
- If +length+ is not given or nil, then this method reads
169
- all data until EOF.
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>.
170
88
 
171
- When EOF is reached, this method returns nil if +length+ is given
172
- and not nil, or "" if +length+ is not given or is nil.
89
+ ==== <tt>rack.protocol</tt>
173
90
 
174
- If +buffer+ is given, then the read data will be placed
175
- into +buffer+ instead of a newly created String object.
176
- * +each+ must be called without arguments and only yield Strings.
177
- * +close+ can be called on the input stream to indicate that
178
- any remaining input is not needed.
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
+
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.
179
157
 
180
158
  === The Error Stream
181
159
 
182
- The error stream must respond to +puts+, +write+ and +flush+.
160
+ The error stream must respond to +puts+, +write+ and +flush+:
183
161
  * +puts+ must be called with a single argument that responds to +to_s+.
184
- * +write+ must be called with a single argument that is a String.
185
- * +flush+ must be called without arguments and must be called
186
- 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.
187
164
  * +close+ must never be called on the error stream.
188
165
 
189
166
  === Hijacking
190
167
 
191
- The hijacking interfaces provides a means for an application to take
192
- control of the HTTP connection. There are two distinct hijack
193
- interfaces: full hijacking where the application takes over the raw
194
- connection, and partial hijacking where the application takes over
195
- just the response body stream. In both cases, the application is
196
- 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.
197
169
 
198
- Full hijacking only works with HTTP/1. Partial hijacking is functionally
199
- equivalent to streaming bodies, and is still optionally supported for
200
- 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.
201
171
 
202
172
  ==== Full Hijack
203
173
 
204
- Full hijack is used to completely take over an HTTP/1 connection. It
205
- occurs before any headers are written and causes the request to
206
- ignores any response generated by the application.
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.
207
175
 
208
- It is intended to be used when applications need access to raw HTTP/1
209
- connection.
210
-
211
- If +rack.hijack+ is present in +env+, it must respond to +call+
212
- and return an +IO+ instance which can be used to read and write
213
- to the underlying connection using HTTP/1 semantics and
214
- 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.
215
177
 
216
178
  ==== Partial Hijack
217
179
 
218
- Partial hijack is used for bi-directional streaming of the request and
219
- response body. It occurs after the status and headers are written by
220
- the server and causes the server to ignore the Body of the response.
221
-
222
- It is intended to be used when applications need bi-directional
223
- streaming.
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.
224
181
 
225
- If +rack.hijack?+ is present in +env+ and truthy,
226
- an application may set the special response header +rack.hijack+
227
- to an object that responds to +call+,
228
- accepting a +stream+ argument.
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.
229
183
 
230
- After the response status and headers have been sent, this hijack
231
- callback will be invoked with a +stream+ argument which follows the
232
- same interface as outlined in "Streaming Body". Servers must
233
- ignore the +body+ part of the response tuple when the
234
- +rack.hijack+ response header is present. Using an empty +Array+
235
- instance is recommended.
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.
236
185
 
237
- The special response header +rack.hijack+ must only be set
238
- if the request +env+ has a truthy +rack.hijack?+.
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.
239
187
 
240
188
  === Early Hints
241
189
 
242
- The application or any middleware may call the <tt>rack.early_hints</tt>
243
- with an object which would be valid as the headers of a Rack response.
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.
244
191
 
245
- If <tt>rack.early_hints</tt> is present, it must respond to #call.
246
- If <tt>rack.early_hints</tt> is called, it must be called with
247
- valid Rack response headers.
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.
248
194
 
249
195
  == The Response
250
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
+
251
199
  === The Status
252
200
 
253
- This is an HTTP status. It must be an Integer greater than or equal to
254
- 100.
201
+ This is an HTTP status. It must be an Integer greater than or equal to 100.
255
202
 
256
203
  === The Headers
257
204
 
258
- The headers must be a unfrozen Hash.
259
- The header keys must be Strings.
260
- Special headers starting "rack." are for communicating with the
261
- server, and must not be sent back to the client.
262
- The header must not contain a +Status+ key.
263
- Header keys must conform to RFC7230 token specification, i.e. cannot
264
- contain non-printable ASCII, DQUOTE or "(),/:;<=>?@[\]{}".
265
- Header keys must not contain uppercase ASCII characters (A-Z).
266
- Header values must be either a String instance,
267
- or an Array of String instances,
268
- 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>).
269
211
 
270
- ==== The +content-type+ Header
212
+ ==== The <tt>content-type</tt> Header
271
213
 
272
- There must not be a <tt>content-type</tt> header key when the +Status+ is 1xx,
273
- 204, or 304.
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>.
274
215
 
275
- ==== The +content-length+ Header
216
+ ==== The <tt>content-length</tt> Header
276
217
 
277
- There must not be a <tt>content-length</tt> header key when the
278
- +Status+ is 1xx, 204, or 304.
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>.
279
219
 
280
- ==== The +rack.protocol+ Header
220
+ ==== The <tt>rack.protocol</tt> Header
281
221
 
282
- If the +rack.protocol+ header is present, it must be a +String+, and
283
- must be one of the values from the +rack.protocol+ array from the
284
- environment.
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.
285
223
 
286
- Setting this value informs the server that it should perform a
287
- connection upgrade. In HTTP/1, this is done using the +upgrade+
288
- header. In HTTP/2, this is done by accepting the request.
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.
289
225
 
290
226
  === The Body
291
227
 
292
- The Body is typically an +Array+ of +String+ instances, an enumerable
293
- that yields +String+ instances, a +Proc+ instance, or a File-like
294
- object.
295
-
296
- The Body must respond to +each+ or +call+. It may optionally respond
297
- to +to_path+ or +to_ary+. A Body that responds to +each+ is considered
298
- to be an Enumerable Body. A Body that responds to +call+ is considered
299
- to be a Streaming Body.
300
-
301
- A Body that responds to both +each+ and +call+ must be treated as an
302
- Enumerable Body, not a Streaming Body. If it responds to +each+, you
303
- must call +each+ and not +call+. If the Body doesn't respond to
304
- +each+, then you can assume it responds to +call+.
305
-
306
- The Body must either be consumed or returned. The Body is consumed by
307
- optionally calling either +each+ or +call+.
308
- Then, if the Body responds to +close+, it must be called to release
309
- any resources associated with the generation of the body.
310
- In other words, +close+ must always be called at least once; typically
311
- after the web server has sent the response to the client, but also in
312
- cases where the Rack application makes internal/virtual requests and
313
- discards the response.
314
-
315
-
316
- After calling +close+, the Body is considered closed and should not
317
- be consumed again.
318
- If the original Body is replaced by a new Body, the new Body must
319
- also consume the original Body by calling +close+ if possible.
320
-
321
- If the Body responds to +to_path+, it must return a +String+
322
- path for the local file system whose contents are identical
323
- to that produced by calling +each+; this may be used by the
324
- server as an alternative, possibly more efficient way to
325
- transport the response. The +to_path+ method does not consume
326
- 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.
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.
327
239
 
328
240
  ==== Enumerable Body
329
241
 
330
- The Enumerable Body must respond to +each+.
331
- It must only be called once.
332
- It must not be called after being closed,
333
- and must only yield String values.
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.
334
243
 
335
- Middleware must not call +each+ directly on the Body.
336
- Instead, middleware can return a new Body that calls +each+ on the
337
- original Body, yielding at least once per iteration.
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.
338
245
 
339
- If the Body responds to +to_ary+, it must return an +Array+ whose
340
- contents are identical to that produced by calling +each+.
341
- Middleware may call +to_ary+ directly on the Body and return a new
342
- Body in its place. In other words, middleware can only process the
343
- Body directly if it responds to +to_ary+. If the Body responds to both
344
- +to_ary+ and +close+, its implementation of +to_ary+ must call
345
- +close+.
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+.
346
247
 
347
248
  ==== Streaming Body
348
249
 
349
- The Streaming Body must respond to +call+.
350
- It must only be called once.
351
- It must not be called after being closed.
352
- It takes a +stream+ argument.
353
-
354
- The +stream+ argument must implement:
355
- <tt>read, write, <<, flush, close, close_read, close_write, closed?</tt>
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.
356
251
 
357
- The semantics of these IO methods must be a best effort match to
358
- those of a normal Ruby IO or Socket object, using standard arguments
359
- and raising standard exceptions. Servers are encouraged to simply
360
- pass on real IO objects, although it is recognized that this approach
361
- is not directly compatible with HTTP/2.
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.
362
253
 
363
254
  == Thanks
364
- Some parts of this specification are adopted from {PEP 333 – Python Web Server Gateway Interface v1.0}[https://peps.python.org/pep-0333/]
365
- 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
 
data/lib/rack/builder.rb CHANGED
@@ -162,6 +162,8 @@ module Rack
162
162
  @use << proc { |app| generate_map(app, mapping) }
163
163
  end
164
164
  @use << proc { |app| middleware.new(app, *args, &block) }
165
+
166
+ nil
165
167
  end
166
168
  # :nocov:
167
169
  ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
@@ -194,6 +196,8 @@ module Rack
194
196
  raise ArgumentError, "Both app and block given!" if app && block_given?
195
197
 
196
198
  @run = app || block
199
+
200
+ nil
197
201
  end
198
202
 
199
203
  # Takes a lambda or block that is used to warm-up the application. This block is called
@@ -252,6 +256,8 @@ module Rack
252
256
  def map(path, &block)
253
257
  @map ||= {}
254
258
  @map[path] = block
259
+
260
+ nil
255
261
  end
256
262
 
257
263
  # Freeze the app (set using run) and all middleware instances when building the application
@@ -34,9 +34,10 @@ module Rack
34
34
  response[0] = 304
35
35
  headers.delete(CONTENT_TYPE)
36
36
  headers.delete(CONTENT_LENGTH)
37
- response[2] = Rack::BodyProxy.new([]) do
38
- body.close if body.respond_to?(:close)
39
- end
37
+
38
+ # We are done with the body:
39
+ body.close if body.respond_to?(:close)
40
+ response[2] = []
40
41
  end
41
42
  response
42
43
  else
@@ -54,6 +54,7 @@ module Rack
54
54
  RACK_MULTIPART_BUFFER_SIZE = 'rack.multipart.buffer_size'
55
55
  RACK_MULTIPART_TEMPFILE_FACTORY = 'rack.multipart.tempfile_factory'
56
56
  RACK_RESPONSE_FINISHED = 'rack.response_finished'
57
+ RACK_PROTOCOL = 'rack.protocol'
57
58
  RACK_REQUEST_FORM_INPUT = 'rack.request.form_input'
58
59
  RACK_REQUEST_FORM_HASH = 'rack.request.form_hash'
59
60
  RACK_REQUEST_FORM_PAIRS = 'rack.request.form_pairs'