1010// License for the specific language governing permissions and limitations under
1111// the License.
1212
13- const { HttpsCookieAgent, HttpCookieAgent } = require ( 'http-cookie-agent/http' )
1413const { URL } = require ( 'url' )
14+ const http = require ( 'http' )
15+ const https = require ( 'https' )
1516const assert = require ( 'assert' )
1617const querystring = require ( 'qs' )
1718const axios = require ( 'axios' )
18- const { CookieJar } = require ( 'tough-cookie' )
19- const cookieJar = new CookieJar ( )
2019const stream = require ( 'stream' )
2120const 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 )
2324const SCRUBBED_STR = 'XXXXXX'
24- const defaultHttpAgent = new HttpCookieAgent ( AGENT_DEFAULTS )
25- const defaultHttpsAgent = new HttpsCookieAgent ( AGENT_DEFAULTS )
2625const ChangesReader = require ( './changesreader.js' )
26+ const CookieJar = require ( './cookie.js' )
2727const MultiPartFactory = require ( './multipart.js' )
2828
2929function isEmpty ( val ) {
@@ -77,6 +77,9 @@ module.exports = exports = function dbScope (cfg) {
7777 const log = typeof cfg . log === 'function' ? cfg . log : dummyLogger
7878 const parseUrl = 'parseUrl' in cfg ? cfg . parseUrl : true
7979
80+ // create cookieJar for this Nano
81+ cfg . cookieJar = new CookieJar ( )
82+
8083 function maybeExtractDatabaseComponent ( ) {
8184 if ( ! parseUrl ) {
8285 return
@@ -123,6 +126,16 @@ module.exports = exports = function dbScope (cfg) {
123126 let body = response . data
124127 response . statusCode = statusCode
125128
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+
126139 // let parsed
127140 const responseHeaders = Object . assign ( {
128141 uri : scrubURL ( req . url ) ,
@@ -282,7 +295,6 @@ module.exports = exports = function dbScope (cfg) {
282295 }
283296
284297 if ( isJar ) {
285- req . jar = cookieJar
286298 req . withCredentials = true
287299 }
288300
@@ -350,6 +362,12 @@ module.exports = exports = function dbScope (cfg) {
350362 req . qs = qs
351363 }
352364
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+
353371 if ( opts . body ) {
354372 if ( Buffer . isBuffer ( opts . body ) || opts . dontStringify ) {
355373 req . body = opts . body
@@ -375,8 +393,6 @@ module.exports = exports = function dbScope (cfg) {
375393 // ?drilldown=["author","Dickens"]&drilldown=["publisher","Penguin"]
376394 req . qsStringifyOptions = { arrayFormat : 'repeat' }
377395
378- cfg . cookies = cookieJar . getCookiesSync ( cfg . url )
379-
380396 // This where the HTTP request is made.
381397 // Nano used to use the now-deprecated "request" library but now we're going to
382398 // use axios, so let's modify the "req" object to suit axios
@@ -409,8 +425,6 @@ module.exports = exports = function dbScope (cfg) {
409425 // add http agents
410426 req . httpAgent = cfg . requestDefaults . agent || defaultHttpAgent
411427 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
414428 const ax = axios . create ( {
415429 httpAgent : req . httpAgent ,
416430 httpsAgent : req . httpsAgent
0 commit comments