Cscott has uploaded a new change for review. https://gerrit.wikimedia.org/r/212577
Change subject: Refactor request default options into a common helper function. ...................................................................... Refactor request default options into a common helper function. Change-Id: Ia86b601fa433e6507b6330e740a6296c4424894d --- M lib/mediawiki.ApiRequest.js 1 file changed, 32 insertions(+), 100 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid refs/changes/77/212577/1 diff --git a/lib/mediawiki.ApiRequest.js b/lib/mediawiki.ApiRequest.js index c353b6c..7298a22 100644 --- a/lib/mediawiki.ApiRequest.js +++ b/lib/mediawiki.ApiRequest.js @@ -48,6 +48,25 @@ } }; +var applyDefaultOptions = function(env, options) { + var proxy = env.conf.wiki.apiProxy; + options.headers = options.headers || {}; + options.headers['User-Agent'] = USER_AGENT; + options.headers.Connection = 'close'; + options.strictSSL = env.conf.parsoid.strictSSL; + if (proxy) { + options.proxy = proxy.uri; + options.proxy_strip_https = proxy.strip_https; + if (proxy.headers) { + Object.assign(options.headers, proxy.headers); + } + } + if ( env.cookie ) { + // Forward the cookie if set + this.requestOptions.headers.Cookie = env.cookie; + } +}; + // Helper to return a promise returning function for the result of an // (Ctor-type) ApiRequest. var promiseFor = function(Ctor) { @@ -295,31 +314,15 @@ } var uri = env.conf.wiki.apiURI; - var proxy = env.conf.wiki.apiProxy; - this.requestOptions = { + this.requestOptions = applyDefaultOptions(env, { method: 'GET', followRedirect: true, uri: uri, qs: apiargs, timeout: MW_API_SRC_FETCH_TIMEOUT, - proxy: proxy && proxy.uri, - proxy_strip_https: proxy && proxy.strip_https, - strictSSL: env.conf.parsoid.strictSSL, - headers: { - 'User-Agent': USER_AGENT, - 'Connection': 'close' - } - }; + }); - if ( proxy && proxy.headers ) { - Object.assign( this.requestOptions.headers, proxy.headers ); - } - - if ( env.cookie ) { - // Forward the cookie if set - this.requestOptions.headers.Cookie = env.cookie; - } // Start the request this.request( this.requestOptions, this._requestCB.bind(this) ); @@ -456,9 +459,8 @@ } var uri = env.conf.wiki.apiURI; - var proxy = env.conf.wiki.apiProxy; - this.requestOptions = { + this.requestOptions = applyDefaultOptions(env, { // Use POST since we are passing a bit of source, and GET has a very // limited length. You'll be greeted by "HTTP Error 414 Request URI // too long" otherwise ;) @@ -467,23 +469,7 @@ followRedirect: true, uri: uri, timeout: MW_API_PREPROCESSOR_TIMEOUT, - proxy: proxy && proxy.uri, - proxy_strip_https: proxy && proxy.strip_https, - strictSSL: env.conf.parsoid.strictSSL, - headers: { - 'User-Agent': USER_AGENT, - 'Connection': 'close' - } - }; - - if ( proxy && proxy.headers ) { - Object.assign( this.requestOptions.headers, proxy.headers ); - } - - if ( env.cookie ) { - // Forward the cookie if set - this.requestOptions.headers.Cookie = env.cookie; - } + }); // Start the request this.request( this.requestOptions, this._requestCB.bind(this) ); @@ -582,7 +568,7 @@ apiargs.title = title; } - this.requestOptions = { + this.requestOptions = applyDefaultOptions(env, { // Use POST since we are passing a bit of source, and GET has a very // limited length. You'll be greeted by "HTTP Error 414 Request URI // too long" otherwise ;) @@ -591,23 +577,7 @@ followRedirect: true, uri: uri, timeout: MW_API_EXTPARSE_TIMEOUT, - proxy: proxy && proxy.uri, - proxy_strip_https: proxy && proxy.strip_https, - strictSSL: env.conf.parsoid.strictSSL, - headers: { - 'User-Agent': USER_AGENT, - 'Connection': 'close' - } - }; - - if ( proxy && proxy.headers ) { - Object.assign( this.requestOptions.headers, proxy.headers ); - } - - if ( env.cookie ) { - // Forward the cookie if set - this.requestOptions.headers.Cookie = env.cookie; - } + }); // Start the request this.request( this.requestOptions, this._requestCB.bind(this) ); @@ -720,24 +690,17 @@ env.conf.wiki.iwp + '/' + encodeURIComponent(title.replace(/ /g, '_')); this.retries = PARSOID_CACHE_MAX_RETRIES; - this.requestOptions = { + this.requestOptions = applyDefaultOptions(env, { // Use GET so that our request is cacheable method: 'GET', followRedirect: false, uri: uri, qs: apiargs, - strictSSL: env.conf.parsoid.strictSSL, headers: { - 'User-Agent': USER_AGENT, - 'Connection': 'close', 'x-parsoid-request': 'cache' } - }; - - if (env.cookie) { - // Forward the cookie if set - this.requestOptions.headers.Cookie = env.cookie; - } + }); + // XXX this doesn't use proxy settings. Is this intentional? if (options.evenIfNotCached) { this.requestOptions.timeout = PARSOID_CACHE_REQ_TIMEOUT + PARSOID_REPARSE_TIMEOUT; @@ -810,29 +773,13 @@ rawcontinue: 1, }; - this.requestOptions = { + this.requestOptions = applyDefaultOptions(env, { method: 'GET', followRedirect: true, uri: uri, qs: apiargs, timeout: MW_API_CONFIG_INFO_TIMEOUT, - proxy: proxy && proxy.uri, - proxy_strip_https: proxy && proxy.strip_https, - strictSSL: env.conf.parsoid.strictSSL, - headers: { - 'User-Agent': USER_AGENT, - 'Connection': 'close', - }, - }; - - if (proxy && proxy.headers) { - Object.assign(this.requestOptions.headers, proxy.headers); - } - - if (env.cookie) { - // Forward the cookie if set - this.requestOptions.headers.Cookie = env.cookie; - } + }); this.request(this.requestOptions, this._requestCB.bind(this)); }; @@ -919,28 +866,13 @@ var proxy = env.conf.wiki.apiProxy; - this.requestOptions = { + this.requestOptions = applyDefaultOptions(env, { method: 'GET', followRedirect: true, uri: uri, qs: apiArgs, timeout: MW_API_IMG_INFO_TIMEOUT, - proxy: proxy && proxy.uri, - proxy_strip_https: proxy && proxy.strip_https, - headers: { - 'User-Agent': USER_AGENT, - 'Connection': 'close' - } - }; - - if ( proxy && proxy.headers ) { - Object.assign( this.requestOptions.headers, proxy.headers ); - } - - if ( env.cookie ) { - // Forward the cookie if set - this.requestOptions.headers.Cookie = env.cookie; - } + }); this.request( this.requestOptions, this._requestCB.bind( this ) ); } -- To view, visit https://gerrit.wikimedia.org/r/212577 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia86b601fa433e6507b6330e740a6296c4424894d Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/parsoid Gerrit-Branch: master Gerrit-Owner: Cscott <canan...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits