scoop:scoop.http:0.1.0
src/message/content_range.scoop
Selected production source · src/message/content_range.scoop
1
package scoop.http3
/**4
* A Content-Range value with an optional satisfied interval and optional complete representation5
* length.6
*/7
public class HttpContentRange private constructor(8
/**9
* The satisfied inclusive byte interval, or None for an unsatisfied range.10
*/11
public val range: HttpResolvedRange?,12
/**13
* The complete representation length when known.14
*/15
public val completeLength: Long?,16
/**17
* The original parsed field-value bytes, or None for a programmatically constructed value.18
*/19
public val originalValue: Array<Byte>?20
) : ToString {21
public constructor(range: HttpResolvedRange, completeLength: Long? = None)22
: this(Some(range), checkedContentLength(range, completeLength), None) {}24
/**25
* Formats the structured value as HTTP field text.26
*/27
public override fun toString(): String {28
val part = when (range) { case Some(val value) -> value.toString(); case None -> "*" }29
val length = when (completeLength) { case Some(val value) -> value.toString(); case None -> "*" }30
return "bytes " + part + "/" + length31
}32
/**33
* Serializes the structured value into HTTP field-value bytes.34
*/35
public fun toByteArray(): Array<Byte> = toString().toByteArray()36
/**37
* Builds the corresponding named HTTP field from the structured value.38
*/39
public fun toHttpField(): HttpField = HttpField("Content-Range".toByteArray(), toByteArray())41
/**42
* Construction and parsing of this structured HTTP field.43
*/44
public companion object {45
/**46
* Constructs an unsatisfied bytes range with the known complete length.47
*/48
public fun unsatisfied(completeLength: Long): HttpContentRange {49
if (completeLength < 0L) throw IllegalArgumentException(Some("negative representation length"))50
return HttpContentRange(None, Some(completeLength), None)51
}53
/**54
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.55
*/56
public fun parse(value: List<Byte>): HttpContentRange {57
val reader = HttpFieldReader("Content-Range", value)58
readBytesUnit(reader)59
reader.expect(32)60
val range: HttpResolvedRange? = if (reader.peek() == 42) {61
reader.position += 1L62
None63
} else {64
val first = reader.decimal()65
reader.expect(45)66
val offset = reader.position67
val last = reader.decimal()68
try { Some(HttpResolvedRange(first, last)) }69
catch (_: IllegalArgumentException) { reader.failAt(offset, "invalid or unrepresentable byte interval") }70
}71
reader.expect(47)72
val lengthOffset = reader.position73
val length: Long? = if (reader.peek() == 42) {74
reader.position += 1L75
None76
} else Some(reader.decimal())77
when (range) {78
case None -> if (length == None) reader.failAt(lengthOffset, "unsatisfied range needs a known length")79
case Some(val value) -> when (length) {80
case Some(val total) -> if (total <= value.last) reader.failAt(lengthOffset, "complete length does not exceed range endpoint")81
case None -> {}82
}83
}84
reader.skipOws()85
if (!reader.ended) reader.fail("unexpected text after Content-Range")86
return HttpContentRange(range, length, Some(reader.bytes))87
}88
/**89
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.90
*/91
public fun parse(value: String): HttpContentRange = parse(value.toByteArray())92
/**93
* Parses the complete value, returning None for HttpFieldParseException while propagating94
* unrelated failures.95
*/96
public fun parseOrNone(value: List<Byte>): HttpContentRange? {97
try { return Some(parse(value)) }98
catch (_: HttpFieldParseException) { return None }99
}100
/**101
* Parses the complete value, returning None for HttpFieldParseException while propagating102
* unrelated failures.103
*/104
public fun parseOrNone(value: String): HttpContentRange? = parseOrNone(value.toByteArray())105
}106
}108
private fun checkedContentLength(range: HttpResolvedRange, length: Long?): Long? {109
when (length) {110
case Some(val total) -> if (total <= range.last) throw IllegalArgumentException(Some("complete length does not exceed range endpoint"))111
case None -> {}112
}113
return length114
}