-
Notifications
You must be signed in to change notification settings - Fork 576
/
_http_common.ts
190 lines (170 loc) · 4.91 KB
/
_http_common.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
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
import { setImmediate } from "./timers.ts";
import type { Buffer } from "./buffer.ts";
import { FreeList } from "./internal/freelist.ts";
// We use http-parser-js (https://www.npmjs.com/package/http-parser-js)
// instead of llhttp
// @deno-types="./vendor/http_parser.d.ts"
import { HTTPParser, HTTPParserJS } from "./vendor/http_parser.js";
function parserOnHeaders() {
// skip
}
function parserOnHeadersComplete(
this: {
onIncoming: (res: unknown, shouldKeepAlive: boolean) => void;
socket: unknown;
},
versionMajor: unknown,
versionMinor: unknown,
headers: unknown,
method: unknown,
url: unknown,
statusCode: unknown,
statusMessage: unknown,
upgrade: unknown,
shouldKeepAlive: boolean,
) {
// FIXME(kt3k): Implement IncomingMessage class correctly.
// Uses socket object as incoming for now
const incoming = Object.assign(this.socket, {
versionMajor,
versionMinor,
headers,
method,
url,
statusCode,
statusMessage,
upgrade,
});
this.onIncoming(incoming, shouldKeepAlive);
}
function parserOnBody(
// deno-lint-ignore no-explicit-any
this: { incoming: any; socket: any },
b: Buffer,
start: number,
len: number,
) {
const stream = this.incoming;
// If the stream has already been removed, then drop it.
if (stream == null) {
return;
}
// Pretend this was the result of a stream._read call.
if (len > 0 && !stream._dumped) {
const slice = b.slice(start, start + len);
const ret = stream.push(slice);
if (!ret) {
if (this.socket) {
this.socket.pause();
}
}
}
}
function parserOnMessageComplete(
// deno-lint-ignore no-explicit-any
this: { incoming: any; _headers: any; _url: any; socket: any },
) {
// deno-lint-ignore no-this-alias
const parser = this;
const stream = parser.incoming;
if (stream != null) {
stream.complete = true;
// Emit any trailing headers.
const headers = parser._headers;
if (headers.length) {
stream._addHeaderLines(headers, headers.length);
parser._headers = [];
parser._url = "";
}
// For emit end event
stream.push(null);
}
// Force to read the next incoming message
const socket = parser.socket;
if (socket && !socket._paused && socket.readable) {
socket.resume();
}
}
export const parsers = new FreeList("parsers", 1000, function parsersCb() {
const parser = new HTTPParser();
parser[HTTPParser.kOnHeaders] = parserOnHeaders;
// deno-lint-ignore no-explicit-any
(parser as any)[HTTPParser.kOnHeadersComplete] = parserOnHeadersComplete;
parser[HTTPParser.kOnBody] = parserOnBody;
parser[HTTPParser.kOnMessageComplete] = parserOnMessageComplete;
return parser;
});
function closeParserInstance(parser: HTTPParserJS) {
parser.close();
}
// Free the parser and also break any links that it
// might have to any other things.
// TODO: All parser data should be attached to a
// single object, so that it can be easily cleaned
// up by doing `parser.data = {}`, which should
// be done in FreeList.free. `parsers.free(parser)`
// should be all that is needed.
export function freeParser(
parser: HTTPParserJS,
// deno-lint-ignore no-explicit-any
req: any,
// deno-lint-ignore no-explicit-any
socket: any,
) {
if (parsers.free(parser) === false) {
// Make sure the parser's stack has unwound before deleting the
// corresponding C++ object through .close().
setImmediate(closeParserInstance, parser);
} else {
// Since the Parser destructor isn't going to run the destroy() callbacks
// it needs to be triggered manually.
parser.free();
}
if (req) {
req.parser = null;
}
if (socket) {
socket.parser = null;
}
}
const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
/**
* Verifies that the given val is a valid HTTP token
* per the rules defined in RFC 7230
* See https://tools.ietf.org/html/rfc7230#section-3.2.6
*/
function checkIsHttpToken(val: string) {
return tokenRegExp.test(val);
}
const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
/**
* True if val contains an invalid field-vchar
* field-value = *( field-content / obs-fold )
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
* field-vchar = VCHAR / obs-text
*/
function checkInvalidHeaderChar(val: string) {
return headerCharRegex.test(val);
}
export function prepareError(
// deno-lint-ignore no-explicit-any
err: any,
parser: HTTPParserJS,
rawPacket: Buffer,
) {
err.rawPacket = rawPacket || parser.getCurrentBuffer();
if (typeof err.reason === "string") {
err.message = `Parse Error: ${err.reason}`;
}
}
export function isLenient() {
return false;
}
export const chunkExpression = /(?:^|\W)chunked(?:$|\W)/i;
export {
checkInvalidHeaderChar as _checkInvalidHeaderChar,
checkIsHttpToken as _checkIsHttpToken,
HTTPParser,
};