From 9f8471ce57c825480e0698547383c9d590ee7a61 Mon Sep 17 00:00:00 2001 From: Michael Holloway Date: Fri, 21 Sep 2018 18:58:02 +0500 Subject: [PATCH] Pass through X-Client-IP header to WDQS This will allow for throttling in WDQS in the event of a particular client making excessive resource demands. Bug: https://phabricator.wikimedia.org/T200594 --- geoshapes.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/geoshapes.js b/geoshapes.js index 1c59769..03f7c8e 100644 --- a/geoshapes.js +++ b/geoshapes.js @@ -159,7 +159,11 @@ function handler(type, req, res, next) { return Promise.try( () => { geoshape = new GeoShapes(type, req.query); - return geoshape.execute(); + const lowerHeaders = Object.keys(req.headers).reduce((newHeaders, key) => { + newHeaders[key.toLowerCase()] = headers[key]; + return newHeaders; + }, {}); + return geoshape.execute(lowerHeaders['x-client-ip']); } ).then(geodata => { core.setResponseHeaders(res); @@ -208,10 +212,10 @@ function GeoShapes(type, reqParams) { * Main execution method * @return {Promise} */ -GeoShapes.prototype.execute = function execute () { +GeoShapes.prototype.execute = function execute (xClientIp) { let self = this; return Promise.try( - () => self.runWikidataQuery() + () => self.runWikidataQuery(xClientIp) ).then( () => Promise.all([self.runSqlQuery(), self.expandProperties()]) ).then( @@ -223,7 +227,7 @@ GeoShapes.prototype.execute = function execute () { * * @return {Promise|undefined} */ -GeoShapes.prototype.runWikidataQuery = function runWikidataQuery () { +GeoShapes.prototype.runWikidataQuery = function runWikidataQuery (xClientIp) { let self = this; // If there is no query, we only use the ids given in the request if (!self.sparqlQuery) return; @@ -234,7 +238,7 @@ GeoShapes.prototype.runWikidataQuery = function runWikidataQuery () { format: 'json', query: self.sparqlQuery }, - headers: config.sparqlHeaders + headers: Object.assign(config.sparqlHeaders, { 'X-Client-IP': xClientIp }); }).then(queryResult => { if (queryResult.headers['content-type'] !== 'application/sparql-results+json') { throw new Err('Unexpected content type %s', queryResult.headers['content-type']);