Question

I'm using Transfer-Encoding: chunked to write an HTTP response.

The response is split into pieces via the following:

my $template = "a$buffer_size" x int(length($response)/$buffer_size) . 'a*';

foreach my $buffer (unpack $template, $response){
    ...
}

This works fine when the content type is text/html, but it is corrupting binary data, such as application/pdf.

Can unpack be used to split binary data into equal lengths?

Was it helpful?

Solution

Still not sure why unpack is failing in this context, but I stumbled upon a solution.

If I manipulate the response with an in-memory file, unpack works correctly:

my $resp;
open (my $fh, '>', \$resp);
my $fh_old = select($fh);
print $response;
close $fh;
select($fh_old);
$response = $resp;

Any insight into why this works?

OTHER TIPS

That works perfectly fine with binary data. The problem is elsewhere. (Did you binmode all relevant handles?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top