Skip to content

Commit 0c8b8da

Browse files
committed
feat: support authority pseudo header for requests
Closes oakserver#658
1 parent cbf6ac1 commit 0c8b8da

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

request.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,41 @@ export class Request {
123123
get url(): URL {
124124
if (!this.#url) {
125125
const serverRequest = this.#serverRequest;
126-
if (!this.#proxy) {
127-
// between 1.9.0 and 1.9.1 the request.url of the native HTTP started
128-
// returning the full URL, where previously it only returned the path
129-
// so we will try to use that URL here, but default back to old logic
130-
// if the URL isn't valid.
126+
// between Deno 1.9.0 and 1.9.1 the request.url of the native HTTP started
127+
// returning the full URL, where previously it only returned the path
128+
// so we will try to use that URL here, but default back to old logic
129+
// if the URL isn't valid.
130+
try {
131+
if (serverRequest.rawUrl) {
132+
this.#url = new URL(serverRequest.rawUrl);
133+
}
134+
} catch {
135+
// we don't care about errors here
136+
}
137+
if (this.#proxy || !this.#url) {
138+
let proto: string;
139+
let host: string;
140+
if (this.#proxy) {
141+
proto = serverRequest
142+
.headers.get("x-forwarded-proto")?.split(/\s*,\s*/, 1)[0] ??
143+
"http";
144+
host = serverRequest.headers.get("x-forwarded-host") ??
145+
this.#url?.hostname ??
146+
serverRequest.headers.get("host") ??
147+
serverRequest.headers.get(":authority") ?? "";
148+
} else {
149+
proto = this.#secure ? "https" : "http";
150+
host = serverRequest.headers.get("host") ??
151+
serverRequest.headers.get(":authority") ?? "";
152+
}
131153
try {
132-
if (serverRequest.rawUrl) {
133-
this.#url = new URL(serverRequest.rawUrl);
134-
return this.#url;
135-
}
154+
this.#url = new URL(`${proto}://${host}${serverRequest.url}`);
136155
} catch {
137-
// we don't care about errors here
156+
throw new TypeError(
157+
`The server request URL of "${proto}://${host}${serverRequest.url}" is invalid.`,
158+
);
138159
}
139160
}
140-
let proto: string;
141-
let host: string;
142-
if (this.#proxy) {
143-
proto = serverRequest
144-
.headers.get("x-forwarded-proto")?.split(/\s*,\s*/, 1)[0] ??
145-
"http";
146-
host = serverRequest.headers.get("x-forwarded-host") ??
147-
serverRequest.headers.get("host") ?? "";
148-
} else {
149-
proto = this.#secure ? "https" : "http";
150-
host = serverRequest.headers.get("host") ?? "";
151-
}
152-
try {
153-
this.#url = new URL(`${proto}://${host}${serverRequest.url}`);
154-
} catch {
155-
throw new TypeError(
156-
`The server request URL of "${proto}://${host}${serverRequest.url}" is invalid.`,
157-
);
158-
}
159161
}
160162
return this.#url;
161163
}

0 commit comments

Comments
 (0)