github.com/blend/go-sdk@v1.20220411.3/webutil/header_util.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package webutil
     9  
    10  import (
    11  	"net/http"
    12  	"strings"
    13  )
    14  
    15  // HeaderLastValue returns the last value of a potential csv of headers.
    16  func HeaderLastValue(headers http.Header, key string) (string, bool) {
    17  	if rawHeaderValue := headers.Get(key); rawHeaderValue != "" {
    18  		if !strings.ContainsRune(rawHeaderValue, ',') {
    19  			return strings.TrimSpace(rawHeaderValue), true
    20  		}
    21  		vals := strings.Split(rawHeaderValue, ",")
    22  		return strings.TrimSpace(vals[len(vals)-1]), true
    23  	}
    24  	return "", false
    25  }
    26  
    27  // HeaderAny returns if any pieces of a header match a given value.
    28  func HeaderAny(headers http.Header, key, value string) bool {
    29  	if rawHeaderValue := headers.Get(key); rawHeaderValue != "" {
    30  		if !strings.ContainsRune(rawHeaderValue, ',') {
    31  			return strings.TrimSpace(rawHeaderValue) == value
    32  		}
    33  		headerValues := strings.Split(rawHeaderValue, ",")
    34  		for _, headerValue := range headerValues {
    35  			if strings.TrimSpace(headerValue) == value {
    36  				return true
    37  			}
    38  		}
    39  	}
    40  	return false
    41  }
    42  
    43  // Headers creates headers from a given map.
    44  func Headers(from map[string]string) http.Header {
    45  	output := make(http.Header)
    46  	for key, value := range from {
    47  		output[key] = []string{value}
    48  	}
    49  	return output
    50  }