aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/encoding/httpbinding/header.go
blob: f9256e175fc92df1bca4f365dbd684151d1af02b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package httpbinding

import (
	"encoding/base64"
	"math"
	"math/big"
	"net/http"
	"strconv"
	"strings"
)

// Headers is used to encode header keys using a provided prefix
type Headers struct {
	header http.Header
	prefix string
}

// AddHeader returns a HeaderValue used to append values to prefix+key
func (h Headers) AddHeader(key string) HeaderValue {
	return h.newHeaderValue(key, true)
}

// SetHeader returns a HeaderValue used to set the value of prefix+key
func (h Headers) SetHeader(key string) HeaderValue {
	return h.newHeaderValue(key, false)
}

func (h Headers) newHeaderValue(key string, append bool) HeaderValue {
	return newHeaderValue(h.header, h.prefix+strings.TrimSpace(key), append)
}

// HeaderValue is used to encode values to an HTTP header
type HeaderValue struct {
	header http.Header
	key    string
	append bool
}

func newHeaderValue(header http.Header, key string, append bool) HeaderValue {
	return HeaderValue{header: header, key: strings.TrimSpace(key), append: append}
}

func (h HeaderValue) modifyHeader(value string) {
	if h.append {
		h.header[h.key] = append(h.header[h.key], value)
	} else {
		h.header[h.key] = append(h.header[h.key][:0], value)
	}
}

// String encodes the value v as the header string value
func (h HeaderValue) String(v string) {
	h.modifyHeader(v)
}

// Byte encodes the value v as a query string value
func (h HeaderValue) Byte(v int8) {
	h.Long(int64(v))
}

// Short encodes the value v as a query string value
func (h HeaderValue) Short(v int16) {
	h.Long(int64(v))
}

// Integer encodes the value v as the header string value
func (h HeaderValue) Integer(v int32) {
	h.Long(int64(v))
}

// Long encodes the value v as the header string value
func (h HeaderValue) Long(v int64) {
	h.modifyHeader(strconv.FormatInt(v, 10))
}

// Boolean encodes the value v as a query string value
func (h HeaderValue) Boolean(v bool) {
	h.modifyHeader(strconv.FormatBool(v))
}

// Float encodes the value v as a query string value
func (h HeaderValue) Float(v float32) {
	h.float(float64(v), 32)
}

// Double encodes the value v as a query string value
func (h HeaderValue) Double(v float64) {
	h.float(v, 64)
}

func (h HeaderValue) float(v float64, bitSize int) {
	switch {
	case math.IsNaN(v):
		h.String(floatNaN)
	case math.IsInf(v, 1):
		h.String(floatInfinity)
	case math.IsInf(v, -1):
		h.String(floatNegInfinity)
	default:
		h.modifyHeader(strconv.FormatFloat(v, 'f', -1, bitSize))
	}
}

// BigInteger encodes the value v as a query string value
func (h HeaderValue) BigInteger(v *big.Int) {
	h.modifyHeader(v.String())
}

// BigDecimal encodes the value v as a query string value
func (h HeaderValue) BigDecimal(v *big.Float) {
	if i, accuracy := v.Int64(); accuracy == big.Exact {
		h.Long(i)
		return
	}
	h.modifyHeader(v.Text('e', -1))
}

// Blob encodes the value v as a base64 header string value
func (h HeaderValue) Blob(v []byte) {
	encodeToString := base64.StdEncoding.EncodeToString(v)
	h.modifyHeader(encodeToString)
}