@amritk/nish-aarch64-linux 0.37.0 → 0.39.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.
Files changed (73) hide show
  1. package/INSTALL.md +28 -16
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +380 -47
  4. package/package.json +1 -1
  5. package/runtime/core/arrays.d.ts +4 -0
  6. package/runtime/core/arrays.mjs +33 -0
  7. package/runtime/core/host.d.ts +11 -0
  8. package/runtime/core/host.mjs +35 -0
  9. package/runtime/nish.d.ts +645 -66
  10. package/runtime/nish.h +101 -15
  11. package/runtime/nish.mjs +69 -6
  12. package/runtime/node.mjs +117 -0
  13. package/runtime/runtime-aes.c +12 -4
  14. package/runtime/runtime-async.c +267 -51
  15. package/runtime/runtime-checksum.c +207 -15
  16. package/runtime/runtime-cross.c +2 -1
  17. package/runtime/runtime-net.c +32 -3
  18. package/runtime/runtime-os.c +174 -27
  19. package/runtime/runtime-region.c +170 -8
  20. package/runtime/runtime-sandbox.c +377 -17
  21. package/runtime/runtime-sha256.c +11 -4
  22. package/runtime/runtime-text.c +1068 -0
  23. package/runtime/runtime-wasm.c +18 -3
  24. package/runtime/runtime.c +55 -961
  25. package/runtime/vite.mjs +48 -11
  26. package/scripts/build.sh +113 -36
  27. package/std/LICENSE-fdlibm +8 -0
  28. package/std/README.md +7 -4
  29. package/std/array.ts +266 -0
  30. package/std/compress/checksum.ts +13 -0
  31. package/std/compress/deflate.ts +0 -2
  32. package/std/crypto/p256.ts +247 -74
  33. package/std/crypto/p384.ts +3 -1
  34. package/std/crypto/pki.ts +35 -9
  35. package/std/crypto/x25519.ts +153 -64
  36. package/std/crypto/x509.ts +113 -10
  37. package/std/host/text.ts +86 -0
  38. package/std/json-tokenizer.ts +20 -0
  39. package/std/json-typed.ts +217 -8
  40. package/std/math.ts +743 -41
  41. package/std/net/dns-resolver.ts +46 -6
  42. package/std/net/dns.ts +50 -13
  43. package/std/net/fetch.ts +691 -129
  44. package/std/net/handler.ts +142 -16
  45. package/std/net/http-compress.ts +17 -9
  46. package/std/net/http-fields.ts +19 -4
  47. package/std/net/http1-codec.ts +517 -154
  48. package/std/net/http1-server.ts +508 -25
  49. package/std/net/http2-cleartext.ts +526 -0
  50. package/std/net/http2-tls.ts +48 -0
  51. package/std/net/http2.ts +84 -0
  52. package/std/net/postgres-async.ts +2223 -0
  53. package/std/net/postgres-carrier.ts +343 -10
  54. package/std/net/postgres.ts +129 -11
  55. package/std/net/prefork.ts +195 -11
  56. package/std/net/quic-datagram.ts +30 -0
  57. package/std/net/quic-listener.ts +38 -12
  58. package/std/net/quic-packet.ts +19 -6
  59. package/std/net/quic-recovery.ts +134 -19
  60. package/std/net/quic-stream.ts +174 -36
  61. package/std/net/quic.ts +418 -18
  62. package/std/net/router.ts +39 -17
  63. package/std/net/serve.ts +867 -0
  64. package/std/net/tls/codec.ts +110 -32
  65. package/std/net/tls/record-server.ts +37 -7
  66. package/std/net/tls-tcp.ts +56 -1
  67. package/std/net/tls.ts +196 -50
  68. package/std/net/websocket.ts +55 -18
  69. package/std/net/webtransport.ts +179 -1
  70. package/std/regex.ts +69 -47
  71. package/std/region.ts +62 -11
  72. package/std/secret.ts +26 -59
  73. package/std/text.ts +464 -5
@@ -182,8 +182,8 @@ export class Http1Parser {
182
182
  // `buffered`, `http1TakeLine` and the head's lines move, then the flags,
183
183
  // then the rest of the request's numbers and the caller's limits. What only
184
184
  // `keepText`, an error or a chunked body reads follows. Each group packs as
185
- // widest first does, and the flags' two bytes of padding are the only ones:
186
- // the struct is the 200 bytes its fields sum to, rounded to 8.
185
+ // widest first does, and the flags' two bytes of padding are the only ones
186
+ // inside it: its fields sum to 228 bytes, which round to 232.
187
187
 
188
188
  // --- the request's buffers -------------------------------------------------
189
189
  /**
@@ -194,10 +194,14 @@ export class Http1Parser {
194
194
  */
195
195
  buf: u8[]
196
196
  /**
197
- * The request line and the kept field lines of the request, copied out of
197
+ * The request line (method to version, no CRLF) and then the header
198
+ * section's lines with their CRLFs, the closing CRLF last, copied out of
198
199
  * `buf` as each is read, names in lowercase: what the spans below index.
199
- * Kept from one request to the next, so a slot that has seen its largest
200
- * head allocates nothing for the next one.
200
+ * A field's line runs from its name's start to its value's end, any OWS
201
+ * around the value, and a CRLF; nothing else is in the head. The bytes from
202
+ * `headLength` on are slack: the section scan stores a line's first bytes
203
+ * there before the line is whole. Kept from one request to the next, so a
204
+ * slot that has seen its largest head allocates nothing for the next one.
201
205
  */
202
206
  head: u8[]
203
207
  /** Four entries per field, into `head`: name start and end, value start and end (OWS trimmed). */
@@ -245,7 +249,10 @@ export class Http1Parser {
245
249
  lineEnd: i32 = 0
246
250
  /** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
247
251
  headerCount: i32 = 0
248
- /** How much of `head` the request uses. */
252
+ /**
253
+ * How much of `head` the request uses: the request line, each kept field
254
+ * line and its CRLF, and the closing CRLF, so `headerCount + 1` CRLFs.
255
+ */
249
256
  headLength: i32 = 0
250
257
  /**
251
258
  * Whether the line being scanned holds a byte a field value may not: a
@@ -289,6 +296,41 @@ export class Http1Parser {
289
296
  * @internal Read and written by the parser's free functions in this module to enforce the 431 limit.
290
297
  */
291
298
  fieldCount: i32 = 0
299
+ /**
300
+ * Host fields the request's head has kept so far.
301
+ * @internal Counted by the field-line parser in this module as each name is lowered, and read by `http1EndOfHead`.
302
+ */
303
+ hostFields: i32 = 0
304
+ /**
305
+ * Content-Length fields the request's head has kept so far.
306
+ * @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
307
+ */
308
+ lengthFields: i32 = 0
309
+ /**
310
+ * The index of the last Content-Length field, or -1.
311
+ * @internal Set by the field-line parser in this module, and read by `http1EndOfHead`.
312
+ */
313
+ lengthField: i32 = -1
314
+ /**
315
+ * Transfer-Encoding fields the request's head has kept so far.
316
+ * @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
317
+ */
318
+ codingFields: i32 = 0
319
+ /**
320
+ * Upgrade fields the request's head has kept so far.
321
+ * @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
322
+ */
323
+ upgradeFields: i32 = 0
324
+ /**
325
+ * The bytes of the Upgrade fields' values, OWS trimmed, summed.
326
+ * @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
327
+ */
328
+ upgradeValueBytes: i32 = 0
329
+ /**
330
+ * The `HTTP1_CONNECTION_` options the Connection fields so far name.
331
+ * @internal Gathered by the field-line parser in this module as each Connection field is read, and read by `http1EndOfHead`.
332
+ */
333
+ connectionOptions: i32 = 0
292
334
  /** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
293
335
  minor: i32 = 1
294
336
  /**
@@ -667,6 +709,13 @@ const http1StartRequest = (p: Http1Parser): void => {
667
709
  p.bodyTotal = 0
668
710
  p.sectionBytes = 0
669
711
  p.fieldCount = 0
712
+ p.hostFields = 0
713
+ p.lengthFields = 0
714
+ p.lengthField = -1
715
+ p.codingFields = 0
716
+ p.upgradeFields = 0
717
+ p.upgradeValueBytes = 0
718
+ p.connectionOptions = 0
670
719
  p.headHeld = false
671
720
  }
672
721
 
@@ -910,13 +959,21 @@ const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean):
910
959
  if (a < 0 || b - a !== n || b > toI32(bytes.length)) {
911
960
  return false
912
961
  }
913
- for (let k: i32 = 0; k < n && a + k < toI32(bytes.length); k += 1) {
962
+ if (!fold) {
963
+ for (let k: i32 = 0; k < n; k += 1) {
964
+ if (toI32(word.charCodeAt(k)) !== toI32(bytes[a + k])) {
965
+ return false
966
+ }
967
+ }
968
+ return true
969
+ }
970
+ for (let k: i32 = 0; k < n; k += 1) {
914
971
  let c: i32 = toI32(word.charCodeAt(k))
915
972
  let d: i32 = toI32(bytes[a + k])
916
- if (fold && c >= 65 && c <= 90) {
973
+ if (c >= 65 && c <= 90) {
917
974
  c += 32
918
975
  }
919
- if (fold && d >= 65 && d <= 90) {
976
+ if (d >= 65 && d <= 90) {
920
977
  d += 32
921
978
  }
922
979
  if (c !== d) {
@@ -927,41 +984,60 @@ const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean):
927
984
  }
928
985
 
929
986
  /**
930
- * Copies `buf[from .. to)` to the end of `head`, growing it when it must, and
931
- * answers where the copy starts.
987
+ * Grows `p.head` so that `n` more bytes and sixteen of slack fit past
988
+ * `p.headLength`, keeping every byte already there, those `http1Section` has
989
+ * copied past `headLength` for a line not yet whole included: the rare path
990
+ * of `http1HeadAppend` and of the section scan's copy, apart so that the
991
+ * common one stays small enough to inline.
932
992
  */
933
- const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
934
- const n: i32 = to - from
993
+ const http1HeadGrow = (p: Http1Parser, n: i32): void => {
935
994
  const at: i32 = p.headLength
936
995
  let size: i32 = toI32(p.head.length)
937
- if (n > size - at) {
938
- if (size < HTTP1_FIRST_HEAD) {
939
- size = HTTP1_FIRST_HEAD
940
- }
941
- while (size - at < n) {
942
- if (size > 1073741823) {
943
- panic("Http1Parser: a head of more than 2^31 - 1 bytes")
944
- }
945
- size = size * 2
946
- }
947
- const grown: u8[] = new Array<u8>(size)
948
- const old: u8[] = p.head
949
- for (let k: i32 = 0; k < at && k < toI32(grown.length) && k < toI32(old.length); k += 1) {
950
- grown[k] = old[k]
996
+ if (size < HTTP1_FIRST_HEAD) {
997
+ size = HTTP1_FIRST_HEAD
998
+ }
999
+ while (size - at - 16 < n) {
1000
+ if (size > 1073741823) {
1001
+ panic("Http1Parser: a head of more than 2^31 - 1 bytes")
951
1002
  }
952
- p.head = grown
1003
+ size = size * 2
1004
+ }
1005
+ const grown: u8[] = new Array<u8>(size)
1006
+ const old: u8[] = p.head
1007
+ const kept: i32 = toI32(old.length) < size ? toI32(old.length) : size
1008
+ if (kept > 0) {
1009
+ copyBytes(grown, 0, old, 0, kept)
953
1010
  }
1011
+ p.head = grown
1012
+ }
1013
+
1014
+ /**
1015
+ * Copies `buf[from .. to)` to the end of `head` and answers where the copy
1016
+ * starts: the request line, the one line the section scan does not copy
1017
+ * itself. It relies on the caller having called `http1HeadGrow` when the
1018
+ * line would leave fewer than sixteen bytes of slack; without that it copies
1019
+ * nothing and only moves `headLength`. The head keeps sixteen bytes past its
1020
+ * last line, so a short line is copied as whole vectors and a vector load at
1021
+ * any byte of the head is in bounds: the name and value compares read one. A
1022
+ * vector's lanes past the line write bytes past `headLength`, which nothing
1023
+ * reads until the section scan overwrites them.
1024
+ */
1025
+ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
1026
+ const n: i32 = to - from
1027
+ const at: i32 = p.headLength
954
1028
  const head: u8[] = p.head
955
1029
  const buf: u8[] = p.buf
956
- if (n > 0 && from >= 0 && from <= toI32(buf.length) - n && at <= toI32(head.length) - n) {
957
- // The line lies in `buf` and the head was grown to hold it: one move.
1030
+ // A line of up to 32 bytes is one or two vector stores; a longer one, or
1031
+ // one that ends within a vector of `buf`'s end, is one `copyBytes`, which
1032
+ // outruns a loop of stores at a Cookie or User-Agent line's length.
1033
+ const room: i32 = n <= 16 ? 16 : 32
1034
+ if (n <= 32 && from >= 0 && from <= toI32(buf.length) - room && at <= toI32(head.length) - room) {
1035
+ store(loadU8x16(buf, from), head, at)
1036
+ if (n > 16) {
1037
+ store(loadU8x16(buf, from + 16), head, at + 16)
1038
+ }
1039
+ } else if (n > 0 && from >= 0 && from <= toI32(buf.length) - n && at <= toI32(head.length) - n) {
958
1040
  copyBytes(head, at, buf, from, n)
959
- } else {
960
- for (let k: i32 = 0; k < n && at + k < toI32(head.length) && from + k < toI32(buf.length); k += 1) {
961
- if (from + k >= 0) {
962
- head[at + k] = buf[from + k]
963
- }
964
- }
965
1041
  }
966
1042
  p.headLength = at + n
967
1043
  return at
@@ -1032,53 +1108,134 @@ const http1Tally = (p: Http1Parser, name: string, word: string): Http1ListTally
1032
1108
  return tally
1033
1109
  }
1034
1110
 
1035
- /** `http1ConnectionOptions`: the Connection list names `close`. */
1111
+ /** `http1FramingField`: a Host field. */
1112
+ const HTTP1_FIELD_HOST: i32 = 1
1113
+ /** `http1FramingField`: a Content-Length field. */
1114
+ const HTTP1_FIELD_LENGTH: i32 = 2
1115
+ /** `http1FramingField`: a Transfer-Encoding field. */
1116
+ const HTTP1_FIELD_CODING: i32 = 3
1117
+ /** `http1FramingField`: a Connection field. */
1118
+ const HTTP1_FIELD_CONNECTION: i32 = 4
1119
+ /** `http1FramingField`: an Upgrade field. */
1120
+ const HTTP1_FIELD_UPGRADE: i32 = 5
1121
+
1122
+ /**
1123
+ * Whether `head[at .. at + n)` is `word`'s first `n` bytes, `n` at most 16,
1124
+ * each upper-case ASCII letter of the head lowered when `fold`: one vector
1125
+ * compare, or `http1HeadIs` when fewer than sixteen bytes follow `at`.
1126
+ */
1127
+ const http1HeadIsVector = (
1128
+ head: u8[],
1129
+ at: i32,
1130
+ n: i32,
1131
+ word: u8x16,
1132
+ text: string,
1133
+ fold: boolean
1134
+ ): boolean => {
1135
+ if (at < 0 || at > toI32(head.length) - 16 || n > 16) {
1136
+ return http1HeadIs(head, at, at + n, text, fold)
1137
+ }
1138
+ let v: u8x16 = loadU8x16(head, at)
1139
+ if (fold) {
1140
+ v = select(lt(subWrapping(v, splatU8x16(65)), splatU8x16(26)), or(v, splatU8x16(32)), v)
1141
+ }
1142
+ const want: i32 = n >= 16 ? 65535 : (1 << n) - 1
1143
+ return (bitmask(eq(v, word)) & want) === want
1144
+ }
1145
+
1146
+ /**
1147
+ * Which of the fields that frame a message (RFC 9112 §6, §9.6, RFC 9110 §7.2,
1148
+ * §7.8) the lowercase name `head[at .. at + n)` is, as an `HTTP1_FIELD_`
1149
+ * kind, or 0. Every kept field line asks once, as its name is read, so the
1150
+ * end of the head counts them without reading a name again; the length picks
1151
+ * the one name a field could be, and one vector compare answers.
1152
+ */
1153
+ const http1FramingField = (head: u8[], at: i32, n: i32): i32 => {
1154
+ if (n === 4) {
1155
+ const host: u8x16 = u8x16(104, 111, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
1156
+ return http1HeadIsVector(head, at, n, host, "host", false) ? HTTP1_FIELD_HOST : 0
1157
+ }
1158
+ if (n === 7) {
1159
+ const upgrade: u8x16 = u8x16(117, 112, 103, 114, 97, 100, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0)
1160
+ return http1HeadIsVector(head, at, n, upgrade, "upgrade", false) ? HTTP1_FIELD_UPGRADE : 0
1161
+ }
1162
+ if (n === 10) {
1163
+ const connection: u8x16 = u8x16(99, 111, 110, 110, 101, 99, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0)
1164
+ return http1HeadIsVector(head, at, n, connection, "connection", false) ? HTTP1_FIELD_CONNECTION : 0
1165
+ }
1166
+ if (n === 14) {
1167
+ const length: u8x16 = u8x16(99, 111, 110, 116, 101, 110, 116, 45, 108, 101, 110, 103, 116, 104, 0, 0)
1168
+ return http1HeadIsVector(head, at, n, length, "content-length", false) ? HTTP1_FIELD_LENGTH : 0
1169
+ }
1170
+ if (n === 17) {
1171
+ // "transfer-encodin" in the vector, and the "g" after it.
1172
+ const coding: u8x16 = u8x16(116, 114, 97, 110, 115, 102, 101, 114, 45, 101, 110, 99, 111, 100, 105, 110)
1173
+ const g: boolean = at >= 0 && at + 16 < toI32(head.length) && head[at + 16] === 103
1174
+ return g && http1HeadIsVector(head, at, 16, coding, "transfer-encodin", false) ? HTTP1_FIELD_CODING : 0
1175
+ }
1176
+ return 0
1177
+ }
1178
+
1179
+ /** `http1ConnectionList`: the Connection list names `close`. */
1036
1180
  const HTTP1_CONNECTION_CLOSE: i32 = 1
1037
- /** `http1ConnectionOptions`: the Connection list names `keep-alive`. */
1181
+ /** `http1ConnectionList`: the Connection list names `keep-alive`. */
1038
1182
  const HTTP1_CONNECTION_KEEP_ALIVE: i32 = 2
1039
- /** `http1ConnectionOptions`: the Connection list names `upgrade`. */
1183
+ /** `http1ConnectionList`: the Connection list names `upgrade`. */
1040
1184
  const HTTP1_CONNECTION_UPGRADE: i32 = 4
1041
1185
 
1042
1186
  /**
1043
- * Which of `close`, `keep-alive` and `upgrade` the Connection fields name,
1044
- * in any case, as `HTTP1_CONNECTION_` bits: one walk of every Connection
1045
- * field's list, where three `http1Tally` walks would each read it all. An
1046
- * element is trimmed of OWS and compared whole, as `http1TallyRun` compares.
1187
+ * Which of `close`, `keep-alive` and `upgrade` the Connection value
1188
+ * `head[from .. to)` names, in any case, as `HTTP1_CONNECTION_` bits. The
1189
+ * commas are found sixteen bytes at a time; each element is trimmed of OWS and
1190
+ * compared whole (RFC 9110 §5.6.1, §7.6.1), its length picking the one word it
1191
+ * could be, and empty elements are skipped.
1047
1192
  */
1048
- const http1ConnectionOptions = (p: Http1Parser): i32 => {
1049
- const spans: i32[] = p.spans
1050
- const head: u8[] = p.head
1193
+ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
1194
+ const close: u8x16 = u8x16(99, 108, 111, 115, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
1195
+ const keepAlive: u8x16 = u8x16(107, 101, 101, 112, 45, 97, 108, 105, 118, 101, 0, 0, 0, 0, 0, 0)
1196
+ const upgrade: u8x16 = u8x16(117, 112, 103, 114, 97, 100, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0)
1197
+ const length: i32 = toI32(head.length)
1051
1198
  let found: i32 = 0
1052
- for (let i: i32 = 0; i < p.headerCount && 4 * i + 3 < toI32(spans.length); i += 1) {
1053
- if (!http1HeadIs(head, spans[4 * i], spans[4 * i + 1], "connection", false)) {
1054
- continue
1055
- }
1056
- const to: i32 = spans[4 * i + 3]
1057
- let start: i32 = spans[4 * i + 2]
1058
- for (let k: i32 = start; k <= to; k += 1) {
1059
- const c: i32 = k < to && k >= 0 && k < toI32(head.length) ? toI32(head[k]) : 44
1060
- if (c !== 44) {
1061
- continue
1062
- }
1063
- let a: i32 = start
1064
- let b: i32 = k
1065
- while (a >= 0 && a < b && a < toI32(head.length) && http1IsOws(toI32(head[a]))) {
1066
- a += 1
1067
- }
1068
- while (b > a && b - 1 >= 0 && b - 1 < toI32(head.length) && http1IsOws(toI32(head[b - 1]))) {
1069
- b -= 1
1070
- }
1071
- // The length picks the one word an element could be.
1072
- const n: i32 = b - a
1073
- if (n === 5 && http1HeadIs(head, a, b, "close", true)) {
1074
- found = found | HTTP1_CONNECTION_CLOSE
1075
- } else if (n === 10 && http1HeadIs(head, a, b, "keep-alive", true)) {
1076
- found = found | HTTP1_CONNECTION_KEEP_ALIVE
1077
- } else if (n === 7 && http1HeadIs(head, a, b, "upgrade", true)) {
1078
- found = found | HTTP1_CONNECTION_UPGRADE
1199
+ let start: i32 = from
1200
+ while (start >= 0 && start <= to) {
1201
+ // The element runs to the next comma, or to the value's end.
1202
+ let end: i32 = start
1203
+ let seeking: boolean = true
1204
+ while (seeking && end < to) {
1205
+ if (end <= length - 16) {
1206
+ const commas: i32 = bitmask(eq(loadU8x16(head, end), splatU8x16(44)))
1207
+ if (commas !== 0) {
1208
+ end += toI32(31) - toI32(Math.clz32(toU32(commas & -commas)))
1209
+ seeking = false
1210
+ } else {
1211
+ end += 16
1212
+ }
1213
+ } else if (end >= 0 && end < toI32(head.length) && head[end] !== 44) {
1214
+ end += 1
1215
+ } else {
1216
+ seeking = false
1079
1217
  }
1080
- start = k + 1
1081
1218
  }
1219
+ if (end > to) {
1220
+ end = to
1221
+ }
1222
+ let a: i32 = start
1223
+ let b: i32 = end
1224
+ while (a < b && a >= 0 && a < toI32(head.length) && http1IsOws(toI32(head[a]))) {
1225
+ a += 1
1226
+ }
1227
+ while (b > a && b - 1 < length && http1IsOws(toI32(head[b - 1]))) {
1228
+ b -= 1
1229
+ }
1230
+ const n: i32 = b - a
1231
+ if (n === 5 && http1HeadIsVector(head, a, n, close, "close", true)) {
1232
+ found = found | HTTP1_CONNECTION_CLOSE
1233
+ } else if (n === 10 && http1HeadIsVector(head, a, n, keepAlive, "keep-alive", true)) {
1234
+ found = found | HTTP1_CONNECTION_KEEP_ALIVE
1235
+ } else if (n === 7 && http1HeadIsVector(head, a, n, upgrade, "upgrade", true)) {
1236
+ found = found | HTTP1_CONNECTION_UPGRADE
1237
+ }
1238
+ start = end + 1
1082
1239
  }
1083
1240
  return found
1084
1241
  }
@@ -1095,8 +1252,17 @@ const http1MethodOverrun = (p: Http1Parser): boolean => {
1095
1252
  }
1096
1253
  const buf: u8[] = p.buf
1097
1254
  const n: i32 = toI32(buf.length)
1255
+ // A space, CR or LF in the first sixteen bytes, buffered or not, is
1256
+ // before `HTTP1_MAX_METHOD`: the loop below would answer false at it, or
1257
+ // at `end` before it.
1258
+ if (p.start >= 0 && p.start <= n - 16) {
1259
+ const v: u8x16 = loadU8x16(buf, p.start)
1260
+ if (any(or(or(eq(v, splatU8x16(32)), eq(v, splatU8x16(13))), eq(v, splatU8x16(10))))) {
1261
+ return false
1262
+ }
1263
+ }
1098
1264
  const stop: i32 = p.start + HTTP1_MAX_METHOD
1099
- for (let k: i32 = p.start; k >= 0 && k < p.end && k < n; k += 1) {
1265
+ for (let k: i32 = p.start; k >= 0 && k < p.end && k < toI32(buf.length); k += 1) {
1100
1266
  if (buf[k] === 32 || buf[k] === 13 || buf[k] === 10) {
1101
1267
  return false
1102
1268
  }
@@ -1116,8 +1282,23 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1116
1282
  const n: i32 = toI32(buf.length)
1117
1283
  const a: i32 = p.lineStart
1118
1284
  const b: i32 = p.lineEnd < n ? p.lineEnd : n
1285
+ // The method is tchars, sixteen at a time as a field name is read; the
1286
+ // line's CR, a control byte, ends the run if no space or other byte does.
1119
1287
  let i: i32 = a
1120
- while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
1288
+ let ended: boolean = false
1289
+ while (!ended && i >= 0 && i < b && i <= toI32(buf.length) - 16) {
1290
+ const bad: i32 = http1NonTokenLanes(loadU8x16(buf, i))
1291
+ if (bad !== 0) {
1292
+ i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
1293
+ ended = true
1294
+ } else {
1295
+ i += 16
1296
+ }
1297
+ }
1298
+ if (i > b) {
1299
+ i = b
1300
+ }
1301
+ while (!ended && i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
1121
1302
  i += 1
1122
1303
  }
1123
1304
  if (i === a || i - a > HTTP1_MAX_METHOD || i >= b || buf[i] !== 32) {
@@ -1153,26 +1334,38 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1153
1334
  }
1154
1335
  const targetEnd: i32 = i
1155
1336
  i += 1
1156
- // "HTTP/" DIGIT "." DIGIT, case-sensitive (§2.3), and the end of the line.
1157
- if (
1158
- b - i !== 8 ||
1159
- buf[i] !== 72 ||
1160
- buf[i + 1] !== 84 ||
1161
- buf[i + 2] !== 84 ||
1162
- buf[i + 3] !== 80 ||
1163
- buf[i + 4] !== 47 ||
1164
- buf[i + 5] < 48 ||
1165
- buf[i + 5] > 57 ||
1166
- buf[i + 6] !== 46 ||
1167
- buf[i + 7] < 48 ||
1168
- buf[i + 7] > 57
1169
- ) {
1337
+ // "HTTP/" DIGIT "." DIGIT, case-sensitive (§2.3), and the end of the line:
1338
+ // with sixteen bytes to load, one compare for the five letters and the dot
1339
+ // and one range test for the two digits, else a byte at a time.
1340
+ let version: boolean = false
1341
+ if (b - i === 8 && i >= 0 && i <= toI32(buf.length) - 16) {
1342
+ const v: u8x16 = loadU8x16(buf, i)
1343
+ const fixed: i32 = bitmask(eq(v, u8x16(72, 84, 84, 80, 47, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0)))
1344
+ const digits: i32 = bitmask(lt(subWrapping(v, splatU8x16(48)), splatU8x16(10)))
1345
+ version = (fixed & 95) === 95 && (digits & 160) === 160
1346
+ } else if (b - i === 8) {
1347
+ version =
1348
+ buf[i] === 72 &&
1349
+ buf[i + 1] === 84 &&
1350
+ buf[i + 2] === 84 &&
1351
+ buf[i + 3] === 80 &&
1352
+ buf[i + 4] === 47 &&
1353
+ buf[i + 5] >= 48 &&
1354
+ buf[i + 5] <= 57 &&
1355
+ buf[i + 6] === 46 &&
1356
+ buf[i + 7] >= 48 &&
1357
+ buf[i + 7] <= 57
1358
+ }
1359
+ if (!version) {
1170
1360
  return http1Fail(p, 400, "a malformed HTTP version")
1171
1361
  }
1172
1362
  if (buf[i + 5] !== 49) {
1173
1363
  return http1Fail(p, 505, "an HTTP major version other than 1")
1174
1364
  }
1175
1365
  p.minor = buf[i + 7] === 48 ? 0 : 1
1366
+ if (b - a > toI32(p.head.length) - p.headLength - 16) {
1367
+ http1HeadGrow(p, b - a)
1368
+ }
1176
1369
  const at: i32 = http1HeadAppend(p, a, b)
1177
1370
  p.methodStart = at
1178
1371
  p.methodEnd = at + (methodEnd - a)
@@ -1188,25 +1381,23 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1188
1381
 
1189
1382
  /**
1190
1383
  * Reads one field line in `lineStart .. lineEnd` (§5) into `names` and
1191
- * `values`, or, for a trailer, only checks it. Answers `HTTP1_NEED_MORE` or the
1192
- * refusal.
1384
+ * `values`, or, for a trailer, only checks it. A kept line is already in the
1385
+ * head, `http1Section`'s copy of it: `buf[x]` is at `head[x + delta]`.
1386
+ * Answers `HTTP1_NEED_MORE` or the refusal.
1193
1387
  */
1194
- const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
1388
+ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
1195
1389
  const buf: u8[] = p.buf
1196
1390
  const n: i32 = toI32(buf.length)
1197
1391
  const a: i32 = p.lineStart
1198
1392
  const b: i32 = p.lineEnd < n ? p.lineEnd : n
1199
- if (a >= 0 && a < b && (buf[a] === 32 || buf[a] === 9)) {
1200
- return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
1201
- }
1202
- // A kept line is copied into the head first, and its name is read there,
1203
- // each upper-case letter lowered as the scan passes it: one walk over the
1204
- // name, not a check of `buf` and then a second walk of the copy. The copy
1205
- // of a line refused below is never read, since a refusal ends the parse.
1393
+ // A kept line's name is read in the head's copy, each upper-case letter
1394
+ // lowered as the scan passes it: one walk over the name, not a check of
1395
+ // `buf` and then a second walk of the copy. The copy of a line refused
1396
+ // below is never read, since a refusal ends the parse.
1206
1397
  let at: i32 = 0
1207
1398
  let i: i32 = a
1208
1399
  if (keep) {
1209
- at = http1HeadAppend(p, a, b)
1400
+ at = a + delta
1210
1401
  i = a + (http1LowerName(p.head, at, at + (b - a)) - at)
1211
1402
  } else {
1212
1403
  while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
@@ -1214,6 +1405,11 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
1214
1405
  }
1215
1406
  }
1216
1407
  if (i === a || i >= b || buf[i] !== 58) {
1408
+ // Whitespace is no tchar, so an obs-fold line has an empty name: its
1409
+ // own refusal is told apart here, off the path every field takes.
1410
+ if (i === a && a >= 0 && a < b && (buf[a] === 32 || buf[a] === 9)) {
1411
+ return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
1412
+ }
1217
1413
  return http1Fail(p, 400, "a malformed field name")
1218
1414
  }
1219
1415
  const nameEnd: i32 = i
@@ -1234,13 +1430,30 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
1234
1430
  valueStart >= 0 &&
1235
1431
  valueStart < valueEnd &&
1236
1432
  valueStart < toI32(buf.length) &&
1237
- http1IsOws(toI32(buf[valueStart]))
1433
+ (buf[valueStart] === 32 || buf[valueStart] === 9)
1238
1434
  ) {
1239
1435
  valueStart += 1
1240
1436
  }
1241
- while (valueEnd > valueStart && valueEnd - 1 >= 0 && http1IsOws(toI32(buf[valueEnd - 1]))) {
1437
+ while (valueEnd > valueStart && (buf[valueEnd - 1] === 32 || buf[valueEnd - 1] === 9)) {
1242
1438
  valueEnd -= 1
1243
1439
  }
1440
+ // The fields that frame the message are counted here, as their names
1441
+ // are read, for `http1EndOfHead`: every kept field asks the same question.
1442
+ const kind: i32 = http1FramingField(p.head, at, nameEnd - a)
1443
+ if (kind === HTTP1_FIELD_HOST) {
1444
+ p.hostFields += 1
1445
+ } else if (kind === HTTP1_FIELD_LENGTH) {
1446
+ p.lengthFields += 1
1447
+ p.lengthField = p.headerCount
1448
+ } else if (kind === HTTP1_FIELD_CODING) {
1449
+ p.codingFields += 1
1450
+ } else if (kind === HTTP1_FIELD_CONNECTION) {
1451
+ p.connectionOptions =
1452
+ p.connectionOptions | http1ConnectionList(p.head, at + (valueStart - a), at + (valueEnd - a))
1453
+ } else if (kind === HTTP1_FIELD_UPGRADE) {
1454
+ p.upgradeFields += 1
1455
+ p.upgradeValueBytes += valueEnd - valueStart
1456
+ }
1244
1457
  const base: i32 = 4 * p.headerCount
1245
1458
  http1SpanSet(p, base + 3, at + (valueEnd - a))
1246
1459
  // The last of the four is set first, so the array has room for them all.
@@ -1261,40 +1474,20 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
1261
1474
 
1262
1475
  /**
1263
1476
  * The header section has ended: decide the framing and the connection's fate
1264
- * (§6.3, §9.3) and answer `HTTP1_HEAD`, or the refusal. It reads the spans,
1265
- * and allocates nothing unless `keepText` asks for `upgrade` as a string.
1477
+ * (§6.3, §9.3) and answer `HTTP1_HEAD`, or the refusal. It reads the counts
1478
+ * the field lines left, and allocates nothing unless `keepText` asks for
1479
+ * `upgrade` as a string.
1266
1480
  */
1267
1481
  const http1EndOfHead = (p: Http1Parser): i32 => {
1268
1482
  const spans: i32[] = p.spans
1269
1483
  const head: u8[] = p.head
1270
- let hosts: i32 = 0
1271
- let lengths: i32 = 0
1272
- let lengthField: i32 = -1
1273
- let codingFields: i32 = 0
1274
- let upgradeFields: i32 = 0
1275
- let upgradeBytes: i32 = 0
1276
- let connectionFields: i32 = 0
1277
- for (let i: i32 = 0; i < p.headerCount && 4 * i + 3 < toI32(spans.length); i += 1) {
1278
- const a: i32 = spans[4 * i]
1279
- const b: i32 = spans[4 * i + 1]
1280
- if (http1HeadIs(head, a, b, "host", false)) {
1281
- hosts += 1
1282
- } else if (http1HeadIs(head, a, b, "content-length", false)) {
1283
- lengths += 1
1284
- lengthField = i
1285
- } else if (http1HeadIs(head, a, b, "transfer-encoding", false)) {
1286
- codingFields += 1
1287
- } else if (http1HeadIs(head, a, b, "connection", false)) {
1288
- connectionFields += 1
1289
- } else if (http1HeadIs(head, a, b, "upgrade", false)) {
1290
- upgradeFields += 1
1291
- upgradeBytes += spans[4 * i + 3] - spans[4 * i + 2]
1292
- }
1293
- }
1484
+ const hosts: i32 = p.hostFields
1485
+ const lengths: i32 = p.lengthFields
1486
+ const lengthField: i32 = p.lengthField
1294
1487
  if ((p.minor === 1 && hosts !== 1) || hosts > 1) {
1295
1488
  return http1Fail(p, 400, "a request without exactly one Host")
1296
1489
  }
1297
- if (codingFields > 0) {
1490
+ if (p.codingFields > 0) {
1298
1491
  if (lengths > 0) {
1299
1492
  return http1Fail(p, 400, "both Content-Length and Transfer-Encoding")
1300
1493
  }
@@ -1329,16 +1522,14 @@ const http1EndOfHead = (p: Http1Parser): i32 => {
1329
1522
  }
1330
1523
  p.contentLength = size
1331
1524
  }
1332
- // One walk of the Connection list finds all three options, and none is
1333
- // made for a request without a Connection field.
1334
- const options: i32 = connectionFields > 0 ? http1ConnectionOptions(p) : 0
1525
+ const options: i32 = p.connectionOptions
1335
1526
  const close: boolean = (options & HTTP1_CONNECTION_CLOSE) !== 0
1336
1527
  const keep: boolean = (options & HTTP1_CONNECTION_KEEP_ALIVE) !== 0
1337
1528
  const upgradeAsked: boolean = (options & HTTP1_CONNECTION_UPGRADE) !== 0
1338
1529
  p.keepAlive = p.minor === 1 ? !close : keep && !close
1339
1530
  // The fields of one name joined with ", " are empty only when there is one
1340
1531
  // field and it is empty: the same test, without the join.
1341
- p.upgrading = p.minor === 1 && upgradeAsked && (upgradeFields > 1 || upgradeBytes > 0)
1532
+ p.upgrading = p.minor === 1 && upgradeAsked && (p.upgradeFields > 1 || p.upgradeValueBytes > 0)
1342
1533
  if (p.keepText && p.upgrading) {
1343
1534
  const upgrades: string[] = []
1344
1535
  const names: string[] = p.names
@@ -1493,6 +1684,179 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
1493
1684
  return HTTP1_BODY
1494
1685
  }
1495
1686
 
1687
+ /**
1688
+ * Reads a header or trailer section's lines while whole ones are buffered:
1689
+ * `http1TakeLine` and `http1FieldLine` in one loop, each line limited by what
1690
+ * is left of `maxHeaderBytes` as `http1TakeLine` limits it, and the section's
1691
+ * bytes counted after it. The control bytes of the vector that held one line's
1692
+ * CRLF are kept for the next line, which mostly starts in the same vector, so
1693
+ * each buffered byte is loaded once however many lines a read brought.
1694
+ * Answers `HTTP1_LINE` when a trailer section has ended and the state machine
1695
+ * goes on, or what `next()` answers: `HTTP1_HEAD`, `HTTP1_NEED_MORE` or the
1696
+ * refusal. Its limits, refusals, spans and `scan` are `http1TakeLine`'s.
1697
+ */
1698
+ const http1Section = (p: Http1Parser): i32 => {
1699
+ const buf: u8[] = p.buf
1700
+ const length: i32 = toI32(buf.length)
1701
+ const stop: i32 = p.end < length ? p.end : length
1702
+ const trailer: boolean = p.state === HTTP1_S_TRAILERS
1703
+ let i: i32 = p.start
1704
+ if (p.scan > p.start) {
1705
+ i = p.scan
1706
+ } else {
1707
+ p.lineControl = false
1708
+ }
1709
+ if (i < 0) {
1710
+ i = 0
1711
+ }
1712
+ // A header section is copied into the head as it is scanned, each vector
1713
+ // stored as it is loaded, CRLFs and all: `buf[x]` lands at `head[x +
1714
+ // delta]`, so a field line is in the head before it is read, and a line
1715
+ // that ends in a later call finds its first bytes where this one left them.
1716
+ // Nothing past `copyStop` is copied: a line reaching it is over its limit
1717
+ // (see the 431 below; what is left of `maxHeaderBytes` is the same at every
1718
+ // line), so the copy, and the head, stay within the limit.
1719
+ const copy: boolean = !trailer
1720
+ const delta: i32 = p.headLength - p.start
1721
+ const room: i32 = p.maxHeaderBytes - p.sectionBytes + 2
1722
+ const copyStop: i32 = p.start > 2147483647 - room ? 2147483647 : p.start + room
1723
+ // A vector at `q` is copied when `q < copyEnd`; the head is grown when `q`
1724
+ // passes `growAt`, so most vectors pay one compare for each.
1725
+ const copyEnd: i32 = copy ? copyStop : -2147483647
1726
+ let head: u8[] = p.head
1727
+ let growAt: i32 = toI32(head.length) - 32 - delta
1728
+ // Bit `k` of `bits` is set when `buf[base + k]` is a control byte or DEL
1729
+ // at or past `i`: the vector last loaded, its lanes before `i` cleared.
1730
+ let base: i32 = i - 16
1731
+ let bits: i32 = 0
1732
+ while (true) {
1733
+ // The next control byte at or past `i`, or the end of the bytes so far.
1734
+ let j: i32 = stop
1735
+ let seeking: boolean = true
1736
+ while (seeking) {
1737
+ if (bits !== 0) {
1738
+ j = base + (toI32(31) - toI32(Math.clz32(toU32(bits & -bits))))
1739
+ seeking = false
1740
+ } else {
1741
+ const q: i32 = base + 16 > i ? base + 16 : i
1742
+ if (q >= stop) {
1743
+ seeking = false
1744
+ } else if (q >= 0 && q <= length - 16) {
1745
+ // The vector may run past `stop` into bytes not yet fed: its lanes
1746
+ // there are dropped from `bits`, and its copy of them is
1747
+ // overwritten when they arrive.
1748
+ const v: u8x16 = loadU8x16(buf, q)
1749
+ if (q < copyEnd) {
1750
+ if (q > growAt) {
1751
+ http1HeadGrow(p, q + delta + 16 - p.headLength)
1752
+ head = p.head
1753
+ growAt = toI32(head.length) - 32 - delta
1754
+ }
1755
+ store(v, head, q + delta)
1756
+ }
1757
+ bits = bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127))))
1758
+ if (q > stop - 16) {
1759
+ bits = bits & ((toI32(1) << (stop - q)) - 1)
1760
+ }
1761
+ base = q
1762
+ } else {
1763
+ // Fewer than sixteen bytes are left in `buf`: a byte at a time.
1764
+ let k: i32 = q
1765
+ while (k >= 0 && k < stop && k < toI32(buf.length) && buf[k] >= 32 && buf[k] !== 127) {
1766
+ k += 1
1767
+ }
1768
+ const last: i32 = k < stop ? k + 1 : stop
1769
+ const end: i32 = last < copyStop ? last : copyStop
1770
+ if (copy && q < end) {
1771
+ if (end + delta > toI32(p.head.length) - 16) {
1772
+ http1HeadGrow(p, end + delta - p.headLength)
1773
+ }
1774
+ copyBytes(p.head, q + delta, buf, q, end - q)
1775
+ head = p.head
1776
+ growAt = toI32(head.length) - 32 - delta
1777
+ }
1778
+ j = k
1779
+ base = k + 1 - 16
1780
+ seeking = false
1781
+ }
1782
+ }
1783
+ }
1784
+ if (j < 0 || j >= stop || j >= toI32(buf.length)) {
1785
+ i = j < 0 ? i : j
1786
+ break
1787
+ }
1788
+ const c: i32 = toI32(buf[j])
1789
+ let lf: i32 = -1
1790
+ if (c === 13 && j + 1 < stop && j + 1 < toI32(buf.length) && buf[j + 1] === 10) {
1791
+ lf = j + 1
1792
+ } else if (c === 10) {
1793
+ lf = j
1794
+ }
1795
+ if (lf >= 0) {
1796
+ const start: i32 = p.start
1797
+ if (lf === start || buf[lf - 1] !== 13) {
1798
+ return http1Fail(p, 400, "a line ended in a bare LF")
1799
+ }
1800
+ const sectionBytes: i32 = p.sectionBytes + (lf + 1 - start)
1801
+ if (sectionBytes - 2 > p.maxHeaderBytes) {
1802
+ return http1Fail(p, 431, "a line over its limit")
1803
+ }
1804
+ // The scan stops at the CR, so the LF after it may be in no vector
1805
+ // or tail copied: store it, and the head holds each line's CRLF.
1806
+ const at: i32 = lf + delta
1807
+ if (copy && at >= 0 && at < toI32(head.length)) {
1808
+ head[at] = 10
1809
+ }
1810
+ p.lineStart = start
1811
+ p.lineEnd = lf - 1
1812
+ p.start = lf + 1
1813
+ p.scan = lf + 1
1814
+ i = lf + 1
1815
+ // The lowest bit is the line's end at `j`, and a CR's LF is the next
1816
+ // one when it is in this vector, as a control byte: drop them both.
1817
+ bits = bits & (bits - 1)
1818
+ if (c === 13) {
1819
+ bits = bits & (bits - 1)
1820
+ }
1821
+ p.sectionBytes = sectionBytes
1822
+ if (sectionBytes > p.maxHeaderBytes) {
1823
+ return http1Fail(p, 431, "a header section over the limit")
1824
+ }
1825
+ if (lf - 1 === start) {
1826
+ if (trailer) {
1827
+ p.state = HTTP1_S_MESSAGE_END
1828
+ return HTTP1_LINE
1829
+ }
1830
+ p.headLength = p.start + delta
1831
+ return http1EndOfHead(p)
1832
+ }
1833
+ if (http1FieldLine(p, copy, delta) === HTTP1_ERROR) {
1834
+ return HTTP1_ERROR
1835
+ }
1836
+ p.lineControl = false
1837
+ } else if (c === 13 && j + 1 >= stop) {
1838
+ // A CR at the end of the bytes so far: whether it ends the line is
1839
+ // the next byte's to say, so the next call looks at it again.
1840
+ i = j
1841
+ break
1842
+ } else {
1843
+ if (c !== 9) {
1844
+ p.lineControl = true
1845
+ }
1846
+ i = j + 1
1847
+ bits = bits & (bits - 1)
1848
+ }
1849
+ }
1850
+ p.scan = i
1851
+ if (copy) {
1852
+ p.headLength = p.start + delta
1853
+ }
1854
+ if (p.end - p.start > p.maxHeaderBytes - p.sectionBytes + 1) {
1855
+ return http1Fail(p, 431, "a line over its limit")
1856
+ }
1857
+ return HTTP1_NEED_MORE
1858
+ }
1859
+
1496
1860
  /** The state machine behind `next()`. */
1497
1861
  const http1Next = (p: Http1Parser): i32 => {
1498
1862
  while (true) {
@@ -1522,25 +1886,10 @@ const http1Next = (p: Http1Parser): i32 => {
1522
1886
  }
1523
1887
  case HTTP1_S_FIELDS:
1524
1888
  case HTTP1_S_TRAILERS: {
1525
- const got: i32 = http1TakeLine(p, p.maxHeaderBytes - p.sectionBytes, 431)
1889
+ const got: i32 = http1Section(p)
1526
1890
  if (got !== HTTP1_LINE) {
1527
1891
  return got
1528
1892
  }
1529
- p.sectionBytes += p.lineEnd - p.lineStart + 2
1530
- if (p.sectionBytes > p.maxHeaderBytes) {
1531
- return http1Fail(p, 431, "a header section over the limit")
1532
- }
1533
- const trailer: boolean = p.state === HTTP1_S_TRAILERS
1534
- if (p.lineEnd === p.lineStart) {
1535
- if (trailer) {
1536
- p.state = HTTP1_S_MESSAGE_END
1537
- break
1538
- }
1539
- return http1EndOfHead(p)
1540
- }
1541
- if (http1FieldLine(p, !trailer) === HTTP1_ERROR) {
1542
- return HTTP1_ERROR
1543
- }
1544
1893
  break
1545
1894
  }
1546
1895
  case HTTP1_S_LENGTH_BODY:
@@ -1648,7 +1997,8 @@ export const http1IsFieldText = (text: string): boolean => {
1648
1997
  * (which would end the line early and let the rest be read as a field or a
1649
1998
  * second response); a field name that is not a token; `names` and `values` of
1650
1999
  * different lengths; a `Content-Length` or `Transfer-Encoding` among the
1651
- * caller's fields, since the framing is decided here; a 1xx or 204 response
2000
+ * caller's fields, since the framing is decided here; two `Date` fields in
2001
+ * any case, since `Date` is a singleton (RFC 9110 §5.3); a 1xx or 204 response
1652
2002
  * with a body length (RFC 9110 §15.2, §15.3.5); and any other `bodyLength`
1653
2003
  * below zero.
1654
2004
  *
@@ -1680,6 +2030,7 @@ export const http1ResponseHead = (
1680
2030
  return null
1681
2031
  }
1682
2032
  const parts: string[] = [`HTTP/1.1 ${status} ${reason}\r\n`]
2033
+ let dated: boolean = false
1683
2034
  for (let i: i32 = 0; i < count && i < toI32(names.length) && i < toI32(values.length); i += 1) {
1684
2035
  const name: string = names[i]
1685
2036
  const value: string = values[i]
@@ -1693,6 +2044,12 @@ export const http1ResponseHead = (
1693
2044
  ) {
1694
2045
  return null
1695
2046
  }
2047
+ if (http1SameWord(name, 0, nameLength, "date")) {
2048
+ if (dated) {
2049
+ return null
2050
+ }
2051
+ dated = true
2052
+ }
1696
2053
  parts.push(`${name}: ${value}\r\n`)
1697
2054
  }
1698
2055
  if (bodyLength >= 0) {
@@ -2007,7 +2364,8 @@ const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
2007
2364
  *
2008
2365
  * Answers where the head ends; `HTTP1_REFUSED` for everything
2009
2366
  * `http1ResponseHead` refuses, a `Connection` among the caller's fields when
2010
- * `connection` is given, a `connection` that is not field text, and a `date`
2367
+ * `connection` is given, a `connection` that is not field text, two `date`
2368
+ * fields among the caller's (a singleton, RFC 9110 §5.3), and a `date`
2011
2369
  * that is not field text on a final status, the only one it is sent on;
2012
2370
  * `HTTP1_NO_ROOM` when it is well formed but does not fit, the `Date` line
2013
2371
  * counted when it is sent. Either way `out` is untouched.
@@ -2051,7 +2409,7 @@ export const http1WriteResponseHead = (
2051
2409
  // field, the connection field and the empty line: counted before anything
2052
2410
  // is written, in 64 bits so that no caller's lengths can wrap the sum.
2053
2411
  let need: i64 = toI64(17) + toI64(reason.length)
2054
- let sendDate: boolean = status >= 200 && toI32(date.length) > 0
2412
+ let dated: boolean = false
2055
2413
  for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length); i += 1) {
2056
2414
  const name: u8[] = names[i]
2057
2415
  const value: u8[] = values[i]
@@ -2065,10 +2423,15 @@ export const http1WriteResponseHead = (
2065
2423
  return HTTP1_REFUSED
2066
2424
  }
2067
2425
  if (http1BytesAre(name, "date")) {
2068
- sendDate = false
2426
+ // Date is a singleton (RFC 9110 §5.3): a second one is refused.
2427
+ if (dated) {
2428
+ return HTTP1_REFUSED
2429
+ }
2430
+ dated = true
2069
2431
  }
2070
2432
  need = need + toI64(name.length) + toI64(value.length) + toI64(4)
2071
2433
  }
2434
+ const sendDate: boolean = status >= 200 && toI32(date.length) > 0 && !dated
2072
2435
  if (sendDate) {
2073
2436
  // "Date: " value CRLF: 37 bytes for an IMF-fixdate.
2074
2437
  need = need + toI64(8) + toI64(date.length)