evdb / HTTP-Async

process multiple HTTP requests in parallel without blocking
http://search.cpan.org/dist/HTTP-Async/lib/HTTP/Async.pm
8 stars 14 forks source link

Content negotiation impossible on some redirects #8

Closed kloevschall closed 10 years ago

kloevschall commented 10 years ago

Hi,

I'm having problems with this wonderful module when setting a special Accept header and getting a 303 redirect from the server.

I cannot get the following request with HTTP::Async:

curl --head -H 'Accept: application/x-research-info-systems' http://dx.doi.org/10.1126/science.169.3946.635                                         

HTTP/1.1 303 See Other
Server: Apache-Coyote/1.1
Vary: Accept
Location: http://data.crossref.org/10.1126%2Fscience.169.3946.635
Expires: Fri, 21 Mar 2014 10:24:17 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 196
Date: Thu, 20 Mar 2014 13:50:37 GMT

Testing it in Perl with HTTP::Async 0.24:

use strict;
use warnings;
use v5.10;

use HTTP::Async;
use HTTP::Request;

my $async = HTTP::Async->new();
my $headers = HTTP::Headers->new( Accept => 'application/x-research-info-systems' );

my $error = $async->add(HTTP::Request->new( GET => 'http://dx.doi.org/10.1126/science.169.3946.635', $headers));
my $ok = $async->add(HTTP::Request->new( GET => 'http://data.crossref.org/10.1126%2Fscience.169.3946.635', $headers));

while ( my ($response, $req_id) = $async->wait_for_next_response ) {
        say "$req_id) " . $response->code . ": " . $response->message . " from: " . $response->base;
        say "Content-Type is: ". $response->header('Content-Type');
}

I would have hoped that both requests would be fine but it is not the case:

2) 200: OK from: http://data.crossref.org/10.1126%2Fscience.169.3946.635
Content-Type is: application/x-research-info-systems
1) 500: Internal Server Error from: http://data.crossref.org/10.1126%2Fscience.169.3946.635
Content-Type is: text/html

It would look as if HTTP::Async does not retain the original header.

If you let curl follow the redirect it works as I would expect:

curl -LH 'Accept: application/x-research-info-systems'  http://dx.doi.org/10.1126/science.169.3946.635                                           

TY  - JOUR
DO  - 10.1126/science.169.3946.635
UR  - http://dx.doi.org/10.1126/science.169.3946.635
TI  - The Structure of Ordinary Water: New data and interpretations are yielding new insights into this fascinating substance
T2  - Science
AU  - Frank, H. S.
PY  - 1970
DA  - 1970/08/14
PB  - American Association for the Advancement of Science (AAAS)
SP  - 635-641
IS  - 3946
VL  - 169
SN  - 0036-8075
SN  - 1095-9203
ER  - 

Thanks, Kasper

kaoru commented 10 years ago

Hi kloevschall,

Thanks for the detailed bug report. I will take a look at this sometime this week.

kaoru commented 10 years ago

Fixed at https://github.com/evdb/HTTP-Async/commit/1d9b902241c046eeca8937a7776c105c1c9823b0

Thanks for the clear and useful example, the actual fix was only two lines :-)

kloevschall commented 10 years ago

This is just wonderful! Thanks a lot for the help.