Properly handle serverURL and publicServerUrl in Batch requests #6980 (#7049)

* fix: detect if the caller is accessing us via local or parse for batch requests (#6980)

* chore: minor cleanup from PR
This commit is contained in:
Zach Goldberg
2020-12-09 12:16:24 -08:00
committed by GitHub
parent ca1b78220f
commit abdfe61b82
2 changed files with 35 additions and 3 deletions

View File

@@ -36,12 +36,23 @@ function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
if (serverURL && publicServerURL && serverURL.path != publicServerURL.path) {
const localPath = serverURL.path;
const publicPath = publicServerURL.path;
// Override the api prefix
apiPrefix = localPath;
return function (requestPath) {
// Build the new path by removing the public path
// and joining with the local path
const newPath = path.posix.join('/', localPath, '/', requestPath.slice(publicPath.length));
// Figure out which server url was used by figuring out which
// path more closely matches requestPath
const startsWithLocal = requestPath.startsWith(localPath);
const startsWithPublic = requestPath.startsWith(publicPath);
const pathLengthToUse =
startsWithLocal && startsWithPublic
? Math.max(localPath.length, publicPath.length)
: startsWithLocal
? localPath.length
: publicPath.length;
const newPath = path.posix.join('/', localPath, '/', requestPath.slice(pathLengthToUse));
// Use the method for local routing
return makeRoutablePath(newPath);
};