Reputation: 175
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?
Upvotes: 2
Views: 1004
Reputation: 175
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?
Upvotes: 1
Reputation: 385546
That works perfectly fine with binary data. The problem is elsewhere. (Did you binmode
all relevant handles?)
Upvotes: 0