@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +105 -2
- package/package.json +65 -4
- package/src/bootstrap/client/index.ts +191 -0
- package/src/bootstrap/dispatcher/index.ts +204 -0
- package/src/bootstrap/index.ts +22 -0
- package/src/bootstrap/reconnect/index.ts +334 -0
- package/src/bootstrap/server/index.ts +284 -0
- package/src/buffer/bits/index.ts +2 -0
- package/src/buffer/bits/reader/index.ts +64 -0
- package/src/buffer/bits/writer/index.ts +79 -0
- package/src/buffer/index.ts +1375 -0
- package/src/buffer/pool/index.ts +46 -0
- package/src/buffer/quantizer/index.ts +101 -0
- package/src/channel/attribute/index.ts +89 -0
- package/src/channel/context/index.ts +621 -0
- package/src/channel/group/index.ts +256 -0
- package/src/channel/handler/duplex/index.ts +105 -0
- package/src/channel/handler/inbound/index.ts +78 -0
- package/src/channel/handler/index.ts +135 -0
- package/src/channel/handler/outbound/index.ts +33 -0
- package/src/channel/index.ts +619 -0
- package/src/channel/initializer/index.ts +77 -0
- package/src/channel/outbound/index.ts +611 -0
- package/src/channel/pipeline/head/index.ts +100 -0
- package/src/channel/pipeline/index.ts +414 -0
- package/src/channel/pipeline/tail/index.ts +45 -0
- package/src/channel/pipeline/tracker/index.ts +120 -0
- package/src/channel/queue/index.ts +203 -0
- package/src/channel/resolved/index.ts +34 -0
- package/src/channel/server/index.ts +164 -0
- package/src/channel/shutdown/index.ts +25 -0
- package/src/channel/simple/index.ts +38 -0
- package/src/channel/transport/index.ts +74 -0
- package/src/channel/transport/socket/index.ts +80 -0
- package/src/cluster/index.ts +360 -0
- package/src/codec/binary/index.ts +102 -0
- package/src/codec/decoder/index.ts +479 -0
- package/src/codec/delimiter/index.ts +189 -0
- package/src/codec/encoder/index.ts +48 -0
- package/src/codec/error/index.ts +33 -0
- package/src/codec/fixed/index.ts +45 -0
- package/src/codec/http/aggregator/index.ts +236 -0
- package/src/codec/http/client/index.ts +120 -0
- package/src/codec/http/decoder/index.ts +4 -0
- package/src/codec/http/decoder/object/index.ts +644 -0
- package/src/codec/http/decoder/request/index.ts +49 -0
- package/src/codec/http/decoder/response/index.ts +59 -0
- package/src/codec/http/encoder/index.ts +3 -0
- package/src/codec/http/encoder/object/index.ts +196 -0
- package/src/codec/http/encoder/request/index.ts +24 -0
- package/src/codec/http/encoder/response/index.ts +26 -0
- package/src/codec/http/headers/index.ts +287 -0
- package/src/codec/http/index.ts +33 -0
- package/src/codec/http/message/content/index.ts +36 -0
- package/src/codec/http/message/full/index.ts +10 -0
- package/src/codec/http/message/full/request/index.ts +45 -0
- package/src/codec/http/message/full/response/index.ts +43 -0
- package/src/codec/http/message/head/index.ts +79 -0
- package/src/codec/http/message/index.ts +16 -0
- package/src/codec/http/message/last/index.ts +27 -0
- package/src/codec/http/message/request/index.ts +39 -0
- package/src/codec/http/message/response/index.ts +31 -0
- package/src/codec/http/method/index.ts +101 -0
- package/src/codec/http/server/index.ts +81 -0
- package/src/codec/http/status/index.ts +170 -0
- package/src/codec/http/version/index.ts +70 -0
- package/src/codec/index.ts +83 -0
- package/src/codec/length/index.ts +330 -0
- package/src/codec/line/index.ts +45 -0
- package/src/codec/message/decoder/index.ts +84 -0
- package/src/codec/message/encoder/index.ts +56 -0
- package/src/codec/message/index.ts +2 -0
- package/src/codec/msgpack/index.ts +51 -0
- package/src/codec/prepender/index.ts +296 -0
- package/src/codec/protobuf/index.ts +57 -0
- package/src/codec/string/decoder/index.ts +47 -0
- package/src/codec/string/encoder/index.ts +26 -0
- package/src/codec/string/index.ts +2 -0
- package/src/codec/websocket/client/index.ts +235 -0
- package/src/codec/websocket/decoder/index.ts +371 -0
- package/src/codec/websocket/encoder/index.ts +170 -0
- package/src/codec/websocket/error/index.ts +42 -0
- package/src/codec/websocket/frame/base/index.ts +55 -0
- package/src/codec/websocket/frame/binary/index.ts +24 -0
- package/src/codec/websocket/frame/close/index.ts +82 -0
- package/src/codec/websocket/frame/continuation/index.ts +31 -0
- package/src/codec/websocket/frame/index.ts +10 -0
- package/src/codec/websocket/frame/opcode/index.ts +35 -0
- package/src/codec/websocket/frame/ping/index.ts +24 -0
- package/src/codec/websocket/frame/pong/index.ts +24 -0
- package/src/codec/websocket/frame/text/index.ts +31 -0
- package/src/codec/websocket/handshake/index.ts +38 -0
- package/src/codec/websocket/index.ts +25 -0
- package/src/codec/websocket/key/index.ts +41 -0
- package/src/codec/websocket/protocol/index.ts +147 -0
- package/src/codec/websocket/server/index.ts +233 -0
- package/src/codec/websocket/status/index.ts +97 -0
- package/src/codec/websocket/upgrade/index.ts +116 -0
- package/src/datagram/bootstrap/index.ts +269 -0
- package/src/datagram/channel/index.ts +103 -0
- package/src/datagram/index.ts +10 -0
- package/src/datagram/packet/index.ts +72 -0
- package/src/datagram/transport/index.ts +239 -0
- package/src/handler/flush/index.ts +187 -0
- package/src/handler/idle/index.ts +295 -0
- package/src/handler/index.ts +22 -0
- package/src/handler/logging/index.ts +195 -0
- package/src/handler/metrics/index.ts +300 -0
- package/src/handler/offload/index.ts +141 -0
- package/src/handler/timeout/error/index.ts +29 -0
- package/src/handler/timeout/index.ts +3 -0
- package/src/handler/timeout/read/index.ts +44 -0
- package/src/handler/timeout/write/index.ts +89 -0
- package/src/handler/traffic/index.ts +259 -0
- package/src/index.ts +258 -0
- package/src/logger/index.ts +47 -0
- package/src/reliable/bootstrap/index.ts +154 -0
- package/src/reliable/cipher/index.ts +227 -0
- package/src/reliable/cookie/index.ts +78 -0
- package/src/reliable/handler/index.ts +1202 -0
- package/src/reliable/handshake/index.ts +144 -0
- package/src/reliable/index.ts +38 -0
- package/src/reliable/lane/index.ts +420 -0
- package/src/reliable/message/index.ts +102 -0
- package/src/reliable/packet/index.ts +117 -0
- package/src/reliable/peer/index.ts +929 -0
- package/src/reliable/registry/index.ts +107 -0
- package/src/serve/bootstrap/index.ts +606 -0
- package/src/serve/channel/index.ts +246 -0
- package/src/serve/index.ts +17 -0
- package/src/serve/outbox/index.ts +292 -0
- package/src/serve/transport/index.ts +96 -0
- package/src/serve/writer/index.ts +119 -0
- package/src/shard/envelope/index.ts +44 -0
- package/src/shard/index.ts +10 -0
- package/src/shard/link/index.ts +743 -0
- package/src/shard/map/index.ts +91 -0
- package/src/worker/group/index.ts +175 -0
- package/src/worker/index.ts +10 -0
- package/src/worker/pool/index.ts +235 -0
- package/src/worker/service/index.ts +55 -0
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
import type { HttpObject } from '../../message/index.ts'
|
|
2
|
+
import type { HandlerContext } from '../../../../channel/context/index.ts'
|
|
3
|
+
import { ByteToMessageDecoder } from '../../../decoder/index.ts'
|
|
4
|
+
import { DecoderError } from '../../../error/index.ts'
|
|
5
|
+
import { HttpHeaders } from '../../headers/index.ts'
|
|
6
|
+
import { HttpContent } from '../../message/content/index.ts'
|
|
7
|
+
import type { HttpMessage } from '../../message/head/index.ts'
|
|
8
|
+
import { LastHttpContent } from '../../message/last/index.ts'
|
|
9
|
+
import { HttpRequest } from '../../message/request/index.ts'
|
|
10
|
+
import type { ByteBuf } from '../../../../buffer/index.ts'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Size limits for HttpObjectDecoder
|
|
14
|
+
*/
|
|
15
|
+
export type HttpDecoderOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* Longest first line in bytes, 4096 by default
|
|
18
|
+
*/
|
|
19
|
+
readonly maxInitialLineLength?: number
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Header block cap in bytes, 8192 by default
|
|
23
|
+
*/
|
|
24
|
+
readonly maxHeaderSize?: number
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Biggest chunk size we'll accept, 8192 by default
|
|
28
|
+
*/
|
|
29
|
+
readonly maxChunkSize?: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Which part of the message the decoder expects next
|
|
34
|
+
*/
|
|
35
|
+
type DecoderState =
|
|
36
|
+
| 'initial'
|
|
37
|
+
| 'headers'
|
|
38
|
+
| 'fixed'
|
|
39
|
+
| 'chunkSize'
|
|
40
|
+
| 'chunkData'
|
|
41
|
+
| 'chunkEnd'
|
|
42
|
+
| 'trailers'
|
|
43
|
+
| 'untilClose'
|
|
44
|
+
| 'upgraded'
|
|
45
|
+
| 'bad'
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* How we'll know where the body ends
|
|
49
|
+
*/
|
|
50
|
+
type BodyMode = 'none' | 'fixed' | 'chunked' | 'close'
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Shared state machine for reading HTTP/1.x heads and bodies
|
|
54
|
+
* @class
|
|
55
|
+
*/
|
|
56
|
+
export abstract class HttpObjectDecoder extends ByteToMessageDecoder<HttpObject> {
|
|
57
|
+
/**
|
|
58
|
+
* 4 KiB
|
|
59
|
+
*/
|
|
60
|
+
public static readonly defaultMaxInitialLineLength: number = 4096
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 8 KiB
|
|
64
|
+
*/
|
|
65
|
+
public static readonly defaultMaxHeaderSize: number = 8192
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 8 KiB
|
|
69
|
+
*/
|
|
70
|
+
public static readonly defaultMaxChunkSize: number = 8192
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Cap on the request or status line
|
|
74
|
+
*/
|
|
75
|
+
private readonly maxInitialLineLength: number
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Cap on the header or trailer block
|
|
79
|
+
*/
|
|
80
|
+
private readonly maxHeaderSize: number
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Cap on any one chunk
|
|
84
|
+
*/
|
|
85
|
+
private readonly maxChunkSize: number
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Current state, bad once something has gone wrong
|
|
89
|
+
*/
|
|
90
|
+
private state: DecoderState
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Head of the message in progress, or null
|
|
94
|
+
*/
|
|
95
|
+
private message: HttpMessage | null
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Headers or trailers being filled in
|
|
99
|
+
*/
|
|
100
|
+
private headers: HttpHeaders
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Running size of the block, checked against the cap
|
|
104
|
+
*/
|
|
105
|
+
private headerSize: number
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Header that might still get folded lines, or null
|
|
109
|
+
*/
|
|
110
|
+
private pendingName: string | null
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Value so far, including any folded lines
|
|
114
|
+
*/
|
|
115
|
+
private pendingValue: string
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Bytes left in a fixed body or the current chunk
|
|
119
|
+
*/
|
|
120
|
+
private remaining: number
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Sets up a decoder with the given limits
|
|
124
|
+
* @param options defaults are used for anything left out
|
|
125
|
+
*/
|
|
126
|
+
public constructor(options: HttpDecoderOptions = {}) {
|
|
127
|
+
super()
|
|
128
|
+
|
|
129
|
+
const reason = HttpObjectDecoder.refusal(options)
|
|
130
|
+
if (reason !== null) throw new RangeError(reason)
|
|
131
|
+
|
|
132
|
+
this.maxInitialLineLength =
|
|
133
|
+
options.maxInitialLineLength ??
|
|
134
|
+
HttpObjectDecoder.defaultMaxInitialLineLength
|
|
135
|
+
this.maxHeaderSize =
|
|
136
|
+
options.maxHeaderSize ?? HttpObjectDecoder.defaultMaxHeaderSize
|
|
137
|
+
this.maxChunkSize =
|
|
138
|
+
options.maxChunkSize ?? HttpObjectDecoder.defaultMaxChunkSize
|
|
139
|
+
this.state = 'initial'
|
|
140
|
+
this.message = null
|
|
141
|
+
this.headers = new HttpHeaders()
|
|
142
|
+
this.headerSize = 0
|
|
143
|
+
this.pendingName = null
|
|
144
|
+
this.pendingValue = ''
|
|
145
|
+
this.remaining = 0
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Each limit has to be a positive whole number
|
|
150
|
+
* @param options what the caller passed in
|
|
151
|
+
*/
|
|
152
|
+
private static refusal(options: HttpDecoderOptions): string | null {
|
|
153
|
+
const limits = [
|
|
154
|
+
options.maxInitialLineLength,
|
|
155
|
+
options.maxHeaderSize,
|
|
156
|
+
options.maxChunkSize,
|
|
157
|
+
]
|
|
158
|
+
|
|
159
|
+
for (const limit of limits) {
|
|
160
|
+
if (limit === undefined) continue
|
|
161
|
+
if (!Number.isInteger(limit) || limit <= 0) return 'Limit is invalid'
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Wraps up a read until close body when the connection drops
|
|
169
|
+
* @param context where the last chunk or error goes
|
|
170
|
+
*/
|
|
171
|
+
public override async channelInactive(
|
|
172
|
+
context: HandlerContext,
|
|
173
|
+
): Promise<void> {
|
|
174
|
+
if (this.pendingBytes === 0) {
|
|
175
|
+
const ending = this.ending()
|
|
176
|
+
if (ending instanceof DecoderError) {
|
|
177
|
+
await context.fireExceptionCaught(ending)
|
|
178
|
+
} else if (ending !== null) {
|
|
179
|
+
await context.fireChannelRead(ending)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
await super.channelInactive(context)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Turns the first line into a request or response head
|
|
188
|
+
* @param line without the line ending
|
|
189
|
+
*/
|
|
190
|
+
protected abstract createMessage(line: string): HttpMessage
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* True when the head rules a body out, like a 204
|
|
194
|
+
* @param message head that has come in
|
|
195
|
+
*/
|
|
196
|
+
protected abstract isContentAlwaysEmpty(message: HttpMessage): boolean
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* True once the connection belongs to another protocol
|
|
200
|
+
* @param message head that has come in
|
|
201
|
+
*/
|
|
202
|
+
protected abstract isSwitchingProtocols(message: HttpMessage): boolean
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Moves the state machine on, giving up for good after any error
|
|
206
|
+
* @param context unused here
|
|
207
|
+
* @param input bytes gathered so far
|
|
208
|
+
* @param out heads and body chunks go here
|
|
209
|
+
*/
|
|
210
|
+
protected decode(
|
|
211
|
+
context: HandlerContext,
|
|
212
|
+
input: ByteBuf,
|
|
213
|
+
out: HttpObject[],
|
|
214
|
+
): void {
|
|
215
|
+
if (this.state === 'bad') {
|
|
216
|
+
input.skipBytes(input.readableBytes)
|
|
217
|
+
return
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
this.advance(input, out)
|
|
222
|
+
} catch (error) {
|
|
223
|
+
this.state = 'bad'
|
|
224
|
+
this.message = null
|
|
225
|
+
input.skipBytes(input.readableBytes)
|
|
226
|
+
throw error
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Drains the leftovers, then ends the message or reports it cut short
|
|
232
|
+
* @param context passed through to decode
|
|
233
|
+
* @param input whatever arrived before the close
|
|
234
|
+
* @param out heads and body chunks go here
|
|
235
|
+
*/
|
|
236
|
+
protected override decodeLast(
|
|
237
|
+
context: HandlerContext,
|
|
238
|
+
input: ByteBuf,
|
|
239
|
+
out: HttpObject[],
|
|
240
|
+
): void {
|
|
241
|
+
const before = input.readableBytes
|
|
242
|
+
|
|
243
|
+
this.decode(context, input, out)
|
|
244
|
+
const stalled = input.readableBytes === before && out.length === 0
|
|
245
|
+
if (input.isReadable() && !stalled) return
|
|
246
|
+
|
|
247
|
+
const ending = this.ending()
|
|
248
|
+
if (ending instanceof DecoderError) {
|
|
249
|
+
input.skipBytes(input.readableBytes)
|
|
250
|
+
throw ending
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (ending !== null) out.push(ending)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Last chunk for a read until close body, an error if we were mid message, or null
|
|
258
|
+
*/
|
|
259
|
+
private ending(): LastHttpContent | DecoderError | null {
|
|
260
|
+
switch (this.state) {
|
|
261
|
+
case 'untilClose':
|
|
262
|
+
this.finish()
|
|
263
|
+
return new LastHttpContent()
|
|
264
|
+
case 'initial':
|
|
265
|
+
case 'upgraded':
|
|
266
|
+
case 'bad':
|
|
267
|
+
return null
|
|
268
|
+
default:
|
|
269
|
+
this.state = 'bad'
|
|
270
|
+
this.message = null
|
|
271
|
+
return new DecoderError('Message is incomplete')
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Picks the reader for the current state
|
|
277
|
+
* @param input bytes gathered so far
|
|
278
|
+
* @param out heads and body chunks go here
|
|
279
|
+
*/
|
|
280
|
+
private advance(input: ByteBuf, out: HttpObject[]): void {
|
|
281
|
+
switch (this.state) {
|
|
282
|
+
case 'initial':
|
|
283
|
+
this.readInitialLine(input)
|
|
284
|
+
return
|
|
285
|
+
case 'headers':
|
|
286
|
+
case 'trailers':
|
|
287
|
+
this.readHeaderLine(input, out)
|
|
288
|
+
return
|
|
289
|
+
case 'fixed':
|
|
290
|
+
this.readFixed(input, out)
|
|
291
|
+
return
|
|
292
|
+
case 'chunkSize':
|
|
293
|
+
this.readChunkSize(input)
|
|
294
|
+
return
|
|
295
|
+
case 'chunkData':
|
|
296
|
+
this.readChunkData(input, out)
|
|
297
|
+
return
|
|
298
|
+
case 'chunkEnd':
|
|
299
|
+
this.readChunkEnd(input)
|
|
300
|
+
return
|
|
301
|
+
case 'untilClose':
|
|
302
|
+
this.readUntilClose(input, out)
|
|
303
|
+
return
|
|
304
|
+
case 'upgraded':
|
|
305
|
+
case 'bad':
|
|
306
|
+
return
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Reads the first line, skipping blank lines before it
|
|
312
|
+
* @param input bytes gathered so far
|
|
313
|
+
*/
|
|
314
|
+
private readInitialLine(input: ByteBuf): void {
|
|
315
|
+
const line = this.readLine(
|
|
316
|
+
input,
|
|
317
|
+
this.maxInitialLineLength,
|
|
318
|
+
'Line is too long',
|
|
319
|
+
)
|
|
320
|
+
if (line === null || line.trim() === '') return
|
|
321
|
+
|
|
322
|
+
const message = this.createMessage(line)
|
|
323
|
+
this.message = message
|
|
324
|
+
this.begin(message.headers, 'headers')
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Waits for the blank line, then parses every header in the block at once
|
|
329
|
+
* @param input bytes gathered so far
|
|
330
|
+
* @param out gets the head or the trailers
|
|
331
|
+
*/
|
|
332
|
+
private readHeaderLine(input: ByteBuf, out: HttpObject[]): void {
|
|
333
|
+
const limit = this.maxHeaderSize - this.headerSize
|
|
334
|
+
const end = HttpObjectDecoder.blockEnd(input)
|
|
335
|
+
if (end === -1) {
|
|
336
|
+
if (input.readableBytes > limit + 2) {
|
|
337
|
+
throw new DecoderError('Header is too large')
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const size = end - input.readerIndex
|
|
344
|
+
const lines = input.readString(size).split('\n')
|
|
345
|
+
for (let index = 0; index < lines.length - 1; index++) {
|
|
346
|
+
const raw = lines[index] ?? ''
|
|
347
|
+
const line = raw.endsWith('\r') ? raw.slice(0, -1) : raw
|
|
348
|
+
if (line.length > this.maxHeaderSize - this.headerSize) {
|
|
349
|
+
throw new DecoderError('Header is too large')
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
this.headerSize += raw.length + 1
|
|
353
|
+
|
|
354
|
+
if (line === '') {
|
|
355
|
+
this.commitHeader()
|
|
356
|
+
this.endHeaderBlock(out)
|
|
357
|
+
return
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (line.startsWith(' ') || line.startsWith('\t')) {
|
|
361
|
+
this.foldHeader(line)
|
|
362
|
+
} else {
|
|
363
|
+
this.commitHeader()
|
|
364
|
+
this.startHeader(line)
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Index right after the blank line, or -1 if it hasn't arrived
|
|
371
|
+
* @param input bytes to scan
|
|
372
|
+
*/
|
|
373
|
+
private static blockEnd(input: ByteBuf): number {
|
|
374
|
+
let start = input.readerIndex
|
|
375
|
+
|
|
376
|
+
for (;;) {
|
|
377
|
+
const newline = input.indexOf(0x0a, start)
|
|
378
|
+
if (newline === -1) return -1
|
|
379
|
+
const length = newline - start
|
|
380
|
+
if (length === 0 || (length === 1 && input.getUint8(start) === 0x0d)) {
|
|
381
|
+
return newline + 1
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
start = newline + 1
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Handles obsolete line folding by joining it onto the last header
|
|
390
|
+
* @param line continuation starting with a space or tab
|
|
391
|
+
*/
|
|
392
|
+
private foldHeader(line: string): void {
|
|
393
|
+
const value = line.trim()
|
|
394
|
+
if (this.pendingName === null) throw new DecoderError('Header is invalid')
|
|
395
|
+
if (!HttpHeaders.isValidValue(value)) {
|
|
396
|
+
throw new DecoderError('Header is invalid')
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
this.pendingValue = `${this.pendingValue} ${value}`
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Splits a name: value line and holds it in case folded lines follow
|
|
404
|
+
* @param line one header line
|
|
405
|
+
*/
|
|
406
|
+
private startHeader(line: string): void {
|
|
407
|
+
const colon = line.indexOf(':')
|
|
408
|
+
const name = line.slice(0, Math.max(colon, 0))
|
|
409
|
+
const value = line.slice(colon + 1).trim()
|
|
410
|
+
if (colon <= 0 || !HttpHeaders.isValidName(name)) {
|
|
411
|
+
throw new DecoderError('Header is invalid')
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (!HttpHeaders.isValidValue(value)) {
|
|
415
|
+
throw new DecoderError('Header is invalid')
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
this.pendingName = name
|
|
419
|
+
this.pendingValue = value
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Moves the pending header into the block
|
|
424
|
+
*/
|
|
425
|
+
private commitHeader(): void {
|
|
426
|
+
if (this.pendingName === null) return
|
|
427
|
+
|
|
428
|
+
this.headers.append(this.pendingName, this.pendingValue)
|
|
429
|
+
this.pendingName = null
|
|
430
|
+
this.pendingValue = ''
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Emits the head or trailers and picks how to read the body
|
|
435
|
+
* @param out where they go
|
|
436
|
+
*/
|
|
437
|
+
private endHeaderBlock(out: HttpObject[]): void {
|
|
438
|
+
if (this.state === 'trailers') {
|
|
439
|
+
out.push(new LastHttpContent(new Uint8Array(0), this.headers))
|
|
440
|
+
this.finish()
|
|
441
|
+
return
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const message = this.message
|
|
445
|
+
if (message === null) throw new DecoderError('Message is missing')
|
|
446
|
+
|
|
447
|
+
const mode = this.bodyMode(message)
|
|
448
|
+
out.push(message)
|
|
449
|
+
if (mode === 'fixed' && this.remaining > 0) {
|
|
450
|
+
this.state = 'fixed'
|
|
451
|
+
} else if (mode === 'chunked') {
|
|
452
|
+
this.state = 'chunkSize'
|
|
453
|
+
} else if (mode === 'close') {
|
|
454
|
+
this.state = 'untilClose'
|
|
455
|
+
} else {
|
|
456
|
+
out.push(new LastHttpContent())
|
|
457
|
+
this.finish()
|
|
458
|
+
if (this.isSwitchingProtocols(message)) this.state = 'upgraded'
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Chunked beats Content-Length, and responses with neither read until close
|
|
464
|
+
* @param message head to look at
|
|
465
|
+
*/
|
|
466
|
+
private bodyMode(message: HttpMessage): BodyMode {
|
|
467
|
+
if (this.isContentAlwaysEmpty(message)) return 'none'
|
|
468
|
+
if (message.isChunked()) {
|
|
469
|
+
message.headers.remove('content-length')
|
|
470
|
+
return 'chunked'
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (message.headers.has('transfer-encoding')) {
|
|
474
|
+
if (message instanceof HttpRequest) {
|
|
475
|
+
throw new DecoderError('Transfer encoding is unsupported')
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return 'close'
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const length = this.readContentLength(message)
|
|
482
|
+
if (length !== null) {
|
|
483
|
+
this.remaining = length
|
|
484
|
+
return 'fixed'
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
return message instanceof HttpRequest ? 'none' : 'close'
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Every Content-Length has to agree, or the message is refused
|
|
492
|
+
* @param message head to look at
|
|
493
|
+
*/
|
|
494
|
+
private readContentLength(message: HttpMessage): number | null {
|
|
495
|
+
const values = message.headers.tokens('content-length')
|
|
496
|
+
const first = values[0]
|
|
497
|
+
if (first === undefined) return null
|
|
498
|
+
|
|
499
|
+
const length = Number(first)
|
|
500
|
+
const valid = values.every(
|
|
501
|
+
(value: string): boolean => /^\d+$/.test(value) && value === first,
|
|
502
|
+
)
|
|
503
|
+
if (!valid || !Number.isSafeInteger(length)) {
|
|
504
|
+
throw new DecoderError('Content length is invalid')
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return length
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Passes on what has arrived, ending with LastHttpContent once it's all in
|
|
512
|
+
* @param input bytes gathered so far
|
|
513
|
+
* @param out body chunks go here
|
|
514
|
+
*/
|
|
515
|
+
private readFixed(input: ByteBuf, out: HttpObject[]): void {
|
|
516
|
+
const size = Math.min(this.remaining, input.readableBytes)
|
|
517
|
+
if (size === 0) return
|
|
518
|
+
|
|
519
|
+
const content = input.readSlice(size)
|
|
520
|
+
this.remaining -= size
|
|
521
|
+
if (this.remaining > 0) {
|
|
522
|
+
out.push(new HttpContent(content))
|
|
523
|
+
return
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
out.push(new LastHttpContent(content))
|
|
527
|
+
this.finish()
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Reads the hex size, dropping extensions, and moves on to trailers at zero
|
|
532
|
+
* @param input bytes gathered so far
|
|
533
|
+
*/
|
|
534
|
+
private readChunkSize(input: ByteBuf): void {
|
|
535
|
+
const line = this.readLine(
|
|
536
|
+
input,
|
|
537
|
+
this.maxInitialLineLength,
|
|
538
|
+
'Chunk size is invalid',
|
|
539
|
+
)
|
|
540
|
+
if (line === null) return
|
|
541
|
+
|
|
542
|
+
const text = (line.split(';')[0] ?? '').trim()
|
|
543
|
+
if (!/^[0-9a-fA-F]{1,13}$/.test(text)) {
|
|
544
|
+
throw new DecoderError('Chunk size is invalid')
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const size = Number.parseInt(text, 16)
|
|
548
|
+
if (size > this.maxChunkSize) throw new DecoderError('Chunk is too large')
|
|
549
|
+
|
|
550
|
+
if (size === 0) {
|
|
551
|
+
this.begin(new HttpHeaders(), 'trailers')
|
|
552
|
+
return
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
this.remaining = size
|
|
556
|
+
this.state = 'chunkData'
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Passes chunk bytes on as they arrive
|
|
561
|
+
* @param input bytes gathered so far
|
|
562
|
+
* @param out body chunks go here
|
|
563
|
+
*/
|
|
564
|
+
private readChunkData(input: ByteBuf, out: HttpObject[]): void {
|
|
565
|
+
const size = Math.min(this.remaining, input.readableBytes)
|
|
566
|
+
if (size === 0) return
|
|
567
|
+
|
|
568
|
+
out.push(new HttpContent(input.readSlice(size)))
|
|
569
|
+
this.remaining -= size
|
|
570
|
+
if (this.remaining === 0) this.state = 'chunkEnd'
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Expects an empty line straight after the chunk data
|
|
575
|
+
* @param input bytes gathered so far
|
|
576
|
+
*/
|
|
577
|
+
private readChunkEnd(input: ByteBuf): void {
|
|
578
|
+
const line = this.readLine(input, 0, 'Chunk is invalid')
|
|
579
|
+
if (line === null) return
|
|
580
|
+
if (line !== '') throw new DecoderError('Chunk is invalid')
|
|
581
|
+
|
|
582
|
+
this.state = 'chunkSize'
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Passes on everything as it comes in
|
|
587
|
+
* @param input bytes gathered so far
|
|
588
|
+
* @param out body chunks go here
|
|
589
|
+
*/
|
|
590
|
+
private readUntilClose(input: ByteBuf, out: HttpObject[]): void {
|
|
591
|
+
if (!input.isReadable()) return
|
|
592
|
+
|
|
593
|
+
out.push(new HttpContent(input.readSlice(input.readableBytes)))
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Resets the header state for a new block
|
|
598
|
+
* @param headers block to fill in
|
|
599
|
+
* @param state headers or trailers
|
|
600
|
+
*/
|
|
601
|
+
private begin(headers: HttpHeaders, state: 'headers' | 'trailers'): void {
|
|
602
|
+
this.headers = headers
|
|
603
|
+
this.headerSize = 0
|
|
604
|
+
this.pendingName = null
|
|
605
|
+
this.pendingValue = ''
|
|
606
|
+
this.state = state
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Back to waiting for a first line
|
|
611
|
+
*/
|
|
612
|
+
private finish(): void {
|
|
613
|
+
this.state = 'initial'
|
|
614
|
+
this.message = null
|
|
615
|
+
this.remaining = 0
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Reads up to LF, dropping an optional CR, or null if the line isn't complete
|
|
620
|
+
* @param input bytes gathered so far
|
|
621
|
+
* @param limit line length cap, not counting the ending
|
|
622
|
+
* @param error message to throw when it's over the cap
|
|
623
|
+
*/
|
|
624
|
+
private readLine(
|
|
625
|
+
input: ByteBuf,
|
|
626
|
+
limit: number,
|
|
627
|
+
error: string,
|
|
628
|
+
): string | null {
|
|
629
|
+
const end = input.indexOf(0x0a)
|
|
630
|
+
if (end === -1) {
|
|
631
|
+
if (input.readableBytes > limit + 1) throw new DecoderError(error)
|
|
632
|
+
return null
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
const length = end - input.readerIndex
|
|
636
|
+
const carriage = length > 0 && input.getUint8(end - 1) === 0x0d
|
|
637
|
+
const size = carriage ? length - 1 : length
|
|
638
|
+
if (size > limit) throw new DecoderError(error)
|
|
639
|
+
|
|
640
|
+
const line = input.readString(size)
|
|
641
|
+
input.skipBytes(length - size + 1)
|
|
642
|
+
return line
|
|
643
|
+
}
|
|
644
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DecoderError } from '../../../error/index.ts'
|
|
2
|
+
import { HttpMethod } from '../../method/index.ts'
|
|
3
|
+
import { HttpRequest } from '../../message/request/index.ts'
|
|
4
|
+
import { HttpVersion } from '../../version/index.ts'
|
|
5
|
+
import { HttpObjectDecoder } from '../object/index.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Server side decoder that reads HTTP/1.x requests
|
|
9
|
+
* @class
|
|
10
|
+
*/
|
|
11
|
+
export class HttpRequestDecoder extends HttpObjectDecoder {
|
|
12
|
+
/**
|
|
13
|
+
* Method, target and version separated by spaces or tabs
|
|
14
|
+
*/
|
|
15
|
+
private static readonly pattern: RegExp =
|
|
16
|
+
/^([!#$%&'*+.^_`|~0-9A-Za-z-]+)[ \t]+(\S+)[ \t]+(HTTP\/\d\.\d)[ \t]*$/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Builds a request head, refusing anything but HTTP/1.x
|
|
20
|
+
* @param line first line of the request
|
|
21
|
+
*/
|
|
22
|
+
protected createMessage(line: string): HttpRequest {
|
|
23
|
+
const match = HttpRequestDecoder.pattern.exec(line.trimStart())
|
|
24
|
+
const method = match?.[1]
|
|
25
|
+
const uri = match?.[2]
|
|
26
|
+
const version = HttpVersion.parse(match?.[3] ?? '')
|
|
27
|
+
if (method === undefined || uri === undefined) {
|
|
28
|
+
throw new DecoderError('Request line is invalid')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (version?.major !== 1) throw new DecoderError('Version is unsupported')
|
|
32
|
+
|
|
33
|
+
return new HttpRequest(HttpMethod.of(method), uri, version)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Requests never rule out a body up front
|
|
38
|
+
*/
|
|
39
|
+
protected isContentAlwaysEmpty(): boolean {
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Only a 101 response can switch protocols
|
|
45
|
+
*/
|
|
46
|
+
protected isSwitchingProtocols(): boolean {
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
}
|