summaryrefslogtreecommitdiff
path: root/vendor/github.com/aws/smithy-go/encoding/httpbinding
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-22 17:35:49 -0600
committermo khan <mo@mokhan.ca>2025-07-22 17:35:49 -0600
commit20ef0d92694465ac86b550df139e8366a0a2b4fa (patch)
tree3f14589e1ce6eb9306a3af31c3a1f9e1af5ed637 /vendor/github.com/aws/smithy-go/encoding/httpbinding
parent44e0d272c040cdc53a98b9f1dc58ae7da67752e6 (diff)
feat: connect to spicedb
Diffstat (limited to 'vendor/github.com/aws/smithy-go/encoding/httpbinding')
-rw-r--r--vendor/github.com/aws/smithy-go/encoding/httpbinding/encode.go123
-rw-r--r--vendor/github.com/aws/smithy-go/encoding/httpbinding/header.go122
-rw-r--r--vendor/github.com/aws/smithy-go/encoding/httpbinding/path_replace.go108
-rw-r--r--vendor/github.com/aws/smithy-go/encoding/httpbinding/query.go107
-rw-r--r--vendor/github.com/aws/smithy-go/encoding/httpbinding/uri.go111
5 files changed, 571 insertions, 0 deletions
diff --git a/vendor/github.com/aws/smithy-go/encoding/httpbinding/encode.go b/vendor/github.com/aws/smithy-go/encoding/httpbinding/encode.go
new file mode 100644
index 0000000..543e7cf
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/encoding/httpbinding/encode.go
@@ -0,0 +1,123 @@
+package httpbinding
+
+import (
+ "fmt"
+ "net/http"
+ "net/url"
+ "strconv"
+ "strings"
+)
+
+const (
+ contentLengthHeader = "Content-Length"
+ floatNaN = "NaN"
+ floatInfinity = "Infinity"
+ floatNegInfinity = "-Infinity"
+)
+
+// An Encoder provides encoding of REST URI path, query, and header components
+// of an HTTP request. Can also encode a stream as the payload.
+//
+// Does not support SetFields.
+type Encoder struct {
+ path, rawPath, pathBuffer []byte
+
+ query url.Values
+ header http.Header
+}
+
+// NewEncoder creates a new encoder from the passed in request. It assumes that
+// raw path contains no valuable information at this point, so it passes in path
+// as path and raw path for subsequent trans
+func NewEncoder(path, query string, headers http.Header) (*Encoder, error) {
+ return NewEncoderWithRawPath(path, path, query, headers)
+}
+
+// NewHTTPBindingEncoder creates a new encoder from the passed in request. All query and
+// header values will be added on top of the request's existing values. Overwriting
+// duplicate values.
+func NewEncoderWithRawPath(path, rawPath, query string, headers http.Header) (*Encoder, error) {
+ parseQuery, err := url.ParseQuery(query)
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse query string: %w", err)
+ }
+
+ e := &Encoder{
+ path: []byte(path),
+ rawPath: []byte(rawPath),
+ query: parseQuery,
+ header: headers.Clone(),
+ }
+
+ return e, nil
+}
+
+// Encode returns a REST protocol encoder for encoding HTTP bindings.
+//
+// Due net/http requiring `Content-Length` to be specified on the http.Request#ContentLength directly. Encode
+// will look for whether the header is present, and if so will remove it and set the respective value on http.Request.
+//
+// Returns any error occurring during encoding.
+func (e *Encoder) Encode(req *http.Request) (*http.Request, error) {
+ req.URL.Path, req.URL.RawPath = string(e.path), string(e.rawPath)
+ req.URL.RawQuery = e.query.Encode()
+
+ // net/http ignores Content-Length header and requires it to be set on http.Request
+ if v := e.header.Get(contentLengthHeader); len(v) > 0 {
+ iv, err := strconv.ParseInt(v, 10, 64)
+ if err != nil {
+ return nil, err
+ }
+ req.ContentLength = iv
+ e.header.Del(contentLengthHeader)
+ }
+
+ req.Header = e.header
+
+ return req, nil
+}
+
+// AddHeader returns a HeaderValue for appending to the given header name
+func (e *Encoder) AddHeader(key string) HeaderValue {
+ return newHeaderValue(e.header, key, true)
+}
+
+// SetHeader returns a HeaderValue for setting the given header name
+func (e *Encoder) SetHeader(key string) HeaderValue {
+ return newHeaderValue(e.header, key, false)
+}
+
+// Headers returns a Header used for encoding headers with the given prefix
+func (e *Encoder) Headers(prefix string) Headers {
+ return Headers{
+ header: e.header,
+ prefix: strings.TrimSpace(prefix),
+ }
+}
+
+// HasHeader returns if a header with the key specified exists with one or
+// more value.
+func (e Encoder) HasHeader(key string) bool {
+ return len(e.header[key]) != 0
+}
+
+// SetURI returns a URIValue used for setting the given path key
+func (e *Encoder) SetURI(key string) URIValue {
+ return newURIValue(&e.path, &e.rawPath, &e.pathBuffer, key)
+}
+
+// SetQuery returns a QueryValue used for setting the given query key
+func (e *Encoder) SetQuery(key string) QueryValue {
+ return NewQueryValue(e.query, key, false)
+}
+
+// AddQuery returns a QueryValue used for appending the given query key
+func (e *Encoder) AddQuery(key string) QueryValue {
+ return NewQueryValue(e.query, key, true)
+}
+
+// HasQuery returns if a query with the key specified exists with one or
+// more values.
+func (e *Encoder) HasQuery(key string) bool {
+ return len(e.query.Get(key)) != 0
+}
diff --git a/vendor/github.com/aws/smithy-go/encoding/httpbinding/header.go b/vendor/github.com/aws/smithy-go/encoding/httpbinding/header.go
new file mode 100644
index 0000000..f9256e1
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/encoding/httpbinding/header.go
@@ -0,0 +1,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)
+}
diff --git a/vendor/github.com/aws/smithy-go/encoding/httpbinding/path_replace.go b/vendor/github.com/aws/smithy-go/encoding/httpbinding/path_replace.go
new file mode 100644
index 0000000..9ae3085
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/encoding/httpbinding/path_replace.go
@@ -0,0 +1,108 @@
+package httpbinding
+
+import (
+ "bytes"
+ "fmt"
+)
+
+const (
+ uriTokenStart = '{'
+ uriTokenStop = '}'
+ uriTokenSkip = '+'
+)
+
+func bufCap(b []byte, n int) []byte {
+ if cap(b) < n {
+ return make([]byte, 0, n)
+ }
+
+ return b[0:0]
+}
+
+// replacePathElement replaces a single element in the path []byte.
+// Escape is used to control whether the value will be escaped using Amazon path escape style.
+func replacePathElement(path, fieldBuf []byte, key, val string, escape bool) ([]byte, []byte, error) {
+ // search for "{<key>}". If not found, search for the greedy version "{<key>+}". If none are found, return error
+ fieldBuf = bufCap(fieldBuf, len(key)+2) // { <key> }
+ fieldBuf = append(fieldBuf, uriTokenStart)
+ fieldBuf = append(fieldBuf, key...)
+ fieldBuf = append(fieldBuf, uriTokenStop)
+
+ start := bytes.Index(path, fieldBuf)
+ encodeSep := true
+ if start < 0 {
+ fieldBuf = bufCap(fieldBuf, len(key)+3) // { <key> [+] }
+ fieldBuf = append(fieldBuf, uriTokenStart)
+ fieldBuf = append(fieldBuf, key...)
+ fieldBuf = append(fieldBuf, uriTokenSkip)
+ fieldBuf = append(fieldBuf, uriTokenStop)
+
+ start = bytes.Index(path, fieldBuf)
+ if start < 0 {
+ return path, fieldBuf, fmt.Errorf("invalid path index, start=%d. %s", start, path)
+ }
+ encodeSep = false
+ }
+ end := start + len(fieldBuf)
+
+ if escape {
+ val = EscapePath(val, encodeSep)
+ }
+
+ fieldBuf = bufCap(fieldBuf, len(val))
+ fieldBuf = append(fieldBuf, val...)
+
+ keyLen := end - start
+ valLen := len(fieldBuf)
+
+ if keyLen == valLen {
+ copy(path[start:], fieldBuf)
+ return path, fieldBuf, nil
+ }
+
+ newLen := len(path) + (valLen - keyLen)
+ if len(path) < newLen {
+ path = path[:cap(path)]
+ }
+ if cap(path) < newLen {
+ newURI := make([]byte, newLen)
+ copy(newURI, path)
+ path = newURI
+ }
+
+ // shift
+ copy(path[start+valLen:], path[end:])
+ path = path[:newLen]
+ copy(path[start:], fieldBuf)
+
+ return path, fieldBuf, nil
+}
+
+// EscapePath escapes part of a URL path in Amazon style.
+func EscapePath(path string, encodeSep bool) string {
+ var buf bytes.Buffer
+ for i := 0; i < len(path); i++ {
+ c := path[i]
+ if noEscape[c] || (c == '/' && !encodeSep) {
+ buf.WriteByte(c)
+ } else {
+ fmt.Fprintf(&buf, "%%%02X", c)
+ }
+ }
+ return buf.String()
+}
+
+var noEscape [256]bool
+
+func init() {
+ for i := 0; i < len(noEscape); i++ {
+ // AWS expects every character except these to be escaped
+ noEscape[i] = (i >= 'A' && i <= 'Z') ||
+ (i >= 'a' && i <= 'z') ||
+ (i >= '0' && i <= '9') ||
+ i == '-' ||
+ i == '.' ||
+ i == '_' ||
+ i == '~'
+ }
+}
diff --git a/vendor/github.com/aws/smithy-go/encoding/httpbinding/query.go b/vendor/github.com/aws/smithy-go/encoding/httpbinding/query.go
new file mode 100644
index 0000000..c2e7d0a
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/encoding/httpbinding/query.go
@@ -0,0 +1,107 @@
+package httpbinding
+
+import (
+ "encoding/base64"
+ "math"
+ "math/big"
+ "net/url"
+ "strconv"
+)
+
+// QueryValue is used to encode query key values
+type QueryValue struct {
+ query url.Values
+ key string
+ append bool
+}
+
+// NewQueryValue creates a new QueryValue which enables encoding
+// a query value into the given url.Values.
+func NewQueryValue(query url.Values, key string, append bool) QueryValue {
+ return QueryValue{
+ query: query,
+ key: key,
+ append: append,
+ }
+}
+
+func (qv QueryValue) updateKey(value string) {
+ if qv.append {
+ qv.query.Add(qv.key, value)
+ } else {
+ qv.query.Set(qv.key, value)
+ }
+}
+
+// Blob encodes v as a base64 query string value
+func (qv QueryValue) Blob(v []byte) {
+ encodeToString := base64.StdEncoding.EncodeToString(v)
+ qv.updateKey(encodeToString)
+}
+
+// Boolean encodes v as a query string value
+func (qv QueryValue) Boolean(v bool) {
+ qv.updateKey(strconv.FormatBool(v))
+}
+
+// String encodes v as a query string value
+func (qv QueryValue) String(v string) {
+ qv.updateKey(v)
+}
+
+// Byte encodes v as a query string value
+func (qv QueryValue) Byte(v int8) {
+ qv.Long(int64(v))
+}
+
+// Short encodes v as a query string value
+func (qv QueryValue) Short(v int16) {
+ qv.Long(int64(v))
+}
+
+// Integer encodes v as a query string value
+func (qv QueryValue) Integer(v int32) {
+ qv.Long(int64(v))
+}
+
+// Long encodes v as a query string value
+func (qv QueryValue) Long(v int64) {
+ qv.updateKey(strconv.FormatInt(v, 10))
+}
+
+// Float encodes v as a query string value
+func (qv QueryValue) Float(v float32) {
+ qv.float(float64(v), 32)
+}
+
+// Double encodes v as a query string value
+func (qv QueryValue) Double(v float64) {
+ qv.float(v, 64)
+}
+
+func (qv QueryValue) float(v float64, bitSize int) {
+ switch {
+ case math.IsNaN(v):
+ qv.String(floatNaN)
+ case math.IsInf(v, 1):
+ qv.String(floatInfinity)
+ case math.IsInf(v, -1):
+ qv.String(floatNegInfinity)
+ default:
+ qv.updateKey(strconv.FormatFloat(v, 'f', -1, bitSize))
+ }
+}
+
+// BigInteger encodes v as a query string value
+func (qv QueryValue) BigInteger(v *big.Int) {
+ qv.updateKey(v.String())
+}
+
+// BigDecimal encodes v as a query string value
+func (qv QueryValue) BigDecimal(v *big.Float) {
+ if i, accuracy := v.Int64(); accuracy == big.Exact {
+ qv.Long(i)
+ return
+ }
+ qv.updateKey(v.Text('e', -1))
+}
diff --git a/vendor/github.com/aws/smithy-go/encoding/httpbinding/uri.go b/vendor/github.com/aws/smithy-go/encoding/httpbinding/uri.go
new file mode 100644
index 0000000..f04e119
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/encoding/httpbinding/uri.go
@@ -0,0 +1,111 @@
+package httpbinding
+
+import (
+ "math"
+ "math/big"
+ "strconv"
+ "strings"
+)
+
+// URIValue is used to encode named URI parameters
+type URIValue struct {
+ path, rawPath, buffer *[]byte
+
+ key string
+}
+
+func newURIValue(path *[]byte, rawPath *[]byte, buffer *[]byte, key string) URIValue {
+ return URIValue{path: path, rawPath: rawPath, buffer: buffer, key: key}
+}
+
+func (u URIValue) modifyURI(value string) (err error) {
+ *u.path, *u.buffer, err = replacePathElement(*u.path, *u.buffer, u.key, value, false)
+ if err != nil {
+ return err
+ }
+ *u.rawPath, *u.buffer, err = replacePathElement(*u.rawPath, *u.buffer, u.key, value, true)
+ return err
+}
+
+// Boolean encodes v as a URI string value
+func (u URIValue) Boolean(v bool) error {
+ return u.modifyURI(strconv.FormatBool(v))
+}
+
+// String encodes v as a URI string value
+func (u URIValue) String(v string) error {
+ return u.modifyURI(v)
+}
+
+// Byte encodes v as a URI string value
+func (u URIValue) Byte(v int8) error {
+ return u.Long(int64(v))
+}
+
+// Short encodes v as a URI string value
+func (u URIValue) Short(v int16) error {
+ return u.Long(int64(v))
+}
+
+// Integer encodes v as a URI string value
+func (u URIValue) Integer(v int32) error {
+ return u.Long(int64(v))
+}
+
+// Long encodes v as a URI string value
+func (u URIValue) Long(v int64) error {
+ return u.modifyURI(strconv.FormatInt(v, 10))
+}
+
+// Float encodes v as a query string value
+func (u URIValue) Float(v float32) error {
+ return u.float(float64(v), 32)
+}
+
+// Double encodes v as a query string value
+func (u URIValue) Double(v float64) error {
+ return u.float(v, 64)
+}
+
+func (u URIValue) float(v float64, bitSize int) error {
+ switch {
+ case math.IsNaN(v):
+ return u.String(floatNaN)
+ case math.IsInf(v, 1):
+ return u.String(floatInfinity)
+ case math.IsInf(v, -1):
+ return u.String(floatNegInfinity)
+ default:
+ return u.modifyURI(strconv.FormatFloat(v, 'f', -1, bitSize))
+ }
+}
+
+// BigInteger encodes v as a query string value
+func (u URIValue) BigInteger(v *big.Int) error {
+ return u.modifyURI(v.String())
+}
+
+// BigDecimal encodes v as a query string value
+func (u URIValue) BigDecimal(v *big.Float) error {
+ if i, accuracy := v.Int64(); accuracy == big.Exact {
+ return u.Long(i)
+ }
+ return u.modifyURI(v.Text('e', -1))
+}
+
+// SplitURI parses a Smithy HTTP binding trait URI
+func SplitURI(uri string) (path, query string) {
+ queryStart := strings.IndexRune(uri, '?')
+ if queryStart == -1 {
+ path = uri
+ return path, query
+ }
+
+ path = uri[:queryStart]
+ if queryStart+1 >= len(uri) {
+ return path, query
+ }
+ query = uri[queryStart+1:]
+
+ return path, query
+}