Scoop API

scoop:scoop.http:0.1.0

src/message/expect.scoop

Selected production source · src/message/expect.scoop

1package scoop.http
3import scoop.text.lowercaseAscii
5/**
6 * Tests parsed Expect fields for the 100-continue expectation.
7 */
8public fun httpExpectsContinue(fields: List<HttpField>): Boolean = parseHttpExpectations(fields).continueRequested
10/** The standard Continue signal and presence of other syntactically valid expectations. */
11public struct HttpExpectations(
12 /** Whether at least one unqualified 100-continue expectation is present. */
13 val continueRequested: Boolean,
14 /** Whether at least one expectation requires an unsupported extension. */
15 val hasUnsupported: Boolean
18/** Parses every Expect field once, retaining both recognized and unknown expectation facts. */
19public fun parseHttpExpectations(fields: List<HttpField>): HttpExpectations {
20 var found = false
21 var unsupported = false
22 for (field in fields.findAll("Expect".toByteArray())) {
23 val reader = HttpFieldReader("Expect", field.value)
24 reader.skipOws()
25 while (!reader.ended) {
26 if (reader.peek() == 44) { reader.position += 1L; reader.skipOws(); continue }
27 if (readHttpExpectation(reader)) found = true else unsupported = true
28 reader.skipOws()
29 if (!reader.ended) { reader.expect(44); reader.skipOws() }
30 }
31 }
32 return HttpExpectations(found, unsupported)
35private fun readHttpExpectation(reader: HttpFieldReader): Boolean {
36 val name = reader.token().lowercaseAscii()
37 if (reader.peek() != 61) return name == "100-continue"
38 reader.position += 1L
39 reader.parameterValue()
40 reader.skipOws()
41 while (reader.peek() == 59) {
42 reader.position += 1L
43 reader.skipOws()
44 if (reader.ended || reader.peek() == 59 || reader.peek() == 44) continue
45 reader.token()
46 reader.expect(61)
47 reader.parameterValue()
48 reader.skipOws()
49 }
50 return false