Duplicate Header Detection in HTTP :: Response

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

When this happens if the content length value is "43215" when I read the header value with:

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

result:

 4321543215 

How to determine if the header and access to the real value are duplicated?

+4
source share
2 answers

From the thin guide for HTTP::Headers :

A multi-valued field will be returned as separate values ​​in the context of the list and will be combined with "," as a separator in a scalar context.

and this is the list context:

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

So $response->header() returns both the Content-length headers to the list, and the result, essentially:

 print join('', 43215, 43215) 

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

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

If you get multiple Content-length headers, and they are different, then someone is very confused.

+11
source

You cannot detect it, at least not reliably. Of course, you can split the header value in the middle and try to find out if the left value matches the rule, but when you get sizes like 4444, you don't know if it was duplicated or not. The only chance to fix this is to commit it to an upstream server that sends you duplicate headers.

You might be trying to access the length of the content using the content_length property:

 $response->content_length 

This may be known about duplicate headers, but I have not tried.

+1
source

All Articles