Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for relative public URL #1464

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function fixUrl(req, url, publicUrl) {
* @param {object} req - Express request object.
* @returns {URL} - URL object with correct host and optionally path.
*/
function getUrlObject(req) {
function getUrlObject(req, prefix) {
const urlObject = new URL(`${req.protocol}://${req.headers.host}/`);
// support overriding hostname by sending X-Forwarded-Host http header
urlObject.hostname = req.hostname;
Expand All @@ -104,7 +104,7 @@ function getUrlObject(req) {
}

// support add url prefix by sending X-Forwarded-Path http header
const xForwardedPath = req.get('X-Forwarded-Path');
const xForwardedPath = prefix || req.get('X-Forwarded-Path');
if (xForwardedPath) {
urlObject.pathname = path.posix.join(xForwardedPath, urlObject.pathname);
}
Expand All @@ -119,8 +119,13 @@ function getUrlObject(req) {
*/
export function getPublicUrl(publicUrl, req) {
if (publicUrl) {
return publicUrl;
if (/^https?:\/\//.test(publicUrl)) {
return publicUrl;
} else {
return getUrlObject(req, publicUrl).toString();
}
}

return getUrlObject(req).toString();
}

Expand Down Expand Up @@ -196,11 +201,11 @@ export function getTileUrls(
}

const uris = [];
if (!publicUrl) {
let xForwardedPath = `${req.get('X-Forwarded-Path') ? '/' + req.get('X-Forwarded-Path') : ''}`;
if (!publicUrl || !/^https?:\/\//.test(publicUrl)) {
let xForwardedPath = publicUrl || `${req.get('X-Forwarded-Path') ? '/' + req.get('X-Forwarded-Path') : ''}`;
for (const domain of domains) {
uris.push(
`${req.protocol}://${domain}${xForwardedPath}/${path}/${tileParams}${format}${query}`,
`${req.protocol}://${domain}${xForwardedPath.replace(/\/$/, "")}/${path}/${tileParams}${format}${query}`,
);
}
} else {
Expand Down