Scoop API

scoop:scoop.http:0.1.0

src/message/range_types.scoop

Selected production source · src/message/range_types.scoop

1package scoop.http
3/**
4 * A closed interval, open-ended interval, or suffix byte-count request.
5 */
6public enum HttpByteRange {
7 /**
8 * Requests first through last byte offsets, both inclusive.
9 */
10 FromTo(Long, Long),
11 /**
12 * Requests the first offset through the representation end.
13 */
14 From(Long),
15 /**
16 * Requests the final count bytes of the representation.
17 */
18 Suffix(Long) }
20/**
21 * A validated nonnegative inclusive byte interval whose length fits Long.
22 */
23public class HttpResolvedRange public constructor(
24 /**
25 * The inclusive first byte offset.
26 */
27 public val first: Long,
28 /**
29 * The inclusive final byte offset.
30 */
31 public val last: Long) : ToString {
32 init {
33 if (first < 0L || last < first || last - first == Long.MAX_VALUE)
34 throw IllegalArgumentException(Some("invalid or unrepresentable byte interval"))
35 }
36 /**
37 * The exact number of bytes, last - first + 1.
38 */
39 public val length: Long get() = last - first + 1L
40 /**
41 * Formats the inclusive first-last interval.
42 */
43 public override fun toString(): String = first.toString() + "-" + last.toString()
46/**
47 * The satisfiable resolved byte intervals and whether any requested range can be served.
48 */
49public class HttpRangeResolution internal constructor(
50 /**
51 * The resolved satisfiable intervals.
52 */
53 public val ranges: Array<HttpResolvedRange>,
54 /**
55 * Whether at least one requested interval can be served.
56 */
57 public val isSatisfiable: Boolean
60internal fun checkedHttpRanges(ranges: List<HttpByteRange>): Array<HttpByteRange> {
61 if (ranges.size == 0L) throw IllegalArgumentException(Some("empty byte range set"))
62 val result = ranges.toArray()
63 for (range in result) {
64 val valid = when (range) {
65 case FromTo(val first, val last) -> first >= 0L && last >= first
66 case From(val first) -> first >= 0L
67 case Suffix(val length) -> length >= 0L
68 }
69 if (!valid) throw IllegalArgumentException(Some("invalid byte range"))
70 }
71 return result
74internal fun formatHttpByteRange(range: HttpByteRange): String = when (range) {
75 case FromTo(val first, val last) -> first.toString() + "-" + last.toString()
76 case From(val first) -> first.toString() + "-"
77 case Suffix(val length) -> "-" + length.toString()