Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Pass through X-Client-IP header to WDQS #1

Merged
merged 1 commit into from
Sep 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions geoshapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -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']);
Expand Down