Skip to content

Commit

Permalink
added basic cookiejar support closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
icodeforlove committed Nov 3, 2012
1 parent 8af7c61 commit 63cc689
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions Readme.md
Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions lib/requester.js
Expand Up @@ -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;
Expand All @@ -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 = {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion 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": {
Expand Down

0 comments on commit 63cc689

Please sign in to comment.