github.com/useflyent/fhttp@v0.0.0-20211004035111-333f430cfbbf/http2/headermap.go (about)

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