Skip to content

Commit

Permalink
feat: support authority pseudo header for requests
Browse files Browse the repository at this point in the history
Closes #658
  • Loading branch information
kitsonk committed Jun 8, 2024
1 parent cbf6ac1 commit 0c8b8da
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,39 +123,41 @@ export class Request {
get url(): URL {
if (!this.#url) {
const serverRequest = this.#serverRequest;
if (!this.#proxy) {
// between 1.9.0 and 1.9.1 the request.url of the native HTTP started
// returning the full URL, where previously it only returned the path
// so we will try to use that URL here, but default back to old logic
// if the URL isn't valid.
// between Deno 1.9.0 and 1.9.1 the request.url of the native HTTP started
// returning the full URL, where previously it only returned the path
// so we will try to use that URL here, but default back to old logic
// if the URL isn't valid.
try {
if (serverRequest.rawUrl) {
this.#url = new URL(serverRequest.rawUrl);
}
} catch {
// we don't care about errors here
}
if (this.#proxy || !this.#url) {
let proto: string;
let host: string;
if (this.#proxy) {
proto = serverRequest
.headers.get("x-forwarded-proto")?.split(/\s*,\s*/, 1)[0] ??
"http";
host = serverRequest.headers.get("x-forwarded-host") ??
this.#url?.hostname ??
serverRequest.headers.get("host") ??
serverRequest.headers.get(":authority") ?? "";
} else {
proto = this.#secure ? "https" : "http";
host = serverRequest.headers.get("host") ??
serverRequest.headers.get(":authority") ?? "";
}
try {
if (serverRequest.rawUrl) {
this.#url = new URL(serverRequest.rawUrl);
return this.#url;
}
this.#url = new URL(`${proto}://${host}${serverRequest.url}`);
} catch {
// we don't care about errors here
throw new TypeError(
`The server request URL of "${proto}://${host}${serverRequest.url}" is invalid.`,
);
}
}
let proto: string;
let host: string;
if (this.#proxy) {
proto = serverRequest
.headers.get("x-forwarded-proto")?.split(/\s*,\s*/, 1)[0] ??
"http";
host = serverRequest.headers.get("x-forwarded-host") ??
serverRequest.headers.get("host") ?? "";
} else {
proto = this.#secure ? "https" : "http";
host = serverRequest.headers.get("host") ?? "";
}
try {
this.#url = new URL(`${proto}://${host}${serverRequest.url}`);
} catch {
throw new TypeError(
`The server request URL of "${proto}://${host}${serverRequest.url}" is invalid.`,
);
}
}
return this.#url;
}
Expand Down

0 comments on commit 0c8b8da

Please sign in to comment.