From f5ac0c084c37515680fcdd3a045435044da921ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20S=CC=8Cirka?= Date: Tue, 2 Jan 2018 15:13:30 +0100 Subject: [PATCH] Add cookies (uncomplete). --- utils.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/utils.js b/utils.js index f805c0447..0a60a518f 100755 --- a/utils.js +++ b/utils.js @@ -512,9 +512,14 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin method = flags[i].toUpperCase(); !headers['Content-Type'] && (headers['Content-Type'] = 'application/x-www-form-urlencoded'); break; + case 'dnscache': options.resolve = true; break; + + case 'cookies': + options.cookies = cookies; + break; } } } @@ -555,6 +560,8 @@ 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); @@ -652,7 +659,7 @@ function request_response(res, uri, options) { if (options.noredirect) { if (options.callback) { - options.callback(null, '', res.statusCode, res.headers, uri.host); + options.callback(null, '', res.statusCode, res.headers, uri.host, EMPTYOBJECT); options.callback = null; } @@ -671,7 +678,7 @@ function request_response(res, uri, options) { if (options.redirect > 3) { if (options.callback) { - options.callback(new Error('Too many redirects.'), '', 0, undefined, uri.host); + options.callback(new Error('Too many redirects.'), '', 0, undefined, uri.host, EMPTYOBJECT); options.callback = null; } @@ -717,6 +724,17 @@ function request_response(res, uri, options) { options.length = +res.headers['content-length'] || 0; options.evt && options.evt.$events.begin && options.evt.emit('begin', options.length); + // 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)); + } + } + res.on('data', function(chunk) { var self = this; if (options.max && self._bufferlength > options.max) @@ -737,13 +755,13 @@ function request_response(res, uri, options) { self._buffer = undefined; if (options.evt) { - options.evt.$events.end && options.evt.emit('end', str, self.statusCode, self.headers, uri.host); + options.evt.$events.end && options.evt.emit('end', str, self.statusCode, self.headers, uri.host, options.cookies); options.evt.removeAllListeners(); options.evt = null; } if (options.callback) { - options.callback(null, uri.method === 'HEAD' ? self.headers : str, self.statusCode, self.headers, uri.host); + options.callback(null, uri.method === 'HEAD' ? self.headers : str, self.statusCode, self.headers, uri.host, options.cookies); options.callback = null; }