Skip to content

Commit

Permalink
Extend U.request() + RESTBuilder by adding persisten cookies.
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed Jan 2, 2018
1 parent f5ac0c0 commit 8c31032
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
14 changes: 12 additions & 2 deletions builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -3659,6 +3659,7 @@ function RESTBuilder(url) {
this.$length = 0;
this.$transform = transforms['restbuilder_default'];
this.$files = null;
this.$persistentcookies = false;

// this.$flags;
// this.$data = {};
Expand Down Expand Up @@ -3894,9 +3895,18 @@ RESTBuilder.prototype.raw = function(value) {
return this;
};

RESTBuilder.prototype.cook = function(value) {
this.$persistentcookies = value !== false;
return this;
};

RESTBuilder.prototype.cookies = function(obj) {
this.$cookies = obj;
return this;
};

RESTBuilder.prototype.cookie = function(name, value) {
if (!this.$cookies)
this.$cookies = {};
!this.$cookies && (this.$cookies = {});
this.$cookies[name] = value;
return this;
};
Expand Down
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
- added: `LOGMAIL()` global alias for `F.logmail()`
- added: `MAIL()` global alias for `F.mail()`
- added: own and improved implementation of `onFinished`
- added: `RESTBuilder.cookies(obj)` can set cookies as raw object
- added: `RESTBuilder.cook([true/false])` enables persistent cookies

- updated: `F.load()`, now supports `string` for `debug` or `release` mode
- updated: `F.cluster.request()` can be executed from master process
- updated: `Image.miniature()` change a default filter from `Box` to `Hamming`
- updated: `U.request()` supports a new flag `cookies` which enables a parsing cookies from response

- fixed: schema validation (problem with Arrays)
- fixed: determines `x-forwarded-proto`
Expand Down
32 changes: 22 additions & 10 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ var CONTENTTYPES = {
'zip': 'application/zip'
};

var persistentcookies = {};
var dnscache = {};
var datetimeformat = {};
const hasOwnProperty = Object.prototype.hasOwnProperty;
Expand Down Expand Up @@ -435,6 +436,7 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
var options = { length: 0, timeout: timeout || 10000, evt: new EventEmitter2(), encoding: typeof(encoding) !== 'string' ? ENCODING : encoding, callback: callback, post: false, redirect: 0 };
var method;
var type = 0;
var isCookies = false;

if (headers)
headers = exports.extend({}, headers);
Expand Down Expand Up @@ -518,7 +520,7 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
break;

case 'cookies':
options.cookies = cookies;
isCookies = true;
break;
}
}
Expand Down Expand Up @@ -548,6 +550,8 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
options.data = data;

if (cookies) {
if (isCookies)
options.cookies = cookies;
var builder = '';
for (var m in cookies)
builder += (builder ? '; ' : '') + m + '=' + cookies[m];
Expand All @@ -560,8 +564,6 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
uri.agent = false;
uri.headers = headers;

if (options.cookies)

if (options.resolve) {
exports.resolve(url, function(err, u) {
!err && (uri.host = u.host);
Expand Down Expand Up @@ -726,12 +728,23 @@ function request_response(res, uri, options) {

// Shared cookies
if (options.cookies) {
var arr = (res.headers['cookie'] || '').split(';');
for (var i = 0, length = arr.length; i < length; i++) {
var line = arr[i].trim();
var index = line.indexOf('=');
if (index !== -1)
options.cookies[line.substring(0, index)] = decodeURIComponent(line.substring(index + 1));
var arr = (res.headers['set-cookie'] || '');

// Only the one value
if (arr && !(arr instanceof Array))
arr = [arr];

if (arr instanceof Array) {
for (var i = 0, length = arr.length; i < length; i++) {
var line = arr[i];
var end = line.indexOf(';');
if (end === -1)
end = line.length;
line = line.substring(0, end);
var index = line.indexOf('=');
if (index !== -1)
options.cookies[line.substring(0, index)] = decodeURIComponent(line.substring(index + 1));
}
}
}

Expand Down Expand Up @@ -907,7 +920,6 @@ exports.download = function(url, flags, data, callback, cookies, headers, encodi
case 'dnscache':
options.resolve = true;
break;

}
}
}
Expand Down

0 comments on commit 8c31032

Please sign in to comment.