2015-09-16 20:05:04 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2018-10-24 13:15:06 +00:00
|
|
|
"bytes"
|
2015-09-16 20:05:04 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2016-11-03 15:37:23 +00:00
|
|
|
"io"
|
2015-09-16 20:05:04 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
2020-02-17 21:13:33 +00:00
|
|
|
awsStrings "github.com/aws/aws-sdk-go/internal/strings"
|
2018-10-24 13:15:06 +00:00
|
|
|
"github.com/aws/aws-sdk-go/private/protocol"
|
2015-09-16 20:05:04 +00:00
|
|
|
)
|
|
|
|
|
2016-11-03 15:37:23 +00:00
|
|
|
// UnmarshalHandler is a named request handler for unmarshaling rest protocol requests
|
|
|
|
var UnmarshalHandler = request.NamedHandler{Name: "awssdk.rest.Unmarshal", Fn: Unmarshal}
|
|
|
|
|
|
|
|
// UnmarshalMetaHandler is a named request handler for unmarshaling rest protocol request metadata
|
|
|
|
var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.rest.UnmarshalMeta", Fn: UnmarshalMeta}
|
|
|
|
|
2015-09-16 20:05:04 +00:00
|
|
|
// Unmarshal unmarshals the REST component of a response in a REST service.
|
|
|
|
func Unmarshal(r *request.Request) {
|
|
|
|
if r.DataFilled() {
|
|
|
|
v := reflect.Indirect(reflect.ValueOf(r.Data))
|
2020-02-17 21:13:33 +00:00
|
|
|
if err := unmarshalBody(r, v); err != nil {
|
|
|
|
r.Error = err
|
|
|
|
}
|
2016-11-03 15:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalMeta unmarshals the REST metadata of a response in a REST service
|
|
|
|
func UnmarshalMeta(r *request.Request) {
|
|
|
|
r.RequestID = r.HTTPResponse.Header.Get("X-Amzn-Requestid")
|
|
|
|
if r.RequestID == "" {
|
|
|
|
// Alternative version of request id in the header
|
|
|
|
r.RequestID = r.HTTPResponse.Header.Get("X-Amz-Request-Id")
|
|
|
|
}
|
|
|
|
if r.DataFilled() {
|
2020-02-17 21:13:33 +00:00
|
|
|
if err := UnmarshalResponse(r.HTTPResponse, r.Data, aws.BoolValue(r.Config.LowerCaseHeaderMaps)); err != nil {
|
|
|
|
r.Error = err
|
|
|
|
}
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:13:33 +00:00
|
|
|
// UnmarshalResponse attempts to unmarshal the REST response headers to
|
|
|
|
// the data type passed in. The type must be a pointer. An error is returned
|
|
|
|
// with any error unmarshaling the response into the target datatype.
|
|
|
|
func UnmarshalResponse(resp *http.Response, data interface{}, lowerCaseHeaderMaps bool) error {
|
|
|
|
v := reflect.Indirect(reflect.ValueOf(data))
|
|
|
|
return unmarshalLocationElements(resp, v, lowerCaseHeaderMaps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalBody(r *request.Request, v reflect.Value) error {
|
2016-11-03 15:37:23 +00:00
|
|
|
if field, ok := v.Type().FieldByName("_"); ok {
|
2015-09-16 20:05:04 +00:00
|
|
|
if payloadName := field.Tag.Get("payload"); payloadName != "" {
|
|
|
|
pfield, _ := v.Type().FieldByName(payloadName)
|
|
|
|
if ptag := pfield.Tag.Get("type"); ptag != "" && ptag != "structure" {
|
|
|
|
payload := v.FieldByName(payloadName)
|
|
|
|
if payload.IsValid() {
|
|
|
|
switch payload.Interface().(type) {
|
|
|
|
case []byte:
|
2016-11-03 15:37:23 +00:00
|
|
|
defer r.HTTPResponse.Body.Close()
|
2015-09-16 20:05:04 +00:00
|
|
|
b, err := ioutil.ReadAll(r.HTTPResponse.Body)
|
|
|
|
if err != nil {
|
2020-02-17 21:13:33 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization, "failed to decode REST response", err)
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
2020-02-17 21:13:33 +00:00
|
|
|
|
|
|
|
payload.Set(reflect.ValueOf(b))
|
|
|
|
|
2015-09-16 20:05:04 +00:00
|
|
|
case *string:
|
2016-11-03 15:37:23 +00:00
|
|
|
defer r.HTTPResponse.Body.Close()
|
2015-09-16 20:05:04 +00:00
|
|
|
b, err := ioutil.ReadAll(r.HTTPResponse.Body)
|
|
|
|
if err != nil {
|
2020-02-17 21:13:33 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization, "failed to decode REST response", err)
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
2020-02-17 21:13:33 +00:00
|
|
|
|
|
|
|
str := string(b)
|
|
|
|
payload.Set(reflect.ValueOf(&str))
|
|
|
|
|
2015-09-16 20:05:04 +00:00
|
|
|
default:
|
|
|
|
switch payload.Type().String() {
|
2018-10-24 13:15:06 +00:00
|
|
|
case "io.ReadCloser":
|
2015-09-16 20:05:04 +00:00
|
|
|
payload.Set(reflect.ValueOf(r.HTTPResponse.Body))
|
2020-02-17 21:13:33 +00:00
|
|
|
|
2018-10-24 13:15:06 +00:00
|
|
|
case "io.ReadSeeker":
|
|
|
|
b, err := ioutil.ReadAll(r.HTTPResponse.Body)
|
|
|
|
if err != nil {
|
2020-02-17 21:13:33 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization,
|
2018-10-24 13:15:06 +00:00
|
|
|
"failed to read response body", err)
|
|
|
|
}
|
|
|
|
payload.Set(reflect.ValueOf(ioutil.NopCloser(bytes.NewReader(b))))
|
2020-02-17 21:13:33 +00:00
|
|
|
|
2015-09-16 20:05:04 +00:00
|
|
|
default:
|
2016-11-03 15:37:23 +00:00
|
|
|
io.Copy(ioutil.Discard, r.HTTPResponse.Body)
|
2020-02-17 21:13:33 +00:00
|
|
|
r.HTTPResponse.Body.Close()
|
|
|
|
return awserr.New(request.ErrCodeSerialization,
|
2015-09-16 20:05:04 +00:00
|
|
|
"failed to decode REST response",
|
|
|
|
fmt.Errorf("unknown payload type %s", payload.Type()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 21:13:33 +00:00
|
|
|
|
|
|
|
return nil
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:13:33 +00:00
|
|
|
func unmarshalLocationElements(resp *http.Response, v reflect.Value, lowerCaseHeaderMaps bool) error {
|
2015-09-16 20:05:04 +00:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
m, field := v.Field(i), v.Type().Field(i)
|
|
|
|
if n := field.Name; n[0:1] == strings.ToLower(n[0:1]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.IsValid() {
|
|
|
|
name := field.Tag.Get("locationName")
|
|
|
|
if name == "" {
|
|
|
|
name = field.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
switch field.Tag.Get("location") {
|
|
|
|
case "statusCode":
|
2020-02-17 21:13:33 +00:00
|
|
|
unmarshalStatusCode(m, resp.StatusCode)
|
|
|
|
|
2015-09-16 20:05:04 +00:00
|
|
|
case "header":
|
2020-02-17 21:13:33 +00:00
|
|
|
err := unmarshalHeader(m, resp.Header.Get(name), field.Tag)
|
2015-09-16 20:05:04 +00:00
|
|
|
if err != nil {
|
2020-02-17 21:13:33 +00:00
|
|
|
return awserr.New(request.ErrCodeSerialization, "failed to decode REST response", err)
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
2020-02-17 21:13:33 +00:00
|
|
|
|
2015-09-16 20:05:04 +00:00
|
|
|
case "headers":
|
|
|
|
prefix := field.Tag.Get("locationName")
|
2020-02-17 21:13:33 +00:00
|
|
|
err := unmarshalHeaderMap(m, resp.Header, prefix, lowerCaseHeaderMaps)
|
2015-09-16 20:05:04 +00:00
|
|
|
if err != nil {
|
2020-02-17 21:13:33 +00:00
|
|
|
awserr.New(request.ErrCodeSerialization, "failed to decode REST response", err)
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 21:13:33 +00:00
|
|
|
|
|
|
|
return nil
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalStatusCode(v reflect.Value, statusCode int) {
|
|
|
|
if !v.IsValid() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.Interface().(type) {
|
|
|
|
case *int64:
|
|
|
|
s := int64(statusCode)
|
|
|
|
v.Set(reflect.ValueOf(&s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:13:33 +00:00
|
|
|
func unmarshalHeaderMap(r reflect.Value, headers http.Header, prefix string, normalize bool) error {
|
2019-09-06 20:00:19 +00:00
|
|
|
if len(headers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-16 20:05:04 +00:00
|
|
|
switch r.Interface().(type) {
|
|
|
|
case map[string]*string: // we only support string map value types
|
|
|
|
out := map[string]*string{}
|
|
|
|
for k, v := range headers {
|
2020-02-17 21:13:33 +00:00
|
|
|
if awsStrings.HasPrefixFold(k, prefix) {
|
|
|
|
if normalize == true {
|
|
|
|
k = strings.ToLower(k)
|
|
|
|
} else {
|
|
|
|
k = http.CanonicalHeaderKey(k)
|
|
|
|
}
|
2015-09-16 20:05:04 +00:00
|
|
|
out[k[len(prefix):]] = &v[0]
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 20:00:19 +00:00
|
|
|
if len(out) != 0 {
|
|
|
|
r.Set(reflect.ValueOf(out))
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:15:06 +00:00
|
|
|
func unmarshalHeader(v reflect.Value, header string, tag reflect.StructTag) error {
|
2019-09-06 20:00:19 +00:00
|
|
|
switch tag.Get("type") {
|
|
|
|
case "jsonvalue":
|
2018-10-24 13:15:06 +00:00
|
|
|
if len(header) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-06 20:00:19 +00:00
|
|
|
case "blob":
|
|
|
|
if len(header) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if !v.IsValid() || (header == "" && v.Elem().Kind() != reflect.String) {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-16 20:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v.Interface().(type) {
|
|
|
|
case *string:
|
|
|
|
v.Set(reflect.ValueOf(&header))
|
|
|
|
case []byte:
|
|
|
|
b, err := base64.StdEncoding.DecodeString(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-06 20:00:19 +00:00
|
|
|
v.Set(reflect.ValueOf(b))
|
2015-09-16 20:05:04 +00:00
|
|
|
case *bool:
|
|
|
|
b, err := strconv.ParseBool(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Set(reflect.ValueOf(&b))
|
|
|
|
case *int64:
|
|
|
|
i, err := strconv.ParseInt(header, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Set(reflect.ValueOf(&i))
|
|
|
|
case *float64:
|
|
|
|
f, err := strconv.ParseFloat(header, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Set(reflect.ValueOf(&f))
|
|
|
|
case *time.Time:
|
2019-05-20 18:08:26 +00:00
|
|
|
format := tag.Get("timestampFormat")
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = protocol.RFC822TimeFormatName
|
|
|
|
}
|
|
|
|
t, err := protocol.ParseTime(format, header)
|
2015-09-16 20:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Set(reflect.ValueOf(&t))
|
2018-10-24 13:15:06 +00:00
|
|
|
case aws.JSONValue:
|
|
|
|
escaping := protocol.NoEscape
|
|
|
|
if tag.Get("location") == "header" {
|
|
|
|
escaping = protocol.Base64Escape
|
|
|
|
}
|
|
|
|
m, err := protocol.DecodeJSONValue(header, escaping)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Set(reflect.ValueOf(m))
|
2015-09-16 20:05:04 +00:00
|
|
|
default:
|
|
|
|
err := fmt.Errorf("Unsupported value for param %v (%s)", v.Interface(), v.Type())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|