10
10
// License for the specific language governing permissions and limitations under
11
11
// the License.
12
12
13
- const { HttpsCookieAgent, HttpCookieAgent } = require ( 'http-cookie-agent/http' )
14
13
const { URL } = require ( 'url' )
14
+ const http = require ( 'http' )
15
+ const https = require ( 'https' )
15
16
const assert = require ( 'assert' )
16
17
const querystring = require ( 'qs' )
17
18
const axios = require ( 'axios' )
18
- const { CookieJar } = require ( 'tough-cookie' )
19
- const cookieJar = new CookieJar ( )
20
19
const stream = require ( 'stream' )
21
20
const pkg = require ( '../package.json' )
22
- const AGENT_DEFAULTS = { cookies : { jar : cookieJar } , keepAlive : true , maxSockets : 50 , keepAliveMsecs : 30000 }
21
+ const AGENT_DEFAULTS = { keepAlive : true , maxSockets : 50 , keepAliveMsecs : 30000 }
22
+ const defaultHttpAgent = new http . Agent ( AGENT_DEFAULTS )
23
+ const defaultHttpsAgent = new https . Agent ( AGENT_DEFAULTS )
23
24
const SCRUBBED_STR = 'XXXXXX'
24
- const defaultHttpAgent = new HttpCookieAgent ( AGENT_DEFAULTS )
25
- const defaultHttpsAgent = new HttpsCookieAgent ( AGENT_DEFAULTS )
26
25
const ChangesReader = require ( './changesreader.js' )
26
+ const CookieJar = require ( './cookie.js' )
27
27
const MultiPartFactory = require ( './multipart.js' )
28
28
29
29
function isEmpty ( val ) {
@@ -77,6 +77,9 @@ module.exports = exports = function dbScope (cfg) {
77
77
const log = typeof cfg . log === 'function' ? cfg . log : dummyLogger
78
78
const parseUrl = 'parseUrl' in cfg ? cfg . parseUrl : true
79
79
80
+ // create cookieJar for this Nano
81
+ cfg . cookieJar = new CookieJar ( )
82
+
80
83
function maybeExtractDatabaseComponent ( ) {
81
84
if ( ! parseUrl ) {
82
85
return
@@ -123,6 +126,16 @@ module.exports = exports = function dbScope (cfg) {
123
126
let body = response . data
124
127
response . statusCode = statusCode
125
128
129
+ // cookie parsing
130
+ if ( response . headers ) {
131
+ const h = response . headers [ 'set-cookie' ]
132
+ if ( h && h . length ) {
133
+ h . forEach ( ( header ) => {
134
+ cfg . cookieJar . parse ( header , req . url )
135
+ } )
136
+ }
137
+ }
138
+
126
139
// let parsed
127
140
const responseHeaders = Object . assign ( {
128
141
uri : scrubURL ( req . url ) ,
@@ -282,7 +295,6 @@ module.exports = exports = function dbScope (cfg) {
282
295
}
283
296
284
297
if ( isJar ) {
285
- req . jar = cookieJar
286
298
req . withCredentials = true
287
299
}
288
300
@@ -350,6 +362,12 @@ module.exports = exports = function dbScope (cfg) {
350
362
req . qs = qs
351
363
}
352
364
365
+ // add any cookies for this domain
366
+ const cookie = cfg . cookieJar . getCookieString ( req . uri )
367
+ if ( cookie ) {
368
+ req . headers . cookie = cookie
369
+ }
370
+
353
371
if ( opts . body ) {
354
372
if ( Buffer . isBuffer ( opts . body ) || opts . dontStringify ) {
355
373
req . body = opts . body
@@ -375,8 +393,6 @@ module.exports = exports = function dbScope (cfg) {
375
393
// ?drilldown=["author","Dickens"]&drilldown=["publisher","Penguin"]
376
394
req . qsStringifyOptions = { arrayFormat : 'repeat' }
377
395
378
- cfg . cookies = cookieJar . getCookiesSync ( cfg . url )
379
-
380
396
// This where the HTTP request is made.
381
397
// Nano used to use the now-deprecated "request" library but now we're going to
382
398
// use axios, so let's modify the "req" object to suit axios
@@ -409,8 +425,6 @@ module.exports = exports = function dbScope (cfg) {
409
425
// add http agents
410
426
req . httpAgent = cfg . requestDefaults . agent || defaultHttpAgent
411
427
req . httpsAgent = cfg . requestDefaults . agent || defaultHttpsAgent
412
- req . httpAgent . jar = req . httpAgent . jar ? req . httpAgent . jar : cookieJar
413
- req . httpsAgent . jar = req . httpsAgent . jar ? req . httpsAgent . jar : cookieJar
414
428
const ax = axios . create ( {
415
429
httpAgent : req . httpAgent ,
416
430
httpsAgent : req . httpsAgent
0 commit comments