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