Skip to content

Commit

Permalink
fix: support for X-Forwarded-Host http header (#733)
Browse files Browse the repository at this point in the history
* fix: fix support for overriding hostname by setting X-Forwarded-Host http header

Signed-off-by: Manuel Roth <manuelroth@hotmail.ch>

* fix: improve getUrlObject

Signed-off-by: Manuel Roth <manuelroth@hotmail.ch>

* Add documentation for getUrlObject function

Co-authored-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>

* Simplify getPublicUrl function

Co-authored-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>

* Remove variable

Co-authored-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>

* Use urlObject directly

Co-authored-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>

* fix: small fix

Signed-off-by: Manuel Roth <manuelroth@hotmail.ch>

---------

Signed-off-by: Manuel Roth <manuelroth@hotmail.ch>
Co-authored-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>
  • Loading branch information
manuelroth and vinayakkulkarni authored Feb 1, 2023
1 parent 00a536f commit ea186d3
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ import fs from 'node:fs';
import clone from 'clone';
import glyphCompose from '@mapbox/glyph-pbf-composite';

export const getPublicUrl = (publicUrl, req) =>
publicUrl || `${req.protocol}://${req.headers.host}/`;
/**
* Generate new URL object
* @params {object} req - Express request
* @returns {URL} object
**/
const getUrlObject = (req) => {
const urlObject = new URL(`${req.protocol}://${req.headers.host}/`);
// support overriding hostname by sending X-Forwarded-Host http header
urlObject.hostname = req.hostname;
return urlObject;
};

export const getPublicUrl = (publicUrl, req) => {
if (publicUrl) {
return publicUrl;
}
return getUrlObject(req).toString();
};

export const getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
const urlObject = getUrlObject(req);
if (domains) {
if (domains.constructor === String && domains.length > 0) {
domains = domains.split(',');
}
const host = req.headers.host;
const hostParts = host.split('.');
const hostParts = urlObject.host.split('.');
const relativeSubdomainsUsable =
hostParts.length > 1 &&
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(host);
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(urlObject.host);
const newDomains = [];
for (const domain of domains) {
if (domain.indexOf('*') !== -1) {
Expand All @@ -34,7 +50,7 @@ export const getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
domains = newDomains;
}
if (!domains || domains.length == 0) {
domains = [req.headers.host];
domains = [urlObject.host];
}

const queryParams = [];
Expand Down

0 comments on commit ea186d3

Please sign in to comment.