Scoop API

scoop:scoop.http:0.1.0

src/message/preconditions.scoop

Selected production source · src/message/preconditions.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
4import scoop.time.Instant
6/** The result of evaluating origin request preconditions for a selected representation. */
7public enum HttpPreconditionResult {
8 /** Continue the ordinary representation operation, including any later range selection. */
9 Proceed,
10 /** A GET or HEAD validator permits a 304 response without representation content. */
11 NotModified,
12 /** An applicable precondition requires a 412 response. */
13 PreconditionFailed
16/**
17 * Evaluates validators in HTTP precedence order after normal request checks. The reference is
18 * explicit for obsolete HTTP dates. Invalid tag syntax throws; unusable dates are ignored.
19 */
20public fun evaluateHttpPreconditions(method: String, fields: List<HttpField>, exists: Boolean,
21 entityTag: HttpEntityTag?, lastModified: Instant?, reference: Instant): HttpPreconditionResult {
22 if (method == "CONNECT" || method == "OPTIONS" || method == "TRACE") return HttpPreconditionResult.Proceed
23 val matching = requestTagCondition(fields, "If-Match")
24 if (conditionPresent(matching)) {
25 if (!matchesRequestTags(matching!!, exists, entityTag, true)) return HttpPreconditionResult.PreconditionFailed
26 } else if (exists && conditionPresent(lastModified)) {
27 val date = requestConditionDate(fields, "If-Unmodified-Since", reference)
28 if (conditionPresent(date) && lastModified!!.epochSeconds > date!!.epochSeconds) return HttpPreconditionResult.PreconditionFailed
29 }
30 val unchanged = requestTagCondition(fields, "If-None-Match")
31 val retrieval = method == "GET" || method == "HEAD"
32 if (conditionPresent(unchanged)) {
33 if (matchesRequestTags(unchanged!!, exists, entityTag, false))
34 return if (retrieval) HttpPreconditionResult.NotModified else HttpPreconditionResult.PreconditionFailed
35 } else if (retrieval && exists && conditionPresent(lastModified)) {
36 val date = requestConditionDate(fields, "If-Modified-Since", reference)
37 if (conditionPresent(date) && lastModified!!.epochSeconds <= date!!.epochSeconds) return HttpPreconditionResult.NotModified
38 }
39 return HttpPreconditionResult.Proceed
42private fun matchesRequestTags(condition: HttpEntityTagCondition, exists: Boolean, tag: HttpEntityTag?, strong: Boolean): Boolean {
43 if (!exists) return false
44 if (condition.isWildcard) return true
45 if (!conditionPresent(tag)) return false
46 for (candidate in condition.tags) {
47 if (if (strong) candidate.stronglyMatches(tag!!) else candidate.weaklyMatches(tag!!)) return true
48 }
49 return false
52private fun requestTagCondition(fields: List<HttpField>, name: String): HttpEntityTagCondition? {
53 val selected = fields.findAll(name.toByteArray())
54 if (selected.size == 0L) return None
55 val value = ByteArrayBuilder()
56 var first = true
57 for (field in selected) {
58 if (!first) value.append(", ".toByteArray())
59 value.append(field.value)
60 first = false
61 }
62 return Some(HttpEntityTagCondition.parse(value.toByteArray(), name))
65private fun requestConditionDate(fields: List<HttpField>, name: String, reference: Instant): Instant? {
66 val selected = fields.findAll(name.toByteArray())
67 return if (selected.size == 1L) HttpDate.parseOrNone(selected[0L].value, reference) else None
70private fun <T> conditionPresent(value: T?): Boolean = when (value) { case Some(_) -> true; case None -> false }