Scoop API

scoop:scoop.http:0.1.0

src/message/trailers.scoop

Selected production source · src/message/trailers.scoop

1package scoop.http
3import scoop.collections.HashSet
5/**
6 * The names declared in Trailer, with explicit validation of received or outgoing trailer fields.
7 */
8public class HttpTrailerDeclaration public constructor(names: List<String>) {
9 /**
10 * The normalized declared trailer names.
11 */
12 public val names: Array<String> = declaredTrailerNames(names)
14 /**
15 * Tests a trailer name using ASCII case folding.
16 */
17 public fun contains(name: String): Boolean {
18 val normalized = httpNormalizedToken(name)
19 for (value in names) if (value == normalized) return true
20 return false
21 }
23 /**
24 * Builds the Trailer declaration field.
25 */
26 public fun toHttpField(): HttpField {
27 val value = StringBuilder()
28 var first = true
29 for (name in names) {
30 if (!first) value.append(", ")
31 value.append(name)
32 first = false
33 }
34 return HttpField("Trailer".toByteArray(), value.toString().toByteArray())
35 }
37 /**
38 * Validates that each supplied trailer was declared and is permitted as a trailer.
39 */
40 public fun requireDeclared(fields: List<HttpField>) {
41 for (field in fields) {
42 var found = false
43 for (name in names) {
44 if (field.hasName(name.toByteArray())) { found = true; break }
45 }
46 if (!found) throw IllegalArgumentException(Some("HTTP trailer field was not declared"))
47 }
48 }
51private fun declaredTrailerNames(names: List<String>): Array<String> {
52 val result = ArrayList<String>()
53 val seen = HashSet<String>()
54 for (name in names) {
55 val normalized = httpNormalizedToken(name)
56 requireHttpTrailerName(normalized.toByteArray())
57 if (seen.add(normalized)) result.add(normalized)
58 }
59 return result.toArray()
62internal fun requireHttpTrailerName(name: List<Byte>) {
63 for (forbidden in forbiddenTrailerNames) {
64 if (httpAsciiEqual(name, forbidden))
65 throw IllegalArgumentException(Some("HTTP field cannot be sent in trailers: " + String.fromUtf8(forbidden)))
66 }
69private val forbiddenTrailerNames: Array<Array<Byte>> = makeForbiddenTrailerNames()
71private fun makeForbiddenTrailerNames(): Array<Array<Byte>> {
72 val names = ["content-length", "transfer-encoding", "trailer", "host", "connection",
73 "keep-alive", "proxy-connection", "upgrade", "te", "authorization", "proxy-authorization",
74 "www-authenticate", "proxy-authenticate", "cookie", "set-cookie", "expect", "max-forwards",
75 "range", "if-match", "if-none-match", "if-modified-since", "if-unmodified-since", "if-range",
76 "accept", "accept-encoding", "accept-language", "accept-charset", "cache-control", "pragma",
77 "age", "date", "expires", "location", "retry-after", "vary", "warning", "content-encoding",
78 "content-type", "content-range", "content-disposition", "content-location"]
79 return Array<Array<Byte>>(names.size) { index -> names[index].toByteArray() }