-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpHandler.js
524 lines (464 loc) · 15.3 KB
/
HttpHandler.js
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import { posix } from 'node:path';
import { promisify } from 'node:util';
import { isWritable } from '../utils/stream.js';
import HttpRequest from './HttpRequest.js';
import HttpResponse from './HttpResponse.js';
import HttpTransaction from './HttpTransaction.js';
/** @typedef {import('../types').Middleware} Middleware */
/** @typedef {import('../types').MiddlewareErrorHandler} MiddlewareErrorHandler */
/** @typedef {import('../types').MiddlewareFlowInstruction} MiddlewareFlowInstruction */
/** @typedef {import('../types').RequestMethod} RequestMethod */
/** @type {HttpHandler} */
let defaultInstance = null;
/**
* @typedef {Object} HttpHandlerOptions
* @prop {Middleware[]} [middleware]
* @prop {MiddlewareErrorHandler[]} [errorHandlers]
*/
export default class HttpHandler {
/** @type {true} */
static CONTINUE = true;
/** @type {false} */
static BREAK = false;
/** @type {0} */
static END = 0;
/** @return {HttpHandler} */
static get defaultInstance() {
if (!defaultInstance) {
defaultInstance = new HttpHandler();
}
return defaultInstance;
}
/**
* @param {any} result
* @return {?MiddlewareFlowInstruction}
*/
static ParseResultSync(result) {
// Fast return
switch (result) {
case true:
case null:
case undefined:
case HttpHandler.CONTINUE:
return HttpHandler.CONTINUE;
case false:
case HttpHandler.BREAK:
return HttpHandler.BREAK;
case 0:
return HttpHandler.END;
default:
}
return null;
}
/**
* @param {string} scheme
* @param {string} authority
* @param {string} path
*/
static parseURL(scheme, authority, path) {
let query = '';
let fragment = '';
const authoritySplit = authority.split(':');
let pathname = '';
let search = '';
let hash = '';
const queryIndex = path.indexOf('?');
const fragmentIndex = path.indexOf('#');
// URL variables
pathname = path;
if (queryIndex !== -1) {
pathname = path.slice(0, queryIndex);
search = path.slice(queryIndex, fragmentIndex === -1 ? undefined : fragmentIndex);
query = search.slice(1);
} else if (fragmentIndex === -1) {
pathname = path;
} else {
pathname = path.slice(0, fragmentIndex);
hash = path.slice(fragmentIndex);
fragment = hash.slice(1);
}
// Remove dot segments
pathname = posix.normalize(pathname);
return {
href: `${scheme}://${authority}${pathname}${search}${hash}`,
origin: `${scheme}://${authority}`,
protocol: `${scheme}:`,
username: '',
password: '',
host: authority,
hostname: authoritySplit[0],
port: authoritySplit[1] ?? '',
pathname,
search,
hash,
query,
fragment,
url: `${scheme}://${authority}${path}`,
};
}
/** @param {HttpHandlerOptions} options */
constructor(options = {}) {
this.middleware = options.middleware || [];
this.errorHandlers = options.errorHandlers || [];
this.handleTransaction = this.handleTransaction.bind(this);
this.handleHttp1Request = this.handleHttp1Request.bind(this);
this.handleHttp2Stream = this.handleHttp2Stream.bind(this);
}
/**
* @param {HttpTransaction} transaction
* @param {Middleware} middleware
* @return {Promise<MiddlewareFlowInstruction>}
*/
async processMiddleware(transaction, middleware) {
if (middleware == null) return HttpHandler.CONTINUE;
// Check if error handler
if (typeof middleware === 'object' && typeof middleware.onError === 'function') {
// Skip it if not errored
if (!transaction.error) return HttpHandler.CONTINUE;
// Clear error state
// eslint-disable-next-line no-param-reassign
transaction.error = null;
try {
let result;
result = middleware.constructor.name === 'AsyncFunction'
? await middleware.onError(transaction)
: middleware.onError(transaction);
if (result == null) return HttpHandler.CONTINUE;
// Sync operation returned Promise
if (typeof result === 'object' && typeof result.then === 'function') {
result = await result;
}
const syncResult = HttpHandler.ParseResultSync(result);
if (syncResult != null) return syncResult;
// Slip in support for functions that return an Array
if (Array.isArray(result)) {
return transaction.response.end(result);
}
return await this.processMiddleware(transaction, result);
} catch (error) {
// Catch failed error handler
console.warn('Error handler threw an error.', transaction.request.path, error);
transaction.error = error;
return HttpHandler.CONTINUE;
}
}
// Is errored, only default error handler allowed further
if (transaction.error && middleware !== this.errorHandlers) {
return HttpHandler.CONTINUE;
}
let syncResult = HttpHandler.ParseResultSync(middleware);
if (syncResult != null) {
return syncResult;
}
/** @type {?MiddlewareFlowInstruction} */
let result;
switch (typeof middleware) {
case 'number':
transaction.response.status = middleware;
return transaction.response.end();
case 'function':
try {
result = middleware.constructor.name === 'AsyncFunction'
? await middleware(transaction)
: middleware(transaction);
if (result == null) return HttpHandler.CONTINUE;
// Sync operation returned Promise
if (typeof result === 'object' && typeof result.then === 'function') {
result = await result;
}
syncResult = HttpHandler.ParseResultSync(result);
if (syncResult != null) return syncResult;
// Slip in support for functions that return an Array
if (Array.isArray(result)) {
return transaction.response.end(result);
}
return await this.processMiddleware(transaction, result);
} catch (error) {
console.warn('Caught runtime error', error.message, error.stack);
transaction.error = error;
return HttpHandler.CONTINUE;
}
case 'object':
if (Array.isArray(middleware)) {
const { treeIndex } = transaction.state;
treeIndex.push(-1);
for (const innerMiddleware of middleware) {
treeIndex[treeIndex.length - 1] += 1;
if (innerMiddleware == null) continue;
result = HttpHandler.ParseResultSync(innerMiddleware);
if (result == null) {
// eslint-disable-next-line no-await-in-loop
result = await this.processMiddleware(transaction, innerMiddleware);
}
if (result === HttpHandler.END) {
break;
}
if (result === HttpHandler.BREAK) {
// Break from branch and continue in parent
result = HttpHandler.CONTINUE;
break;
}
// Continue in branch
}
treeIndex.pop();
return result;
}
if ('execute' in middleware && typeof middleware.execute === 'function') {
return await this.processMiddleware(transaction, middleware.execute.bind(middleware));
}
// Static caller
if ('Execute' in middleware && typeof middleware.Execute === 'function') {
return await this.processMiddleware(transaction, middleware.Execute);
}
if ('then' in middleware && typeof middleware.then === 'function') {
return await this.processMiddleware(transaction, await middleware);
}
// Fallthrough for Objects
case 'string':
return transaction.response.end(middleware);
default:
console.warn('Unknown middleware', middleware);
return HttpHandler.CONTINUE;
}
}
/**
* @param {HttpTransaction} transaction
* @return {Promise<HttpTransaction>}
*/
async handleTransaction(transaction) {
const allMiddleware = [
this.middleware,
this.errorHandlers,
];
const finalFinalResponse = await this.processMiddleware(transaction, allMiddleware);
if (finalFinalResponse !== HttpHandler.END) {
console.warn(transaction.request.url, "Middleware resolution did not complete with 'end'");
}
if (transaction.error) {
console.warn('Webhoster did not find error handler. Crash prevented.', transaction.request.path, transaction.error);
}
return transaction;
}
/**
* @param {import('http').IncomingMessage} incomingMessage
* @param {import('http').ServerResponse} serverResponse
* @return {Promise<HttpTransaction>}
*/
async handleHttp1Request(incomingMessage, serverResponse) {
/** @throws {Error} */
function onMalformed() {
const error = new Error('PROTOCOL_ERROR');
incomingMessage.destroy(error);
serverResponse.destroy(error);
throw error;
}
const {
method, headers, socket, url: path,
} = incomingMessage;
if (!method) onMalformed();
// @ts-expect-error If TLSSocketLike
const scheme = socket.encrypted ? 'https' : 'http';
const authority = headers.host;
if (!authority) onMalformed();
if (authority.includes('@')) onMalformed();
let urlOptions;
if (method === 'CONNECT') {
if (scheme || path) onMalformed();
} else {
if (!scheme || !path) onMalformed();
if (path === '*') {
// asterisk-form
if (method !== 'OPTIONS') onMalformed();
} else {
urlOptions = HttpHandler.parseURL(scheme, authority, path);
}
}
const request = new HttpRequest({
headers,
method,
stream: incomingMessage,
scheme,
authority,
path,
...urlOptions,
});
const response = new HttpResponse({
stream: serverResponse,
headers: {},
request,
onHeadersSent() {
return serverResponse.headersSent;
},
onSendHeaders(flush, end) {
if (response.status == null) {
throw new Error('NO_STATUS');
}
if (!isWritable(serverResponse)) return false;
serverResponse.writeHead(response.status, response.headers);
if (end) {
serverResponse.end();
} else if (flush) {
serverResponse.flushHeaders();
}
return true;
},
});
const transaction = new HttpTransaction({
httpVersion: '2.0',
request,
response,
socket,
canPing: false,
canPushPath: false,
});
await this.handleTransaction(transaction);
if (isWritable(serverResponse)) {
setTimeout(() => {
if (isWritable(serverResponse)) {
console.warn('Respond stream end lagging more than 60s. Did you forget to call `.end()`?', request.url);
}
}, 60_000);
}
return transaction;
}
/**
* @param {import('http2').ServerHttp2Stream} stream
* @param {import('http2').IncomingHttpHeaders} headers
* @param {Partial<import('./HttpTransaction.js').HttpTransactionOptions<unknown>>} [transactionOptions]
* @return {Promise<HttpTransaction>}
*/
async handleHttp2Stream(stream, headers, transactionOptions = {}) {
/** @throws {Error} */
function onMalformed() {
const error = new Error('PROTOCOL_ERROR');
stream.destroy(error);
throw error;
}
const {
':method': method,
':scheme': scheme,
':authority': authorityHeader,
host: hostHeader,
} = headers;
if (!method) onMalformed();
// HTTP/2 to HTTP/1 translation
const authority = /** @type {string} */ (authorityHeader || hostHeader);
if (!authority) onMalformed();
if (authority.includes('@')) onMalformed();
const path = headers[':path'];
let urlOptions;
if (method === 'CONNECT') {
if (scheme || path) onMalformed();
} else {
if (!scheme || !path) onMalformed();
if (path === '*') {
// asterisk-form
if (method !== 'OPTIONS') onMalformed();
} else {
urlOptions = HttpHandler.parseURL(scheme, authority, path);
}
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;
/** @type {Promise<any>[]} */
const pendingStreamLocks = [];
const request = new HttpRequest({
headers,
method,
stream,
scheme,
authority,
path,
...urlOptions,
});
const response = new HttpResponse({
request,
stream,
onHeadersSent() {
return stream.headersSent;
},
onSendHeaders(flush, end) {
if (response.headers[':status'] == null) {
if (response.status == null) {
throw new Error('NO_STATUS');
}
response.headers[':status'] = response.status;
}
if (!isWritable(stream)) return false;
stream.respond(response.headers, { endStream: end });
return true;
},
});
const transaction = new HttpTransaction({
httpVersion: '2.0',
request,
response,
socket: stream.session.socket,
canPing: true,
onPing: promisify(stream.session.ping).bind(stream.session),
canPushPath: () => stream.pushAllowed,
onPushPath: async (pushPath) => {
if (!stream.pushAllowed) {
throw new Error('PUSH_NOT_ALLOWED');
}
const newHeaders = {
':scheme': headers[':scheme'],
':authority': headers[':authority'],
':path': pushPath,
':method': 'GET',
};
for (const passedHeader of [
'accept',
'accept-encoding',
'accept-language',
'user-agent',
'cache-control',
]) {
if (passedHeader in headers) {
// @ts-ignore Coerce
newHeaders[passedHeader] = headers[passedHeader];
}
}
// Build promise function
const promiseFunction = async () => {
try {
const pushStream = await new Promise((resolve, reject) => {
stream.pushStream(newHeaders, ((error, newStream) => (error ? reject(error) : resolve(newStream))));
});
pushStream.addListener('error', (error) => {
if (error?.code === 'ECONNRESET') {
console.warn('HTTP/2 stream connection reset.', headers[':path']);
} else {
console.error('HTTP/2 stream error', headers, error);
}
});
await context.handleHttp2Stream(pushStream, newHeaders, { canPushPath: false });
} catch (error) {
console.error('onPushFailed', error, error.stack);
throw error;
}
};
// Schedule microtask
const promiseExecution = promiseFunction();
// Add as stream lock
pendingStreamLocks.push(promiseExecution);
// Wait for promise to complete before returning
await promiseExecution;
},
...transactionOptions,
});
await this.handleTransaction(transaction);
if (pendingStreamLocks.length) {
// Wait for all child push streams to terminate before we return.
await Promise.allSettled(pendingStreamLocks);
}
if (isWritable(stream)) {
setTimeout(() => {
if (isWritable(stream)) {
console.warn('Respond stream end lagging more than 60s. Did you forget to call `.end()`?', request.url);
}
}, 60_000);
}
return transaction;
}
}