Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added basic cookiejar support closes #2

  • Loading branch information...
commit 63cc689db314580ca809f7e4021facc991527a78 1 parent 8af7c61
@icodeforlove authored
Showing with 11 additions and 4 deletions.
  1. +1 −0  Readme.md
  2. +9 −3 lib/requester.js
  3. +1 −1  package.json
View
1  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,
View
12 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;
View
2  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": {
Please sign in to comment.
Something went wrong with that request. Please try again.