2016-01-22 02:17:53 +00:00
|
|
|
// Package rest provides RESTful serialization of AWS requests and responses.
|
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-01-12 10:16:28 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-01-22 02:17:53 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
2017-11-29 22:20:27 +00:00
|
|
|
"github.com/aws/aws-sdk-go/private/protocol"
|
2016-01-22 02:17:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Whether the byte value can be sent without escaping in AWS URLs
|
|
|
|
var noEscape [256]bool
|
|
|
|
|
|
|
|
var errValueNotSet = fmt.Errorf("value not set")
|
|
|
|
|
2020-03-09 07:09:54 +00:00
|
|
|
var byteSliceType = reflect.TypeOf([]byte{})
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
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 == '~'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildHandler is a named request handler for building rest protocol requests
|
|
|
|
var BuildHandler = request.NamedHandler{Name: "awssdk.rest.Build", Fn: Build}
|
|
|
|
|
|
|
|
// Build builds the REST component of a service request.
|
|
|
|
func Build(r *request.Request) {
|
|
|
|
if r.ParamsFilled() {
|
|
|
|
v := reflect.ValueOf(r.Params).Elem()
|
2017-01-12 10:16:28 +00:00
|
|
|
buildLocationElements(r, v, false)
|
2016-01-22 02:17:53 +00:00
|
|
|
buildBody(r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 10:16:28 +00:00
|
|
|
// BuildAsGET builds the REST component of a service request with the ability to hoist
|
|
|
|
// data from the body.
|
|
|
|
func BuildAsGET(r *request.Request) {
|
|
|
|
if r.ParamsFilled() {
|
|
|
|
v := reflect.ValueOf(r.Params).Elem()
|
|
|
|
buildLocationElements(r, v, true)
|
|
|
|
buildBody(r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildLocationElements(r *request.Request, v reflect.Value, buildGETQuery bool) {
|
2016-01-22 02:17:53 +00:00
|
|
|
query := r.HTTPRequest.URL.Query()
|
|
|
|
|
2017-01-12 10:16:28 +00:00
|
|
|
// Setup the raw path to match the base path pattern. This is needed
|
|
|
|
// so that when the path is mutated a custom escaped version can be
|
|
|
|
// stored in RawPath that will be used by the Go client.
|
|
|
|
r.HTTPRequest.URL.RawPath = r.HTTPRequest.URL.Path
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
m := v.Field(i)
|
|
|
|
if n := v.Type().Field(i).Name; n[0:1] == strings.ToLower(n[0:1]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.IsValid() {
|
|
|
|
field := v.Type().Field(i)
|
|
|
|
name := field.Tag.Get("locationName")
|
|
|
|
if name == "" {
|
|
|
|
name = field.Name
|
|
|
|
}
|
2017-11-29 22:20:27 +00:00
|
|
|
if kind := m.Kind(); kind == reflect.Ptr {
|
2016-01-22 02:17:53 +00:00
|
|
|
m = m.Elem()
|
2017-11-29 22:20:27 +00:00
|
|
|
} else if kind == reflect.Interface {
|
|
|
|
if !m.Elem().IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
if !m.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
2017-01-12 10:16:28 +00:00
|
|
|
if field.Tag.Get("ignore") != "" {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
|
2020-03-09 07:09:54 +00:00
|
|
|
// Support the ability to customize values to be marshaled as a
|
|
|
|
// blob even though they were modeled as a string. Required for S3
|
2022-01-04 22:09:44 +00:00
|
|
|
// API operations like SSECustomerKey is modeled as string but
|
2020-03-09 07:09:54 +00:00
|
|
|
// required to be base64 encoded in request.
|
|
|
|
if field.Tag.Get("marshal-as") == "blob" {
|
|
|
|
m = m.Convert(byteSliceType)
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
var err error
|
|
|
|
switch field.Tag.Get("location") {
|
|
|
|
case "headers": // header maps
|
2017-11-29 22:20:27 +00:00
|
|
|
err = buildHeaderMap(&r.HTTPRequest.Header, m, field.Tag)
|
2016-01-22 02:17:53 +00:00
|
|
|
case "header":
|
2017-11-29 22:20:27 +00:00
|
|
|
err = buildHeader(&r.HTTPRequest.Header, m, name, field.Tag)
|
2016-01-22 02:17:53 +00:00
|
|
|
case "uri":
|
2017-11-29 22:20:27 +00:00
|
|
|
err = buildURI(r.HTTPRequest.URL, m, name, field.Tag)
|
2016-01-22 02:17:53 +00:00
|
|
|
case "querystring":
|
2017-11-29 22:20:27 +00:00
|
|
|
err = buildQueryString(query, m, name, field.Tag)
|
2017-01-12 10:16:28 +00:00
|
|
|
default:
|
|
|
|
if buildGETQuery {
|
2017-11-29 22:20:27 +00:00
|
|
|
err = buildQueryString(query, m, name, field.Tag)
|
2017-01-12 10:16:28 +00:00
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
r.Error = err
|
|
|
|
}
|
|
|
|
if r.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.HTTPRequest.URL.RawQuery = query.Encode()
|
2017-01-12 10:16:28 +00:00
|
|
|
if !aws.BoolValue(r.Config.DisableRestProtocolURICleaning) {
|
|
|
|
cleanPath(r.HTTPRequest.URL)
|
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildBody(r *request.Request, v reflect.Value) {
|
|
|
|
if field, ok := v.Type().FieldByName("_"); ok {
|
|
|
|
if payloadName := field.Tag.Get("payload"); payloadName != "" {
|
|
|
|
pfield, _ := v.Type().FieldByName(payloadName)
|
|
|
|
if ptag := pfield.Tag.Get("type"); ptag != "" && ptag != "structure" {
|
|
|
|
payload := reflect.Indirect(v.FieldByName(payloadName))
|
|
|
|
if payload.IsValid() && payload.Interface() != nil {
|
|
|
|
switch reader := payload.Interface().(type) {
|
|
|
|
case io.ReadSeeker:
|
|
|
|
r.SetReaderBody(reader)
|
|
|
|
case []byte:
|
|
|
|
r.SetBufferBody(reader)
|
|
|
|
case string:
|
|
|
|
r.SetStringBody(reader)
|
|
|
|
default:
|
2020-03-09 07:09:54 +00:00
|
|
|
r.Error = awserr.New(request.ErrCodeSerialization,
|
2016-01-22 02:17:53 +00:00
|
|
|
"failed to encode REST request",
|
|
|
|
fmt.Errorf("unknown payload type %s", payload.Type()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:20:27 +00:00
|
|
|
func buildHeader(header *http.Header, v reflect.Value, name string, tag reflect.StructTag) error {
|
|
|
|
str, err := convertType(v, tag)
|
2016-01-22 02:17:53 +00:00
|
|
|
if err == errValueNotSet {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2020-03-09 07:09:54 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 07:09:54 +00:00
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
str = strings.TrimSpace(str)
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
header.Add(name, str)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:20:27 +00:00
|
|
|
func buildHeaderMap(header *http.Header, v reflect.Value, tag reflect.StructTag) error {
|
|
|
|
prefix := tag.Get("locationName")
|
2016-01-22 02:17:53 +00:00
|
|
|
for _, key := range v.MapKeys() {
|
2017-11-29 22:20:27 +00:00
|
|
|
str, err := convertType(v.MapIndex(key), tag)
|
2016-01-22 02:17:53 +00:00
|
|
|
if err == errValueNotSet {
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
2020-03-09 07:09:54 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
|
2016-01-22 02:17:53 +00:00
|
|
|
|
|
|
|
}
|
2020-03-09 07:09:54 +00:00
|
|
|
keyStr := strings.TrimSpace(key.String())
|
|
|
|
str = strings.TrimSpace(str)
|
2016-01-22 02:17:53 +00:00
|
|
|
|
2020-03-09 07:09:54 +00:00
|
|
|
header.Add(prefix+keyStr, str)
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:20:27 +00:00
|
|
|
func buildURI(u *url.URL, v reflect.Value, name string, tag reflect.StructTag) error {
|
|
|
|
value, err := convertType(v, tag)
|
2016-01-22 02:17:53 +00:00
|
|
|
if err == errValueNotSet {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2020-03-09 07:09:54 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 10:16:28 +00:00
|
|
|
u.Path = strings.Replace(u.Path, "{"+name+"}", value, -1)
|
|
|
|
u.Path = strings.Replace(u.Path, "{"+name+"+}", value, -1)
|
|
|
|
|
|
|
|
u.RawPath = strings.Replace(u.RawPath, "{"+name+"}", EscapePath(value, true), -1)
|
|
|
|
u.RawPath = strings.Replace(u.RawPath, "{"+name+"+}", EscapePath(value, false), -1)
|
2016-01-22 02:17:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:20:27 +00:00
|
|
|
func buildQueryString(query url.Values, v reflect.Value, name string, tag reflect.StructTag) error {
|
2016-01-22 02:17:53 +00:00
|
|
|
switch value := v.Interface().(type) {
|
|
|
|
case []*string:
|
|
|
|
for _, item := range value {
|
|
|
|
query.Add(name, *item)
|
|
|
|
}
|
|
|
|
case map[string]*string:
|
|
|
|
for key, item := range value {
|
|
|
|
query.Add(key, *item)
|
|
|
|
}
|
|
|
|
case map[string][]*string:
|
|
|
|
for key, items := range value {
|
|
|
|
for _, item := range items {
|
|
|
|
query.Add(key, *item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2017-11-29 22:20:27 +00:00
|
|
|
str, err := convertType(v, tag)
|
2016-01-22 02:17:53 +00:00
|
|
|
if err == errValueNotSet {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2020-03-09 07:09:54 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
query.Set(name, str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-12 10:16:28 +00:00
|
|
|
func cleanPath(u *url.URL) {
|
|
|
|
hasSlash := strings.HasSuffix(u.Path, "/")
|
2016-01-22 02:17:53 +00:00
|
|
|
|
2017-01-12 10:16:28 +00:00
|
|
|
// clean up path, removing duplicate `/`
|
|
|
|
u.Path = path.Clean(u.Path)
|
|
|
|
u.RawPath = path.Clean(u.RawPath)
|
2016-01-22 02:17:53 +00:00
|
|
|
|
2017-01-12 10:16:28 +00:00
|
|
|
if hasSlash && !strings.HasSuffix(u.Path, "/") {
|
|
|
|
u.Path += "/"
|
|
|
|
u.RawPath += "/"
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-07-12 19:47:37 +00:00
|
|
|
fmt.Fprintf(&buf, "%%%02X", c)
|
2016-01-22 02:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:20:27 +00:00
|
|
|
func convertType(v reflect.Value, tag reflect.StructTag) (str string, err error) {
|
2016-01-22 02:17:53 +00:00
|
|
|
v = reflect.Indirect(v)
|
|
|
|
if !v.IsValid() {
|
|
|
|
return "", errValueNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
switch value := v.Interface().(type) {
|
|
|
|
case string:
|
2022-03-11 01:27:01 +00:00
|
|
|
if tag.Get("suppressedJSONValue") == "true" && tag.Get("location") == "header" {
|
|
|
|
value = base64.StdEncoding.EncodeToString([]byte(value))
|
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
str = value
|
|
|
|
case []byte:
|
|
|
|
str = base64.StdEncoding.EncodeToString(value)
|
|
|
|
case bool:
|
|
|
|
str = strconv.FormatBool(value)
|
|
|
|
case int64:
|
|
|
|
str = strconv.FormatInt(value, 10)
|
|
|
|
case float64:
|
|
|
|
str = strconv.FormatFloat(value, 'f', -1, 64)
|
|
|
|
case time.Time:
|
2018-08-14 19:13:40 +00:00
|
|
|
format := tag.Get("timestampFormat")
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = protocol.RFC822TimeFormatName
|
|
|
|
if tag.Get("location") == "querystring" {
|
|
|
|
format = protocol.ISO8601TimeFormatName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str = protocol.FormatTime(format, value)
|
2017-11-29 22:20:27 +00:00
|
|
|
case aws.JSONValue:
|
|
|
|
if len(value) == 0 {
|
|
|
|
return "", errValueNotSet
|
|
|
|
}
|
|
|
|
escaping := protocol.NoEscape
|
|
|
|
if tag.Get("location") == "header" {
|
|
|
|
escaping = protocol.Base64Escape
|
|
|
|
}
|
|
|
|
str, err = protocol.EncodeJSONValue(value, escaping)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to encode JSONValue, %v", err)
|
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
default:
|
2017-11-29 22:20:27 +00:00
|
|
|
err := fmt.Errorf("unsupported value for param %v (%s)", v.Interface(), v.Type())
|
2016-01-22 02:17:53 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2022-03-11 01:27:01 +00:00
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
return str, nil
|
|
|
|
}
|