Skip to content
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
31 changes: 27 additions & 4 deletions adjacent-servers/adjacent-servers-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function initAdjacentServersProxy(app, isDocker, ensureAdmin, ensureUserForAdjac
},
selfHandleResponse: true,
on: {
proxyReq: (proxyReq, req, res) => {
Comment thread
tariqksoliman marked this conversation as resolved.
proxyReq.setHeader("X-Forwarded-Host", req.get("host"));
proxyReq.setHeader("X-Forwarded-Proto", req.headers["x-forwarded-proto"] || req.protocol);
},
proxyRes: createSwaggerInterceptor("stac", stacTarget),
},
})
Expand All @@ -47,6 +51,10 @@ function initAdjacentServersProxy(app, isDocker, ensureAdmin, ensureUserForAdjac
},
selfHandleResponse: true,
on: {
proxyReq: (proxyReq, req, res) => {
proxyReq.setHeader("X-Forwarded-Host", req.get("host"));
proxyReq.setHeader("X-Forwarded-Proto", req.headers["x-forwarded-proto"] || req.protocol);
},
proxyRes: createSwaggerInterceptor("tipg", tipgTarget),
},
})
Expand Down Expand Up @@ -85,6 +93,10 @@ function initAdjacentServersProxy(app, isDocker, ensureAdmin, ensureUserForAdjac
},
selfHandleResponse: true,
on: {
proxyReq: (proxyReq, req, res) => {
proxyReq.setHeader("X-Forwarded-Host", req.get("host"));
proxyReq.setHeader("X-Forwarded-Proto", req.headers["x-forwarded-proto"] || req.protocol);
},
proxyRes: createSwaggerInterceptor("titiler", titilerTarget),
},
})
Expand All @@ -109,6 +121,10 @@ function initAdjacentServersProxy(app, isDocker, ensureAdmin, ensureUserForAdjac
},
selfHandleResponse: true,
on: {
proxyReq: (proxyReq, req, res) => {
proxyReq.setHeader("X-Forwarded-Host", req.get("host"));
proxyReq.setHeader("X-Forwarded-Proto", req.headers["x-forwarded-proto"] || req.protocol);
},
proxyRes: createSwaggerInterceptor(
"titilerpgstac",
titilerpgstacTarget
Expand Down Expand Up @@ -272,10 +288,17 @@ const createSwaggerInterceptor = (path, target) => {
res.get("Content-Type").includes("html"))
) {
newResponse = newResponse || responseBuffer.toString("utf8");
newResponse = newResponse.replaceAll(
target,
`${req.protocol}://${req.get("host")}/${path}`
);
const { hostname: serviceHost, port: servicePort } = new URL(target);
const publicBase = `${req.protocol}://${req.get("host")}/${path}`;
// Replace port-qualified variants first to avoid partial matches on bare hostname
if (servicePort) {
newResponse = newResponse.replaceAll(`https://${serviceHost}:${servicePort}/`, `${publicBase}/`);
newResponse = newResponse.replaceAll(`https://${serviceHost}:${servicePort}`, publicBase);
}
newResponse = newResponse.replaceAll(`https://${serviceHost}/`, `${publicBase}/`);
newResponse = newResponse.replaceAll(`https://${serviceHost}`, publicBase);
newResponse = newResponse.replaceAll(`http://${serviceHost}/`, `${publicBase}/`);
newResponse = newResponse.replaceAll(target, publicBase);
}

return newResponse || finalReturn;
Expand Down
Loading