| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | // httpDateLayout is the RFC 9110 preferred date format (net/http.TimeFormat), |
| 8 | // a fixed-width 29-byte layout that doubles as the formatting template: every |
| 9 | // separator byte is already in place, so AppendHTTPDate only overwrites the |
| 10 | // fields. |
| 11 | const ( |
| 12 | httpDateLayout = "Mon, 02 Jan 2006 15:04:05 GMT" |
| 13 | httpDateLen = len(httpDateLayout) |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | httpWeekdays = [7]string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"} |
| 18 | httpMonths = [12]string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} |
| 19 | |
| 20 | // httpDaysInMonth is indexed by time.Month; February is adjusted for |
| 21 | // leap years in daysInHTTPMonth. |
| 22 | httpDaysInMonth = [13]int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
| 23 | ) |
| 24 | |
| 25 | // AppendHTTPDate appends t in the RFC 9110 preferred HTTP date format |
| 26 | // ("Mon, 02 Jan 2006 15:04:05 GMT", net/http.TimeFormat) to dst and returns |
| 27 | // the extended slice. The output is byte-identical to |
| 28 | // t.UTC().AppendFormat(dst, http.TimeFormat) and always 29 bytes for the |
| 29 | // years 0..9999 that HTTP dates can represent; times outside that range |
| 30 | // delegate to time.AppendFormat. |
| 31 | func AppendHTTPDate(dst []byte, t time.Time) []byte { |
| 32 | t = t.UTC() |
| 33 | year, month, day := t.Date() |
| 34 | if year < 0 || year > 9999 { |
| 35 | // RFC 1123 assumes a four-digit year; keep stdlib behavior for the |
| 36 | // unrepresentable rest instead of mis-padding it. |
| 37 | return t.AppendFormat(dst, httpDateLayout) |
| 38 | } |
| 39 | hour, minute, sec := t.Clock() |
| 40 | off := len(dst) |
| 41 | dst = append(dst, httpDateLayout...) |
| 42 | b := dst[off : off+httpDateLen : off+httpDateLen] |
| 43 | copy(b, httpWeekdays[t.Weekday()]) |
| 44 | b[5] = byte(day/10) + '0' |
| 45 | b[6] = byte(day%10) + '0' |
| 46 | copy(b[8:11], httpMonths[month-1]) |
| 47 | b[12] = byte(year/1000) + '0' |
| 48 | b[13] = byte(year/100%10) + '0' |
| 49 | b[14] = byte(year/10%10) + '0' |
| 50 | b[15] = byte(year%10) + '0' |
| 51 | b[17] = byte(hour/10) + '0' |
| 52 | b[18] = byte(hour%10) + '0' |
| 53 | b[20] = byte(minute/10) + '0' |
| 54 | b[21] = byte(minute%10) + '0' |
| 55 | b[23] = byte(sec/10) + '0' |
| 56 | b[24] = byte(sec%10) + '0' |
| 57 | return dst |
| 58 | } |
| 59 | |
| 60 | // FormatHTTPDate returns t in the RFC 9110 preferred HTTP date format, equal |
| 61 | // to t.UTC().Format(http.TimeFormat) with a single allocation for the result. |
| 62 | func FormatHTTPDate(t time.Time) string { |
| 63 | var buf [httpDateLen]byte |
| 64 | return string(AppendHTTPDate(buf[:0], t)) |
| 65 | } |
| 66 | |
| 67 | // ParseHTTPDate parses an HTTP date the way net/http.ParseTime does: |
| 68 | // the RFC 9110 preferred format ("Mon, 02 Jan 2006 15:04:05 GMT") plus the |
| 69 | // obsolete RFC 850 and ANSI C asctime forms, with surrounding ASCII |
| 70 | // whitespace tolerated. Canonical preferred-format input takes a fast scalar |
| 71 | // path that never calls time.Parse; every other input — legacy formats, |
| 72 | // unusual casing, non-GMT zone names, padding — falls back to time.Parse |
| 73 | // with byte-for-byte stdlib semantics, including its errors. The returned |
| 74 | // time is in time.UTC on the fast path and whatever time.Parse yields on the |
| 75 | // fallback; the instants agree in both cases. |
| 76 | func ParseHTTPDate[S byteSeq](s S) (time.Time, error) { |
| 77 | if t, ok := parseRFC1123(s); ok { |
| 78 | return t, nil |
| 79 | } |
| 80 | return parseHTTPDateSlow(string(s)) |
| 81 | } |
| 82 | |
| 83 | // parseRFC1123 is the strict fast path for canonical |
| 84 | // "Mon, 02 Jan 2006 15:04:05 GMT" input: exact length, exact separators, |
| 85 | // exact-case names. It reports ok=false for anything else — including |
| 86 | // out-of-range fields — so the slow path can produce stdlib-identical |
| 87 | // results and errors for the long tail. |
| 88 | func parseRFC1123[S byteSeq](s S) (time.Time, bool) { |
| 89 | if len(s) != httpDateLen { |
| 90 | return time.Time{}, false |
| 91 | } |
| 92 | if s[3] != ',' || s[4] != ' ' || s[7] != ' ' || s[11] != ' ' || s[16] != ' ' || |
| 93 | s[19] != ':' || s[22] != ':' || s[25] != ' ' || |
| 94 | s[26] != 'G' || s[27] != 'M' || s[28] != 'T' { |
| 95 | return time.Time{}, false |
| 96 | } |
| 97 | if !isHTTPWeekday(pack3(s[0], s[1], s[2])) { |
| 98 | return time.Time{}, false |
| 99 | } |
| 100 | month := httpMonthNum(pack3(s[8], s[9], s[10])) |
| 101 | if month == 0 { |
| 102 | return time.Time{}, false |
| 103 | } |
| 104 | day, okDay := twoDigits(s[5], s[6]) |
| 105 | yh, okYh := twoDigits(s[12], s[13]) |
| 106 | yl, okYl := twoDigits(s[14], s[15]) |
| 107 | hour, okHour := twoDigits(s[17], s[18]) |
| 108 | minute, okMin := twoDigits(s[20], s[21]) |
| 109 | sec, okSec := twoDigits(s[23], s[24]) |
| 110 | if !okDay || !okYh || !okYl || !okHour || !okMin || !okSec { |
| 111 | return time.Time{}, false |
| 112 | } |
| 113 | year := yh*100 + yl |
| 114 | if hour > 23 || minute > 59 || sec > 59 || |
| 115 | day < 1 || day > daysInHTTPMonth(month, year) { |
| 116 | // time.Parse rejects these; let it produce the exact error. |
| 117 | return time.Time{}, false |
| 118 | } |
| 119 | return time.Date(year, time.Month(month), day, hour, minute, sec, 0, time.UTC), true |
| 120 | } |
| 121 | |
| 122 | // parseHTTPDateSlow mirrors net/http.ParseTime: trim the whitespace |
| 123 | // textproto would, then try the three allowed layouts in order, returning |
| 124 | // the last error when none matches. |
| 125 | func parseHTTPDateSlow(s string) (time.Time, error) { |
| 126 | start, end := 0, len(s) |
| 127 | for start < end && isHTTPDateSpace(s[start]) { |
| 128 | start++ |
| 129 | } |
| 130 | for start < end && isHTTPDateSpace(s[end-1]) { |
| 131 | end-- |
| 132 | } |
| 133 | s = s[start:end] |
| 134 | t, err := time.Parse(httpDateLayout, s) |
| 135 | if err == nil { |
| 136 | return t, nil |
| 137 | } |
| 138 | if t, err850 := time.Parse(time.RFC850, s); err850 == nil { |
| 139 | return t, nil |
| 140 | } |
| 141 | t, errANSIC := time.Parse(time.ANSIC, s) |
| 142 | if errANSIC == nil { |
| 143 | return t, nil |
| 144 | } |
| 145 | return time.Time{}, errANSIC |
| 146 | } |
| 147 | |
| 148 | // isHTTPDateSpace matches textproto.TrimString's notion of whitespace. |
| 149 | func isHTTPDateSpace(c byte) bool { |
| 150 | return c == ' ' || c == '\t' || c == '\r' || c == '\n' |
| 151 | } |
| 152 | |
| 153 | // pack3 packs three bytes for the name switches below. |
| 154 | func pack3(a, b, c byte) uint32 { |
| 155 | return uint32(a)<<16 | uint32(b)<<8 | uint32(c) |
| 156 | } |
| 157 | |
| 158 | // isHTTPWeekday reports whether w is one of the seven exact-case short |
| 159 | // weekday names. Like time.Parse, callers do not check the name against the |
| 160 | // date; any valid name is accepted. |
| 161 | func isHTTPWeekday(w uint32) bool { |
| 162 | switch w { |
| 163 | case 'S'<<16 | 'u'<<8 | 'n', 'M'<<16 | 'o'<<8 | 'n', 'T'<<16 | 'u'<<8 | 'e', |
| 164 | 'W'<<16 | 'e'<<8 | 'd', 'T'<<16 | 'h'<<8 | 'u', 'F'<<16 | 'r'<<8 | 'i', |
| 165 | 'S'<<16 | 'a'<<8 | 't': |
| 166 | return true |
| 167 | } |
| 168 | return false |
| 169 | } |
| 170 | |
| 171 | // httpMonthNum maps an exact-case short month name to 1..12, or 0 if w is |
| 172 | // not one. |
| 173 | func httpMonthNum(w uint32) int { |
| 174 | switch w { |
| 175 | case 'J'<<16 | 'a'<<8 | 'n': |
| 176 | return 1 |
| 177 | case 'F'<<16 | 'e'<<8 | 'b': |
| 178 | return 2 |
| 179 | case 'M'<<16 | 'a'<<8 | 'r': |
| 180 | return 3 |
| 181 | case 'A'<<16 | 'p'<<8 | 'r': |
| 182 | return 4 |
| 183 | case 'M'<<16 | 'a'<<8 | 'y': |
| 184 | return 5 |
| 185 | case 'J'<<16 | 'u'<<8 | 'n': |
| 186 | return 6 |
| 187 | case 'J'<<16 | 'u'<<8 | 'l': |
| 188 | return 7 |
| 189 | case 'A'<<16 | 'u'<<8 | 'g': |
| 190 | return 8 |
| 191 | case 'S'<<16 | 'e'<<8 | 'p': |
| 192 | return 9 |
| 193 | case 'O'<<16 | 'c'<<8 | 't': |
| 194 | return 10 |
| 195 | case 'N'<<16 | 'o'<<8 | 'v': |
| 196 | return 11 |
| 197 | case 'D'<<16 | 'e'<<8 | 'c': |
| 198 | return 12 |
| 199 | } |
| 200 | return 0 |
| 201 | } |
| 202 | |
| 203 | // twoDigits parses two ASCII digits into an int, reporting whether both |
| 204 | // bytes were digits. |
| 205 | func twoDigits(a, b byte) (int, bool) { |
| 206 | a -= '0' |
| 207 | b -= '0' |
| 208 | if a > 9 || b > 9 { |
| 209 | return 0, false |
| 210 | } |
| 211 | return int(a)*10 + int(b), true |
| 212 | } |
| 213 | |
| 214 | // daysInHTTPMonth returns the number of days in month for year, using the |
| 215 | // same leap-year rule as the time package. |
| 216 | func daysInHTTPMonth(month, year int) int { |
| 217 | if month == 2 && year%4 == 0 && (year%100 != 0 || year%400 == 0) { |
| 218 | return 29 |
| 219 | } |
| 220 | return httpDaysInMonth[month] |
| 221 | } |