scoop:scoop.http:0.1.0
src/sse/state.scoop
Selected production source · src/sse/state.scoop
1
package scoop.http3
import scoop.bytes.ByteArrayBuilder4
import scoop.charset.*5
import scoop.text.*7
internal 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 = 0L11
private var firstLine: Boolean = true12
private var skipLf: Boolean = false13
private var eventType: String = ""14
private var idBuffer: String = initialLastEventId15
private var blockId: String? = None16
var lastEventId: String = initialLastEventId17
var retryMilliseconds: Long? = None18
var pending: SseEvent? = None19
var ended: Boolean = false20
val hasPending: Boolean get() = when (pending) { case None -> false; case Some(_) -> true }22
fun accept(byte: Byte) {23
if (skipLf) {24
skipLf = false25
if (byte == 10.toByte()) return26
}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 = lastEventId41
}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 = false47
line.clear()48
if (text.byteLength == 0L) { dispatch(); return }49
val colon = text.indexOf(':')50
if (colon == 0L) return51
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 = value57
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 1L65
val remaining = limits.maxEventBytes - dataBytes66
if (separator > remaining || value.byteLength > remaining - separator)67
throw SseLimitException(SseLimitKind.EventBytes, limits.maxEventBytes)68
dataBytes += separator + value.byteLength69
data.add(value)70
}72
private fun dispatch() {73
lastEventId = idBuffer74
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 = 0L88
eventType = ""89
blockId = None90
}91
}93
private fun sseRetry(value: String): Long? {94
if (value.byteLength == 0L) return None95
var result = 0L96
for (byte in value.toByteArray()) {97
val digit = byte.toUByte().toLong() - 48L98
if (digit < 0L || digit > 9L) return None99
result = if (result > (Long.MAX_VALUE - digit) / 10L) Long.MAX_VALUE else result * 10L + digit100
}101
return Some(result)102
}