308 lines
7.4 KiB
Markdown
308 lines
7.4 KiB
Markdown
---
|
|
import:
|
|
utf8: "std:text/utf8"
|
|
---
|
|
|
|
# @exec HTTP response framing
|
|
|
|
## failure
|
|
|
|
`params(code, message)`
|
|
`return {ok: false, code: code, message: message}`
|
|
|
|
## number
|
|
|
|
Parse a bounded unsigned decimal or hexadecimal number.
|
|
|
|
`params(token, base, limit)`
|
|
`points = text.codepoints(token)`
|
|
`if 长度(points) == 0`
|
|
`return None`
|
|
`end`
|
|
`n = 0`
|
|
`i = 0`
|
|
`while i < 长度(points)`
|
|
`p = points[i]`
|
|
`digit = p - 48`
|
|
`if p < 48 or p > 57`
|
|
`digit = base`
|
|
`if base == 16 and p >= 65 and p <= 70`
|
|
`digit = p - 55`
|
|
`end`
|
|
`if base == 16 and p >= 97 and p <= 102`
|
|
`digit = p - 87`
|
|
`end`
|
|
`end`
|
|
`if digit >= base`
|
|
`return None`
|
|
`end`
|
|
`n = n * base + digit`
|
|
`if n > limit`
|
|
`return None`
|
|
`end`
|
|
`i = i + 1`
|
|
`end`
|
|
`return n`
|
|
|
|
## raw_byte
|
|
|
|
`params(initial)`
|
|
`cursor = initial`
|
|
`if cursor.index == cursor.count and not cursor.eof`
|
|
`read = resource.read(cursor.handle, cursor.buf, 0, 1024)`
|
|
`if not read.ok`
|
|
`return read`
|
|
`end`
|
|
`cursor = {handle: cursor.handle, buf: cursor.buf, index: 0, count: read.value.count, eof: read.value.eof}`
|
|
`end`
|
|
`if cursor.index == cursor.count`
|
|
`return {ok: true, value: {byte: None, cursor: cursor}}`
|
|
`end`
|
|
`b = cursor.buf[cursor.index]`
|
|
`return {ok: true, value: {byte: b, cursor: map.set(cursor, "index", cursor.index + 1)}}`
|
|
|
|
## raw_line
|
|
|
|
Read a bounded ASCII line ending in CRLF, including across resource reads.
|
|
|
|
`params(cursor)`
|
|
`line = ""`
|
|
`ended = false`
|
|
`while 长度(line) <= 2048`
|
|
`result = raw_byte(cursor)`
|
|
`if not result.ok`
|
|
`return result`
|
|
`end`
|
|
`cursor = result.value.cursor`
|
|
`b = result.value.byte`
|
|
`if b == None`
|
|
`return failure("http.eof", "Unexpected EOF in HTTP framing")`
|
|
`end`
|
|
`if ended and b == 10`
|
|
`return {ok: true, value: {line: line, cursor: cursor}}`
|
|
`end`
|
|
`if ended or b >= 127 or (b < 32 and b != 9 and b != 13)`
|
|
`return failure("http.framing", "Invalid HTTP framing line")`
|
|
`end`
|
|
`if b == 13`
|
|
`ended = true`
|
|
`else`
|
|
`line = line + text.from_codepoint(b)`
|
|
`end`
|
|
`end`
|
|
`return failure("http.line_limit", "HTTP framing line exceeds 2048 bytes")`
|
|
|
|
## field
|
|
|
|
`params(line)`
|
|
`points = text.codepoints(line)`
|
|
`i = 0`
|
|
`key = ""`
|
|
`while i < 长度(points) and points[i] != 58`
|
|
`p = points[i]`
|
|
`if p >= 65 and p <= 90`
|
|
`p = p + 32`
|
|
`end`
|
|
`if not ((p >= 97 and p <= 122) or (p >= 48 and p <= 57) or p == 45)`
|
|
`return failure("http.header", "Unsupported HTTP header name")`
|
|
`end`
|
|
`key = key + text.from_codepoint(p)`
|
|
`i = i + 1`
|
|
`end`
|
|
`if i == 0 or i == 长度(points)`
|
|
`return failure("http.header", "Invalid HTTP header")`
|
|
`end`
|
|
`i = i + 1`
|
|
`j = 长度(points)`
|
|
`while i < j and (points[i] == 32 or points[i] == 9)`
|
|
`i = i + 1`
|
|
`end`
|
|
`while j > i and (points[j - 1] == 32 or points[j - 1] == 9)`
|
|
`j = j - 1`
|
|
`end`
|
|
`value = ""`
|
|
`while i < j`
|
|
`value = value + text.from_codepoint(points[i])`
|
|
`i = i + 1`
|
|
`end`
|
|
`return {ok: true, value: {key: key, text: value}}`
|
|
|
|
## open
|
|
|
|
`params(handle, buf)`
|
|
`first = raw_line({handle: handle, buf: buf, index: 0, count: 0, eof: false})`
|
|
`if not first.ok`
|
|
`return first`
|
|
`end`
|
|
`cursor = first.value.cursor`
|
|
`points = text.codepoints(first.value.line)`
|
|
`if 长度(points) < 12`
|
|
`return failure("http.status", "Invalid HTTP status line")`
|
|
`end`
|
|
`prefix = ""`
|
|
`i = 0`
|
|
`while i < 9`
|
|
`prefix = prefix + text.from_codepoint(points[i])`
|
|
`i = i + 1`
|
|
`end`
|
|
`if prefix != "HTTP/1.1 " and prefix != "HTTP/1.0 "`
|
|
`return failure("http.status", "Unsupported HTTP protocol version")`
|
|
`end`
|
|
`digits = text.from_codepoint(points[9]) + text.from_codepoint(points[10]) + text.from_codepoint(points[11])`
|
|
`status = number(digits, 10, 599)`
|
|
`if status == None or status < 200 or (长度(points) > 12 and points[12] != 32)`
|
|
`return failure("http.status", "Unsupported HTTP status")`
|
|
`end`
|
|
`headers = {}`
|
|
`remaining = 32`
|
|
`while remaining > 0`
|
|
`part = raw_line(cursor)`
|
|
`if not part.ok`
|
|
`return part`
|
|
`end`
|
|
`cursor = part.value.cursor`
|
|
`line = part.value.line`
|
|
`if line == ""`
|
|
`return framed(cursor, status, headers)`
|
|
`end`
|
|
`header = field(line)`
|
|
`if not header.ok`
|
|
`return header`
|
|
`end`
|
|
`entry = header.value`
|
|
`if map.has(headers, entry.key) and (entry.key == "content-length" or entry.key == "transfer-encoding")`
|
|
`return failure("http.framing", "Duplicate HTTP framing header")`
|
|
`end`
|
|
`headers = map.set(headers, entry.key, entry.text)`
|
|
`remaining = remaining - 1`
|
|
`end`
|
|
`return failure("http.header_limit", "Too many HTTP headers")`
|
|
|
|
## framed
|
|
|
|
`params(cursor, status, headers)`
|
|
`size = map.get(headers, "content-length")`
|
|
`coding = map.get(headers, "transfer-encoding")`
|
|
`framing = "close"`
|
|
`left = 0`
|
|
`if size != None`
|
|
`left = number(size, 10, 32768)`
|
|
`if left == None or coding != None`
|
|
`return failure("http.framing", "Invalid or ambiguous response length")`
|
|
`end`
|
|
`framing = "fixed"`
|
|
`end`
|
|
`if coding != None`
|
|
`if coding != "chunked" and coding != "Chunked"`
|
|
`return failure("http.framing", "Unsupported Transfer-Encoding")`
|
|
`end`
|
|
`framing = "chunked"`
|
|
`end`
|
|
`if status == 204 or status == 304`
|
|
`framing = "fixed"`
|
|
`left = 0`
|
|
`end`
|
|
`stream = {cursor: cursor, framing: framing, left: left, separator: false, done: false, total: 0}`
|
|
`return {ok: true, value: {status: status, headers: headers, stream: stream}}`
|
|
|
|
## next_chunk
|
|
|
|
`params(initial)`
|
|
`stream = initial`
|
|
`if stream.separator`
|
|
`separator = raw_line(stream.cursor)`
|
|
`if not separator.ok`
|
|
`return separator`
|
|
`end`
|
|
`if separator.value.line != ""`
|
|
`return failure("http.chunked", "Missing CRLF after HTTP chunk")`
|
|
`end`
|
|
`stream = map.set(stream, "cursor", separator.value.cursor)`
|
|
`end`
|
|
`size = raw_line(stream.cursor)`
|
|
`if not size.ok`
|
|
`return size`
|
|
`end`
|
|
`digits = ""`
|
|
`points = text.codepoints(size.value.line)`
|
|
`i = 0`
|
|
`while i < 长度(points) and points[i] != 59`
|
|
`digits = digits + text.from_codepoint(points[i])`
|
|
`i = i + 1`
|
|
`end`
|
|
`count = number(digits, 16, 32768 - stream.total)`
|
|
`if count == None`
|
|
`return failure("http.chunked", "Invalid or oversized HTTP chunk")`
|
|
`end`
|
|
`stream = map.set(stream, "cursor", size.value.cursor)`
|
|
`if count > 0`
|
|
`return {ok: true, value: map.set(map.set(stream, "left", count), "separator", true)}`
|
|
`end`
|
|
`remaining = 32`
|
|
`while remaining > 0`
|
|
`trailer = raw_line(stream.cursor)`
|
|
`if not trailer.ok`
|
|
`return trailer`
|
|
`end`
|
|
`stream = map.set(stream, "cursor", trailer.value.cursor)`
|
|
`if trailer.value.line == ""`
|
|
`return {ok: true, value: map.set(stream, "done", true)}`
|
|
`end`
|
|
`remaining = remaining - 1`
|
|
`end`
|
|
`return failure("http.header_limit", "Too many HTTP trailers")`
|
|
|
|
## byte
|
|
|
|
`params(initial)`
|
|
`stream = initial`
|
|
`if stream.framing == "chunked" and stream.left == 0 and not stream.done`
|
|
`chunk = next_chunk(stream)`
|
|
`if not chunk.ok`
|
|
`return chunk`
|
|
`end`
|
|
`stream = chunk.value`
|
|
`end`
|
|
`if stream.done or (stream.framing == "fixed" and stream.left == 0)`
|
|
`return {ok: true, value: {byte: None, stream: stream}}`
|
|
`end`
|
|
`raw = raw_byte(stream.cursor)`
|
|
`if not raw.ok`
|
|
`return raw`
|
|
`end`
|
|
`stream = map.set(stream, "cursor", raw.value.cursor)`
|
|
`b = raw.value.byte`
|
|
`if b == None and stream.framing != "close"`
|
|
`return failure("http.eof", "Unexpected EOF in HTTP body")`
|
|
`end`
|
|
`if b != None`
|
|
`if stream.total >= 32768`
|
|
`return failure("http.body_limit", "Response exceeds 32768 bytes")`
|
|
`end`
|
|
`stream = map.set(stream, "total", stream.total + 1)`
|
|
`if stream.framing != "close"`
|
|
`stream = map.set(stream, "left", stream.left - 1)`
|
|
`end`
|
|
`end`
|
|
`return {ok: true, value: {byte: b, stream: stream}}`
|
|
|
|
## collect
|
|
|
|
`params(initial)`
|
|
`stream = initial`
|
|
`body = memory.new(1, 1)`
|
|
`length = 0`
|
|
`while true`
|
|
`result = byte(stream)`
|
|
`if not result.ok`
|
|
`return result`
|
|
`end`
|
|
`stream = result.value.stream`
|
|
`b = result.value.byte`
|
|
`if b == None`
|
|
`return utf8.解码(body, 0, length)`
|
|
`end`
|
|
`body[length] = b`
|
|
`length = length + 1`
|
|
`end`
|