--- import: http: "./index.mq.md" response: "./response.mq.md" utf8: "std:text/utf8" --- # @exec Server-Sent Events ## start `params(url, method = "GET", body = "")` `opened = http.start(url, method, body, "text/event-stream")` `if not opened.ok` `return opened` `end` `incoming = opened.value` `if incoming.status != 200` `resource.close(incoming.stream.cursor.handle)` `return response.failure("sse.status", "Expected HTTP 200, got " + 文本(incoming.status))` `end` `content_type = map.get(incoming.headers, "content-type")` `if not event_stream(content_type)` `resource.close(incoming.stream.cursor.handle)` `return response.failure("sse.content_type", "Expected text/event-stream")` `end` `return {ok: true, value: {stream: incoming.stream, buffer: memory.new(1, 1), id: ""}}` ## event_stream `params(content_type)` `if content_type == None` `return false` `end` `points = text.codepoints(content_type)` `media = ""` `i = 0` `while i < 长度(points) and points[i] != 59` `p = points[i]` `if p >= 65 and p <= 90` `p = p + 32` `end` `media = media + text.from_codepoint(p)` `i = i + 1` `end` `return media == "text/event-stream"` ## close `params(reader)` `return resource.close(reader.stream.cursor.handle)` ## next Read one complete event and return an updated reader. Carry the last event ID forward. The caller can close the stream after any event. `params(reader)` `stream = reader.stream` `event = ""` `data = ""` `has_data = false` `id = reader.id` `remaining = 8192` `while true` `part = read_line(stream, reader.buffer)` `if not part.ok` `return part` `end` `stream = part.value.stream` `line = part.value.line` `if line == None` `return {ok: true, value: {reader: {stream: stream, buffer: reader.buffer, id: id}, event: None}}` `end` `remaining = remaining - part.value.count - 1` `if remaining < 0` `return response.failure("sse.limit", "SSE frame exceeds 8192 bytes")` `end` `if line == ""` `if has_data` `if event == ""` `event = "message"` `end` `return {ok: true, value: {reader: {stream: stream, buffer: reader.buffer, id: id}, event: {kind: event, id: id, data: data}}}` `end` `event = ""` `remaining = 8192` `else` `field = split_field(line)` `if field.name == "event"` `event = field.value` `end` `if field.name == "id" and not field.nul` `id = field.value` `end` `if field.name == "data"` `if has_data` `data = data + "\n"` `end` `data = data + field.value` `has_data = true` `end` `end` `end` ## read_line `params(initial, buf)` `stream = initial` `length = 0` `while length <= 4096` `item = response.byte(stream)` `if not item.ok` `return item` `end` `stream = item.value.stream` `b = item.value.byte` `if b == None` `return {ok: true, value: {line: None, stream: stream}}` `end` `if b == 10` `size = length` `if size > 0 and buf[size - 1] == 13` `size = size - 1` `end` `decoded = utf8.解码(buf, 0, size)` `if not decoded.ok` `return decoded` `end` `return {ok: true, value: {line: decoded.value, stream: stream, count: length}}` `end` `buf[length] = b` `length = length + 1` `end` `return response.failure("sse.line_limit", "SSE line exceeds 4096 bytes")` ## split_field `params(line)` `points = text.codepoints(line)` `name = ""` `i = 0` `while i < 长度(points) and points[i] != 58` `name = name + text.from_codepoint(points[i])` `i = i + 1` `end` `if i < 长度(points)` `i = i + 1` `end` `if i < 长度(points) and points[i] == 32` `i = i + 1` `end` `value = ""` `nul = false` `while i < 长度(points)` `if points[i] == 0` `nul = true` `end` `value = value + text.from_codepoint(points[i])` `i = i + 1` `end` `return {name: name, value: value, nul: nul}`