@amritk/nish-aarch64-linux 0.49.1 → 0.51.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/INSTALL.md +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +237 -160
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +94 -11
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +14 -11
- package/std/base64.ts +74 -35
- package/std/bytes.ts +207 -97
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +213 -115
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +484 -149
- package/std/crypto/jwt.ts +51 -40
- package/std/crypto/p256.ts +14 -19
- package/std/crypto/pki-roots.ts +35 -12
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1635 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +212 -90
- package/std/crypto/sha512.ts +135 -49
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +236 -119
- package/std/json.ts +81 -46
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +224 -61
- package/std/net/handler.ts +189 -88
- package/std/net/hpack.ts +216 -103
- package/std/net/http-compress.ts +104 -42
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +326 -24
- package/std/net/http1-client-codec.ts +159 -55
- package/std/net/http1-codec.ts +427 -237
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +139 -60
- package/std/net/http3.ts +140 -79
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +97 -64
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +147 -40
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +61 -39
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +36 -27
- package/std/net/quic.ts +84 -54
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +86 -36
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +80 -35
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +194 -50
- package/std/net/upstream.ts +83 -38
- package/std/net/url.ts +170 -94
- package/std/net/websocket.ts +19 -20
- package/std/net/webtransport.ts +123 -33
- package/std/raster.ts +12 -2
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +176 -24
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +38 -16
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/net/multipart` — a `multipart/form-data` body read part by part, as
|
|
3
|
+
* an HTML form with a file input sends it (RFC 7578, framed by RFC 2046
|
|
4
|
+
* §5.1.1). It reads a body the program already holds, such as
|
|
5
|
+
* `HttpServerRequest.body`, and copies no part's data: each part answers
|
|
6
|
+
* where its data sits in that body.
|
|
7
|
+
*
|
|
8
|
+
* import { mediaTypeParse, mediaTypeParam } from "nish/net/http-fields";
|
|
9
|
+
* import { MultipartLimits, MultipartReader, MULTIPART_PART } from "nish/net/multipart";
|
|
10
|
+
*
|
|
11
|
+
* const type = mediaTypeParse(contentType, 0, toI32(contentType.length));
|
|
12
|
+
* const boundary = type === null ? null : mediaTypeParam(type, "boundary");
|
|
13
|
+
* if (boundary !== null) {
|
|
14
|
+
* const parts = new MultipartReader(req.body, req.bodyLength, boundary, new MultipartLimits());
|
|
15
|
+
* while (parts.next() === MULTIPART_PART) {
|
|
16
|
+
* // parts.name, parts.filename, parts.contentType,
|
|
17
|
+
* // req.body[parts.dataStart .. parts.dataStart + parts.dataLength)
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* **The framing.** A delimiter is CR LF, `--` and the boundary; the first may
|
|
22
|
+
* open the body with no CR LF before it. After an opening delimiter come
|
|
23
|
+
* optional spaces and tabs (RFC 2046's transport padding) and CR LF, then the
|
|
24
|
+
* part's header lines, an empty line and its data, which runs to the next
|
|
25
|
+
* delimiter. The close delimiter is a delimiter followed by `--`. What comes
|
|
26
|
+
* before the first delimiter (the preamble) and after the close one (the
|
|
27
|
+
* epilogue) is skipped. A run of `CR LF -- boundary` followed by anything
|
|
28
|
+
* else is data, not a delimiter, so a sender that picks a boundary its data
|
|
29
|
+
* holds still frames each part where it meant to.
|
|
30
|
+
*
|
|
31
|
+
* **What it refuses**, each with its own code, and once refused it answers
|
|
32
|
+
* that code from then on:
|
|
33
|
+
*
|
|
34
|
+
* - a boundary that is empty, longer than 70 octets, holds an octet outside
|
|
35
|
+
* RFC 2046's `bchars` or ends in a space (`MULTIPART_ERR_BOUNDARY`);
|
|
36
|
+
* - a body length below 0 or past the array (`MULTIPART_ERR_WINDOW`);
|
|
37
|
+
* - a body with no opening delimiter, a part whose header lines or data run
|
|
38
|
+
* to the end of the body, and so a body with no close delimiter
|
|
39
|
+
* (`MULTIPART_ERR_UNTERMINATED`);
|
|
40
|
+
* - a header line that ends in a CR or LF alone, starts with a space or tab
|
|
41
|
+
* (an obsolete folded line), has no colon, or whose name or value
|
|
42
|
+
* `httpFieldNameValid` or `httpFieldValueValid` refuses, and a
|
|
43
|
+
* `Content-Type` given twice or that `mediaTypeParse` refuses
|
|
44
|
+
* (`MULTIPART_ERR_HEADER`);
|
|
45
|
+
* - more header lines in one part than `maxHeaders`, or more octets of them
|
|
46
|
+
* than `maxHeaderBytes` (`MULTIPART_ERR_HEADERS`);
|
|
47
|
+
* - a part with no `Content-Disposition`, two of them, one that
|
|
48
|
+
* `formDataDispositionParse` refuses, one that is not `form-data` or one
|
|
49
|
+
* with no `name` parameter (`MULTIPART_ERR_DISPOSITION`);
|
|
50
|
+
* - more parts than `maxParts` (`MULTIPART_ERR_PARTS`).
|
|
51
|
+
*
|
|
52
|
+
* The `filename` parameter is read as given: a browser writes a `"`, CR or LF
|
|
53
|
+
* in it as `%22`, `%0D` or `%0A`, and those stay as they are. `filename*` is
|
|
54
|
+
* not read (RFC 7578 §4.2). A part's own `Content-Transfer-Encoding` is not
|
|
55
|
+
* applied (§4.7), and nothing is written to disk.
|
|
56
|
+
*
|
|
57
|
+
* **What it costs.** Finding a delimiter compares at most the boundary's
|
|
58
|
+
* length plus six octets at each CR of the body, and reads each run of
|
|
59
|
+
* transport padding once; a part's header lines are at most
|
|
60
|
+
* `maxHeaderBytes` octets, each read a fixed number of times, and each
|
|
61
|
+
* holds at most 16 parameters. So a body of `n` octets and a boundary of
|
|
62
|
+
* `b` costs about `n * (b + 6)` comparisons at most, however it is shaped.
|
|
63
|
+
*
|
|
64
|
+
* Private helpers share the importing program's flat symbol namespace
|
|
65
|
+
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
|
|
66
|
+
*
|
|
67
|
+
* Written from RFC 7578, RFC 2046 §5.1 and the HTML form submission
|
|
68
|
+
* algorithm, not ported from another implementation.
|
|
69
|
+
*/
|
|
70
|
+
import {
|
|
71
|
+
formDataDispositionParse,
|
|
72
|
+
httpFieldIs,
|
|
73
|
+
httpFieldNameValid,
|
|
74
|
+
httpFieldValueValid,
|
|
75
|
+
MediaType,
|
|
76
|
+
mediaTypeParam,
|
|
77
|
+
mediaTypeParse,
|
|
78
|
+
} from "nish/net/http-fields"
|
|
79
|
+
|
|
80
|
+
/** `MultipartReader.next`: another part was read; its fields describe it. */
|
|
81
|
+
export const MULTIPART_PART: i32 = 1
|
|
82
|
+
|
|
83
|
+
/** `MultipartReader.next`: the close delimiter was read; no part follows. */
|
|
84
|
+
export const MULTIPART_END: i32 = 0
|
|
85
|
+
|
|
86
|
+
/** The boundary is empty, longer than 70 octets, holds a non-`bchars` octet or ends in a space. */
|
|
87
|
+
export const MULTIPART_ERR_BOUNDARY: i32 = -1
|
|
88
|
+
|
|
89
|
+
/** The body has no opening delimiter, or ends inside a part's header lines or data. */
|
|
90
|
+
export const MULTIPART_ERR_UNTERMINATED: i32 = -2
|
|
91
|
+
|
|
92
|
+
/** A part's header line is malformed, or its `Content-Type` is repeated or malformed. */
|
|
93
|
+
export const MULTIPART_ERR_HEADER: i32 = -3
|
|
94
|
+
|
|
95
|
+
/** A part has more header lines than `maxHeaders` or more octets of them than `maxHeaderBytes`. */
|
|
96
|
+
export const MULTIPART_ERR_HEADERS: i32 = -4
|
|
97
|
+
|
|
98
|
+
/** A part's `Content-Disposition` is absent, repeated, not `form-data` or has no `name`. */
|
|
99
|
+
export const MULTIPART_ERR_DISPOSITION: i32 = -5
|
|
100
|
+
|
|
101
|
+
/** The body holds more parts than `maxParts`. */
|
|
102
|
+
export const MULTIPART_ERR_PARTS: i32 = -6
|
|
103
|
+
|
|
104
|
+
/** The body length given is below 0 or past the end of the array. */
|
|
105
|
+
export const MULTIPART_ERR_WINDOW: i32 = -7
|
|
106
|
+
|
|
107
|
+
/** The longest boundary RFC 2046 §5.1.1 allows. */
|
|
108
|
+
const MULTIPART_BOUNDARY_MAX: i32 = 70
|
|
109
|
+
|
|
110
|
+
/** `MultipartReader.kind`: the last delimiter found is not one. */
|
|
111
|
+
const MULTIPART_NONE: i32 = 0
|
|
112
|
+
|
|
113
|
+
/** `MultipartReader.kind`: the last delimiter found opens a part. */
|
|
114
|
+
const MULTIPART_OPEN: i32 = 1
|
|
115
|
+
|
|
116
|
+
/** `MultipartReader.kind`: the last delimiter found is the close delimiter. */
|
|
117
|
+
const MULTIPART_CLOSE: i32 = 2
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* How much one body may make a `MultipartReader` read. The defaults hold a
|
|
121
|
+
* browser's form with a few dozen fields; a server that takes larger forms
|
|
122
|
+
* raises them.
|
|
123
|
+
*/
|
|
124
|
+
export class MultipartLimits {
|
|
125
|
+
/** The most parts a body may hold; one more is `MULTIPART_ERR_PARTS`. */
|
|
126
|
+
maxParts: i32 = 64
|
|
127
|
+
/** The most octets of header lines one part may hold, each line's CR LF counted and the empty line not. */
|
|
128
|
+
maxHeaderBytes: i32 = 8192
|
|
129
|
+
/** The most header lines one part may hold; one more is `MULTIPART_ERR_HEADERS`. */
|
|
130
|
+
maxHeaders: i32 = 16
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether `c` is one of RFC 2046's `bchars`: a digit, a letter, `'()+_,-./:=?` or a space. */
|
|
134
|
+
const multipartBchar = (c: i32): boolean => {
|
|
135
|
+
if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
|
|
136
|
+
return true
|
|
137
|
+
}
|
|
138
|
+
switch (c) {
|
|
139
|
+
case 32: // space
|
|
140
|
+
case 39: // '
|
|
141
|
+
case 40: // (
|
|
142
|
+
case 41: // )
|
|
143
|
+
case 43: // +
|
|
144
|
+
case 44: // ,
|
|
145
|
+
case 45: // -
|
|
146
|
+
case 46: // .
|
|
147
|
+
case 47: // /
|
|
148
|
+
case 58: // :
|
|
149
|
+
case 61: // =
|
|
150
|
+
case 63: // ?
|
|
151
|
+
case 95: // _
|
|
152
|
+
return true
|
|
153
|
+
default:
|
|
154
|
+
return false
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The octets in `bytes[from .. to)` as a string, a byte at a time because
|
|
160
|
+
* `String.fromBytes` keeps a range check no proof removes; letters in
|
|
161
|
+
* lowercase when `lower`.
|
|
162
|
+
*/
|
|
163
|
+
const multipartString = (bytes: readonly u8[], from: i32, to: i32, lower: boolean): string => {
|
|
164
|
+
const parts: string[] = []
|
|
165
|
+
let i: i32 = from
|
|
166
|
+
while (i >= 0 && i < to && i < toI32(bytes.length)) {
|
|
167
|
+
const c: i32 = toI32(bytes[i])
|
|
168
|
+
parts.push(String.fromCharCode(lower && c >= 65 && c <= 90 ? c | 32 : c))
|
|
169
|
+
i = i + 1
|
|
170
|
+
}
|
|
171
|
+
return parts.join("")
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** A copy of `bytes[from .. to)`, letters in lowercase when `lower`. */
|
|
175
|
+
const multipartCopy = (bytes: readonly u8[], from: i32, to: i32, lower: boolean): u8[] => {
|
|
176
|
+
const out: u8[] = []
|
|
177
|
+
let i: i32 = from
|
|
178
|
+
while (i >= 0 && i < to && i < toI32(bytes.length)) {
|
|
179
|
+
const c: i32 = toI32(bytes[i])
|
|
180
|
+
out.push(lower && c >= 65 && c <= 90 ? toU8(c | 32) : toU8(c))
|
|
181
|
+
i = i + 1
|
|
182
|
+
}
|
|
183
|
+
return out
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Reads a `multipart/form-data` body one part at a time. Each `next()` that
|
|
188
|
+
* answers `MULTIPART_PART` sets `name`, `filename`, `contentType`,
|
|
189
|
+
* `dataStart` and `dataLength` for that part; the body is read in place and
|
|
190
|
+
* must not change while the reader is in use.
|
|
191
|
+
*/
|
|
192
|
+
export class MultipartReader {
|
|
193
|
+
/** The part's `name` parameter of `Content-Disposition`, as given. */
|
|
194
|
+
name: string = ""
|
|
195
|
+
/** The part's `filename` parameter as given, `""` when given empty, or null when absent. */
|
|
196
|
+
filename: string | null = null
|
|
197
|
+
/** The part's `Content-Type` value as given, or `text/plain` when it has none (RFC 7578 §4.4). */
|
|
198
|
+
contentType: string = "text/plain"
|
|
199
|
+
/** @internal The body the parts are read from. */
|
|
200
|
+
body: readonly u8[] = []
|
|
201
|
+
/** @internal CR LF, `--` and the boundary: what each delimiter starts with. */
|
|
202
|
+
delimiter: u8[] = []
|
|
203
|
+
/** Where the part's data starts in the body. */
|
|
204
|
+
dataStart: i32 = 0
|
|
205
|
+
/** How many octets of data the part holds. */
|
|
206
|
+
dataLength: i32 = 0
|
|
207
|
+
/** @internal How many octets of `body` are the message's. */
|
|
208
|
+
length: i32 = 0
|
|
209
|
+
/** @internal The most header lines one part may hold. */
|
|
210
|
+
maxHeaders: i32 = 0
|
|
211
|
+
/** @internal The most octets of header lines one part may hold. */
|
|
212
|
+
maxHeaderBytes: i32 = 0
|
|
213
|
+
/** @internal What the last delimiter found is: `MULTIPART_OPEN` or `MULTIPART_CLOSE`. */
|
|
214
|
+
kind: i32 = 0
|
|
215
|
+
/** @internal Where the octets after the last opening delimiter's line start. */
|
|
216
|
+
after: i32 = 0
|
|
217
|
+
/** @internal How many more parts `next` may answer. */
|
|
218
|
+
partsLeft: i32 = 0
|
|
219
|
+
/** @internal The code every later `next` answers, once one is refused or the body ends; 1 while reading. */
|
|
220
|
+
sticky: i32 = 1
|
|
221
|
+
/** @internal Whether the opening delimiter has been looked for. */
|
|
222
|
+
started: boolean = false
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* A reader over `body[0 .. length)`. A boundary or length it refuses is
|
|
226
|
+
* not reported here: the first `next()` answers it.
|
|
227
|
+
*
|
|
228
|
+
* @param body the octets of the message body
|
|
229
|
+
* @param length how many octets of `body` the body is, such as a request's
|
|
230
|
+
* `bodyLength`
|
|
231
|
+
* @param boundary the `boundary` parameter of the request's `Content-Type`,
|
|
232
|
+
* as `mediaTypeParam` answers it
|
|
233
|
+
* @param limits how many parts, header lines and header octets to read
|
|
234
|
+
*/
|
|
235
|
+
constructor(body: readonly u8[], length: i32, boundary: string, limits: MultipartLimits) {
|
|
236
|
+
this.body = body
|
|
237
|
+
this.partsLeft = limits.maxParts
|
|
238
|
+
this.maxHeaders = limits.maxHeaders
|
|
239
|
+
this.maxHeaderBytes = limits.maxHeaderBytes
|
|
240
|
+
const b: i32 = toI32(boundary.length)
|
|
241
|
+
if (length < 0 || length > toI32(body.length)) {
|
|
242
|
+
this.sticky = MULTIPART_ERR_WINDOW
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
this.length = length
|
|
246
|
+
if (b < 1 || b > MULTIPART_BOUNDARY_MAX || toI32(boundary.charCodeAt(b - 1)) === 32) {
|
|
247
|
+
this.sticky = MULTIPART_ERR_BOUNDARY
|
|
248
|
+
return
|
|
249
|
+
}
|
|
250
|
+
const delimiter: u8[] = [13, 10, 45, 45]
|
|
251
|
+
for (let k: i32 = 0; k < b; k++) {
|
|
252
|
+
const c: i32 = toI32(boundary.charCodeAt(k))
|
|
253
|
+
if (!multipartBchar(c)) {
|
|
254
|
+
this.sticky = MULTIPART_ERR_BOUNDARY
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
delimiter.push(toU8(c))
|
|
258
|
+
}
|
|
259
|
+
this.delimiter = delimiter
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Reads the next part.
|
|
264
|
+
*
|
|
265
|
+
* @returns `MULTIPART_PART` when a part was read, its fields now
|
|
266
|
+
* describing it; `MULTIPART_END` once the close delimiter is read, and
|
|
267
|
+
* from then on; or a negative `MULTIPART_ERR_*` code, which every later
|
|
268
|
+
* call answers too
|
|
269
|
+
*/
|
|
270
|
+
next(): i32 {
|
|
271
|
+
if (this.sticky !== MULTIPART_PART) {
|
|
272
|
+
return this.sticky
|
|
273
|
+
}
|
|
274
|
+
if (!this.started) {
|
|
275
|
+
this.started = true
|
|
276
|
+
// The first delimiter may open the body, with no CR LF before it.
|
|
277
|
+
this.kind = this.delimiterAt(toI32(0), toI32(2))
|
|
278
|
+
if (this.kind === MULTIPART_NONE && this.find(toI32(0)) < 0) {
|
|
279
|
+
return this.fail(MULTIPART_ERR_UNTERMINATED)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (this.kind === MULTIPART_CLOSE) {
|
|
283
|
+
return this.fail(MULTIPART_END)
|
|
284
|
+
}
|
|
285
|
+
const left: i32 = this.partsLeft
|
|
286
|
+
if (left <= 0) {
|
|
287
|
+
return this.fail(MULTIPART_ERR_PARTS)
|
|
288
|
+
}
|
|
289
|
+
const read: i32 = this.readHeaders(this.after)
|
|
290
|
+
if (read !== MULTIPART_PART) {
|
|
291
|
+
return this.fail(read)
|
|
292
|
+
}
|
|
293
|
+
const start: i32 = this.dataStart
|
|
294
|
+
const end: i32 = this.find(start)
|
|
295
|
+
if (start < 0 || end < start) {
|
|
296
|
+
return this.fail(MULTIPART_ERR_UNTERMINATED)
|
|
297
|
+
}
|
|
298
|
+
this.dataLength = end - start
|
|
299
|
+
this.partsLeft = left - 1
|
|
300
|
+
return MULTIPART_PART
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** @internal Records `code` as what every later `next` answers, and answers it. */
|
|
304
|
+
fail(code: i32): i32 {
|
|
305
|
+
this.sticky = code
|
|
306
|
+
this.dataStart = 0
|
|
307
|
+
this.dataLength = 0
|
|
308
|
+
return code
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @internal Whether `body[at ..]` holds `delimiter[skip ..]` followed by
|
|
313
|
+
* `--` (`MULTIPART_CLOSE`) or by spaces and tabs and CR LF
|
|
314
|
+
* (`MULTIPART_OPEN`, `after` set past the LF); `MULTIPART_NONE` otherwise.
|
|
315
|
+
*/
|
|
316
|
+
delimiterAt(at: i32, skip: i32): i32 {
|
|
317
|
+
const body: readonly u8[] = this.body
|
|
318
|
+
const delimiter: u8[] = this.delimiter
|
|
319
|
+
const length: i32 = this.length
|
|
320
|
+
let i: i32 = at
|
|
321
|
+
let k: i32 = skip
|
|
322
|
+
while (k >= 0 && k < toI32(delimiter.length)) {
|
|
323
|
+
if (i < 0 || i >= length || i >= toI32(body.length) || body[i] !== delimiter[k]) {
|
|
324
|
+
return MULTIPART_NONE
|
|
325
|
+
}
|
|
326
|
+
i = i + 1
|
|
327
|
+
k = k + 1
|
|
328
|
+
}
|
|
329
|
+
if (i >= 0 && i < length && i < toI32(body.length) && toI32(body[i]) === 45) {
|
|
330
|
+
const j: i32 = i + 1
|
|
331
|
+
return j < length && j < toI32(body.length) && toI32(body[j]) === 45 ? MULTIPART_CLOSE : MULTIPART_NONE
|
|
332
|
+
}
|
|
333
|
+
while (
|
|
334
|
+
i >= 0 &&
|
|
335
|
+
i < length &&
|
|
336
|
+
i < toI32(body.length) &&
|
|
337
|
+
(toI32(body[i]) === 32 || toI32(body[i]) === 9)
|
|
338
|
+
) {
|
|
339
|
+
i = i + 1
|
|
340
|
+
}
|
|
341
|
+
if (i < 0 || i >= length || i >= toI32(body.length) || toI32(body[i]) !== 13) {
|
|
342
|
+
return MULTIPART_NONE
|
|
343
|
+
}
|
|
344
|
+
const lf: i32 = i + 1
|
|
345
|
+
if (lf >= length || lf >= toI32(body.length) || toI32(body[lf]) !== 10) {
|
|
346
|
+
return MULTIPART_NONE
|
|
347
|
+
}
|
|
348
|
+
this.after = lf + 1
|
|
349
|
+
return MULTIPART_OPEN
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* @internal The first index from `from` where a delimiter starts, its kind
|
|
354
|
+
* in `kind`; -1 when the body holds none.
|
|
355
|
+
*/
|
|
356
|
+
find(from: i32): i32 {
|
|
357
|
+
const body: readonly u8[] = this.body
|
|
358
|
+
const length: i32 = this.length
|
|
359
|
+
let p: i32 = from
|
|
360
|
+
while (p >= 0 && p < length && p < toI32(body.length)) {
|
|
361
|
+
if (toI32(body[p]) === 13) {
|
|
362
|
+
const kind: i32 = this.delimiterAt(p, toI32(0))
|
|
363
|
+
if (kind !== MULTIPART_NONE) {
|
|
364
|
+
this.kind = kind
|
|
365
|
+
return p
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
p = p + 1
|
|
369
|
+
}
|
|
370
|
+
return -1
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* @internal Reads the header lines that start at `at` and the empty line
|
|
375
|
+
* after them, setting `name`, `filename`, `contentType` and `dataStart`.
|
|
376
|
+
* Answers `MULTIPART_PART` or the code that refuses them.
|
|
377
|
+
*/
|
|
378
|
+
readHeaders(at: i32): i32 {
|
|
379
|
+
const body: readonly u8[] = this.body
|
|
380
|
+
const length: i32 = this.length
|
|
381
|
+
this.name = ""
|
|
382
|
+
this.filename = null
|
|
383
|
+
this.contentType = "text/plain"
|
|
384
|
+
let disposition: boolean = false
|
|
385
|
+
let typed: boolean = false
|
|
386
|
+
// What the caps leave, counted down so that no sum can wrap.
|
|
387
|
+
let linesLeft: i32 = this.maxHeaders
|
|
388
|
+
let room: i32 = this.maxHeaderBytes
|
|
389
|
+
let i: i32 = at
|
|
390
|
+
while (i >= 0 && i < length && i < toI32(body.length)) {
|
|
391
|
+
// The line runs to the first CR or LF, each octet of it counted.
|
|
392
|
+
let e: i32 = i
|
|
393
|
+
while (
|
|
394
|
+
e >= 0 &&
|
|
395
|
+
e < length &&
|
|
396
|
+
e < toI32(body.length) &&
|
|
397
|
+
toI32(body[e]) !== 13 &&
|
|
398
|
+
toI32(body[e]) !== 10
|
|
399
|
+
) {
|
|
400
|
+
if (room <= 0) {
|
|
401
|
+
return MULTIPART_ERR_HEADERS
|
|
402
|
+
}
|
|
403
|
+
room = room - 1
|
|
404
|
+
e = e + 1
|
|
405
|
+
}
|
|
406
|
+
if (e < 0 || e >= length || e >= toI32(body.length)) {
|
|
407
|
+
return MULTIPART_ERR_UNTERMINATED
|
|
408
|
+
}
|
|
409
|
+
const lf: i32 = e + 1
|
|
410
|
+
if (toI32(body[e]) !== 13) {
|
|
411
|
+
return MULTIPART_ERR_HEADER
|
|
412
|
+
}
|
|
413
|
+
// A body cut after the CR has not said what comes next.
|
|
414
|
+
if (lf >= length || lf >= toI32(body.length)) {
|
|
415
|
+
return MULTIPART_ERR_UNTERMINATED
|
|
416
|
+
}
|
|
417
|
+
if (toI32(body[lf]) !== 10) {
|
|
418
|
+
return MULTIPART_ERR_HEADER
|
|
419
|
+
}
|
|
420
|
+
if (e === i) {
|
|
421
|
+
if (!disposition) {
|
|
422
|
+
return MULTIPART_ERR_DISPOSITION
|
|
423
|
+
}
|
|
424
|
+
this.dataStart = lf + 1
|
|
425
|
+
return MULTIPART_PART
|
|
426
|
+
}
|
|
427
|
+
if (linesLeft <= 0 || room < 2) {
|
|
428
|
+
return MULTIPART_ERR_HEADERS
|
|
429
|
+
}
|
|
430
|
+
linesLeft = linesLeft - 1
|
|
431
|
+
room = room - 2
|
|
432
|
+
const line: i32 = this.readHeader(i, e, disposition, typed)
|
|
433
|
+
if (line < 0) {
|
|
434
|
+
return line
|
|
435
|
+
}
|
|
436
|
+
disposition = disposition || line === 1
|
|
437
|
+
typed = typed || line === 2
|
|
438
|
+
i = lf + 1
|
|
439
|
+
}
|
|
440
|
+
return MULTIPART_ERR_UNTERMINATED
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* @internal Reads the header line `body[from .. to)`, which holds no CR or
|
|
445
|
+
* LF. Answers 1 for a `Content-Disposition`, 2 for a `Content-Type`, 0 for
|
|
446
|
+
* another field, or the code that refuses it.
|
|
447
|
+
*/
|
|
448
|
+
readHeader(from: i32, to: i32, disposition: boolean, typed: boolean): i32 {
|
|
449
|
+
const body: readonly u8[] = this.body
|
|
450
|
+
if (from < 0 || from >= to || from >= toI32(body.length)) {
|
|
451
|
+
return MULTIPART_ERR_HEADER
|
|
452
|
+
}
|
|
453
|
+
// An obsolete folded line starts with a space or tab, so the name before
|
|
454
|
+
// its colon is not a token and `httpFieldNameValid` refuses it below.
|
|
455
|
+
let colon: i32 = from
|
|
456
|
+
while (colon >= 0 && colon < to && colon < toI32(body.length) && toI32(body[colon]) !== 58) {
|
|
457
|
+
colon = colon + 1
|
|
458
|
+
}
|
|
459
|
+
if (colon >= to) {
|
|
460
|
+
return MULTIPART_ERR_HEADER
|
|
461
|
+
}
|
|
462
|
+
const name: u8[] = multipartCopy(body, from, colon, true)
|
|
463
|
+
// A value's optional whitespace at either end is not part of it.
|
|
464
|
+
let vs: i32 = colon + 1
|
|
465
|
+
while (
|
|
466
|
+
vs >= 0 &&
|
|
467
|
+
vs < to &&
|
|
468
|
+
vs < toI32(body.length) &&
|
|
469
|
+
(toI32(body[vs]) === 32 || toI32(body[vs]) === 9)
|
|
470
|
+
) {
|
|
471
|
+
vs = vs + 1
|
|
472
|
+
}
|
|
473
|
+
let ve: i32 = to
|
|
474
|
+
let trimming: boolean = true
|
|
475
|
+
while (trimming && ve > 0 && ve > vs) {
|
|
476
|
+
const k: i32 = ve - 1
|
|
477
|
+
trimming = k >= 0 && k < toI32(body.length) && (toI32(body[k]) === 32 || toI32(body[k]) === 9)
|
|
478
|
+
if (trimming) {
|
|
479
|
+
ve = k
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (!httpFieldNameValid(name) || !httpFieldValueValid(multipartCopy(body, vs, ve, false))) {
|
|
483
|
+
return MULTIPART_ERR_HEADER
|
|
484
|
+
}
|
|
485
|
+
if (httpFieldIs(name, "content-disposition")) {
|
|
486
|
+
const d: MediaType | null = disposition ? null : formDataDispositionParse(body, vs, ve)
|
|
487
|
+
if (d === null || d.type !== "form-data") {
|
|
488
|
+
return MULTIPART_ERR_DISPOSITION
|
|
489
|
+
}
|
|
490
|
+
const field: string | null = mediaTypeParam(d, "name")
|
|
491
|
+
if (field === null) {
|
|
492
|
+
return MULTIPART_ERR_DISPOSITION
|
|
493
|
+
}
|
|
494
|
+
this.name = field
|
|
495
|
+
this.filename = mediaTypeParam(d, "filename")
|
|
496
|
+
return 1
|
|
497
|
+
}
|
|
498
|
+
if (httpFieldIs(name, "content-type")) {
|
|
499
|
+
if (typed || mediaTypeParse(body, vs, ve) === null) {
|
|
500
|
+
return MULTIPART_ERR_HEADER
|
|
501
|
+
}
|
|
502
|
+
this.contentType = multipartString(body, vs, ve, false)
|
|
503
|
+
return 2
|
|
504
|
+
}
|
|
505
|
+
return 0
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* How many parts a whole `multipart/form-data` body holds, read with the
|
|
511
|
+
* default `MultipartLimits`: a check that it frames, without reading it
|
|
512
|
+
* part by part.
|
|
513
|
+
*
|
|
514
|
+
* @param body the octets of the message body, all of them the body's
|
|
515
|
+
* @param boundary the `boundary` parameter of the request's `Content-Type`
|
|
516
|
+
* @returns the number of parts once the close delimiter is read, or the
|
|
517
|
+
* negative `MULTIPART_ERR_*` code `MultipartReader.next` refuses the body
|
|
518
|
+
* with
|
|
519
|
+
*/
|
|
520
|
+
export const multipartPartCount = (body: u8[], boundary: string): i32 => {
|
|
521
|
+
const parts: MultipartReader = new MultipartReader(
|
|
522
|
+
body,
|
|
523
|
+
toI32(body.length),
|
|
524
|
+
boundary,
|
|
525
|
+
new MultipartLimits()
|
|
526
|
+
)
|
|
527
|
+
let count: i32 = 0
|
|
528
|
+
let code: i32 = parts.next()
|
|
529
|
+
while (code === MULTIPART_PART && count < 2147483647) {
|
|
530
|
+
count = count + 1
|
|
531
|
+
code = parts.next()
|
|
532
|
+
}
|
|
533
|
+
return code === MULTIPART_END ? count : code
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* The data of the first part named `name` in a whole `multipart/form-data`
|
|
538
|
+
* body, read with the default `MultipartLimits`: a form's one text field
|
|
539
|
+
* without a loop over its parts.
|
|
540
|
+
*
|
|
541
|
+
* @param body the octets of the message body, all of them the body's
|
|
542
|
+
* @param boundary the `boundary` parameter of the request's `Content-Type`
|
|
543
|
+
* @param name the field name, compared octet for octet with each part's
|
|
544
|
+
* `name` parameter
|
|
545
|
+
* @returns a copy of that part's data, or null when no part before the
|
|
546
|
+
* close delimiter has that name or `MultipartReader.next` refuses the body
|
|
547
|
+
* before one does
|
|
548
|
+
*/
|
|
549
|
+
export const multipartFieldData = (body: u8[], boundary: string, name: string): u8[] | null => {
|
|
550
|
+
const parts: MultipartReader = new MultipartReader(
|
|
551
|
+
body,
|
|
552
|
+
toI32(body.length),
|
|
553
|
+
boundary,
|
|
554
|
+
new MultipartLimits()
|
|
555
|
+
)
|
|
556
|
+
while (parts.next() === MULTIPART_PART) {
|
|
557
|
+
const start: i32 = parts.dataStart
|
|
558
|
+
const length: i32 = parts.dataLength
|
|
559
|
+
const end: i64 = toI64(start) + toI64(length)
|
|
560
|
+
if (parts.name === name && start >= 0 && length >= 0 && end <= toI64(body.length)) {
|
|
561
|
+
return multipartCopy(body, start, toI32(end), false)
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return null
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The `filename` parameter of the first part named `name` in a whole
|
|
569
|
+
* `multipart/form-data` body, as `MultipartReader.filename` reads it.
|
|
570
|
+
*
|
|
571
|
+
* @param body the octets of the message body, all of them the body's
|
|
572
|
+
* @param boundary the `boundary` parameter of the request's `Content-Type`
|
|
573
|
+
* @param name the field name, compared octet for octet with each part's
|
|
574
|
+
* `name` parameter
|
|
575
|
+
* @returns the file name as given, quotes removed, or null when that part
|
|
576
|
+
* has none, no part before the close delimiter has that name, or
|
|
577
|
+
* `MultipartReader.next` refuses the body before one does
|
|
578
|
+
*/
|
|
579
|
+
export const multipartFieldFilename = (body: u8[], boundary: string, name: string): string | null => {
|
|
580
|
+
const parts: MultipartReader = new MultipartReader(
|
|
581
|
+
body,
|
|
582
|
+
toI32(body.length),
|
|
583
|
+
boundary,
|
|
584
|
+
new MultipartLimits()
|
|
585
|
+
)
|
|
586
|
+
while (parts.next() === MULTIPART_PART) {
|
|
587
|
+
if (parts.name === name) {
|
|
588
|
+
return parts.filename
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return null
|
|
592
|
+
}
|
package/std/net/outbound.ts
CHANGED
|
@@ -215,7 +215,8 @@ export const outboundFilter = (policy: OutboundPolicy, addresses: u8[], count: i
|
|
|
215
215
|
// Where the next kept record goes, and where the record checked now starts.
|
|
216
216
|
let keptAt: i32 = 0
|
|
217
217
|
let at: i32 = 0
|
|
218
|
-
|
|
218
|
+
const n: i32 = toI32(addresses.length)
|
|
219
|
+
for (let i: i32 = 0; i < count && at <= n - DNS_ADDRESS_BYTES; i += 1) {
|
|
219
220
|
if (outboundAddressAllowed(policy, addresses, at)) {
|
|
220
221
|
// Swap, so the refused record stays in the list for an error to name.
|
|
221
222
|
// Both records fit (`keptAt` is at most `at`), so no copy can refuse.
|
|
@@ -229,11 +230,20 @@ export const outboundFilter = (policy: OutboundPolicy, addresses: u8[], count: i
|
|
|
229
230
|
) {
|
|
230
231
|
break
|
|
231
232
|
}
|
|
232
|
-
//
|
|
233
|
-
//
|
|
234
|
-
|
|
233
|
+
// `keptAt` is at most `at`, which is at most the length less a record,
|
|
234
|
+
// so the step always fits; the test restates that for the bounds
|
|
235
|
+
// proof, with `DNS_ADDRESS_BYTES` (18) spelled as a literal it reads.
|
|
236
|
+
if (keptAt < 0 || keptAt > n - 18) {
|
|
237
|
+
break
|
|
238
|
+
}
|
|
239
|
+
keptAt = keptAt + 18
|
|
235
240
|
}
|
|
236
|
-
|
|
241
|
+
// The loop test already keeps `at` at most the length less a record;
|
|
242
|
+
// restated with the literal for the same proof.
|
|
243
|
+
if (at < 0 || at > n - 18) {
|
|
244
|
+
break
|
|
245
|
+
}
|
|
246
|
+
at = at + 18
|
|
237
247
|
}
|
|
238
248
|
return keptAt / DNS_ADDRESS_BYTES
|
|
239
249
|
}
|
|
@@ -354,13 +364,19 @@ const outboundPublic = (a: u8[]): boolean => {
|
|
|
354
364
|
const outboundPermitted = (policy: OutboundPolicy, a: u8[]): boolean => {
|
|
355
365
|
const p: u8[] = policy.permitted
|
|
356
366
|
const entry: u8[] = new Array<u8>(OUTBOUND_PERMIT_BYTES)
|
|
367
|
+
const n: i32 = toI32(p.length)
|
|
357
368
|
let at: i32 = 0
|
|
358
|
-
while (at >= 0 && at <=
|
|
369
|
+
while (at >= 0 && at <= n - OUTBOUND_PERMIT_BYTES) {
|
|
359
370
|
if (tryCopyBytes(entry, 0, p, at, OUTBOUND_PERMIT_BYTES) && outboundPrefixMatch(entry, a)) {
|
|
360
371
|
return true
|
|
361
372
|
}
|
|
362
|
-
// At most the length less a permit before, so the sum fits
|
|
363
|
-
|
|
373
|
+
// At most the length less a permit before, so the sum fits; the test
|
|
374
|
+
// restates that for the bounds proof, with `OUTBOUND_PERMIT_BYTES` (17)
|
|
375
|
+
// spelled as a literal it reads.
|
|
376
|
+
if (at > n - 17) {
|
|
377
|
+
break
|
|
378
|
+
}
|
|
379
|
+
at = at + 17
|
|
364
380
|
}
|
|
365
381
|
return false
|
|
366
382
|
}
|