Scoop API

scoop:scoop.http:0.1.0

src/message/reader.scoop

Selected production source · src/message/reader.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
4import scoop.collections.HashSet
5import scoop.text.lowercaseAscii
7/**
8 * A structured-field syntax or interpretation error, retaining the field name and zero-based
9 * source byte offset.
10 */
11public class HttpFieldParseException public constructor(
12 /**
13 * The HTTP field whose interpretation failed.
14 */
15 public val field: String,
16 /**
17 * The zero-based byte offset in the field input.
18 */
19 public val byteOffset: Long,
20 /**
21 * The parse or negotiation failure explanation.
22 */
23 public val detail: String,
24 /**
25 * The underlying failure when available.
26 */
27 public val cause: Throwable? = None
28) : Exception(Some(field + " at byte " + byteOffset.toString() + ": " + detail))
30internal class HttpFieldReader(val field: String, data: List<Byte>) {
31 val bytes: Array<Byte> = data.toArray()
32 var position: Long = 0L
33 val ended: Boolean get() = position == bytes.size
34 fun peek(): Int = if (ended) -1 else bytes[position].toUByte().toInt()
35 fun fail(detail: String): Nothing = throw HttpFieldParseException(field, position, detail)
36 fun failAt(offset: Long, detail: String): Nothing = throw HttpFieldParseException(field, offset, detail)
37 fun skipOws() { while (peek() == 32 || peek() == 9) position += 1L }
38 fun expect(value: Int) {
39 if (peek() != value) fail("expected '" + value.toChar().toString() + "'")
40 position += 1L
41 }
42 fun token(): String {
43 val start = position
44 while (httpTokenByte(peek())) position += 1L
45 if (start == position) fail("expected HTTP token")
46 return String.fromUtf8(slice(start, position))
47 }
48 fun slice(start: Long, end: Long): Array<Byte> = Array<Byte>(end - start) { index -> bytes[start + index] }
50 fun decimal(saturating: Boolean = false): Long {
51 if (peek() < 48 || peek() > 57) fail("expected nonnegative decimal integer")
52 var value = 0L
53 while (peek() >= 48 && peek() <= 57) {
54 val digit = (peek() - 48).toLong()
55 if (value > (Long.MAX_VALUE - digit) / 10L) {
56 if (!saturating) fail("decimal integer exceeds Long")
57 value = Long.MAX_VALUE
58 } else value = value * 10L + digit
59 position += 1L
60 }
61 return value
62 }
64 fun tokenList(): Array<String> {
65 val result = ArrayList<String>()
66 skipOws()
67 while (!ended) {
68 if (peek() == 44) { position += 1L; skipOws(); continue }
69 result.add(token().lowercaseAscii())
70 skipOws()
71 if (!ended) { expect(44); skipOws() }
72 }
73 return result.toArray()
74 }
76 fun parameterValue(): (Array<Byte>, Boolean) {
77 if (peek() != 34) return (token().toByteArray(), false)
78 position += 1L
79 val value = ByteArrayBuilder()
80 while (!ended) {
81 var current = peek()
82 if (current == 34) { position += 1L; return (value.toByteArray(), true) }
83 if (current == 92) {
84 position += 1L
85 if (ended) fail("incomplete quoted-pair")
86 current = peek()
87 }
88 if (!httpQuotedByte(current)) fail("invalid quoted-string byte")
89 value.append(current.toByte())
90 position += 1L
91 }
92 fail("unterminated quoted-string")
93 }
95 fun parameters(allowEmpty: Boolean = false, allowBws: Boolean = false): Array<HttpParameter> {
96 val result = ArrayList<HttpParameter>()
97 val names = HashSet<String>()
98 skipOws()
99 while (!ended) {
100 expect(59); skipOws()
101 if (allowEmpty && (ended || peek() == 59)) continue
102 val start = position
103 val name = token().lowercaseAscii()
104 if (!names.add(name)) failAt(start, "duplicate parameter " + name)
105 if (allowBws) skipOws()
106 expect(61)
107 if (allowBws) skipOws()
108 val offset = position
109 val (value, quoted) = parameterValue()
110 result.add(HttpParameter(name, value, quoted, offset, field))
111 skipOws()
112 }
113 return result.toArray()
114 }
117internal fun httpTokenByte(c: Int): Boolean =
118 (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) ||
119 c == 33 || c == 35 || c == 36 || c == 37 || c == 38 || c == 39 || c == 42 ||
120 c == 43 || c == 45 || c == 46 || c == 94 || c == 95 || c == 96 || c == 124 || c == 126
122internal fun httpQuotedByte(c: Int): Boolean = c == 9 || (c >= 32 && c != 127 && c <= 255)
124internal fun httpNormalizedToken(text: String): String {
125 if (!httpToken(text.toByteArray())) throw IllegalArgumentException(Some("invalid HTTP token"))
126 return text.lowercaseAscii()