Scoop API

scoop:scoop.http:0.1.0

src/sse/state.scoop

Selected production source · src/sse/state.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
4import scoop.charset.*
5import scoop.text.*
7internal class SseState(private val limits: SseLimits, initialLastEventId: String) {
8 private var line: ByteArrayBuilder = ByteArrayBuilder()
9 private var data: ArrayList<String> = ArrayList<String>()
10 private var dataBytes: Long = 0L
11 private var firstLine: Boolean = true
12 private var skipLf: Boolean = false
13 private var eventType: String = ""
14 private var idBuffer: String = initialLastEventId
15 private var blockId: String? = None
16 var lastEventId: String = initialLastEventId
17 var retryMilliseconds: Long? = None
18 var pending: SseEvent? = None
19 var ended: Boolean = false
20 val hasPending: Boolean get() = when (pending) { case None -> false; case Some(_) -> true }
22 fun accept(byte: Byte) {
23 if (skipLf) {
24 skipLf = false
25 if (byte == 10.toByte()) return
26 }
27 if (byte == 13.toByte() || byte == 10.toByte()) {
28 skipLf = byte == 13.toByte()
29 finishLine()
30 } else {
31 if (line.size == limits.maxLineBytes) throw SseLimitException(SseLimitKind.LineBytes, limits.maxLineBytes)
32 line.append(byte)
33 }
34 }
36 fun discardIncomplete() {
37 line = ByteArrayBuilder()
38 data = ArrayList<String>()
39 resetBlock()
40 idBuffer = lastEventId
41 }
43 private fun finishLine() {
44 val text = if (line.size == 0L) "" else Charset.Utf8.decodeLossy(line.toByteArray(), CharsetDecodeOptions(
45 bom = if (firstLine) CharsetBomPolicy.Consume else CharsetBomPolicy.Preserve))
46 firstLine = false
47 line.clear()
48 if (text.byteLength == 0L) { dispatch(); return }
49 val colon = text.indexOf(':')
50 if (colon == 0L) return
51 val name = if (colon < 0L) text else text.substring(0L, colon)
52 var value = if (colon < 0L) "" else text.substring(colon + 1L)
53 if (value.startsWith(" ")) value = value.substring(1L)
54 when (name) {
55 case "data" -> appendData(value)
56 case "event" -> eventType = value
57 case "id" -> if (value.indexOf('\u0000') < 0L) { idBuffer = value; blockId = Some(value) }
58 case "retry" -> when (sseRetry(value)) { case Some(val milliseconds) -> retryMilliseconds = Some(milliseconds); case None -> {} }
59 else -> {}
60 }
61 }
63 private fun appendData(value: String) {
64 val separator = if (data.size == 0L) 0L else 1L
65 val remaining = limits.maxEventBytes - dataBytes
66 if (separator > remaining || value.byteLength > remaining - separator)
67 throw SseLimitException(SseLimitKind.EventBytes, limits.maxEventBytes)
68 dataBytes += separator + value.byteLength
69 data.add(value)
70 }
72 private fun dispatch() {
73 lastEventId = idBuffer
74 if (data.size > 0L) {
75 val text = StringBuilder()
76 for (index in 0L..<data.size) {
77 if (index > 0L) text.append("\n")
78 text.append(data[index])
79 }
80 pending = Some(SseEvent(if (eventType.byteLength == 0L) "message" else eventType, text.toString(), blockId, lastEventId))
81 }
82 resetBlock()
83 }
85 private fun resetBlock() {
86 data.clear()
87 dataBytes = 0L
88 eventType = ""
89 blockId = None
90 }
93private fun sseRetry(value: String): Long? {
94 if (value.byteLength == 0L) return None
95 var result = 0L
96 for (byte in value.toByteArray()) {
97 val digit = byte.toUByte().toLong() - 48L
98 if (digit < 0L || digit > 9L) return None
99 result = if (result > (Long.MAX_VALUE - digit) / 10L) Long.MAX_VALUE else result * 10L + digit
100 }
101 return Some(result)