Skip to content

Commit 0ea7146

Browse files
authored
fix: Correctly set x-ms-original-url to the full original request URL (#286) (#314)
* Correctly preserving x-ms-original-url * reverted changes * Correctly serve static assets, but also correctly fallback to functions * Refactored getResponse to return a boolean if it was succesfully handled * Set the x-ms-original-url to equal the full request URL * Added e2e test for verifying x-ms-original-url is set
1 parent 5a08d83 commit 0ea7146

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

cypress/integration/functions.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ context.only("/api", () => {
1616
expect(body).to.include("x-swa-custom");
1717
});
1818
});
19+
it("Should correctly set x-ms-original-url to the full request url", () => {
20+
const HEADER_URL = "http://0.0.0.0:1234/api/headers";
21+
cy.request({ url: HEADER_URL, failOnStatusCode: false }).then((response) => {
22+
expect(response.status).to.eq(200);
23+
expect(response.body["x-ms-original-url"]).to.equal(HEADER_URL);
24+
});
25+
});
1926
});
2027

2128
describe(`Accessing /api/status`, () => {
@@ -46,7 +53,7 @@ context.only("/api", () => {
4653

4754
cy.request({ url: `http://0.0.0.0:1234/api/info`, failOnStatusCode: false }).then((response) => {
4855
expect(response.status).to.eq(200);
49-
expect(response.body).to.eq('authorized');
56+
expect(response.body).to.eq("authorized");
5057
});
5158
});
5259
});

src/msha/handlers/function.handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ registerProcessExit(() => {
1616

1717
function injectHeaders(req: http.ClientRequest, host: string) {
1818
logger.silly(`injecting headers to Functions request:`);
19-
20-
req.setHeader("x-ms-original-url", encodeURI(new URL(req.path!, host).toString()));
21-
logger.silly(` - x-ms-original-url: ${chalk.yellow(req.getHeader("x-ms-original-url"))}`);
22-
19+
if (!req.getHeader("x-ms-original-url")) {
20+
req.setHeader("x-ms-original-url", encodeURI(new URL(req.path!, host).toString()));
21+
logger.silly(` - x-ms-original-url: ${chalk.yellow(req.getHeader("x-ms-original-url"))}`);
22+
}
2323
// generate a fake correlation ID
2424
req.setHeader("x-ms-request-id", `SWA-CLI-${Math.random().toString(36).substring(2).toUpperCase()}`);
2525
logger.silly(` - x-ms-request-id: ${chalk.yellow(req.getHeader("x-ms-request-id"))}`);

src/msha/middlewares/request.middleware.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,7 @@ export async function requestMiddleware(
301301
return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
302302
}
303303

304-
getResponse(req, res, matchingRouteRule, userConfig, isFunctionReq);
305-
306-
if (!isFunctionReq) {
304+
if (!getResponse(req, res, matchingRouteRule, userConfig, isFunctionReq)) {
307305
logger.silly(` - url: ${chalk.yellow(req.url)}`);
308306
logger.silly(` - target: ${chalk.yellow(target)}`);
309307

src/msha/middlewares/response.middleware.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,35 @@ export function getResponse(
1919
matchedRoute: SWAConfigFileRoute | undefined,
2020
userConfig: SWAConfigFile | undefined,
2121
isFunctionRequest: boolean
22-
) {
22+
): boolean {
2323
const statusCodeToServe = parseInt(`${matchedRoute?.statusCode}`, 10);
2424
const redirect = matchedRoute?.redirect;
2525
const rewrite = matchedRoute?.rewrite;
26-
2726
logger.silly(`using userConfig`);
2827
logger.silly({ userConfig });
2928

3029
if (redirect) {
3130
logger.silly(` - redirect rule detected. Exit`);
3231

33-
return applyRedirectResponse(req, res, matchedRoute);
32+
applyRedirectResponse(req, res, matchedRoute);
33+
return false;
3434
}
35-
35+
// We should always set the x-ms-original-url to be the full request URL.
36+
req.headers["x-ms-original-url"] = new URL(req.url!, `http://${req.headers.host}`).href;
3637
if (rewrite) {
3738
req.url = rewrite;
3839
}
3940

4041
if ([403, 401].includes(statusCodeToServe)) {
4142
logger.silly(` - ${statusCodeToServe} code detected. Exit`);
4243

43-
return handleErrorPage(req, res, statusCodeToServe, userConfig?.responseOverrides);
44+
handleErrorPage(req, res, statusCodeToServe, userConfig?.responseOverrides);
45+
return false;
4446
}
4547

4648
if (isFunctionRequest) {
47-
return handleFunctionRequest(req, res);
49+
handleFunctionRequest(req, res);
50+
return true;
4851
}
4952

5053
const storageResult = getStorageContent(
@@ -60,12 +63,14 @@ export function getResponse(
6063
);
6164

6265
if (storageResult.isFunctionFallbackRequest) {
63-
return handleFunctionRequest(req, res);
66+
req.url = userConfig?.navigationFallback.rewrite!;
67+
handleFunctionRequest(req, res);
68+
return true;
6469
}
65-
6670
if (statusCodeToServe) {
6771
res.statusCode = statusCodeToServe;
6872
}
73+
return false;
6974
}
7075

7176
export function getStorageContent(

0 commit comments

Comments
 (0)