Back to Home

PSGI - an interface between web servers and perl web applications

perl · psgi · plack

PSGI - an interface between web servers and perl web applications

    Not so long ago there was an interface specification between web servers and applications / frameworks on perl PSGI - Perl Web Server Gateway Interface Specification . PSGI adds an abstraction layer that allows you not to worry about a specific way to connect to a web server, and implement a single interface for specification. You can run such an application on servers that support PSGI - at the moment it is Plack (a set of servers and utilities), nginx (with a patch for PSGI support and built-in perl) and Apache with mod_psgi.

    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.
    Also, the hash may contain additional variables:
    • 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.
    An application can analyze environment variables, and take into account the features of the server or complete its execution if the server does not support what the application needs (for example, the launched application is non-blocking and the server is written in a synchronous style).

    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:
    I want to separately mention Plack :: Server :: AnyEvent and Plack :: Server :: Coro - these servers support file AIO, so small non-blocking PSGI applications based on them can be used to distribute statics with a complete absence of blocking, while AIO support is not ready in nginx.

    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

    Read Next