-
Notifications
You must be signed in to change notification settings - Fork 235
/
request.ts
326 lines (301 loc) · 10.9 KB
/
request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
/**
* Contains the {@linkcode Request} abstraction used by oak.
*
* Most end users would not need to directly access this module.
*
* @module
*/
import { Body } from "./body.ts";
import { ServerSentEventStreamTarget } from "./deps.ts";
import {
accepts,
acceptsEncodings,
acceptsLanguages,
type HTTPMethods,
type ServerSentEventTarget,
type ServerSentEventTargetOptions,
UserAgent,
} from "./deps.ts";
import type { ServerRequest, UpgradeWebSocketOptions } from "./types.ts";
interface OakRequestOptions {
jsonBodyReviver?: (key: string, value: unknown) => unknown;
proxy?: boolean;
secure?: boolean;
}
/** An interface which provides information about the current request. The
* instance related to the current request is available on the
* {@linkcode Context}'s `.request` property.
*
* The interface contains several properties to get information about the
* request as well as several methods, which include content negotiation and
* the ability to decode a request body.
*/
export class Request {
#body: Body;
#proxy: boolean;
#secure: boolean;
#serverRequest: ServerRequest;
#url?: URL;
#userAgent: UserAgent;
#getRemoteAddr(): string {
return this.#serverRequest.remoteAddr ?? "";
}
/** An interface to access the body of the request. This provides an API that
* aligned to the **Fetch Request** API, but in a dedicated API.
*/
get body(): Body {
return this.#body;
}
/** Is `true` if the request might have a body, otherwise `false`.
*
* **WARNING** this is an unreliable API. In HTTP/2 in many situations you
* cannot determine if a request has a body or not unless you attempt to read
* the body, due to the streaming nature of HTTP/2. As of Deno 1.16.1, for
* HTTP/1.1, Deno also reflects that behaviour. The only reliable way to
* determine if a request has a body or not is to attempt to read the body.
*/
get hasBody(): boolean {
return this.#body.has;
}
/** The `Headers` supplied in the request. */
get headers(): Headers {
return this.#serverRequest.headers;
}
/** Request remote address. When the application's `.proxy` is true, the
* `X-Forwarded-For` will be used to determine the requesting remote address.
*/
get ip(): string {
return (this.#proxy ? this.ips[0] : this.#getRemoteAddr()) ?? "";
}
/** When the application's `.proxy` is `true`, this will be set to an array of
* IPs, ordered from upstream to downstream, based on the value of the header
* `X-Forwarded-For`. When `false` an empty array is returned. */
get ips(): string[] {
return this.#proxy
? (this.#serverRequest.headers.get("x-forwarded-for") ??
this.#getRemoteAddr()).split(/\s*,\s*/)
: [];
}
/** The HTTP Method used by the request. */
get method(): HTTPMethods {
return this.#serverRequest.method as HTTPMethods;
}
/** Shortcut to `request.url.protocol === "https:"`. */
get secure(): boolean {
return this.#secure;
}
/** Set to the value of the low level oak server request abstraction.
*
* @deprecated this will be removed in future versions of oak. Accessing this
* abstraction is not useful to end users and is now a bit of a misnomer.
*/
get originalRequest(): ServerRequest {
return this.#serverRequest;
}
/** Returns the original Fetch API `Request` if available.
*
* This should be set with requests on Deno, but will not be set when running
* on Node.js.
*/
get source(): globalThis.Request | undefined {
return this.#serverRequest.request;
}
/** A parsed URL for the request which complies with the browser standards.
* When the application's `.proxy` is `true`, this value will be based off of
* the `X-Forwarded-Proto` and `X-Forwarded-Host` header values if present in
* the request. */
get url(): URL {
if (!this.#url) {
const serverRequest = this.#serverRequest;
// 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 {
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;
}
/** An object representing the requesting user agent. If the `User-Agent`
* header isn't defined in the request, all the properties will be undefined.
*
* See [std/http/user_agent#UserAgent](https://deno.land/[email protected]/http/user_agent.ts?s=UserAgent)
* for more information.
*/
get userAgent(): UserAgent {
return this.#userAgent;
}
constructor(
serverRequest: ServerRequest,
{ proxy = false, secure = false, jsonBodyReviver }: OakRequestOptions = {},
) {
this.#proxy = proxy;
this.#secure = secure;
this.#serverRequest = serverRequest;
this.#body = new Body(serverRequest, jsonBodyReviver);
this.#userAgent = new UserAgent(serverRequest.headers.get("user-agent"));
}
/** Returns an array of media types, accepted by the requestor, in order of
* preference. If there are no encodings supplied by the requestor,
* then accepting any is implied is returned.
*/
accepts(): string[] | undefined;
/** For a given set of media types, return the best match accepted by the
* requestor. If there are no encoding that match, then the method returns
* `undefined`.
*/
accepts(...types: string[]): string | undefined;
accepts(...types: string[]): string | string[] | undefined {
if (!this.#serverRequest.headers.has("Accept")) {
return types.length ? types[0] : ["*/*"];
}
if (types.length) {
return accepts(this.#serverRequest, ...types);
}
return accepts(this.#serverRequest);
}
/** Returns an array of encodings, accepted by the requestor, in order of
* preference. If there are no encodings supplied by the requestor,
* then `["*"]` is returned, matching any.
*/
acceptsEncodings(): string[] | undefined;
/** For a given set of encodings, return the best match accepted by the
* requestor. If there are no encodings that match, then the method returns
* `undefined`.
*
* **NOTE:** You should always supply `identity` as one of the encodings
* to ensure that there is a match when the `Accept-Encoding` header is part
* of the request.
*/
acceptsEncodings(...encodings: string[]): string | undefined;
acceptsEncodings(...encodings: string[]): string[] | string | undefined {
if (!this.#serverRequest.headers.has("Accept-Encoding")) {
return encodings.length ? encodings[0] : ["*"];
}
if (encodings.length) {
return acceptsEncodings(this.#serverRequest, ...encodings);
}
return acceptsEncodings(this.#serverRequest);
}
/** Returns an array of languages, accepted by the requestor, in order of
* preference. If there are no languages supplied by the requestor,
* `["*"]` is returned, indicating any language is accepted.
*/
acceptsLanguages(): string[] | undefined;
/** For a given set of languages, return the best match accepted by the
* requestor. If there are no languages that match, then the method returns
* `undefined`. */
acceptsLanguages(...langs: string[]): string | undefined;
acceptsLanguages(...langs: string[]): string[] | string | undefined {
if (!this.#serverRequest.headers.get("Accept-Language")) {
return langs.length ? langs[0] : ["*"];
}
if (langs.length) {
return acceptsLanguages(this.#serverRequest, ...langs);
}
return acceptsLanguages(this.#serverRequest);
}
/** Take the current request and initiate server sent event connection.
*
* > ![WARNING]
* > This is not intended for direct use, as it will not manage the target in
* > the overall context or ensure that additional middleware does not attempt
* > to respond to the request.
*/
async sendEvents(
options?: ServerSentEventTargetOptions,
init?: RequestInit,
): Promise<ServerSentEventTarget> {
const sse = new ServerSentEventStreamTarget(options);
await this.#serverRequest.respond(sse.asResponse(init));
return sse;
}
/** Take the current request and upgrade it to a web socket, returning a web
* standard `WebSocket` object.
*
* If the underlying server abstraction does not support upgrades, this will
* throw.
*
* > ![WARNING]
* > This is not intended for direct use, as it will not manage the websocket
* > in the overall context or ensure that additional middleware does not
* > attempt to respond to the request.
*/
upgrade(options?: UpgradeWebSocketOptions): WebSocket {
if (!this.#serverRequest.upgrade) {
throw new TypeError("Web sockets upgrade not supported in this runtime.");
}
return this.#serverRequest.upgrade(options);
}
[Symbol.for("Deno.customInspect")](
inspect: (value: unknown) => string,
): string {
const { body, hasBody, headers, ip, ips, method, secure, url, userAgent } =
this;
return `${this.constructor.name} ${
inspect({
body,
hasBody,
headers,
ip,
ips,
method,
secure,
url: url.toString(),
userAgent,
})
}`;
}
[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
options: any,
inspect: (value: unknown, options?: unknown) => string,
// deno-lint-ignore no-explicit-any
): any {
if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, "special");
}
const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1,
});
const { body, hasBody, headers, ip, ips, method, secure, url, userAgent } =
this;
return `${options.stylize(this.constructor.name, "special")} ${
inspect(
{ body, hasBody, headers, ip, ips, method, secure, url, userAgent },
newOptions,
)
}`;
}
}