Scoop API

scoop:scoop.http:0.1.0

src/options.scoop

Selected production source · src/options.scoop

1package scoop.http
3/**
4 * Positive local limits on total header bytes, one field's bytes, and field count. These are
5 * enforced independently of peer-advertised settings.
6 */
7public struct HttpHeaderLimits(
8 val maxBytes: Long = 65536L,
9 val maxFieldBytes: Long = 16384L,
10 val maxFields: Long = 100L
13/**
14 * Local HTTP/1 header and start-line byte limits; all limits must be positive.
15 */
16public struct Http1Limits(
17 val headers: HttpHeaderLimits = HttpHeaderLimits(),
18 val maxStartLineBytes: Long = 8192L
21/**
22 * Controls handling of additional input after a message declared that the connection closes.
23 */
24public enum Http1AfterClose {
25 /**
26 * Rejects additional message bytes after a closing message.
27 */
28 Reject,
29 /**
30 * Discards additional input after a closing message.
31 */
32 Discard,
33 /**
34 * Allows parsing another message after a closing message as an explicit compatibility choice.
35 */
36 ParseNextMessage }
37/**
38 * Explicit llhttp receive-compatibility options. They do not relax outgoing validation or extend
39 * the server parser's known-method set; defaults preserve strict framing.
40 */
41public struct Http1ReceiveOptions(
42 val legacyHeaderValues: Boolean = false,
43 val lenientChunkedLength: Boolean = false,
44 val lenientTransferEncoding: Boolean = false,
45 val lenientTextVersion: Boolean = false,
46 val optionalLfAfterCr: Boolean = false,
47 val optionalCrBeforeLf: Boolean = false,
48 val optionalCrLfAfterChunk: Boolean = false,
49 val spacesAfterChunkSize: Boolean = false,
50 val afterClose: Http1AfterClose = Http1AfterClose.Reject
53internal fun requireHttpHeaderLimits(limits: HttpHeaderLimits) {
54 if (limits.maxBytes <= 0L || limits.maxFieldBytes <= 0L || limits.maxFields <= 0L)
55 throw IllegalArgumentException(Some("HTTP field limits must be positive"))
58@CLayout internal struct NativeHttp1Config(
59 val maxHeaderBytes: Long,
60 val maxFieldBytes: Long,
61 val maxFields: Long,
62 val maxStartLineBytes: Long,
63 val flags: UInt,
64 val afterClose: Int
67internal fun http1Config(options: Http1ReceiveOptions, limits: Http1Limits): NativeHttp1Config {
68 requireHttpHeaderLimits(limits.headers)
69 if (limits.maxStartLineBytes <= 0L) throw IllegalArgumentException(Some("HTTP start line limit must be positive"))
70 var flags = 0U
71 if (options.legacyHeaderValues) flags = flags or 1U
72 if (options.lenientChunkedLength) flags = flags or 2U
73 if (options.lenientTransferEncoding) flags = flags or 4U
74 if (options.lenientTextVersion) flags = flags or 8U
75 if (options.optionalLfAfterCr) flags = flags or 16U
76 if (options.optionalCrBeforeLf) flags = flags or 32U
77 if (options.optionalCrLfAfterChunk) flags = flags or 64U
78 if (options.spacesAfterChunkSize) flags = flags or 128U
79 return NativeHttp1Config(limits.headers.maxBytes, limits.headers.maxFieldBytes, limits.headers.maxFields,
80 limits.maxStartLineBytes, flags, when (options.afterClose) {
81 case Reject -> 0
82 case Discard -> 1
83 case ParseNextMessage -> 2
84 })