Scoop API

scoop:scoop.http:0.1.0

src/model.scoop

Selected production source · src/model.scoop

1package scoop.http
3/**
4 * The HTTP framing family selected for a connection.
5 */
6public enum HttpProtocol {
7 /**
8 * HTTP/1 text-message framing.
9 */
10 Http1,
11 /**
12 * HTTP/2 binary framing and multiplexed streams.
13 */
14 Http2
17/**
18 * The protocol version represented by a message head.
19 */
20public enum HttpVersion {
21 /**
22 * HTTP/1.0 message syntax.
23 */
24 Http10,
25 /**
26 * HTTP/1.1 message syntax.
27 */
28 Http11,
29 /**
30 * HTTP/2 message semantics.
31 */
32 Http2
35/**
36 * Selects client or server responsibilities for parsing, framing, and masking.
37 */
38public enum HttpRole {
39 /**
40 * The endpoint initiates requests and receives responses.
41 */
42 Client,
43 /**
44 * The endpoint receives requests and sends responses.
45 */
46 Server
49/**
50 * The protocol state affected by a failure.
51 */
52public enum HttpFailureScope {
53 /**
54 * The requested operation failed.
55 */
56 Operation,
57 /**
58 * One HTTP/2 stream failed.
59 */
60 Stream,
61 /**
62 * The connection-level protocol state failed.
63 */
64 Connection
67/**
68 * A protocol failure carrying operation, backend code, affected scope, stream identity, and
69 * unexpected-EOF information.
70 */
71public class HttpException public constructor(
72 /**
73 * The operation that failed.
74 */
75 public val operation: String,
76 /**
77 * The affected protocol family.
78 */
79 public val protocol: HttpProtocol,
80 /**
81 * Whether the failure affects the operation, one stream, or the connection.
82 */
83 public val scope: HttpFailureScope,
84 /**
85 * The affected stream ID, or zero when no stream is associated.
86 */
87 public val streamId: Int,
88 /**
89 * The protocol backend reporting the failure.
90 */
91 public val backend: String,
92 /**
93 * The backend-specific numeric error code.
94 */
95 public val code: Int,
96 /**
97 * The failure explanation.
98 */
99 public val detail: String,
100 /**
101 * Whether input ended before required protocol bytes arrived.
102 */
103 public val isUnexpectedEof: Boolean = false
104): Exception(Some(operation + ": " + detail))
106/**
107 * An owned snapshot of HTTP field name and value bytes. Duplicate fields and original ordering
108 * remain distinct; text decoding is explicit.
109 */
110public class HttpField public constructor(
111 name: List<Byte>,
112 value: List<Byte>,
113 /**
114 * Whether header compression must avoid indexing this field.
115 */
116 public val neverIndex: Boolean = false
117) {
118 /**
119 * The immutable field-name snapshot.
120 */
121 public val name: Array<Byte> = httpSnapshot(name)
122 /**
123 * The immutable field-value snapshot without implicit character decoding.
124 */
125 public val value: Array<Byte> = httpSnapshot(value)
128/**
129 * Returns all fields whose names match under ASCII case val folding, preserving order without
130 * combining their values.
131 */
132public fun List<HttpField>.findAll(name: List<Byte>): Array<HttpField> {
133 val result = ArrayList<HttpField>()
134 for (field in this)
135 if (httpAsciiEqual(field.name, name))
136 result.add(field)
137 return result.toArray()
140/**
141 * The protocol-specific request-target representation.
142 */
143public enum HttpRequestTarget {
144 /**
145 * Raw HTTP/1 target bytes, including the selected origin, absolute, authority, or asterisk
146 * form.
147 */
148 Http1(Array<Byte>),
149 /**
150 * HTTP/2 scheme, authority, and path pseudo-field bytes, in that order.
151 */
152 Http2(Array<Byte>, Array<Byte>, Array<Byte>),
153 /**
154 * The authority pseudo-field for an HTTP/2 CONNECT request.
155 */
156 Connect(Array<Byte>)
159/**
160 * Never-index flags for the method, scheme, authority, and path pseudo-fields. True requests that
161 * the corresponding value not enter the compression table.
162 */
163public struct Http2RequestIndexing(
164 val method: Boolean = false,
165 val scheme: Boolean = false,
166 val authority: Boolean = false,
167 val path: Boolean = false
170/**
171 * An owned request head with method bytes, protocol-specific target, ordered regular fields, and
172 * HTTP/2 indexing hints.
173 */
174public class HttpRequestHead public constructor(
175 /**
176 * The message protocol version.
177 */
178 public val version: HttpVersion,
179 method: List<Byte>,
180 /**
181 * The protocol-specific request target.
182 */
183 public val target: HttpRequestTarget,
184 fields: List<HttpField> = ArrayList<HttpField>(),
185 /**
186 * Never-index hints for HTTP/2 request pseudo-fields.
187 */
188 public val indexing: Http2RequestIndexing = Http2RequestIndexing()
189) {
190 /**
191 * The case-sensitive method token bytes.
192 */
193 public val method: Array<Byte> = httpSnapshot(method)
194 /**
195 * Ordered regular fields, preserving duplicates.
196 */
197 public val fields: Array<HttpField> = httpSnapshot(fields)
200/**
201 * An owned response head with status, ordered fields, and an optional HTTP/1 reason phrase. HTTP/2
202 * responses have no reason phrase.
203 */
204public class HttpResponseHead public constructor(
205 /**
206 * The message protocol version.
207 */
208 public val version: HttpVersion,
209 /**
210 * The response status code.
211 */
212 public val status: UInt,
213 reason: List<Byte>? = None,
214 fields: List<HttpField> = ArrayList<HttpField>(),
215 /**
216 * Whether HTTP/2 status compression must avoid indexing the status value.
217 */
218 public val neverIndexStatus: Boolean = false
219) {
220 /**
221 * The optional HTTP/1 reason-phrase bytes; absent for HTTP/2.
222 */
223 public val reason: Array<Byte>? =
224 when (reason) {
225 case Some(val bytes) -> Some(httpSnapshot(bytes))
226 case None -> None
227 }
228 /**
229 * Ordered regular fields, preserving duplicates.
230 */
231 public val fields: Array<HttpField> = httpSnapshot(fields)
234internal fun <T> httpSnapshot(values: List<T>): Array<T> =
235 if (values is Array<T>)
236 values
237 else
238 values.toArray()
240internal fun httpAsciiEqual(left: List<Byte>, right: List<Byte>): Boolean {
241 if (left.size != right.size)
242 return false
243 var index = 0L
244 while (index < left.size) {
245 var a = left[index].toUByte().toInt()
246 var b = right[index].toUByte().toInt()
247 if (a >= 65 && a <= 90)
248 a += 32
249 if (b >= 65 && b <= 90)
250 b += 32
251 if (a != b)
252 return false
253 index += 1L
254 }
255 return true