-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(interceptor): add
applyForwardedHeaders
request interceptor
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { assertEquals } from "@std/assert/equals"; | ||
import { applyForwardedHeaders } from "./apply_forwarded_headers.ts"; | ||
|
||
Deno.test("x-forwarded", () => { | ||
const incomingBody = new ReadableStream(); | ||
|
||
const incomingReq = new Request("http://localhost:8000/yeah?this=that", { | ||
method: "PUT", | ||
headers: { | ||
"X-Forwarded-Proto": "https", | ||
"X-Forwarded-Host": "something.cool", | ||
"X-Forwarded-Port": "443", | ||
}, | ||
body: incomingBody, | ||
}); | ||
|
||
const adjustedReq = applyForwardedHeaders(incomingReq); | ||
|
||
assertEquals(adjustedReq.url, "https://something.cool/yeah?this=that"); | ||
assertEquals(adjustedReq.method, "PUT"); | ||
assertEquals(adjustedReq.body, incomingBody); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* A RequestInterceptor that applies any `X-Forwarded-*` headers | ||
* to the URL of the Request object. | ||
* | ||
* Useful where your server may be behind a proxy that forwards the | ||
* request and set these headers. | ||
* | ||
* Deno Deploy conveniently supplies you with a Request object with | ||
* the original URL, so this interceptor isn't required in Deploy. | ||
* | ||
* @example | ||
* ```ts | ||
* Deno.serve(intercept(handler, { request: applyForwardedHeaders })); | ||
* ``` | ||
* | ||
* TODO: check and parse the `Forwarded` header too. | ||
* | ||
* @param req The request | ||
* @returns either the original Request or a new Request with the adjusted URL | ||
*/ | ||
export function applyForwardedHeaders(req: Request): Request { | ||
const proto = req.headers.get("x-forwarded-proto"); | ||
const host = req.headers.get("x-forwarded-host"); | ||
const port = req.headers.get("x-forwarded-port"); | ||
|
||
if (proto || host || port) { | ||
const url = new URL(req.url); | ||
url.protocol = proto ?? url.protocol; | ||
url.host = host ?? url.host; | ||
url.port = port ?? url.port; | ||
return new Request(url, req); | ||
} | ||
|
||
return req; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters