Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
312 lines (193 sloc) 7.78 KB


PSGI - Perl Web Server Gateway Interface Specification


This document specifies a standard interface between web servers and Perl web applications or frameworks, to promote web application portability and reduce the duplicated efforts by web application framework developers.



A PSGI application is a Perl code reference. It takes exactly one argument, the environment and returns an array reference of exactly three values.

  sub app {
      my $env = shift;
      return [
          [ 'Content-Type' => 'text/plain' ],
          [ "Hello World" ], # or IO::Handle-like objects

The Environment

The environment MUST be a hash reference that includes CGI-like headers. The application is free to modify the environment. The environment is required to include these variables (adopted from PEP333, Rack and JSGI) except when they'd be empty, but see below:

  • REQUEST_METHOD: The HTTP request method, such as "GET" or "POST". This cannot ever be an empty string, and so is always required.
  • SCRIPT_NAME: The initial portion of the request URL's path that corresponds to the application, so that the application knows its virtual "location". This may be an empty string, if the application corresponds to the "root" of the server.
  • PATH_INFO: 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 I originating from a URL.
  • QUERY_STRING: The portion of the request URL that follows the ?, if any. May be empty, but is always required.
  • SERVER_NAME, SERVER_PORT: When combined with SCRIPT_NAME and PATH_INFO, these variables can be used to complete the URL. Note, however, that HTTP_HOST, if present, should be used in preference to SERVER_NAME for reconstructing the request URL. SERVER_NAME and SERVER_PORT can never be empty strings, and so are always required.
  • SERVER_PROTOCOL: The version of the protocol the client used to send the request. Typically this will be something like "HTTP/1.0" or "HTTP/1.1" and may be used by the application to determine how to treat any HTTP request headers.
  • HTTP_ Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request.

In addition to this, the PSGI environment MUST include these PSGI-specific variables:

  • psgi.version: An array ref [1,0] representing this version of PSGI.
  • psgi.url_scheme: A string http or https, depending on the request URL.
  • psgi.input: the input stream. See blow.
  • psgi.errors: the error stream. See below.

The PSGI environment MAY include these optional PSGI variables:

  • psgi.run_once: 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).
  • psgi.async: true if the server is calling the application in an asynchronous event loop. See PSGI Async extension.

The server or the 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 psgi. is reserved for use with the PSGI core implementation and other accepted extensions and MUST NOT be used otherwise. The environment MUST NOT contain the keys HTTP_CONTENT_TYPE or HTTP_CONTENT_LENGTH (use the versions without HTTP_). The CGI keys (named without a period) MUST have a scalar variable containing strings. There are the following restrictions:

  • psgi.version MUST be an array of integers.
  • psgi.url_scheme MUST either be a scalar variable containing a string http or https.
  • There MUST be a valid input stream in psgi.input.
  • There MUST be a valid error stream in psgi.errors.
  • The REQUEST_METHOD MUST be a valid token.
  • The SCRIPT_NAME, if non-empty, MUST start with /
  • The PATH_INFO, if non-empty, MUST start with /
  • The CONTENT_LENGTH, if given, MUST consist of digits only.
  • One of SCRIPT_NAME or PATH_INFO MUST be set. PATH_INFO should be / if SCRIPT_NAME is empty. SCRIPT_NAME never should be /, but instead be empty.

The Input Stream

The input stream in psgi.input is an IO::Handle-like object which streams the raw HTTP POST or PUT data. If it is a file handle then it MUST be opened in binary mode. The input stream MUST respond to read and MAY implement seek.

The built-in filehandle or IO::Handle based objects should work fine everywhere. Application developers SHOULD NOT inspect the type or class of the stream, but instead just call read to duck type.

  $input->read($buf, $len [, $offset ]);
  $input->seek($pos, $whence);

The Error Stream

The error stream in psgi.errors is an IO::Handle-like object to print errors. The error stream must implement print.

The built-in filehandle or IO::Handle based objects should work fine everywhere. Application developers SHOULD NOT inspect the type or class of the stream, but instead just call print to duck type.


The Response


HTTP status code, is an integer and MUST be greater than or equal to 100.


The header must be an array reference containing a pair of key and value. Its number of elements MUST be even. The header MUST NOT contain a Status key, contain keys with : or newlines in their name, contain keys that end in - or _ but only contain keys that consist of letters, digits, _ or - and start with a letter. The value of the header must be a scalar value that contains strings. The value string MUST NOT contain characters below chr(37).

If the same key name appears multiple times in an array ref, those header lines MUST be sent to the client separately (e.g. multiple Set-Cookie lines).


There MUST be a Content-Type except when the Status is 1xx, 204 or 304, in which case there MUST be none given.


There MUST NOT be a Content-Length header when the Status is 1xx, 204 or 304.


The response body is returned from the application in one of following two types of scalar variable.

  • An array reference containing body as lines.
      my $body = [ "Hello\n", "World\n" ];
  • An IO::Handle-like object or a built-in filehandle.
      open my $body, "</path/to/file";
      my $body = IO::File->new("/path/to/file");

    Implementors SHOULD NOT check if the type or class of body but instead just call getline to iterate the body and call close when done.


Some parts of this specification are adopted from the following specifications.

I'd like to thank authors of these great documents.


Tatsuhiko Miyagawa <>


Copyright Tatsuhiko Miyagawa, 2009.

This document is licensed under the Creative Commons license by-sa.

Jump to Line
Something went wrong with that request. Please try again.