Scoop API

scoop:scoop.http:0.1.0

src/cookie/date_parse.scoop

Selected production source · src/cookie/date_parse.scoop

1package scoop.http
3import scoop.datetime.*
4import scoop.time.Instant
6internal fun parseCookieDate(reader: HttpFieldReader): Instant {
7 var year = -1; var month = -1; var day = -1
8 var hour = -1; var minute = -1; var second = -1
9 var dayOffset = 0L; var yearOffset = 0L; var timeOffset = 0L
10 while (!reader.ended) {
11 if (cookieDateDelimiter(reader.peek())) { reader.position += 1L; continue }
12 val start = reader.position
13 while (!reader.ended && !cookieDateDelimiter(reader.peek())) reader.position += 1L
14 val end = reader.position
15 if (hour < 0) when (cookieDateTime(reader.bytes, start, end)) {
16 case Some(val time) -> {
17 hour = time._1; minute = time._2; second = time._3; timeOffset = start
18 continue
19 }
20 case None -> {}
21 }
22 if (day < 0) when (cookieDateNumber(reader.bytes, start, end, 1, 2)) {
23 case Some(val value) -> { day = value._1; dayOffset = start; continue }
24 case None -> {}
25 }
26 if (month < 0) {
27 val parsed = cookieDateMonth(reader.bytes, start, end)
28 if (parsed > 0) { month = parsed; continue }
29 }
30 if (year < 0) when (cookieDateNumber(reader.bytes, start, end, 2, 4)) {
31 case Some(val value) -> { year = value._1; yearOffset = start }
32 case None -> {}
33 }
34 }
35 if (year < 0 || month < 0 || day < 0 || hour < 0) reader.fail("incomplete Cookie date")
36 if (year >= 70 && year <= 99) year += 1900
37 else if (year <= 69) year += 2000
38 if (year < 1601 || year > 9999) reader.failAt(yearOffset, "Cookie date year must be 1601..9999")
39 if (hour > 23 || minute > 59 || second > 59) reader.failAt(timeOffset, "invalid Cookie time")
40 val date = try { LocalDate(year, month, day) }
41 catch (_: IllegalArgumentException) { reader.failAt(dayOffset, "invalid Cookie calendar date") }
42 return LocalDateTime(date, LocalTime(hour, minute, second)).toInstant(UtcOffset.ZERO)
45private fun cookieDateDelimiter(c: Int): Boolean = c == 9 || (c >= 32 && c <= 47) ||
46 (c >= 59 && c <= 64) || (c >= 91 && c <= 96) || (c >= 123 && c <= 126)
48private fun cookieDateNumber(bytes: Array<Byte>, start: Long, end: Long, minimum: Int, maximum: Int): (Int, Long)? {
49 var index = start
50 var value = 0
51 while (index < end) {
52 val digit = bytes[index].toUByte().toInt() - 48
53 if (digit < 0 || digit > 9) break
54 if (index - start == maximum.toLong()) return None
55 value = value * 10 + digit; index += 1L
56 }
57 return if (index - start < minimum.toLong()) None else Some((value, index))
60private fun cookieDateTime(bytes: Array<Byte>, start: Long, end: Long): (Int, Int, Int)? {
61 val first = cookieDateNumber(bytes, start, end, 1, 2)
62 when (first) { case None -> return None; case Some(_) -> {} }
63 if (first!!._2 == end || bytes[first!!._2] != 58.toByte()) return None
64 val second = cookieDateNumber(bytes, first!!._2 + 1L, end, 1, 2)
65 when (second) { case None -> return None; case Some(_) -> {} }
66 if (second!!._2 == end || bytes[second!!._2] != 58.toByte()) return None
67 val third = cookieDateNumber(bytes, second!!._2 + 1L, end, 1, 2)
68 return when (third) { case None -> None; case Some(val value) -> Some((first!!._1, second!!._1, value._1)) }
71private fun cookieDateMonth(bytes: Array<Byte>, start: Long, end: Long): Int {
72 if (end - start < 3L) return -1
73 for (index in 0L..<12L) {
74 var matches = true
75 for (offset in 0L..<3L) {
76 if ((bytes[start + offset].toUByte().toInt() or 32) != (httpDateMonths[index].getCharAt(offset).code or 32)) matches = false
77 }
78 if (matches) return index.toInt() + 1
79 }
80 return -1