From 63cc689db314580ca809f7e4021facc991527a78 Mon Sep 17 00:00:00 2001 From: Chad Scira Date: Sat, 3 Nov 2012 16:03:16 -0700 Subject: [PATCH] added basic cookiejar support closes #2 --- Readme.md | 1 + lib/requester.js | 12 +++++++++--- package.json | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 5dd37fa..3c518f5 100644 --- a/Readme.md +++ b/Readme.md @@ -27,6 +27,7 @@ requester.get(/* URL */, /* REQUEST_OBJECT */, function (body) { var Requester = ('requester'); var requester = new Requester({ + cookiejar: true, // basic cookie support, currently doesnt care about domain or path rules cookies: {}, headers: {}, timeout: 4000, diff --git a/lib/requester.js b/lib/requester.js index e440295..902870e 100644 --- a/lib/requester.js +++ b/lib/requester.js @@ -21,6 +21,7 @@ function RequesterHandler ($config) { this._proxies = $config.proxies; this._retries = $config.retries || 0; this._encoding = $config.encoding || 'utf8'; + this._cookiejar = $config.cookiejar ? {} : false; this._follow = _.isUndefined($config.follow) ? true : $config.follow; this._followMax = _.isUndefined($config.followMax) ? 5 : $config.followMax; this._requestID = 0; @@ -29,6 +30,8 @@ function RequesterHandler ($config) { this._processResponse = $config.processResponse; this._dataType = $config.dataType; this._auth = $config.auth; + + if (this._cookiejar) this._cookiejar = _.extend(this._cookiejar, this._cookies); } RequesterHandler.prototype = { @@ -69,6 +72,7 @@ RequesterHandler.prototype = { $options.processResponse = $options.processResponse || this._processResponse; $options.dataType = $options.dataType || this._dataType; + if (this._cookiejar) $options.cookiejar = _.extend(this._cookiejar, $options.cookies); $options.follow = $options.follow || this._follow; $options.followMax = $options.followMax || this._followMax; $options.auth = $options.auth || this._auth; @@ -99,6 +103,7 @@ function Requester ($url, $options, callback) { this._processResponse = $options.processResponse; this._dataType = $options.dataType; + this._cookiejar = $options.cookiejar; this._follow = $options.follow; this._followMax = $options.followMax; this._proxy = $options.proxy; @@ -117,7 +122,7 @@ Requester.prototype = { _prepareHttpOptions: function () { var self = this; - if (!this._headers.cookie && !this._headers.Cookie && this._cookies) this._headers.cookie = this._stringifyCookies(this._cookies); + if (!this._headers.cookie && !this._headers.Cookie && this._cookies) this._headers.cookie = this._stringifyCookies(this._cookiejar || this._cookies); if (this._auth) this._headers[this._proxy ? 'Proxy-Authorization' : 'Authorization'] = 'Basic ' + new Buffer(this._auth.username + ':' + this._auth.password).toString('base64'); if (this._multipart) { @@ -182,7 +187,7 @@ Requester.prototype = { _prepareRequestObject: function () { var self = this; - + this._requestObject = this._protocol.request( { host: this._proxy ? this._proxy.ip : this._url.hostname, @@ -277,6 +282,7 @@ Requester.prototype = { response.cookies = _.extend(response.cookies, this._parseCookies(value)); }, this); } + if (this._cookiejar) this._cookiejar = _.extend(this._cookiejar, response.cookies); if (this._follow && this._followCount <= this._followMax && response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) { this._url = url.parse(response.headers.location); @@ -330,7 +336,7 @@ Requester.prototype = { var cookies = {}; string.split('; ').forEach(function (value) { var parts = value.split('='); - if (!parts[0].match(/path|expires|secure|HttpOnly/)) cookies[parts[0]] = parts[1] || ''; + if (!parts[0].match(/domain|path|expires|secure|httponly/i)) cookies[parts[0]] = parts[1] || ''; }); return cookies; diff --git a/package.json b/package.json index 6936640..052273f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "requester", - "version": "0.1.4", + "version": "0.1.5", "description": "swiss army knife for requests", "main": "index.js", "directories": {