PSGI - an interface between web servers and perl web applications
Applications
A PSGI application is a function that takes a hash reference with environment variables as an argument and returns a response.
Inquiry
The hash passed to the application contains variables whose names are similar to the names of the headers in CGI - REQUEST_METHOD, SCRIPT_NAME, QUERY_STRING, etc., as well as the headers from the HTTP request (starting with HTTP_, for example HTTP_HOST). In addition, the hash must contain PSGI-specific variables:
- psgi.version: A reference to the array [1,0] containing the version of PSGI.
- psgi.url_scheme: http or https, depending on the request.
- psgi.input: input stream.
- psgi.errors: stream for error output.
- psgi.multithread: true if the application can be called in another thread of the same process.
- psgi.multiprocess: true if the application can be called in another process.
- psgi.run_once: true if it is expected (but not guaranteed) that the application will be called only once before the completion of the process (usually true only if the application is connected to the server via CGI).
- psgi.nonblocking: true if the application is called in the event loop.
- psgi.streaming: true if the server supports pending responses and streaming.
Answer
In general, the application should return a reference to an array of three elements - the HTTP response code, headers and the response body. The response HTTP code must be an integer of at least 100. The headers are sent as a reference to the array, and the Content-Type header must be present (except for 1xx, 204, or 304 responses). The response body can be a reference to an array of strings (or the entire response without line-by-line separation), or an IO :: Handle-like object or file descriptor. If a delayed response is required, the application can return a function to which a callback will be sent for a response.
Application example
This is the simplest PSGI application:
sub {[200, ['Content-Type' => 'text/plain'], ['Hi, ' . shift->{REMOTE_ADDR}]]}Middleware
Middleware is similar to a regular application, but takes 2 arguments - environment variables and the response of the PSGI application. Middleware can be used to analyze this data (for example, the usual access log maintenance) or to modify it. Here is an example of middleware adding an X-PSGI-Used header to the response:
my $app = sub {[200, ['Content-Type' => 'text/plain'], ['Hi, ' . shift->{REMOTE_ADDR}]]}
my $middleware = sub {
my $env = shift;
my $res = $app->($env);
push @{$res->[1]}, 'X-PSGI-Used' => 1;
return $res;
};For Plack on CPAN, there is a fairly large set of diverse middlewares .
Servers
The server provides the launch of the PSGI application and must compile a hash with environment variables and transfer it to the application, as well as process the response. Now there are several PSGI servers:
- Plack is a set of middleware and servers, most of which are wrappers over existing Perl modules. In addition, there are several servers in the same namespace that are not present in the composition of the Plack distribution on CPAN: AnyEvent , FCGI :: EV , Danga :: Socket , Coro , POE, and ServerSimple . Running a PSGI application on Plack is simple:
plackup --server Coro --port 9090 --host 127.0.0.1 test.psgi - nginx with patch for psgi support
- mod_psgi for Apache
- Perlbal
- some other servers
Framework support
Now there is PSGI support in all popular web frameworks: Catalyst , CGI :: Application , HTTP :: Engine , Dancer , Mason , Squatting , Continuity , Maypole , Tatsumaki
References
PSGI / Plack website PSGI
specification on CPAN
PSGI :: FAQ
Plack on CPAN