scoop:scoop.http:0.1.0
src/message/entity_tag.scoop
Selected production source · src/message/entity_tag.scoop
1
package scoop.http3
import scoop.bytes.ByteArrayBuilder5
/**6
* An opaque entity tag with an optional weak marker. Weak and strong comparisons are explicit.7
*/8
public class HttpEntityTag private constructor(9
/**10
* The opaque tag bytes without quotes or weak prefix.11
*/12
public val opaque: Array<Byte>,13
/**14
* Whether the tag carries the weak W/ marker.15
*/16
public val isWeak: Boolean) {17
public constructor(opaque: List<Byte>, isWeak: Boolean = false) : this(checkedEntityTag(opaque), isWeak) {}19
/**20
* Requires equal opaque bytes and two nonweak tags.21
*/22
public fun stronglyMatches(other: HttpEntityTag): Boolean = !isWeak && !other.isWeak && weaklyMatches(other)23
/**24
* Compares opaque bytes while ignoring weak markers.25
*/26
public fun weaklyMatches(other: HttpEntityTag): Boolean = httpBytesEqual(opaque, other.opaque)27
/**28
* Serializes the structured value into HTTP field-value bytes.29
*/30
public fun toByteArray(): Array<Byte> {31
val output = ByteArrayBuilder()32
output.append((if (isWeak) "W/\"" else "\"").toByteArray())33
output.append(opaque)34
output.append(34.toByte())35
return output.toByteArray()36
}37
/**38
* Builds the corresponding named HTTP field from the structured value.39
*/40
public fun toHttpField(): HttpField = HttpField("ETag".toByteArray(), toByteArray())42
/**43
* Construction and parsing of this structured HTTP field.44
*/45
public companion object {46
/**47
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.48
*/49
public fun parse(value: List<Byte>): HttpEntityTag {50
val reader = HttpFieldReader("ETag", value)51
reader.skipOws()52
val result = read(reader)53
reader.skipOws()54
if (!reader.ended) reader.fail("unexpected text after entity tag")55
return result56
}57
/**58
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.59
*/60
public fun parse(value: String): HttpEntityTag = parse(value.toByteArray())61
/**62
* Parses the complete value, returning None for HttpFieldParseException while propagating63
* unrelated failures.64
*/65
public fun parseOrNone(value: List<Byte>): HttpEntityTag? {66
try { return Some(parse(value)) }67
catch (_: HttpFieldParseException) { return None }68
}69
/**70
* Parses the complete value, returning None for HttpFieldParseException while propagating71
* unrelated failures.72
*/73
public fun parseOrNone(value: String): HttpEntityTag? = parseOrNone(value.toByteArray())75
internal fun read(reader: HttpFieldReader): HttpEntityTag {76
val weak = reader.peek() == 8777
if (weak) { reader.position += 1L; reader.expect(47) }78
reader.expect(34)79
val start = reader.position80
while (reader.peek() != 34) {81
if (reader.ended) reader.fail("unterminated entity tag")82
if (!entityTagByte(reader.peek())) reader.fail("invalid entity tag byte")83
reader.position += 1L84
}85
val bytes = reader.slice(start, reader.position)86
reader.position += 1L87
return HttpEntityTag(bytes, weak)88
}89
}90
}92
private fun entityTagByte(value: Int): Boolean = value == 33 || (value >= 35 && value != 127 && value <= 255)94
private fun checkedEntityTag(value: List<Byte>): Array<Byte> {95
val bytes = value.toArray()96
for (byte in bytes) if (!entityTagByte(byte.toUByte().toInt()))97
throw IllegalArgumentException(Some("invalid entity tag byte"))98
return bytes99
}