Skip to content


Subversion checkout URL

You can clone with
Download ZIP
100644 172 lines (171 sloc) 9.23 KB
4cf4380 @raggi Adding SPEC with rake task dependencies
raggi authored
1 This specification aims to formalize the Rack protocol. You
2 can (and should) use Rack::Lint to enforce it.
3 When you develop middleware, be sure to add a Lint before and
4 after to catch all mistakes.
5 = Rack applications
f70b894 @raggi Update SPEC
raggi authored
6 A Rack application is a Ruby object (not a class) that
4cf4380 @raggi Adding SPEC with rake task dependencies
raggi authored
7 responds to +call+.
8 It takes exactly one argument, the *environment*
9 and returns an Array of exactly three values:
10 The *status*,
11 the *headers*,
12 and the *body*.
13 == The Environment
14 The environment must be an instance of Hash that includes
15 CGI-like headers. The application is free to modify the
16 environment.
17 The environment is required to include these variables
18 (adopted from PEP333), except when they'd be empty, but see
19 below.
20 <tt>REQUEST_METHOD</tt>:: The HTTP request method, such as
21 "GET" or "POST". This cannot ever
22 be an empty string, and so is
23 always required.
24 <tt>SCRIPT_NAME</tt>:: The initial portion of the request
25 URL's "path" that corresponds to the
26 application object, so that the
27 application knows its virtual
28 "location". This may be an empty
29 string, if the application corresponds
30 to the "root" of the server.
31 <tt>PATH_INFO</tt>:: The remainder of the request URL's
32 "path", designating the virtual
33 "location" of the request's target
34 within the application. This may be an
35 empty string, if the request URL targets
36 the application root and does not have a
37 trailing slash. This value may be
38 percent-encoded when I originating from
39 a URL.
40 <tt>QUERY_STRING</tt>:: The portion of the request URL that
41 follows the <tt>?</tt>, if any. May be
42 empty, but is always required!
43 <tt>SERVER_NAME</tt>, <tt>SERVER_PORT</tt>:: When combined with <tt>SCRIPT_NAME</tt> and <tt>PATH_INFO</tt>, these variables can be used to complete the 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. <tt>SERVER_NAME</tt> and <tt>SERVER_PORT</tt> can never be empty strings, and so are always required.
44 <tt>HTTP_</tt> Variables:: Variables corresponding to the
45 client-supplied HTTP request
46 headers (i.e., variables whose
47 names begin with <tt>HTTP_</tt>). The
48 presence or absence of these
49 variables should correspond with
50 the presence or absence of the
51 appropriate HTTP header in the
52 request.
53 In addition to this, the Rack environment must include these
54 Rack-specific variables:
55 <tt>rack.version</tt>:: The Array [1,1], representing this version of Rack.
56 <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the request URL.
57 <tt>rack.input</tt>:: See below, the input stream.
58 <tt>rack.errors</tt>:: See below, the error stream.
59 <tt>rack.multithread</tt>:: true if the application object may be simultaneously invoked by another thread in the same process, false otherwise.
60 <tt>rack.multiprocess</tt>:: true if an equivalent application object may be simultaneously invoked by another process, false otherwise.
61 <tt>rack.run_once</tt>:: true if the server expects (but does not guarantee!) that the application will only be invoked this one time during the life of its containing process. Normally, this will only be true for a server based on CGI (or something similar).
62 Additional environment specifications have approved to
63 standardized middleware APIs. None of these are required to
64 be implemented by the server.
65 <tt>rack.session</tt>:: A hash like interface for storing request session data.
66 The store must implement:
67 store(key, value) (aliased as []=);
68 fetch(key, default = nil) (aliased as []);
69 delete(key);
70 clear;
71 <tt>rack.logger</tt>:: A common object interface for logging messages.
72 The object must implement:
73 info(message, &block)
74 debug(message, &block)
75 warn(message, &block)
76 error(message, &block)
77 fatal(message, &block)
78 The server or the application can store their own data in the
79 environment, too. The keys must contain at least one dot,
80 and should be prefixed uniquely. The prefix <tt>rack.</tt>
81 is reserved for use with the Rack core distribution and other
82 accepted specifications and must not be used otherwise.
83 The environment must not contain the keys
85 (use the versions without <tt>HTTP_</tt>).
86 The CGI keys (named without a period) must have String values.
87 There are the following restrictions:
88 * <tt>rack.version</tt> must be an array of Integers.
89 * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
90 * There must be a valid input stream in <tt>rack.input</tt>.
91 * There must be a valid error stream in <tt>rack.errors</tt>.
92 * The <tt>REQUEST_METHOD</tt> must be a valid token.
93 * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
94 * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
95 * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
96 * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
97 set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
98 <tt>SCRIPT_NAME</tt> is empty.
99 <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
100 === The Input Stream
101 The input stream is an IO-like object which contains the raw HTTP
102 POST data.
103 When applicable, its external encoding must be "ASCII-8BIT" and it
104 must be opened in binary mode, for Ruby 1.9 compatibility.
105 The input stream must respond to +gets+, +each+, +read+ and +rewind+.
106 * +gets+ must be called without arguments and return a string,
107 or +nil+ on EOF.
108 * +read+ behaves like IO#read. Its signature is <tt>read([length, [buffer]])</tt>.
f70b894 @raggi Update SPEC
raggi authored
109 If given, +length+ must be a non-negative Integer (>= 0) or +nil+, and +buffer+ must
4cf4380 @raggi Adding SPEC with rake task dependencies
raggi authored
110 be a String and may not be nil. If +length+ is given and not nil, then this method
111 reads at most +length+ bytes from the input stream. If +length+ is not given or nil,
112 then this method reads all data until EOF.
113 When EOF is reached, this method returns nil if +length+ is given and not nil, or ""
114 if +length+ is not given or is nil.
115 If +buffer+ is given, then the read data will be placed into +buffer+ instead of a
116 newly created String object.
117 * +each+ must be called without arguments and only yield Strings.
118 * +rewind+ must be called without arguments. It rewinds the input
119 stream back to the beginning. It must not raise Errno::ESPIPE:
120 that is, it may not be a pipe or a socket. Therefore, handler
121 developers must buffer the input data into some rewindable object
122 if the underlying input stream is not rewindable.
123 * +close+ must never be called on the input stream.
124 === The Error Stream
125 The error stream must respond to +puts+, +write+ and +flush+.
126 * +puts+ must be called with a single argument that responds to +to_s+.
127 * +write+ must be called with a single argument that is a String.
128 * +flush+ must be called without arguments and must be called
129 in order to make the error appear for sure.
130 * +close+ must never be called on the error stream.
131 == The Response
132 === The Status
133 This is an HTTP status. When parsed as integer (+to_i+), it must be
134 greater than or equal to 100.
135 === The Headers
136 The header must respond to +each+, and yield values of key and value.
137 The header keys must be Strings.
138 The header must not contain a +Status+ key,
139 contain keys with <tt>:</tt> or newlines in their name,
140 contain keys names that end in <tt>-</tt> or <tt>_</tt>,
141 but only contain keys that consist of
142 letters, digits, <tt>_</tt> or <tt>-</tt> and start with a letter.
143 The values of the header must be Strings,
144 consisting of lines (for multiple header values, e.g. multiple
145 <tt>Set-Cookie</tt> values) seperated by "\n".
146 The lines must not contain characters below 037.
147 === The Content-Type
148 There must be a <tt>Content-Type</tt>, except when the
149 +Status+ is 1xx, 204 or 304, in which case there must be none
150 given.
151 === The Content-Length
152 There must not be a <tt>Content-Length</tt> header when the
153 +Status+ is 1xx, 204 or 304.
154 === The Body
155 The Body must respond to +each+
156 and must only yield String values.
157 The Body itself should not be an instance of String, as this will
158 break in Ruby 1.9.
159 If the Body responds to +close+, it will be called after iteration.
160 If the Body responds to +to_path+, it must return a String
161 identifying the location of a file whose contents are identical
162 to that produced by calling +each+; this may be used by the
163 server as an alternative, possibly more efficient way to
164 transport the response.
165 The Body commonly is an Array of Strings, the application
166 instance itself, or a File-like object.
167 == Thanks
168 Some parts of this specification are adopted from PEP333: Python
169 Web Server Gateway Interface
170 v1.0 ( I'd like to thank
171 everyone involved in that effort.
Something went wrong with that request. Please try again.