tickets
All repositories: gitoria
4.6 KB
// lib/multipart.hl — A multipart/form-data BODY, read from its bytes (tickets#27, mission 017: a comment with files through the API,// `curl -F text=… -F [email protected]`). Plumbing for lib/api.hl; statics only. The body's bytes are searched as hex text (`Bytes.hex()`:// the native string search finds the boundary; only matches at an even offset are byte positions), the parts are cut out as Bytes.// 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).static CRLF = JSON.parse('"' + 'x\y'.slice(1, 2) + 'r' + 'x\y'.slice(1, 2) + 'n"')// the boundary of a `multipart/form-data; boundary=…` content type, or nullstatic boundaryOf = (contentType) => {if (contentType == null || hlTypeName(contentType) != 'String') { return null }if (!contentType.toLowerCase().trim().startsWith('multipart/form-data')) { return null }for (p of contentType.split(';')) {kv = p.trim()if (kv.toLowerCase().startsWith('boundary=')) {let b = kv.slice(9).trim()if (b.startsWith('"') && b.endsWith('"') && b.length >= 2) { b = b.slice(1, b.length - 1) }return b != '' && b.length <= 200 ? b : null}}return null}static isMultipart = (req) => { return boundaryOf(req.headers['content-type']) != null }// `needle` (hex) in `hex` at an even offset (a byte position) at or after `start`, or -1static hexFind = (hex, needle, start) => {let at = hex.indexOf(needle, start)while (at >= 0 && at % 2 == 1) { at = hex.indexOf(needle, at + 1) }return at}// are these bytes UTF-8? (Bytes.toString() stops the program on a bad sequence)static isUtf8 = (b) => {let i = 0n = b.lengthwhile (i < n) {c = b[i]let more = 0if (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 }if (more > 0 && i + more >= n) { return false }let k = 1while (k <= more) {if (b[i + k] < 128 || b[i + k] > 191) { return false }k = k + 1}i = i + more + 1}return true}// one header line's parameters: `form-data; name="file"; filename="a b.png"` → { name, filename }static dispositionOf = (value) => {out = { name = null filename = null }for (p of value.split(';')) {kv = p.trim()eq = kv.indexOf('=')if (eq > 0) {k = kv.slice(0, eq).trim().toLowerCase()let v = kv.slice(eq + 1).trim()if (v.startsWith('"') && v.endsWith('"') && v.length >= 2) { v = v.slice(1, v.length - 1) }if (k == 'name') { out.name = v }if (k == 'filename') { out.filename = v }}}return out}// THE PARTS: { parts: [{ name, filename (null = a plain field), type, bytes }] } or { error }static partsOf = (req) => {boundary = boundaryOf(req.headers['content-type'])if (boundary == null) { return { error = 'a multipart body needs Content-Type: multipart/form-data; boundary=…' } }body = req.bytesif (body == null || hlTypeName(body) != 'Bytes' || body.length == 0) { return { error = 'the multipart body is empty' } }hex = body.hex()delim = toBytes('--' + boundary).hex()next = toBytes(CRLF + '--' + boundary).hex()crlf2 = toBytes(CRLF + CRLF).hex()parts = []let at = hexFind(hex, delim, 0)if (at < 0) { return { error = 'the multipart body does not hold its boundary' } }let guard = 0while (guard < 1000) {guard = guard + 1after = at + delim.length// `--boundary--` ends the bodyif (hex.slice(after, after + 4) == '2d2d') { return { parts = parts } }if (hex.slice(after, after + 4) != '0d0a') { return { error = 'a multipart boundary is not followed by a line break' } }hs = after + 4he = hexFind(hex, crlf2, hs)if (he < 0) { return { error = 'a multipart part has no end of its headers' } }head = body.slice(hs / 2, he / 2)if (!isUtf8(head)) { return { error = 'the headers of a multipart part are not UTF-8' } }let name = nulllet filename = nulllet type = ''for (line of head.toString().split(CRLF)) {colon = line.indexOf(':')if (colon > 0) {k = line.slice(0, colon).trim().toLowerCase()v = line.slice(colon + 1).trim()if (k == 'content-disposition') {d = dispositionOf(v)name = d.namefilename = d.filename}if (k == 'content-type') { type = v }}}if (name == null) { return { error = 'a multipart part has no Content-Disposition name' } }cs = he + crlf2.lengthce = hexFind(hex, next, cs)if (ce < 0) { return { error = 'the multipart body ends inside a part (no closing boundary)' } }parts.push({ name = name filename = filename type = type bytes = body.slice(cs / 2, ce / 2) })at = ce + 4}return { error = 'too many multipart parts' }}
Branches
Latest commits
- b03f11b6Tickets conductor: CONDUCTOR.md (project part), short STATUS.md, old STATUS to archive/2026-10-31mre
- a4123831tickets#42: the /connect and /login/callback error pages are app pages: the function routes redirect to /connect/failed/<code> and /login/failed/<code> (components/problem.hl, sentences in lib/problems.hl), htmlPage and its hand-written stylesheet are gone; gate tests/error-pages.mjsmre
- cefc7b4atickets: a startup error prints its message before exit 1 (hybriel#247)mre
- 99bd6570tickets: lib/migrate.hl writes no table of another topic any more: projects.hl rewriteSlug / addProjectNamed / hasMembers, tickets.hl moveToProject / ticketRecords / ticketRecordsOf, events.hl renameStates / eventRecords / eventRecordsOf; the migration calls them (tickets#47)mre
- b57161e3tickets: the markup names its own rules (Style.pMessage, Style.olTickets, ...) instead of hand-written class strings that copy a stylesheet rule; the shared bodies sit in lib/looks.hl (tickets#38)mre
- f767b24dtickets: 111 needless let became plain declarations (3 in ticket.hl, ticket_list.hl, comments.hl and 108 never-reassigned, not captured loop-body ones); the 212 really reassigned lets stay (tickets#44)mre
- 16c0d3b5tickets: the modal window is phone first: the full-screen sheet is the base rule, the centred window sits in the min-width 60rem block (tickets#39)mre
- 090ee809tickets: every component's styles in its own nested Style {} block; styles.hl keeps only the theme inherit, the accent and the site-wide rules (tickets#36)mre
- d395763ftickets: ident-access gate ports 8769/8768 (8797 is taken on loreana)mre
- afb9a403tickets: an ident access key (Authorization: Bearer idt_…) works in the API (ident#27 app side): lib/ident-access.hl copied from components, read keys GET only, ident down 503; gate tests/ident-access-tickets.mjsmre
- 48c1ba79tickets: md-paste-files gate with the file manager casesmre
- f04afc43tickets: MdEditor from components 59704ce (a file manager copy still attaches, pipe rows, tickets#49)mre
- 06599fa0tickets: MdEditor from components b0f1ec2 (a clipboard with text always pastes the text, tickets#49); real Ctrl+V gatemre
- 2e1b116ftickets: re-vendor Hybriel master acaceaf3 (framework ec8d6c1d): plugins/web patch queue while a page is built, anonymous session cookies refresh; gate waits for the comment editors to draw (W174) and clicks the form editor when it stands still; security gate: main's code runs on its own binary, lazy-session cookie checksmre
- 361bc0fetickets: re-vendor Hybriel 7a684920 (WIP, not deployed); on Error exits 1 at startup; migrate passes its log by reference; gate checks follow lazy sessions and the 413 refusalmre
- 8a69999cident#28: /login/callback never signs out the session it finds; a valid code puts the user on the same sessionmre
- 337e9b21tickets#37: a comment's files are open to everyone who can read the commentmre
- 1800600ftickets: write the standby service out in full so the boot gate can read the main servicemre
- 8179d287tickets: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
- 0398145ctickets: the table grid rule is marked functional for check-thememre