joepie91 / node-bhttp

A sane HTTP client library for Node.js with Streams2 support.
62 stars 12 forks source link

Accept invalid cookies #6

Open pcfreak30 opened 9 years ago

pcfreak30 commented 9 years ago

Please add an acceptInvalidCookies option. An example of a site that is requiring this is http://www.academicjournals.org.

Thanks

joepie91 commented 9 years ago

A temporary workaround for allowing this globally, is to monkeypatch the setCookie method in tough-cookie on an instantiated cookie jar. Something like this:

var jar = new ToughCookie();
var _setCookie = jar.setCookie;

jar.setCookie = function monkeypatchedSetCookie(cookieOrString, currentUrl, options, cb) {
    options.ignoreError = (options.ignoreError != null) ? options.ignoreError : true;
    _setCookie.call(jar, cookieOrString, currentUrl, options, cb);
}

(Note that this might fail if you omit the options argument and only specify a callback. I have not looked into how tough-cookie parses its arguments.)

I have not tested this workaround yet, though, and I'll need to think for a while about the most sensible way to integrate this option into bhttp. Let me know whether it works as intended.

pcfreak30 commented 9 years ago
        jar = new toughCookie.CookieJar(null, false);
        _setCookie = jar.setCookie
        jar.setCookie = (cookieOrString, currentUrl, options, cb) ->
            options.ignoreError = if options.ignoreError != null then options.ignoreError else true
            _setCookie.call(jar, cookieOrString, currentUrl, options, cb)

        @client = bhttp.session(
            agent: new (if @SSL then https else http).Agent(maxSockets: maxRequests)
            cookieJar: jar
        )

Still seems to crash