markstos / CGI--Application

A Perl framework for building reusable web-applications
http://www.cgi-app.org/
22 stars 13 forks source link

Consider allowing a PSGI arrayref as a run mode return value #7

Open markstos opened 11 years ago

markstos commented 11 years ago

Currently there are are two documented value return values for a run mode, a scalar and scalarref.

I think we should consider adding a third: If the return value is an arrayref, it would be expected to be a complete PSGI response that's passed through directly (without adding a response code or headers as we usually would.

Use Case

CGI::Application::Plugin::Stream could be replaced with a native PSGI solution. To stream a file, you could end a run mode like this:

return Plack::App::File->new(file => '/my/private/file.csv');

Like the Stream plugin, this would handle detecting the MIME Type and setting a Content-Type and Last-Modified headers, and converting it to a file handle. The server-side of the PSGI spec takes things from there.

I think if we don't add this, we'll end up needlessly rewriting solutions for cases like this, when we could be taking advantage of a native PSGI solution.

anazawa commented 11 years ago

How can you process the PSGI arrayref in CGI environment? It seems that CGI::Application needs to do what Plack::Handler::CGI does. I believe you are talking about PSGI::Application, not CGI::Application ;)

markstos commented 11 years ago

@anazawa, I only intended the feature to work in CGI::Application. Implementing and documenting it in PSGI::Application would certainly be simpler, as there is only one code path. Still, I think it could of interest in CGI::Application: "When using run_as_psgi()", run modes have a third valid return value, which is to to return the PSGI arrayref directly, which includes in the HTTP response status code, HTTP headers and the full response. This is useful for example for streaming tools, that take care of generating all the headers as well as a callback for the body.