Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect duplicated header in HTTP::Response

I have a problem with a HTTP::Response Perl object from a remote server that sometimes returns the HTTP response with duplicated 'Content-Length' headers.

When this occurs, if the content-length value is '43215', when I read the header value with:

print ($response->header('Content-length'));

the result is:

4321543215

How can I detect if the header is duplicated and access to the real value?

like image 263
ajgarlag Avatar asked Dec 16 '22 19:12

ajgarlag


1 Answers

From the fine manual for HTTP::Headers:

A multi-valued field will be returned as separate values in list context and will be concatenated with "," as separator in scalar context.

and this is list context:

print ($response->header('Content-length'))

So, $response->header() is returning both Content-length headers as a list and the result is, essentially:

print join('', 43215, 43215)

You can either use kork's $response->content_length() approach or grab all the Content-length headers in an array and use the first one as the length:

my @lengths = $response->header('Content-length');
my $length  = $lengths[0];

If you end up getting multiple Content-length headers and they're different then someone is very confused.

like image 104
mu is too short Avatar answered Jan 06 '23 02:01

mu is too short