node-js-libs / node.io

MIT License
1.8k stars 140 forks source link

302 redirect with cookie set doesn't work #140

Closed zazabe closed 9 years ago

zazabe commented 11 years ago

Hi,

I get some troubles when I try to scrape a website doing a 302 redirection and setting a cookie. It's seems that, because request options.jar is hardcoded to false in request.js, the cookie is not correctly set after the redirection... and the content of my page is not what i expected... I did some tests by removing this hardcoded jar: false option, everything work nice after.

A small test showing the problem:

var nodeio = require('node.io'),
    http = require('http'),
    assert = require('assert'),
    JobClass = nodeio.JobClass;

var port = 24510, timeout = 2000;

function close (server) {
    try {
        server.close();
    } catch (e) {}
}

var server = http.createServer(function (req, res) {
    if(!req.headers.cookie){
        res.writeHead(302, {'Content-Type': 'text/plain', 'Location': '/', 'Set-Cookie': 'foo=bar'});
        res.end('redirect with cookie...');
        console.log('redirect');
    }
    else {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Final Content');
    }
});

server.listen(++port);

job = new JobClass({
    redirects: 3
});

job.post('http://127.0.0.1:'+port+'/', {foo:'bar'}, function(err, data, headers) {
    assert.equal('text/plain', headers['content-type']);
    assert.equal('Final Content', data);
    close(server);
});

In this case, the redirection never end because the cookie is not set by the redirect.

Not sure it's a bug but I have no other solution than removing the jar option in your code, there's some side effect by doing this ?

PS: beside this, for my POST request, I have to explicity set the header property Content-Type: application/x-www-form-urlencoded, should it not be done automatically ?

Thanks, hope my questions make sense...

zazabe commented 11 years ago

I had other problem by removing the jar option within request.js file, responses from multiple requests were randomly permutated.

To solve this issue, in a 302 with cookie redirect, I have to set jar = request.jar():

request.js in node.io, ~290:

...
    //Set a request timeout?
    if (this.options.timeout) {
        self.cancel_timeout();
        options.timeout = this.options.timeout * 1000;
    }

    // change jar from "false" to a new jar instance
    options.jar = request.jar();

    request(options, function (err, response, body) {
...

Hope it can help somebody or it can be fixed in node.io.

If I produce any side effect by doing this, please, tell me !

Thanks