scoop:scoop.http:0.1.0
src/negotiation.scoop
Selected production source · src/negotiation.scoop
1
package scoop.http3
import scoop.bytes.ByteArrayBuilder4
import scoop.platform.memory.NativeByteBuffer6
/**7
* HTTP protocol negotiation failed for the named operation.8
*/9
public class HttpNegotiationException public constructor(10
/**11
* The negotiation operation that failed.12
*/13
public val operation: String,14
/**15
* The parse or negotiation failure explanation.16
*/17
public val detail: String)18
: Exception(Some(operation + ": " + detail))20
internal fun httpAllows(protocols: List<HttpProtocol>, protocol: HttpProtocol): Boolean {21
for (value in protocols) if (value == protocol) return true22
return false23
}25
/**26
* Maps negotiated h2 or http/1.1 to an allowed protocol. Absent ALPN uses HTTP/1 only when27
* fallback is explicitly enabled.28
*/29
public fun selectHttpAlpn(selected: List<Byte>?, allowed: List<HttpProtocol>, allowHttp1Fallback: Boolean = false): HttpProtocol {30
if (allowed.size == 0L) throw IllegalArgumentException(Some("no HTTP protocols are allowed"))31
val protocol = when (selected) {32
case None -> {33
if (!allowHttp1Fallback) throw HttpNegotiationException("select ALPN", "peer did not negotiate an HTTP protocol")34
HttpProtocol.Http135
}36
case Some(val bytes) -> {37
if (httpBytesEqual(bytes, "h2".toByteArray())) HttpProtocol.Http238
else if (httpBytesEqual(bytes, "http/1.1".toByteArray())) HttpProtocol.Http139
else throw HttpNegotiationException("select ALPN", "peer negotiated an unknown HTTP protocol")40
}41
}42
if (!httpAllows(allowed, protocol)) throw HttpNegotiationException("select ALPN", "negotiated HTTP protocol is not allowed")43
return protocol44
}46
/**47
* Incrementally distinguishes the HTTP/2 client preface from HTTP/1, retaining consumed prefix48
* bytes and leaving the remaining input untouched.49
*/50
public class HttpCleartextProbe public constructor(allowed: List<HttpProtocol>) {51
private val http1: Boolean = httpAllows(allowed, HttpProtocol.Http1)52
private val http2: Boolean = httpAllows(allowed, HttpProtocol.Http2)53
private val magic: Array<Byte> = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n".toByteArray()54
private val buffered: ByteArrayBuilder = ByteArrayBuilder()55
private var selected: HttpProtocol? = None56
private var failed: Boolean = false57
init { if (!http1 && !http2) throw IllegalArgumentException(Some("no cleartext HTTP protocols are allowed")) }59
/**60
* The selected protocol, or None while the prefix remains ambiguous.61
*/62
public val protocol: HttpProtocol? get() = selected63
/**64
* A snapshot of all bytes consumed during probing, to pass to the selected protocol decoder.65
*/66
public val prefix: Array<Byte> get() = buffered.toByteArray()68
/**69
* Consumes only enough bytes to decide the protocol, preserving the remainder in the input70
* buffer.71
*/72
public fun receive(buffer: NativeByteBuffer, count: Long = buffer.readableBytes): Long {73
if (failed) throw IllegalStateException(Some("HTTP protocol probe has failed"))74
if (count < 0L || count > buffer.readableBytes) throw IllegalArgumentException(Some("invalid HTTP probe input count"))75
var consumed = 0L76
while (consumed < count && selected == None) {77
val byte = buffer.readInt8()78
val index = buffered.size79
buffered.append(byte)80
consumed += 1L81
if (byte != magic[index]) {82
if (!http1) reject("cleartext input is not HTTP/2 prior knowledge")83
selected = Some(HttpProtocol.Http1)84
} else if (buffered.size == magic.size) {85
if (!http2) reject("HTTP/2 prior knowledge is not allowed")86
selected = Some(HttpProtocol.Http2)87
}88
}89
return consumed90
}92
/**93
* Returns the selected protocol or rejects EOF during an incomplete preface.94
*/95
public fun endInput(): HttpProtocol {96
if (failed) throw IllegalStateException(Some("HTTP protocol probe has failed"))97
return when (selected) {98
case Some(_) -> selected!!99
case None -> { reject("input ended before the HTTP/2 preface was complete") }100
}101
}103
private fun reject(message: String): Nothing {104
failed = true105
throw HttpNegotiationException("select cleartext HTTP", message)106
}107
}