diff --git a/src/utils.js b/src/utils.js index 64a24e3..d52576f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -22,10 +22,28 @@ function tileToLat(tileY, zoom) { return (180.0 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))); } -export const getPublicUrl = (publicUrl, req) => publicUrl || `${req.protocol}://${req.headers.host}/`; +/** + * Generate new URL object + * @param req + * @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(',');