[Catalyst] chunked response and Catalyst ??

Tomas Doran bobtfish at bobtfish.net
Wed Dec 7 09:39:56 GMT 2011


On 7 Dec 2011, at 07:32, Denis Spichkin wrote:

> chunked response and Catalyst ??

Totally possible.
> Is there any way of generating chunked response inside Catalyst and
> any its Views.
>

The default way that the views / RenderView extension works is to  
render an entire page and give that to the user, as that's simplest.
> I have inside side that create chunked response , created gradually
> output for example such as
>
You can setup your headers, then call ->render on your view to render  
a chunk, and call $c->write to write chunks out in a loop - this will  
work just fine :)

Doing a multipart streaming response to javascript code (using  
DUI.Stream or whatever on the client side) would work something like  
this:

my $boundary = MIME::Base64::encode(join("", map chr(rand(256)),  
1..$size*3), "");
$boundary =~ s/[\W]/X/g;  # ensure alnum only
$c->res->header("Content-Type" => 'multipart/mixed; boundary="' .  
$boundary . '"');
$c->res->body('');
$c->write("--" . $boundary . "\n");

while (my $thing = $iter->next) {
     $c->stash(item => $thing);
     my $payload = $c->view('JSON')->render;
     $c->write("Content-Type: application/json\n\n$payload\n--" .  
$boundary. "\n" );
}

>
> I have inside side that create chunked response , created gradually
> output for example such as
> http://www.websitepulse.com/help/testreq.php?host=www.ya.ru&location=9&type=1&singletestpage=ping-test&pass=&ttref=http%3A%2F%2Fwww.websitepulse.com%2F&__=1323068838832
>


I note that the example you've given here _is not_ doing a chunked  
response. 'Chunked' implies  RFC 2405/2406 MIME chunking, whereas the  
above serves a plain HTML page and just flushes it to the browser in  
parts (much simpler, but both less powerful).

More analogous code would be something like:

for (1..10) {
    $c->write('response part ' . $_);
     sleep 1;
}

> I need create Catalyst Controller that after getting client request
> will generate request to my inside resource and get chunked response
> and while getting chunked response will generate through any Catalyst
> View chunked response to first client request.
>
I'm not quite sure what you're after here, or if what I've suggested  
above solves it - please feel free to ask for more details :)

Cheers
t0m





More information about the Catalyst mailing list