Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 648 lines (450 sloc) 19.979 kB
30917ff @miyagawa POD fixes
miyagawa authored
1 =encoding utf-8
2
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
3 =head1 NAME
4
5 PSGI - Perl Web Server Gateway Interface Specification
6
7 =head1 ABSTRACT
8
ff0e6de @miyagawa paragraphized
miyagawa authored
9 This document specifies a standard interface between web servers and
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
10 Perl web applications or frameworks. This interface is designed to promote web application
11 portability and reduce the duplication of effort by web application
ff0e6de @miyagawa paragraphized
miyagawa authored
12 framework developers.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
13
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
14 Please keep in mind that PSGI is not Yet Another web application
0f6003f @miyagawa Added a note that PSGI is not an API for end users
miyagawa authored
15 framework. PSGI is a specification to decouple web server environments
38d724b @miyagawa Promote psgi.streaming, nonblocking and run_once keys to be MUST.
miyagawa authored
16 from web application framework code. Nor is PSGI a web application
17 API. Web application developers (end users) will not run their web
18 applications directly using the PSGI interface, but instead are
19 encouraged to use frameworks that support PSGI.
0f6003f @miyagawa Added a note that PSGI is not an API for end users
miyagawa authored
20
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
21 =head1 TERMINOLOGY
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
22
23 =over 4
24
25 =item Servers
26
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
27 A I<Server> is a web server that accepts HTTP requests, dispatches the
28 request to web applications, and returns the HTTP response to the
29 client. In the context of PSGI the server could be a Perl process running
a19c5ba @miyagawa minor grammer fixes
miyagawa authored
30 inside an HTTP server (e.g. mod_perl in Apache), a daemon process
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
31 called from a web server (e.g. FastCGI daemon) or a pure perl HTTP
32 server.
33
a19c5ba @miyagawa minor grammer fixes
miyagawa authored
34 Servers are also called I<PSGI implementations> as well as
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
35 I<Backends>.
36
37 =item Applications
38
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
39 I<Applications> are web applications that accept an HTTP request
40 and return an HTTP response. In PSGI an application is a code reference.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
41
42 =item Middleware
43
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
44 I<Middleware> is a PSGI application (a code reference) I<and> a
45 I<Server>. I<Middleware> looks like an I<application> when called from a
46 I<server>, and it in turn can call other I<applications>. It can be thought of
47 a I<plugin> to extend a PSGI application.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
48
49 =item Framework developers
50
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
51 I<Framework developers> are the authors of web application frameworks. They
52 write adapters (or engines) which accept PSGI input, run a web
53 application, and return a PSGI response to the I<server>.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
54
55 =item Web application developers
56
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
57 I<Web application developers> are developers who write code on top of a web
58 application framework. These developers should never have to deal with PSGI
59 directly.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
60
30917ff @miyagawa POD fixes
miyagawa authored
61 =back
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
62
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
63 =head1 SPECIFICATION
64
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
65 =head2 Application
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
66
ff0e6de @miyagawa paragraphized
miyagawa authored
67 A PSGI application is a Perl code reference. It takes exactly one
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
68 argument, the environment, and returns an array reference containing exactly
ff0e6de @miyagawa paragraphized
miyagawa authored
69 three values.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
70
e0431ce @miyagawa make PSGI app a code ref
miyagawa authored
71 my $app = sub {
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
72 my $env = shift;
73 return [
74 '200',
75 [ 'Content-Type' => 'text/plain' ],
31bd78b @miyagawa s/objects/object/ because it sounded like an array of IO::Handle objects
miyagawa authored
76 [ "Hello World" ], # or IO::Handle-like object
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
77 ];
e0431ce @miyagawa make PSGI app a code ref
miyagawa authored
78 };
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
79
80 =head3 The Environment
81
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
82 The environment B<MUST> be a hash reference that includes CGI-like headers, as
83 detailed below. The application is free to modify the environment. The
84 environment B<MUST> include these keys (adopted from L<PEP
85 333|http://www.python.org/dev/peps/pep-0333/>,
86 L<Rack|http://rack.rubyforge.org/doc/files/SPEC.html> and
87 L<JSGI|http://jackjs.org/jsgi-spec.html>) except when they would normally be
88 empty.
89
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
90 When an environment key is described as a boolean, its value B<MUST> conform
91 to Perl's notion of boolean-ness. This means that an empty string or an
92 explicit C<0> are both valid false values. If a boolean key is not present, an
1d6c6fe @miyagawa changed how to treat when boolean key doesn't exist
miyagawa authored
93 application B<MAY> treat this as a false value.
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
94
95 The values for all CGI keys (named without a period) B<MUST> be a scalar
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
96 string.
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
97
98 See below for details.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
99
100 =over 4
101
102 =item *
103
ff0e6de @miyagawa paragraphized
miyagawa authored
104 C<REQUEST_METHOD>: The HTTP request method, such as "GET" or
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
105 "POST". This B<MUST NOT> be an empty string, and so is always
ff0e6de @miyagawa paragraphized
miyagawa authored
106 required.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
107
108 =item *
109
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
110 C<SCRIPT_NAME>: The initial portion of the request URL's I<path>,
111 corresponding to the application. This tells the application its
65afea5 @miyagawa apply grammer fixes from hanekomu
miyagawa authored
112 virtual "location". This may be an empty string if the application
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
113 corresponds to the server's root URI.
114
115 If this key is not empty, it B<MUST> start with a forward slash (C</>).
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
116
117 =item *
118
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
119 C<PATH_INFO>: The remainder of the request URL's I<path>, designating
ff0e6de @miyagawa paragraphized
miyagawa authored
120 the virtual "location" of the request's target within the
65afea5 @miyagawa apply grammer fixes from hanekomu
miyagawa authored
121 application. This may be an empty string if the request URL targets
ff0e6de @miyagawa paragraphized
miyagawa authored
122 the application root and does not have a trailing slash. This value
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
123 should be URI decoded by servers in order to be compatible with L<RFC 3875|http://www.ietf.org/rfc/rfc3875>.
124
125 If this key is not empty, it B<MUST> start with a forward slash (C</>).
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
126
127 =item *
128
c6d7946 @miyagawa added REQUEST_URI to the PSGI spec
miyagawa authored
129 C<REQUEST_URI>: The undecoded, raw request URL line. It is the raw URI
130 path and query part that appears in the HTTP C<GET /... HTTP/1.x> line
131 and doesn't contain URI scheme and host names.
132
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
133 Unlike C<PATH_INFO>, this value B<SHOULD NOT> be decoded by servers. It is an
134 application's responsibility to properly decode paths in order to map URLs to
135 application handlers if they choose to use this key instead of C<PATH_INFO>.
c6d7946 @miyagawa added REQUEST_URI to the PSGI spec
miyagawa authored
136
137 =item *
138
ff0e6de @miyagawa paragraphized
miyagawa authored
139 C<QUERY_STRING>: The portion of the request URL that follows the C<?>,
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
140 if any. This key B<MAY> be empty, but B<MUST> always be present, even if empty.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
141
142 =item *
143
ff0e6de @miyagawa paragraphized
miyagawa authored
144 C<SERVER_NAME>, C<SERVER_PORT>: When combined with C<SCRIPT_NAME> and
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
145 C<PATH_INFO>, these keys can be used to complete the URL. Note,
ff0e6de @miyagawa paragraphized
miyagawa authored
146 however, that C<HTTP_HOST>, if present, should be used in preference
147 to C<SERVER_NAME> for reconstructing the request URL. C<SERVER_NAME>
9170aac @miyagawa lint pod
miyagawa authored
148 and C<SERVER_PORT> B<MUST NOT> be empty strings, and are always
ff0e6de @miyagawa paragraphized
miyagawa authored
149 required.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
150
151 =item *
152
ff0e6de @miyagawa paragraphized
miyagawa authored
153 C<SERVER_PROTOCOL>: The version of the protocol the client used to
154 send the request. Typically this will be something like "HTTP/1.0" or
155 "HTTP/1.1" and may be used by the application to determine how to
156 treat any HTTP request headers.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
157
158 =item *
159
575189d @miyagawa update CONTENT_LENGTH/CONTENT_TYPE existence
miyagawa authored
160 C<CONTENT_LENGTH>: The length of the content in bytes, as an
161 integer. The presence or absence of this key should correspond to the
162 presence or absence of HTTP Content-Length header in the request.
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
163
164 =item *
165
575189d @miyagawa update CONTENT_LENGTH/CONTENT_TYPE existence
miyagawa authored
166 C<CONTENT_TYPE>: The request's MIME type, as specified by the client.
167 The presence or absence of this key should correspond to the presence
168 or absence of HTTP Content-Type header in the request.
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
169
170 =item *
171
172 C<HTTP_*> Keys: These keys correspond to the client-supplied
173 HTTP request headers. The presence or absence of these keys should
65afea5 @miyagawa apply grammer fixes from hanekomu
miyagawa authored
174 correspond to the presence or absence of the appropriate HTTP header
ff0e6de @miyagawa paragraphized
miyagawa authored
175 in the request.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
176
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
177 If there are multiple header lines sent with the same key, the server
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
178 should treat them as if they were sent in one line and combine them
179 with C<, >, as in L<RFC 2616|http://www.ietf.org/rfc/rfc2616>.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
180
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
181 =back
182
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
183 In addition to the keys above, the PSGI environment B<MUST> also include these
184 PSGI-specific keys:
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
185
186 =over 4
187
188 =item *
189
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
190 C<psgi.version>: An array reference [1,0] representing this version of
191 PSGI. The first number is the major version and the second it the minor
192 version.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
193
194 =item *
195
196 C<psgi.url_scheme>: A string C<http> or C<https>, depending on the request URL.
197
198 =item *
199
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
200 C<psgi.input>: the input stream. See below for details.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
201
202 =item *
203
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
204 C<psgi.errors>: the error stream. See below for details.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
205
eee7655 @miyagawa Added psgi.multithread and psgi.multiprocess per discussion on about …
miyagawa authored
206 =item *
207
9170aac @miyagawa lint pod
miyagawa authored
208 C<psgi.multithread>: This is a boolean value, which B<MUST> be true if the
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
209 application may be simultaneously invoked by another thread in the same
210 process, false otherwise.
eee7655 @miyagawa Added psgi.multithread and psgi.multiprocess per discussion on about …
miyagawa authored
211
212 =item *
213
9170aac @miyagawa lint pod
miyagawa authored
214 C<psgi.multiprocess>: This is a boolean value, which B<MUST> be true if an
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
215 equivalent application object may be simultaneously invoked by another
216 process, false otherwise.
eee7655 @miyagawa Added psgi.multithread and psgi.multiprocess per discussion on about …
miyagawa authored
217
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
218 =item *
219
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
220 C<psgi.run_once>: A boolean which is true if the server expects (but does not
221 guarantee!) that the application will only be invoked this one time during
222 the life of its containing process. Normally, this will only be true for a
ff0e6de @miyagawa paragraphized
miyagawa authored
223 server based on CGI (or something similar).
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
224
225 =item *
226
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
227 C<psgi.nonblocking>: A boolean which is true if the server is calling the
228 application in an non-blocking event loop.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
229
667bda2 @miyagawa Draft streaming response
miyagawa authored
230 =item *
231
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
232 C<psgi.streaming>: A boolean which is true if the server supports callback
233 style delayed response and streaming writer object.
667bda2 @miyagawa Draft streaming response
miyagawa authored
234
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
235 =back
236
f43792e @miyagawa added psgix.logger and psgix.session
miyagawa authored
237 The PSGI environment B<MAY> also include the following additional
238 extensions. They are B<OPTIONAL> and applications and middleware
239 components B<SHOULD> check if they exist in the environment before
240 using it.
241
242 =over 4
243
244 =item *
245
246 C<psgix.logger>: A code reference to log messages. The code reference
247 is passed one argument as a hash reference that represents a message
248 to be logged. The hash reference B<MUST> include at least two keys:
249 C<level> and C<message> where C<level> B<MUST> be one of the following
250 strings: C<debug>, C<warn>, C<info>, C<error> and C<fatal>. C<message>
251 B<SHOULD> be a plain string or a scalar variable that stringifies.
252
253 =item *
254
255 C<psgix.session>: A hash reference for storing and retrieving session
256 data.
257
258 C<psgix.session.options>: A hash reference to tell Middleware
259 components how to manipulate session data after the
260 request. Acceptable keys and values are implementation specific.
261
262 =back
263
264 The server or the application can store its own data in the
265 environment as well. These keys B<MUST> contain at least one dot, and
266 B<SHOULD> be prefixed uniquely.
267
268 The C<psgi.> prefix is reserved for use with the PSGI core
269 specification, and C<psgix.> prefix is reserved for officially blessed
270 extensions. These prefixes B<MUST NOT> be used by other servers or
271 application.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
272
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
273 The environment B<MUST NOT> contain keys named C<HTTP_CONTENT_TYPE> or
274 C<HTTP_CONTENT_LENGTH>.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
275
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
276 One of C<SCRIPT_NAME> or C<PATH_INFO> B<MUST> be set. C<PATH_INFO> should be
277 C</> if C<SCRIPT_NAME> is empty. C<SCRIPT_NAME> B<MUST NOT> be C</>, but
278 B<MAY> be empty.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
279
280 =head3 The Input Stream
281
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
282 The input stream in C<psgi.input> is an L<IO::Handle>-like object which
ff0e6de @miyagawa paragraphized
miyagawa authored
283 streams the raw HTTP POST or PUT data. If it is a file handle then it
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
284 B<MUST> be opened in binary mode. The input stream B<MUST> respond to
285 C<read> and B<MAY> implement C<seek>.
286
287 Perl's built-in filehandles or L<IO::Handle> based objects should work as-is
288 in a PSGI server. Application developers B<SHOULD NOT> inspect the type or
289 class of the stream. Instead, they B<SHOULD> simply call C<read> on the object.
290
291 Application developers B<SHOULD NOT> use Perl's built-in C<read> or iterator
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
292 (C<< <$fh> >>) to read from the input stream. Instead, application
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
293 developers should call C<read> as a method (C<< $fh->read >>) to allow for
294 duck typing.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
295
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
296 Framework developers, if they know the input stream will be used with the
297 built-in read() in any upstream code they can't touch, B<SHOULD> use PerlIO or
298 a tied handle to work around with this problem.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
299
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
300 The input stream objet is expected to provide a C<read> method:
e764e4f @miyagawa Added a note about read() built-in
miyagawa authored
301
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
302 =over 4
303
304 =item read
305
306 $input->read($buf, $len [, $offset ]);
307
a87c263 @miyagawa mentions return value of psgi.input and psgi.errors methods
miyagawa authored
308 Returns the number of characters actually read, 0 at end of file, or
309 undef if there was an error.
310
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
311 =back
312
313 It may also implement an optional C<seek> method.
314
315 =over 4
316
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
317 =item seek
318
319 $input->seek($pos, $whence);
320
a87c263 @miyagawa mentions return value of psgi.input and psgi.errors methods
miyagawa authored
321 Returns 1 on success, 0 otherwise.
322
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
323 =back
324
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
325 See the L<IO::Handle> documentation for more details on exactly how these
326 methods should work.
327
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
328 =head3 The Error Stream
329
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
330 The error stream in C<psgi.errors> is an L<IO::Handle>-like object to
331 print errors. The error stream must implement a C<print> method.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
332
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
333 As with the input stream, Perl's built-in filehandles or L<IO::Handle> based
334 objects should work as-is in a PSGI server. Application developers B<SHOULD
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
335 NOT> inspect the type or class of the stream. Instead, they B<SHOULD> simply
336 call C<print> on the object.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
337
338 =over 4
339
340 =item print
341
342 $errors->print($error);
343
a87c263 @miyagawa mentions return value of psgi.input and psgi.errors methods
miyagawa authored
344 Returns true if successful.
345
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
346 =back
347
348 =head3 The Response
349
f745e83 @miyagawa array or code ref. That is MUST per PSGI spec POV. If you return some…
miyagawa authored
350 Applications B<MUST> return a response as either a three element array
351 reference, or a code reference for a delayed/streaming response.
0d57abb @miyagawa some wording fixes
miyagawa authored
352
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
353 The response array reference consists of the following elements:
667bda2 @miyagawa Draft streaming response
miyagawa authored
354
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
355 =head4 Status
356
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
357 An HTTP status code. This B<MUST> be an integer greater than or equal to 100,
358 and B<SHOULD> be an HTTP status code as documented in L<RFC
359 2616|http://www.w3.org/Protocols/rfc2616>.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
360
361 =head4 Headers
362
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
363 The headers B<MUST> be an array reference (B<not> a hash reference)
364 of key/value pairs. This means it B<MUST> contain an even number of elements.
365
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
366 The header B<MUST NOT> contain a key named C<Status>, onr any keys with C<:>
367 or newlines in their name. It B<MUST NOT> contain any keys that end in C<-> or
368 C<_>.
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
369
370 All keys B<MUST> consist only of letters, digits, C<_> or C<->. All keys
371 B<MUST> start with a letter. The value of the header must be a scalar
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
372 string. The value string B<MUST NOT> contain characters below ASCII chr(37)
373 except for chr(32) (whitespace).
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
374
ff0e6de @miyagawa paragraphized
miyagawa authored
375 If the same key name appears multiple times in an array ref, those
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
376 header lines B<MUST> be sent to the client separately (e.g. multiple
ff0e6de @miyagawa paragraphized
miyagawa authored
377 C<Set-Cookie> lines).
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
378
379 =head4 Content-Type
380
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
381 There B<MUST> be a C<Content-Type> except when the C<Status> is 1xx, 204
382 or 304, in which case there B<MUST NOT> be a content type.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
383
384 =head4 Content-Length
385
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
386 There B<MUST NOT> be a C<Content-Length> header when the C<Status> is
ff0e6de @miyagawa paragraphized
miyagawa authored
387 1xx, 204 or 304.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
388
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
389 If the Status is not 1xx, 204 or 304 and there is no C<Content-Length> header,
390 a PSGI server B<MAY> calculate the content length by looking at the Body. This
391 value can then be appended to the list of headers returned by the application.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
392
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
393 =head4 Body
394
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
395 The response body B<MUST> be returned from the application as either an array
396 reference or a handle.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
397
398 =over 4
399
400 =item *
401
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
402 If the body is an array reference, it is expected to contain an array of lines
403 which make up the body.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
404
405 my $body = [ "Hello\n", "World\n" ];
406
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
407 Note that the elements in an array reference are B<NOT REQUIRED> to end
408 in a newline. A server B<SHOULD> write each elements as-is to the
409 client, and B<SHOULD NOT> care if the line ends with newline or not.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
410
6342aa0 @miyagawa change the variable name to be less confusing
miyagawa authored
411 An array reference with a single value is valid. So C<[ $html ]> is a valid
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
412 response body.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
413
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
414 =item *
415
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
416 The body can instead be a handle, either a Perl built-in filehandle or an
417 L<IO::Handle>-like object.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
418
419 open my $body, "</path/to/file";
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
420 open my $body, "<:via(SomePerlIO)", ...;
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
421 my $body = IO::File->new("/path/to/file");
422
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
423 my $body = SomeClass->new(); # mock class that implements getline() and close()
424
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
425 Servers B<SHOULD NOT> check the type or class of the body. Instead, they should
426 simply call C<getline> to iterate over the body, and
0e5b33e @miyagawa Updated spec: body can respond ->path.
miyagawa authored
427 call C<close> when done.
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
428
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
429 Servers B<MAY> check if the body is a real filehandle using C<fileno> and
430 C<Scalar::Util::reftype>. If the body is real filehandle, the server B<MAY>
431 optimize using techniques like I<sendfile(2)>.
432
433 The body object B<MAY> also respond to a C<path> method. This method is
434 expected to return the path to a file accessible by the server. This allows
435 the server to use this information instead of a file descriptor number to
e0431ce @miyagawa make PSGI app a code ref
miyagawa authored
436 serve the file.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
437
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
438 Servers B<SHOULD> set the C<$/> special variable to the buffer size when
439 reading content from C<$body> using the C<getline> method. This is done by
440 setting C<$/> with a reference to an integer (C<$/ = \8192>).
0e5b33e @miyagawa Updated spec: body can respond ->path.
miyagawa authored
441
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
442 If the body filehandle is a Perl built-in filehandle L<IO::Handle> object,
443 they will respect this value. Similarly, an object which provides the same API
444 B<MAY> also respect this special variable, but are not required to do so.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
445
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
446 =back
447
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
448 =head2 Delayed Response and Streaming Body
667bda2 @miyagawa Draft streaming response
miyagawa authored
449
c5aa24f @miyagawa psgi.streaming MAY -> SHOULD. Remove poll_cb from the Writer spec.
miyagawa authored
450 The PSGI interface allows applications and servers to provide a
451 callback-style response instead of the three-element array
452 reference. This allows for a delayed response and a streaming body
453 (server push).
454
455 This interface B<SHOULD> be implemented by PSGI servers, and
456 C<psgi.streaming> environment B<MUST> be set to true in such servers.
667bda2 @miyagawa Draft streaming response
miyagawa authored
457
c5aa24f @miyagawa psgi.streaming MAY -> SHOULD. Remove poll_cb from the Writer spec.
miyagawa authored
458 To enable a delayed response, the application B<SHOULD> return a
5589b64 @miyagawa Some linting, and fixed wrong FAQ explanation about streaming/nonbloc…
miyagawa authored
459 callback as its response. An application B<MAY> check if the
c5aa24f @miyagawa psgi.streaming MAY -> SHOULD. Remove poll_cb from the Writer spec.
miyagawa authored
460 C<psgi.streaming> environment is true and falls back to the direct
461 response if it isn't.
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
462
463 This callback will be called with I<another> subroutine reference (referred to
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
464 as the I<responder> from now on) as its only argument. The I<responder>
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
465 should in turn be called with the standard three element array reference
466 response. This is best illustrated with an example:
667bda2 @miyagawa Draft streaming response
miyagawa authored
467
468 my $app = sub {
469 my $env = shift;
470
471 # Delays response until it fetches content from the network
472 return sub {
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
473 my $responder = shift;
474
c5aa24f @miyagawa psgi.streaming MAY -> SHOULD. Remove poll_cb from the Writer spec.
miyagawa authored
475 fetch_content_from_server(sub {
476 my $content = shift;
477 $responder->([ 200, $headers, [ $content ] ]);
478 });
667bda2 @miyagawa Draft streaming response
miyagawa authored
479 };
480 };
481
c5aa24f @miyagawa psgi.streaming MAY -> SHOULD. Remove poll_cb from the Writer spec.
miyagawa authored
482 An application B<MAY> omit the third element (the body) when calling
483 the I<responder>. If the body is omitted, the I<responder> B<MUST>
484 return I<yet another> object which implements C<write> and C<close>
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
485 methods. Again, an example illustrates this best.
667bda2 @miyagawa Draft streaming response
miyagawa authored
486
487 my $app = sub {
488 my $env = shift;
489
490 # immediately starts the response and stream the content
491 return sub {
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
492 my $responder = shift;
493 my $writer = $responder->([ 200, [ 'Content-Type', 'application/json' ]]);
667bda2 @miyagawa Draft streaming response
miyagawa authored
494
495 wait_for_events(sub {
496 my $new_event = shift;
497 if ($new_event) {
498 $writer->write($new_event->as_json . "\n");
499 } else {
500 $writer->close;
501 }
502 });
503 };
504 };
505
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
506 This delayed response and streaming API is useful if you want to
507 implement a non-blocking I/O based server streaming or long-poll Comet
5589b64 @miyagawa Some linting, and fixed wrong FAQ explanation about streaming/nonbloc…
miyagawa authored
508 push technology, but could also be used to implement unbuffered writes
509 in a blocking server.
667bda2 @miyagawa Draft streaming response
miyagawa authored
510
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
511 =head2 Middleware
512
b4d5be2 @autarch Various tweaks from discussion with miyagawa in #psgi, and line-wrapp…
autarch authored
513 A I<middleware> component takes another PSGI application and runs it. From the
514 perspective of a server, a middleware component is a PSGI application. From
515 the perspective of the application being run by the middleware component, the
516 middleware is the server. Generally, this will be done in order to implement
517 some sort of pre-processing on the PSGI environment hash or post-processing on
518 the response.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
519
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
520 Here's a simple example that appends a special HTTP header
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
521 I<X-PSGI-Used> to any PSGI application.
522
523 # $app is a simple PSGI application
524 my $app = sub {
525 my $env = shift;
526 return [ '200', [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ];
527 };
528
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
529 # $xheader is a piece of middleware that wraps $app
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
530 my $xheader = sub {
531 my $env = shift;
532 my $res = $app->($env);
533 push @{$res->[1]}, 'X-PSGI-Used' => 1;
534 return $res;
535 };
536
3e2dde3 @autarch English cleanup, rewriting parts for clarity, adding links, and small…
autarch authored
537 Middleware B<MUST> behave exactly like a PSGI application from the perspective
538 of a server. Middleware B<MAY> decide not to support the streaming interface
539 discussed earlier, but B<SHOULD> pass through the response types that it doesn't
540 understand.
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
541
38d724b @miyagawa Promote psgi.streaming, nonblocking and run_once keys to be MUST.
miyagawa authored
542 =head1 CHANGELOGS
543
544 1.1: 2010.02.xx
545
546 =over 4
547
548 =item *
549
f43792e @miyagawa added psgix.logger and psgix.session
miyagawa authored
550 Added optional PSGI keys as extensions: C<psgix.logger> and C<psgix.session>.
551
552 =item *
553
38d724b @miyagawa Promote psgi.streaming, nonblocking and run_once keys to be MUST.
miyagawa authored
554 C<psgi.streaming> B<SHOULD> be implemented by PSGI servers, rather than B<MAY>.
555
556 =item *
557
558 PSGI keys C<psgi.run_once>, C<psgi.nonblocking> and C<psgi.streaming>
559 B<MUST> be set by PSGI servers.
560
561 =item *
562
563 Removed C<poll_cb> from writer methods.
564
565 =back
566
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
567 =head1 ACKNOWLEDGEMENTS
568
569 Some parts of this specification are adopted from the following specifications.
570
571 =over 4
572
573 =item *
574
575 PEP333 Python Web Server Gateway Interface L<http://www.python.org/dev/peps/pep-0333>
576
577 =item *
578
579 Rack L<http://rack.rubyforge.org/doc/SPEC.html>
580
581 =item *
582
583 JSGI Specification L<http://jackjs.org/jsgi-spec.html>
584
585 =back
586
587 I'd like to thank authors of these great documents.
588
589 =head1 AUTHOR
590
591 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
592
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
593 =head1 CONTRIBUTORS
594
595 The following people have contributed to the PSGI specification and
596 Plack implementation by commiting their code, sending patches,
597 reporting bugs, asking questions, suggesting useful advices,
598 nitpicking, chatting on IRC or commenting on my blog (in no particular
599 order):
600
601 Tokuhiro Matsuno
602 Kazuhiro Osawa
603 Yuval Kogman
604 Kazuho Oku
605 Alexis Sukrieh
048f9a3 @miyagawa update Dann's name. README links to the site
miyagawa authored
606 Takatoshi Kitano
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
607 Stevan Little
608 Daisuke Murase
609 mala
610 Pedro Melo
611 Jesse Luehrs
612 John Beppu
613 Shawn M Moore
614 Mark Stosberg
615 Matt S Trout
616 Jesse Vincent
617 Chia-liang Kao
618 Dave Rolsky
619 Hans Dieter Pearcey
620 Randy J Ray
621 Benjamin Trott
622 Max Maischein
623 Slaven Rezić
624 Marcel Grünauer
625 Masayoshi Sekimura
626 Brock Wilcox
627 Piers Cawley
ecdb6c0 @miyagawa added some HTTP::Engine contributors
miyagawa authored
628 Daisuke Maki
629 Kang-min Liu
2b08fe3 @miyagawa oops
miyagawa authored
630 Yasuhiro Matsumoto
5623139 @miyagawa changed the Content-Length from SHOULD to MAY
miyagawa authored
631 Ash Berlin
31284b9 @miyagawa Added Artur for his advice around gearman/schwartz
miyagawa authored
632 Artur Bergman
ba2001a @miyagawa more questions around HTTP::Engine confusions
miyagawa authored
633 Simon Cozens
634 Scott McWhirter
a87c263 @miyagawa mentions return value of psgi.input and psgi.errors methods
miyagawa authored
635 Jiro Nishiguchi
01b3c47 @miyagawa Sorry!
miyagawa authored
636 Masahiro Chiba
1ef1c9c @miyagawa Added patspam for his webgui work
miyagawa authored
637 Patrick Donelan
d7fc7bb @miyagawa Added frodwith for helping us sort out the non-blocking from POE deve…
miyagawa authored
638 Paul Driver
93432fb @miyagawa Added rafl
miyagawa authored
639 Florian Ragwitz
1173dc0 @miyagawa New spec: feedbacks got merged.
miyagawa authored
640
e85dbb3 @miyagawa First draft of the PSGI spec and FAQ.
miyagawa authored
641 =head1 COPYRIGHT AND LICENSE
642
643 Copyright Tatsuhiko Miyagawa, 2009.
644
645 This document is licensed under the Creative Commons license by-sa.
646
647 =cut
Something went wrong with that request. Please try again.