scoop:scoop.http:0.1.0
src/message/preconditions.scoop
Selected production source · src/message/preconditions.scoop
1
package scoop.http3
import scoop.bytes.ByteArrayBuilder4
import scoop.time.Instant6
/** The result of evaluating origin request preconditions for a selected representation. */7
public 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
PreconditionFailed14
}16
/**17
* Evaluates validators in HTTP precedence order after normal request checks. The reference is18
* explicit for obsolete HTTP dates. Invalid tag syntax throws; unusable dates are ignored.19
*/20
public 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.Proceed23
val matching = requestTagCondition(fields, "If-Match")24
if (conditionPresent(matching)) {25
if (!matchesRequestTags(matching!!, exists, entityTag, true)) return HttpPreconditionResult.PreconditionFailed26
} else if (exists && conditionPresent(lastModified)) {27
val date = requestConditionDate(fields, "If-Unmodified-Since", reference)28
if (conditionPresent(date) && lastModified!!.epochSeconds > date!!.epochSeconds) return HttpPreconditionResult.PreconditionFailed29
}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.PreconditionFailed35
} 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.NotModified38
}39
return HttpPreconditionResult.Proceed40
}42
private fun matchesRequestTags(condition: HttpEntityTagCondition, exists: Boolean, tag: HttpEntityTag?, strong: Boolean): Boolean {43
if (!exists) return false44
if (condition.isWildcard) return true45
if (!conditionPresent(tag)) return false46
for (candidate in condition.tags) {47
if (if (strong) candidate.stronglyMatches(tag!!) else candidate.weaklyMatches(tag!!)) return true48
}49
return false50
}52
private fun requestTagCondition(fields: List<HttpField>, name: String): HttpEntityTagCondition? {53
val selected = fields.findAll(name.toByteArray())54
if (selected.size == 0L) return None55
val value = ByteArrayBuilder()56
var first = true57
for (field in selected) {58
if (!first) value.append(", ".toByteArray())59
value.append(field.value)60
first = false61
}62
return Some(HttpEntityTagCondition.parse(value.toByteArray(), name))63
}65
private 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 None68
}70
private fun <T> conditionPresent(value: T?): Boolean = when (value) { case Some(_) -> true; case None -> false }