Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to return PSGI-style streaming response #1003

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ t/12_response/08_drop_content.t
t/12_response/09_headers_to_array.t
t/12_response/10_error_dumper.t
t/12_response/11_CVE-2012-5572.t
t/12_response/12_psgi_streaming.t
t/13_engines/00_load.t
t/13_engines/02_template_init.t
t/14_serializer/01_helpers.t
Expand Down
27 changes: 27 additions & 0 deletions lib/Dancer/Cookbook.pod
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,33 @@ The following routes will be generated for us:



=head2 Low-level access to PSGI interface

You may return a PSGI-formatted response from your Dancer route if needed.
This ability is useful if you are building a streaming app. Note that you will
need a PSGI-compliant server with streaming support.

get '/streaming' => sub {
# use my app infrastructure to make database lookups, session checks etc.
# then return a callback-style response
return sub {
my $respond = shift;
my $writer = $respond->(
[ 200, [ 'Content-Type', 'text/event-stream' ] ]
);
subscribe_user($writer); # set up events in your app
$writer->write(
HTTP::ServerEvent->as_string(
data => 'connected'
)
);
}
};

See L<PSGI> for more details.



=head1 MUSCLE MEMORY: STORING DATA

=head2 Handling sessions
Expand Down
11 changes: 10 additions & 1 deletion lib/Dancer/Handler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,16 @@ sub render_response {

my $content = $response->content;

unless ( ref($content) eq 'GLOB' ) {
if (ref($content) eq 'CODE') {
unless (
Dancer::SharedData->request
&& Dancer::SharedData->request->env->{'psgi.streaming'}
) {
raise core => 'Sorry, streaming is not supported on this server.';
}
return $content;
}
elsif ( ref($content) ne 'GLOB' ) {
my $charset = setting('charset');
my $ctype = $response->header('Content-Type');

Expand Down
2 changes: 1 addition & 1 deletion lib/Dancer/Serializer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sub process_response {

my $content = $response->{content};

if (ref($content) && (ref($content) ne 'GLOB')) {
if (ref($content) && ref($content) ne 'GLOB' && ref($content) ne 'CODE') {
local $@;
eval { $content = engine->serialize($content) };

Expand Down
25 changes: 25 additions & 0 deletions t/12_response/12_psgi_streaming.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main;
use strict;
use warnings;
use Test::More tests => 2, import => ['!pass'];
use Dancer::Test;

{
use Dancer;
set environment => 'psgi.streaming'; # it is required in a real-world app
# but here it makes no difference
get '/psgi' => sub {
sub {
# implementation is irrelevant
}
};
}

note "Testing PSGI-streaming response";
my $req = [ GET => '/psgi' ];
route_exists $req;
my $res = dancer_response @$req;
is(ref($res->content), 'CODE', 'reponse contains coderef');

1;