The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
=head1 NAME

PSGI::FAQ - Frequently Asked Questions and answers

=head1 QUESTIONS

=head2 General

=head3 How do you pronounce PSGI?

We read it simply P-S-G-I, but you may be able to pronounce it "sky" :)

=head3 So what is this?

PSGI is an interface between web servers and perl-based web
applications akin to what CGI does for web servers and CGI scripts.

=head3 Why do we need this?

Perl has L<CGI> as a core module that somewhat abstracts the
difference between CGI, mod_perl and FastCGI. However, most web
application framework developers (e.g. Catalyst and Jifty) usually
avoid using it to maximize the performance and to access low-level
APIs. So they end up writing adapters for all of those different
environments, some of which may be well tested while others are not.

PSGI allows web application framework developers to only write an
adapter for PSGI.  End users can choose from among all the backends that
support the PSGI interface.

=head3 You said PSGI is similar to CGI. How is the PSGI interaface different from CGI?

The PSGI interface is intentionally designed to be very similar to CGI so
that supporting PSGI in addition to CGI would be extremely easy. Here's
a highlight of the key differences between CGI and PSGI:

=over 4

=item *

In CGI, servers are the actual web servers written in any languages
but mostly in C, and script is a script that can be written in any
language such as C, Perl, Shell scripts, Ruby or Python.

In PSGI, servers are still web servers, but they're perl processes that
are usually embedded in the web server (like mod_perl) or a perl daemon
process called by a web server (like FastCGI), or an entirely perl based
web server.

=item *

In CGI, we use STDIN, STDERR, and environment variables to read
parameters and the HTTP request body and to send errors from the
application.

In PSGI, we use the C<$env> hash references and the I<psgi.input> and
I<psgi.errors> streams to pass that data between servers and applications.

=item *

In CGI, applications are supposed to print HTTP headers and body to
STDOUT to pass it back to the web server.

In PSGI, applications are supposed to return a HTTP status code,
headers, and body (as an array ref or a filehandle-like object) to the
application as an array reference.

=back

=head3 My framework already does CGI, FCGI and mod_perl. Why do I want to support PSGI?

If your web application framework already supports most server
environments, performance is good, and the backends are well tested,
there may not be a direct benefit for you to support PSGI immediately --
though you would be able to remove any code that overlaps with PSGI
backends. But if only CGI environment is currently supported, supporting
PSGI in addition should be extremely easy, and the benefit you and your
framework users will enjoy is huge.

=head3 I'm writing a web application. What's the benefit of PSGI for me?

If the framework you're using supports PSGI, that means your
application can run on any of existing and future PSGI
implementations. You can provide a C<.psgi> file that returns PSGI
application, the end users of your application should be able to
configure and run your application in a bunch of different ways.

=head3 But I'm writing a web application in CGI and it works well. Should I switch to PSGI?

If you're writing a web application with a plain CGI.pm and without
using any web frameworks, you're limiting your application in the
plain CGI environments, along with mod_perl and FastCGI with some
tweaks. If you're the only one developer and user of your application
then that's probably fine.

One day you want to deploy your application in the shared hosting for
your clients, or run your server in the standalone mode, or distribute
your application as an open source software. Limiting your application
in the CGI environment by using CGI.pm will bite you then.

You can start using one of PSGI compatible frameworks (either
full-stack ones or micro ones), or use L<Plack::Request> if you are
anti frameworks, to make your application PSGI aware, to be more
future proof.

Even if you ignore PSGI today and write applications in palin CGI, you
can always later switch to PSGI with the L<CGI::PSGI> wrapper.

=head3 What should I do to support PSGI?

If you're a web server developer, write a PSGI implementation that
calls a PSGI application. Or join the development on Plack, the
reference implementation of PSGI, to add backends for more web servers.

If you're a web application framework developer, write an adapter for
PSGI. Now you're freed from supporting all different server
environments.

If you're a web application developer (or a web application framework
user), choose the framework that supports PSGI, or ask the author to
support it. :) If your application is a large scale installable
application that doesn't use any existing frameworks (e.g. WebGUI or
Movable Type) you're considered as a framework developer instead from
the PSGI point of view. So, writing an adapter for PSGI on your
application would make more sense.

=head3 Is PSGI faster than (my framework)?

Again, PSGI is not an implementation, but there's a potential for a
very fast PSGI implementation that preloads everything and runs fully
optimized code as a preforked standalone with XS parsers and
sendfile(2) kernel call, an event-based tiny web server written in C
and embedded perl that supports PSGI, or a plain-old CGI.pm based
backend that doesn't load any modules at all and runs pretty quickly
without eating so much memory under the CGI environment.

The reference implementation Plack already has very fast backends like
Standalone::Prefork and Coro.

Users of your framework can choose which backend is the best for their
needs.  You, as a web application framework developer, don't need to
think about lots of different users with different needs.

=head2 Plack

=head3 What is Plack? What is the difference between PSGI and Plack?

PSGI is a specification, so there's no software or module called PSGI.
End users will need to choose one of PSGI server implementations to
run PSGI applications on. Plack is a reference PSGI implementation
that supports environments like prefork standalone server, CGI,
FastCGI, mod_perl, AnyEvent and Coro.

Plack also has useful APIs and helpers on top of PSGI, such as
L<Plack::Request> to provide a nice object-oriented API on request
objects, L<plackup> that allows you to run an PSGI application from
the command line and configure it using C<app.psgi> (a la Rack's
Rackup), and L<Plack::Test> that allows you to test your application
using standard L<HTTP::Request> and L<HTTP::Response> pair through
mocked HTTP or live HTTP servers. See L<Plack> for details.

=head3 What kind of server backends would be available?

In Plack, we already support most web servers like Apache2, and also the
ones that supports standard CGI or FastCGI, but also want to support
special web servers that can embed perl, like nginx. We think it would
be really nice if Apache module mod_perlite and Google AppEngine
supported PSGI too, so that you could run your PSGI/Plack based perl app
in the cloud.

=head3 Ruby is Rack and JavaScript is Jack. Why is it not called Pack?

Well Pack indeed is a cute name, but Perl has a built-in function pack
so it's a little confusing, especially when speaking instead of writing.

=head3 What namespaces should I use to implement PSGI support?

B<Do not use the PSGI:: namespace to implement PSGI backends
or adapters>.

The PSGI namespace is reserved for PSGI specifications and reference
unit tests that implementors have to pass. It should not be used by
particular implementations.

If you write a plugin or an extension to support PSGI for an
(imaginary) web application framework called C<Camper>, name the code
such as C<Camper::Engine::PSGI>.

If you write a web server that supports PSGI interface, then name it
however you want. You can optionally support L<Plack::Server>'s
abstract interface, which is:

  my $server = Plack::Server::FooBar->new(%opt);
  $server->run($app);

By supporting this C<new> and C<run> in your server, it becomes
plackup compatible, so users can run your app via C<plackup>. You're
recommended to, but not required to follow this API, in which case you
have to provide your own PSGI app launcher.

=head3 I have a CGI or mod_perl application that I want to run on PSGI/Plack. What should I do?

You have several choices:

=over 4

=item CGI::PSGI

If you have a web application (or framework) that uses CGI.pm to handle
query parameters, L<CGI::PSGI> can help you migrate to PSGI.  You'll
need to change how you create CGI objects and how to return the response
headers and body, but the rest of your code will work unchanged.

=item CGI::Emulate::PSGI

If you have a dead old CGI script that you want to change as little as
possible (or even no change at all, by running it with C<do>), then
L<CGI::Emulate::PSGI> can wrap it up as a PSGI application.  Compared
to L<CGI::PSGI>, this is less efficient, but should work with any CGI
implementation, not just CGI.pm.

=item Plack::Request and Plack::Response

If you have an L<HTTP::Engine> based application (framework), or want to
write an app from scratch and need a better interface than L<CGI>, or
you're used to L<Apache::Request>, then L<Plack::Request> and
L<Plack::Response> might be what you want. It gives you a nice
Request/Response object API on top of the PSGI env hash and response
array.

=back

NOTE: Don't forget that whenever you have a CGI script that runs once
and exits, and you turn it into a persistent process, it may have
cleanup that needs to happen after every request -- variables that need
to be reset, files that need to be closed or deleted, etc.  PSGI can do
nothing about that (you have to fix it) except give you this friendly
reminder.

=head2 HTTP::Engine

=head3 Why PSGI/Plack instead of HTTP::Engine?

HTTP::Engine was a great experiment, but it mixed the application
interface (the C<request_handler> interface) with implementations, and
the monolithic class hierarchy and role based interfaces make it really
hard to write a new backend. We kept the existing HTTP::Engine and broke
it into three parts: The interface specification (PSGI), Reference
server implementations (Plack::Server) and Standard APIs and Tools
(Plack).

=head3 Will HTTP::Engine be dead?

It won't be dead. HTTP::Engine will stay as it is and still be useful
if you want to write a micro webserver application rather than a
framework.

=head3 Do I have to rewrite my HTTP::Engine application to follow PSGI interface?

No, you don't need to rewrite your existing HTTP::Engine application.
It can be easily turned into a PSGI application using
L<HTTP::Engine::Interface::PSGI>.

Alternatively, you can use L<Plack::Request> and L<Plack::Response>
which gives compatible APIs to L<HTTP::Engine::Request> and
L<HTTP::Engine::Response>:

  use Plack::Request;
  use Plack::Response;

  sub request_handler {
      my $req = Plack::Request->new(shift);
      my $res = Plack::Response->new;
      # ...
      return $res->finalize;
  }

And this C<request_handler> is a PSGI application now.

=head3 What's the benefit of converting my HTTP::Engine app to run on PSGI?

As of today most web server implementations and middlewares
implemented by Plack are mostly available on HTTP::Engine as well, so
there might not be direct immediate benefit of switching to PSGI. But
PSGI is more future proof, and there are high hope that in the near
future we'll have a pretty fast server environments (think of Passenger
for Ruby Rack) and/or plenty of useful middlewares that HTTP::Engine
doesn't have today.

See the question I<My framework already does CGI, FCGI and
mod_perl. Why do I want to support PSGI?> for more details.
  
=head2 API Design

Keep in mind that most design choices made in the PSGI spec are to
minimize the requirements on backends so they can optimize things.
Adding a fancy interface or allowing flexibility in the PSGI layers
might sound catchy to end users, but it would just add things that
backends have to support, which would end up getting in the way of
optimizations, or introducing more bugs. What makes a fancy API to
attract web application developers is your framework, not PSGI.

=head3 Why a big env hash instead of objects with APIs?

The simplicity of the interface is the key that made WSGI and Rack
successful. PSGI is a low-level interface between backends and web
application framework developers. If we define an API on what type of
objects should be passed and which method they need to implement,
there will be so much duplicated code in the backends, some of
which may be buggy.

For instance, PSGI defines C<< $env->{REMOTE_ADDR} >> as a
string. What if the PSGI spec required it to be an instance of Net::IP?
Backend code would have to depend on the Net::IP module, or have to
write a mock object that implements ALL of Net::IP's methods.
Backends depending on specific modules or having to reinvent lots
of stuff is considered harmful and that's why the interface is as minimal
as possible.

Making a nice API for the end users is a job that web application
frameworks (adapter developers) should do, not something PSGI needs to
define.

=head3 Why is the application a code ref rather than an object with a ->call method?

Requiring an object I<in addition to> a code ref would make EVERY
backend's code a few lines more tedious, while requiring an object
I<instead of> a code ref would make application developers write
another class and instanciate an object.

In other words, yes an object with a C<call> method could work, but
again PSGI was designed to be as simple as possible, and making a code
reference out of class/object is no brainer but the other way round
always requires a few lines of code and possibly a new file.

=head3 Why are the headers returned as an array ref and not a hash ref?

Short: In order to support multiple headers (e.g. C<Set-Cookie>).

Long: In Python WSGI, the response header is a list of (C<header_name>,
C<header_value>) I<tuples> i.e. C<type(response_headers) is ListType>
so there can be multiple entries for the same header key. In Rack and
JSGI, a header value is a String consisting of lines separated by
"C<\n>".

We liked Python's specification here, and since Perl hashes don't
allow multiple entries with the same key (unless it's C<tie>d), using
an array reference to store C<< [ key => value, key => value ] >> is
the simplest solution to keep both framework adapters and
backends simple. Other options, like allowing an array ref
in addition to a plain scalar, make either side of the code
unnecessarily tedious.

Note that I'm talking about multiple header lines with the same key,
and NOT about multiple header values (e.g. C<Accept: text/html,
text/plain, *>). Joining the header values with C<, > is obviously
the application's job. L<HTTP::Headers> does exactly that when it's
passed an array reference as a header value, for instance.

The other option is to always require the application to set a value as
an array ref, even if there is only one entry: this would make backend
code less tedious, but, for the exact reason of multiple header values
vs. multiple header lines with the same name mentioned in the paragraph
before, I think it's confusing.

=head3 No iterators support in $body?

We learned that WSGI and Rack really enjoy the benefit of Python and
Ruby's language beauty, which are iterable objects in Python or
iterators in Ruby.

Rack, for instance, expects the body as an object that responds to
the C<each> method and then yields the buffer, so

  body.each { |buf| request.write(buf) }

would just magically work whether body is an Array, FileIO object or an
object that implements iterators. Perl doesn't have such a beautiful
thing in the language unless L<autobox> is loaded.  PSGI should not make
autobox as a requirement, so we only support a simple array ref or file
handle.

Writing an IO::Handle-like object is pretty easy since it's only
C<getline> and C<close>. You can also use PerlIO to write an object that
behaves like a filehandle, though it might be considered a little
unstable.

See also L<IO::Handle::Util> to turn anything iterators-like into
IO::Handle-like.

=head3 How should server determine to switch to sendfile(2) based serving?

First of all, an application SHOULD always set a IO::Handle-like
object (or an array of chunks) that responds to C<getline> and
C<close> as a body. That is guaranteed to work with any servers.

Optionally, if the server is written in perl or can tell a file
descriptor number to the C-land to serve the file, then the server MAY
check if the body is a real filehandle (possibly using
L<Plack::Util>'s C<is_real_fh> function), then get a file descriptor
with C<fileno> and call sendfile(2) or equivalent zero-copy data
transfer using that.

Otherwise, if the server can't send a file using the file descriptor
but needs a local file path (like mod_perl or nginx), the application
can return an IO::Handle-like object that also responds to C<path>
method. This type of IO-like object can easily be created using
L<IO::File::WithPath>, L<IO::Handle::Util> or L<Plack::Util>'s
C<set_io_path> function.

Middlewares can also look to see if the body has C<path> method and
does something interesting with it, like setting C<X-Sendfile>
headers.

To summarize:

=over 4

=item *

When to serve static files, applications should always return a real
filehandle or IO::Handle object. That should work everywhere, and can
be optimized in some environments.

=item *

Applications can also set IO::Handle like object with an additional
C<path> method, then it should work everywhere again, and can be
optimized in even more environments.

=back

=head3 What if I want to stream content or do a long-poll Comet?

The most straightforward way to implement server push is for your
application to return a IO::Handle-like object as a content body that
implements C<getline> to return pushed content. This is guaranteed to
work everywhere, but it's more like I<pull> than I<push>, and it's
hard to do non-blocking I/O unless you use Coro.

If you want to do server push, where your application runs in an event
loop and push content body to the client as it's ready, you should
first check if the server supports the delayed response, by looking at
C<psgi.streaming> env hash, and then return a callback to delay the
response.

  # long-poll comet like a chat application
  my $app = sub {
      my $env = shift;
      unless ($env->{'psgi.streaming'}) {
          die "This application needs psgi.streaming support";
      }
      return sub {
          my $respond = shift;
          wait_for_new_message(sub {
              my $message = shift;
              my $body = [ $message->to_json ];
              $respond->([200, ['Content-Type', 'application/json'], $body]);
          });
      };
  };

C<wait_for_new_message> can be blocking or non-blocking: it's up to
you. Most of the case you want to run it non-blockingly and should use
event loops like L<AnyEvent>. You're suggested to check
C<psgi.nonblocking> value to see that it's possible.

Also, to stream the content body (like streaming messages over the
Flash socket or multipart XMLHTTPRequest):

  my $app = sub {
      my $env = shift;
      unless ($env->{'psgi.streaming'}) {
          die "This application needs psgi.streaming support";
      }
      return sub {
          my $respond = shift;
          my $writer = $respond->([200, ['Content-Type', 'text/plain']]);
          wait_for_new_message(sub {
              my $message = shift;
              if ($message) {
                  $writer->poll_cb(sub {
                      $_[0]->write($message->to_json);
                  });
              } else {
                  $writer->close;
              }
          });
      };
  };

C<poll_cb> pushes the callback to the buffer to write your content
whenever a client is ready to receive the content. You can just use
C<write> instead of C<poll_cb> but that might cause a problem if you
stream a massive streaming content against a slow HTTP client.

=head3 Which framework should I use to do streaming though?

We have servers that support non-blocking (where C<psgi.nonblocking>
is set to true), but the problem is that framework side doesn't
necessary support streaming. For instance Catalyst has C<write> method
on the response object:

  while ($cond) {
      $c->res->write($some_stuff);
  }

But it obviously blocks in the application unless you run your
application in multithread (or Coro) environments.
L<Catalyst::Engine::PSGI> also supports setting an IO::Handle-like
object that supports C<getline>, so using L<IO::Handle::Util>

  my $io = io_from_getline sub {
       return $data; # or undef when done()
  };
  $c->res->body($io);

And that works fine to do streaming, but it's blocking (I<pull>)
rather than server push, so you should be careful not to run this
application on non-blocking (and non-multiprocess) server
environments.

We expect that more web frameworks will appear that is focused on, or
existent frameworks will add support for, asynchronous and
non-blocking streaming interface.

=head3 Why CGI-style environment variables instead of HTTP headers as a hash?

Most existing web application frameworks already have code or a handler
to run under the CGI environment. Using CGI-style hash keys instead of
HTTP headers makes it trivial for the framework developers to implement
an adapter to support PSGI. For instance, L<Catalyst::Engine::PSGI> is
only a few dozens lines different from L<Catalyst::Engine::CGI> and was
written in less than an hour.

=head3 Why is PATH_INFO URI decoded?

To be compatible with CGI spec (RFC 3875) and most web servers'
implementations (like Apache and lighttpd).

I understand it could be inconvenient that you can't distinguish
C<foo%2fbar> from C<foo/bar> in the trailing path, but the CGI spec
clearly says C<PATH_INFO> should be decoded by servers, and that web
servers can deny such requests containing C<%2f> (since such requests
would lose information in PATH_INFO). Leaving those reserved characters
undecoded (partial decoding) would make things worse, since then you
can't tell C<foo%2fbar> from C<foo%252fbar> and could be a security hole
with double encoding or decoding.

For web application frameworks that need more control over the actual
raw URI (such as L<Catalyst>), we made the C<REQUEST_URI> environment
hash key REQUIRED. The servers should set the undecoded (unparsed)
original URI (containing the query string) to this key. Note that
C<REQUEST_URI> is completely raw even if the encoded entities are
URI-safe.

For comparison, WSGI (PEP-333) defines both C<SCRIPT_NAME> and
C<PATH_INFO> be decoded and Rack leaves it implementation dependent,
while I<fixing> most of PATH_INFO left encoded in Ruby web server
implementations.

L<http://www.python.org/dev/peps/pep-0333/#url-reconstruction>
L<http://groups.google.com/group/rack-devel/browse_thread/thread/ddf4622e69bea53f>

=head1 SEE ALSO

WSGI's FAQ clearly answers lots of questions about how some API design
decisions were made, some of which can directly apply to PSGI.

L<http://www.python.org/dev/peps/pep-0333/#questions-and-answers>

=head1 MORE QUESTIONS?

If you have a question that is not answered here, or things you totally
disagree with, come join the IRC channel #plack on irc.perl.org or
mailing list L<http://groups.google.com/group/psgi-plack>. Be sure you
clarify which hat you're wearing: application developers, server
implementors or middleware developers. And don't criticize the spec just
to criticize it: show your exact code that doesn't work or get too messy
because of spec restrictions etc. We'll ignore all nitpicks and bikeshed
discussion.

=head1 AUTHOR

Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>

=head1 COPYRIGHT AND LICENSE

=cut