fix: support for X-Forwarded-Host http header (#733)
* 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>
This commit is contained in:
parent
00a536f9fb
commit
ea186d34df
1 changed files with 22 additions and 6 deletions
28
src/utils.js
28
src/utils.js
|
|
@ -6,19 +6,35 @@ import fs from 'node:fs';
|
||||||
import clone from 'clone';
|
import clone from 'clone';
|
||||||
import glyphCompose from '@mapbox/glyph-pbf-composite';
|
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) => {
|
export const getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
|
||||||
|
const urlObject = getUrlObject(req);
|
||||||
if (domains) {
|
if (domains) {
|
||||||
if (domains.constructor === String && domains.length > 0) {
|
if (domains.constructor === String && domains.length > 0) {
|
||||||
domains = domains.split(',');
|
domains = domains.split(',');
|
||||||
}
|
}
|
||||||
const host = req.headers.host;
|
const hostParts = urlObject.host.split('.');
|
||||||
const hostParts = host.split('.');
|
|
||||||
const relativeSubdomainsUsable =
|
const relativeSubdomainsUsable =
|
||||||
hostParts.length > 1 &&
|
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 = [];
|
const newDomains = [];
|
||||||
for (const domain of domains) {
|
for (const domain of domains) {
|
||||||
if (domain.indexOf('*') !== -1) {
|
if (domain.indexOf('*') !== -1) {
|
||||||
|
|
@ -34,7 +50,7 @@ export const getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
|
||||||
domains = newDomains;
|
domains = newDomains;
|
||||||
}
|
}
|
||||||
if (!domains || domains.length == 0) {
|
if (!domains || domains.length == 0) {
|
||||||
domains = [req.headers.host];
|
domains = [urlObject.host];
|
||||||
}
|
}
|
||||||
|
|
||||||
const queryParams = [];
|
const queryParams = [];
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue