gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit31cabfcc31cabfccmission 021: E2E script, report, LOG, STATUSmre31cabfcc/lib/multipart.hl

4.6 KB

  1. // lib/multipart.hl — A multipart/form-data BODY, read from its bytes (tickets#27, mission 017: a comment with files through the API,
  2. // `curl -F text=… -F [email protected]`). Plumbing for lib/api.hl; statics only. The body's bytes are searched as hex text (`Bytes.hex()`:
  3. // the native string search finds the boundary; only matches at an even offset are byte positions), the parts are cut out as Bytes.
  4. // A part's headers and a field's value must be UTF-8 (checked before decoding: a bad byte is a 400, not a stop).
  5. static CRLF = JSON.parse('"' + 'x\y'.slice(1, 2) + 'r' + 'x\y'.slice(1, 2) + 'n"')
  6. // the boundary of a `multipart/form-data; boundary=…` content type, or null
  7. static boundaryOf = (contentType) => {
  8. if (contentType == null || hlTypeName(contentType) != 'String') { return null }
  9. if (!contentType.toLowerCase().trim().startsWith('multipart/form-data')) { return null }
  10. for (p of contentType.split(';')) {
  11. let kv = p.trim()
  12. if (kv.toLowerCase().startsWith('boundary=')) {
  13. let b = kv.slice(9).trim()
  14. if (b.startsWith('"') && b.endsWith('"') && b.length >= 2) { b = b.slice(1, b.length - 1) }
  15. return b != '' && b.length <= 200 ? b : null
  16. }
  17. }
  18. return null
  19. }
  20. static isMultipart = (req) => { return boundaryOf(req.headers['content-type']) != null }
  21. // `needle` (hex) in `hex` at an even offset (a byte position) at or after `start`, or -1
  22. static hexFind = (hex, needle, start) => {
  23. let at = hex.indexOf(needle, start)
  24. while (at >= 0 && at % 2 == 1) { at = hex.indexOf(needle, at + 1) }
  25. return at
  26. }
  27. // are these bytes UTF-8? (Bytes.toString() stops the program on a bad sequence)
  28. static isUtf8 = (b) => {
  29. let i = 0
  30. n = b.length
  31. while (i < n) {
  32. let c = b[i]
  33. let more = 0
  34. if (c < 128) { more = 0 } else if (c >= 194 && c <= 223) { more = 1 } else if (c >= 224 && c <= 239) { more = 2 } else if (c >= 240 && c <= 244) { more = 3 } else { return false }
  35. if (more > 0 && i + more >= n) { return false }
  36. let k = 1
  37. while (k <= more) {
  38. if (b[i + k] < 128 || b[i + k] > 191) { return false }
  39. k = k + 1
  40. }
  41. i = i + more + 1
  42. }
  43. return true
  44. }
  45. // one header line's parameters: `form-data; name="file"; filename="a b.png"` → { name, filename }
  46. static dispositionOf = (value) => {
  47. out = { name = null filename = null }
  48. for (p of value.split(';')) {
  49. let kv = p.trim()
  50. let eq = kv.indexOf('=')
  51. if (eq > 0) {
  52. let k = kv.slice(0, eq).trim().toLowerCase()
  53. let v = kv.slice(eq + 1).trim()
  54. if (v.startsWith('"') && v.endsWith('"') && v.length >= 2) { v = v.slice(1, v.length - 1) }
  55. if (k == 'name') { out.name = v }
  56. if (k == 'filename') { out.filename = v }
  57. }
  58. }
  59. return out
  60. }
  61. // THE PARTS: { parts: [{ name, filename (null = a plain field), type, bytes }] } or { error }
  62. static partsOf = (req) => {
  63. boundary = boundaryOf(req.headers['content-type'])
  64. if (boundary == null) { return { error = 'a multipart body needs Content-Type: multipart/form-data; boundary=…' } }
  65. body = req.bytes
  66. if (body == null || hlTypeName(body) != 'Bytes' || body.length == 0) { return { error = 'the multipart body is empty' } }
  67. hex = body.hex()
  68. delim = toBytes('--' + boundary).hex()
  69. next = toBytes(CRLF + '--' + boundary).hex()
  70. crlf2 = toBytes(CRLF + CRLF).hex()
  71. parts = []
  72. let at = hexFind(hex, delim, 0)
  73. if (at < 0) { return { error = 'the multipart body does not hold its boundary' } }
  74. let guard = 0
  75. while (guard < 1000) {
  76. guard = guard + 1
  77. let after = at + delim.length
  78. // `--boundary--` ends the body
  79. if (hex.slice(after, after + 4) == '2d2d') { return { parts = parts } }
  80. if (hex.slice(after, after + 4) != '0d0a') { return { error = 'a multipart boundary is not followed by a line break' } }
  81. let hs = after + 4
  82. let he = hexFind(hex, crlf2, hs)
  83. if (he < 0) { return { error = 'a multipart part has no end of its headers' } }
  84. let head = body.slice(hs / 2, he / 2)
  85. if (!isUtf8(head)) { return { error = 'the headers of a multipart part are not UTF-8' } }
  86. let name = null
  87. let filename = null
  88. let type = ''
  89. for (line of head.toString().split(CRLF)) {
  90. let colon = line.indexOf(':')
  91. if (colon > 0) {
  92. let k = line.slice(0, colon).trim().toLowerCase()
  93. let v = line.slice(colon + 1).trim()
  94. if (k == 'content-disposition') {
  95. d = dispositionOf(v)
  96. name = d.name
  97. filename = d.filename
  98. }
  99. if (k == 'content-type') { type = v }
  100. }
  101. }
  102. if (name == null) { return { error = 'a multipart part has no Content-Disposition name' } }
  103. let cs = he + crlf2.length
  104. let ce = hexFind(hex, next, cs)
  105. if (ce < 0) { return { error = 'the multipart body ends inside a part (no closing boundary)' } }
  106. parts.push({ name = name filename = filename type = type bytes = body.slice(cs / 2, ce / 2) })
  107. at = ce + 4
  108. }
  109. return { error = 'too many multipart parts' }
  110. }

Branches

Latest commits

  • 31cabfccmission 021: E2E script, report, LOG, STATUSmre
  • 6aea8683editor look: .empty rule only for p.empty, files off where tickets has no upload, state note one line, events test scrolls the editor into view; synced md-editor (components d702d76+6f27ad3); gate 377/0, events 52/0mre
  • dce6ffa6Merge branch 'w28-deflake'mre
  • 536092a1tickets (mission 019): de-flake the m016 comment clicks (B: unliked) — calmClick waits until the button stops moving; test only, no app changemre
  • 9c5fd42amission 020: attachments E2E proof (lisbeth Chrome, real-data copy), reportmre
  • 1206404fREADME attachments, report 018, LOG/STATUSmre
  • 3d9e7f3ctests: m017 gate fixes (list count, waits before delete clicks); gate 373/0mre
  • dd41d5bcre-sync components 15be2ac; mission 018mre
  • 420ad1bbtickets (mission 017 WIP, tickets#27): comment attachments — lib/attachments.hl (storage/attachments, limits, type by content, members-only GET /api/attachments/:id, delete drops, edit keeps), multipart POST …/comments (lib/multipart.hl), Respond block in FilesUpload (blocks verbatim from components w27c 1f8d79d), gate m017 checks written (not yet run); report, LOG, STATUSmre
  • a7a1380dmission brief 017mre
  • f83d9c0dtickets (mission 016): report reports/016-comments-block.md, LOG, STATUS, README gate countmre
  • eef80f55tickets (mission 016): comments through the shared Comments block (layouts#2) — edit (author, earlier versions kept), delete (author, asks; leaves one line in the history), like (members, toggle) on the server (lib/comments.hl, append-only tables), faces + API …/comments/:comment/{edit,delete,like}, push commentChanged; every run of comments is a block in place in the history; Markdown fields on the Hybriel MdEditor (shared/md-editor.js retired: it took over the block's <md-editor>); blocks copied verbatim from components.hybriel 9db927d (tools/sync-components.sh); gates browser 351/0 (old 326/0), events 52/0 (old 52/0)mre
  • eb7aeeb9mission brief 016mre
  • e4c34759paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • 26efb23freport w6-events (events test)mre
  • c6bfab04mission 015: re-vendor Hybriel 0f396652 (static build), reportmre
  • b7ec5981mission brief 015mre
  • 5dc68731docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 8a460b16mission 014: Apollo's 'stream opened right after the start gets no live events' is not a tickets bug — events.mjs checks early streamsmre
  • 6309ca81mission brief 014mre