Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [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" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [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", |
Akash Reddy Kankanala | 92dfdf8 | 2025-03-23 22:07:09 +0530 | [diff] [blame^] | 30 | "access-control-allow-credentials", |
| 31 | "access-control-allow-headers", |
| 32 | "access-control-allow-methods", |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 33 | "access-control-allow-origin", |
Akash Reddy Kankanala | 92dfdf8 | 2025-03-23 22:07:09 +0530 | [diff] [blame^] | 34 | "access-control-expose-headers", |
| 35 | "access-control-max-age", |
| 36 | "access-control-request-headers", |
| 37 | "access-control-request-method", |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 38 | "allow", |
| 39 | "authorization", |
| 40 | "cache-control", |
| 41 | "content-disposition", |
| 42 | "content-encoding", |
| 43 | "content-language", |
| 44 | "content-length", |
| 45 | "content-location", |
| 46 | "content-range", |
| 47 | "content-type", |
| 48 | "cookie", |
| 49 | "date", |
| 50 | "etag", |
| 51 | "expect", |
| 52 | "expires", |
| 53 | "from", |
| 54 | "host", |
| 55 | "if-match", |
| 56 | "if-modified-since", |
| 57 | "if-none-match", |
| 58 | "if-unmodified-since", |
| 59 | "last-modified", |
| 60 | "link", |
| 61 | "location", |
| 62 | "max-forwards", |
Akash Reddy Kankanala | 92dfdf8 | 2025-03-23 22:07:09 +0530 | [diff] [blame^] | 63 | "origin", |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 64 | "proxy-authenticate", |
| 65 | "proxy-authorization", |
| 66 | "range", |
| 67 | "referer", |
| 68 | "refresh", |
| 69 | "retry-after", |
| 70 | "server", |
| 71 | "set-cookie", |
| 72 | "strict-transport-security", |
| 73 | "trailer", |
| 74 | "transfer-encoding", |
| 75 | "user-agent", |
| 76 | "vary", |
| 77 | "via", |
| 78 | "www-authenticate", |
Akash Reddy Kankanala | 92dfdf8 | 2025-03-23 22:07:09 +0530 | [diff] [blame^] | 79 | "x-forwarded-for", |
| 80 | "x-forwarded-proto", |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 81 | } |
| 82 | commonLowerHeader = make(map[string]string, len(common)) |
| 83 | commonCanonHeader = make(map[string]string, len(common)) |
| 84 | for _, v := range common { |
| 85 | chk := http.CanonicalHeaderKey(v) |
| 86 | commonLowerHeader[chk] = v |
| 87 | commonCanonHeader[v] = chk |
| 88 | } |
| 89 | } |
| 90 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 91 | func lowerHeader(v string) (lower string, ascii bool) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 92 | buildCommonHeaderMapsOnce() |
| 93 | if s, ok := commonLowerHeader[v]; ok { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 94 | return s, true |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 95 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 96 | return asciiToLower(v) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 97 | } |
Akash Reddy Kankanala | 92dfdf8 | 2025-03-23 22:07:09 +0530 | [diff] [blame^] | 98 | |
| 99 | func canonicalHeader(v string) string { |
| 100 | buildCommonHeaderMapsOnce() |
| 101 | if s, ok := commonCanonHeader[v]; ok { |
| 102 | return s |
| 103 | } |
| 104 | return http.CanonicalHeaderKey(v) |
| 105 | } |