Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package http2 |
| 6 | |
| 7 | import ( |
| 8 | "net/http" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 9 | "sync" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | commonBuildOnce sync.Once |
| 14 | commonLowerHeader map[string]string // Go-Canonical-Case -> lower-case |
| 15 | commonCanonHeader map[string]string // lower-case -> Go-Canonical-Case |
| 16 | ) |
| 17 | |
| 18 | func buildCommonHeaderMapsOnce() { |
| 19 | commonBuildOnce.Do(buildCommonHeaderMaps) |
| 20 | } |
| 21 | |
| 22 | func buildCommonHeaderMaps() { |
| 23 | common := []string{ |
| 24 | "accept", |
| 25 | "accept-charset", |
| 26 | "accept-encoding", |
| 27 | "accept-language", |
| 28 | "accept-ranges", |
| 29 | "age", |
| 30 | "access-control-allow-origin", |
| 31 | "allow", |
| 32 | "authorization", |
| 33 | "cache-control", |
| 34 | "content-disposition", |
| 35 | "content-encoding", |
| 36 | "content-language", |
| 37 | "content-length", |
| 38 | "content-location", |
| 39 | "content-range", |
| 40 | "content-type", |
| 41 | "cookie", |
| 42 | "date", |
| 43 | "etag", |
| 44 | "expect", |
| 45 | "expires", |
| 46 | "from", |
| 47 | "host", |
| 48 | "if-match", |
| 49 | "if-modified-since", |
| 50 | "if-none-match", |
| 51 | "if-unmodified-since", |
| 52 | "last-modified", |
| 53 | "link", |
| 54 | "location", |
| 55 | "max-forwards", |
| 56 | "proxy-authenticate", |
| 57 | "proxy-authorization", |
| 58 | "range", |
| 59 | "referer", |
| 60 | "refresh", |
| 61 | "retry-after", |
| 62 | "server", |
| 63 | "set-cookie", |
| 64 | "strict-transport-security", |
| 65 | "trailer", |
| 66 | "transfer-encoding", |
| 67 | "user-agent", |
| 68 | "vary", |
| 69 | "via", |
| 70 | "www-authenticate", |
| 71 | } |
| 72 | commonLowerHeader = make(map[string]string, len(common)) |
| 73 | commonCanonHeader = make(map[string]string, len(common)) |
| 74 | for _, v := range common { |
| 75 | chk := http.CanonicalHeaderKey(v) |
| 76 | commonLowerHeader[chk] = v |
| 77 | commonCanonHeader[v] = chk |
| 78 | } |
| 79 | } |
| 80 | |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 81 | func lowerHeader(v string) (lower string, ascii bool) { |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 82 | buildCommonHeaderMapsOnce() |
| 83 | if s, ok := commonLowerHeader[v]; ok { |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 84 | return s, true |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 85 | } |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 86 | return asciiToLower(v) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 87 | } |