headermap.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. package http2
  5. import (
  6. "net/http"
  7. "sync"
  8. )
  9. var (
  10. commonBuildOnce sync.Once
  11. commonLowerHeader map[string]string // Go-Canonical-Case -> lower-case
  12. commonCanonHeader map[string]string // lower-case -> Go-Canonical-Case
  13. )
  14. func buildCommonHeaderMapsOnce() {
  15. commonBuildOnce.Do(buildCommonHeaderMaps)
  16. }
  17. func buildCommonHeaderMaps() {
  18. common := []string{
  19. "accept",
  20. "accept-charset",
  21. "accept-encoding",
  22. "accept-language",
  23. "accept-ranges",
  24. "age",
  25. "access-control-allow-origin",
  26. "allow",
  27. "authorization",
  28. "cache-control",
  29. "content-disposition",
  30. "content-encoding",
  31. "content-language",
  32. "content-length",
  33. "content-location",
  34. "content-range",
  35. "content-type",
  36. "cookie",
  37. "date",
  38. "etag",
  39. "expect",
  40. "expires",
  41. "from",
  42. "host",
  43. "if-match",
  44. "if-modified-since",
  45. "if-none-match",
  46. "if-unmodified-since",
  47. "last-modified",
  48. "link",
  49. "location",
  50. "max-forwards",
  51. "proxy-authenticate",
  52. "proxy-authorization",
  53. "range",
  54. "referer",
  55. "refresh",
  56. "retry-after",
  57. "server",
  58. "set-cookie",
  59. "strict-transport-security",
  60. "trailer",
  61. "transfer-encoding",
  62. "user-agent",
  63. "vary",
  64. "via",
  65. "www-authenticate",
  66. }
  67. commonLowerHeader = make(map[string]string, len(common))
  68. commonCanonHeader = make(map[string]string, len(common))
  69. for _, v := range common {
  70. chk := http.CanonicalHeaderKey(v)
  71. commonLowerHeader[chk] = v
  72. commonCanonHeader[v] = chk
  73. }
  74. }
  75. func lowerHeader(v string) (lower string, ascii bool) {
  76. buildCommonHeaderMapsOnce()
  77. if s, ok := commonLowerHeader[v]; ok {
  78. return s, true
  79. }
  80. return asciiToLower(v)
  81. }