build.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Package rest provides RESTful serialization of AWS requests and responses.
  2. package rest
  3. import (
  4. "bytes"
  5. "encoding/base64"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "path"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/aws/aws-sdk-go/aws"
  16. "github.com/aws/aws-sdk-go/aws/awserr"
  17. "github.com/aws/aws-sdk-go/aws/request"
  18. "github.com/aws/aws-sdk-go/private/protocol"
  19. )
  20. // Whether the byte value can be sent without escaping in AWS URLs
  21. var noEscape [256]bool
  22. var errValueNotSet = fmt.Errorf("value not set")
  23. var byteSliceType = reflect.TypeOf([]byte{})
  24. func init() {
  25. for i := 0; i < len(noEscape); i++ {
  26. // AWS expects every character except these to be escaped
  27. noEscape[i] = (i >= 'A' && i <= 'Z') ||
  28. (i >= 'a' && i <= 'z') ||
  29. (i >= '0' && i <= '9') ||
  30. i == '-' ||
  31. i == '.' ||
  32. i == '_' ||
  33. i == '~'
  34. }
  35. }
  36. // BuildHandler is a named request handler for building rest protocol requests
  37. var BuildHandler = request.NamedHandler{Name: "awssdk.rest.Build", Fn: Build}
  38. // Build builds the REST component of a service request.
  39. func Build(r *request.Request) {
  40. if r.ParamsFilled() {
  41. v := reflect.ValueOf(r.Params).Elem()
  42. buildLocationElements(r, v, false)
  43. buildBody(r, v)
  44. }
  45. }
  46. // BuildAsGET builds the REST component of a service request with the ability to hoist
  47. // data from the body.
  48. func BuildAsGET(r *request.Request) {
  49. if r.ParamsFilled() {
  50. v := reflect.ValueOf(r.Params).Elem()
  51. buildLocationElements(r, v, true)
  52. buildBody(r, v)
  53. }
  54. }
  55. func buildLocationElements(r *request.Request, v reflect.Value, buildGETQuery bool) {
  56. query := r.HTTPRequest.URL.Query()
  57. // Setup the raw path to match the base path pattern. This is needed
  58. // so that when the path is mutated a custom escaped version can be
  59. // stored in RawPath that will be used by the Go client.
  60. r.HTTPRequest.URL.RawPath = r.HTTPRequest.URL.Path
  61. for i := 0; i < v.NumField(); i++ {
  62. m := v.Field(i)
  63. if n := v.Type().Field(i).Name; n[0:1] == strings.ToLower(n[0:1]) {
  64. continue
  65. }
  66. if m.IsValid() {
  67. field := v.Type().Field(i)
  68. name := field.Tag.Get("locationName")
  69. if name == "" {
  70. name = field.Name
  71. }
  72. if kind := m.Kind(); kind == reflect.Ptr {
  73. m = m.Elem()
  74. } else if kind == reflect.Interface {
  75. if !m.Elem().IsValid() {
  76. continue
  77. }
  78. }
  79. if !m.IsValid() {
  80. continue
  81. }
  82. if field.Tag.Get("ignore") != "" {
  83. continue
  84. }
  85. // Support the ability to customize values to be marshaled as a
  86. // blob even though they were modeled as a string. Required for S3
  87. // API operations like SSECustomerKey is modeled as stirng but
  88. // required to be base64 encoded in request.
  89. if field.Tag.Get("marshal-as") == "blob" {
  90. m = m.Convert(byteSliceType)
  91. }
  92. var err error
  93. switch field.Tag.Get("location") {
  94. case "headers": // header maps
  95. err = buildHeaderMap(&r.HTTPRequest.Header, m, field.Tag)
  96. case "header":
  97. err = buildHeader(&r.HTTPRequest.Header, m, name, field.Tag)
  98. case "uri":
  99. err = buildURI(r.HTTPRequest.URL, m, name, field.Tag)
  100. case "querystring":
  101. err = buildQueryString(query, m, name, field.Tag)
  102. default:
  103. if buildGETQuery {
  104. err = buildQueryString(query, m, name, field.Tag)
  105. }
  106. }
  107. r.Error = err
  108. }
  109. if r.Error != nil {
  110. return
  111. }
  112. }
  113. r.HTTPRequest.URL.RawQuery = query.Encode()
  114. if !aws.BoolValue(r.Config.DisableRestProtocolURICleaning) {
  115. cleanPath(r.HTTPRequest.URL)
  116. }
  117. }
  118. func buildBody(r *request.Request, v reflect.Value) {
  119. if field, ok := v.Type().FieldByName("_"); ok {
  120. if payloadName := field.Tag.Get("payload"); payloadName != "" {
  121. pfield, _ := v.Type().FieldByName(payloadName)
  122. if ptag := pfield.Tag.Get("type"); ptag != "" && ptag != "structure" {
  123. payload := reflect.Indirect(v.FieldByName(payloadName))
  124. if payload.IsValid() && payload.Interface() != nil {
  125. switch reader := payload.Interface().(type) {
  126. case io.ReadSeeker:
  127. r.SetReaderBody(reader)
  128. case []byte:
  129. r.SetBufferBody(reader)
  130. case string:
  131. r.SetStringBody(reader)
  132. default:
  133. r.Error = awserr.New(request.ErrCodeSerialization,
  134. "failed to encode REST request",
  135. fmt.Errorf("unknown payload type %s", payload.Type()))
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. func buildHeader(header *http.Header, v reflect.Value, name string, tag reflect.StructTag) error {
  143. str, err := convertType(v, tag)
  144. if err == errValueNotSet {
  145. return nil
  146. } else if err != nil {
  147. return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
  148. }
  149. name = strings.TrimSpace(name)
  150. str = strings.TrimSpace(str)
  151. header.Add(name, str)
  152. return nil
  153. }
  154. func buildHeaderMap(header *http.Header, v reflect.Value, tag reflect.StructTag) error {
  155. prefix := tag.Get("locationName")
  156. for _, key := range v.MapKeys() {
  157. str, err := convertType(v.MapIndex(key), tag)
  158. if err == errValueNotSet {
  159. continue
  160. } else if err != nil {
  161. return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
  162. }
  163. keyStr := strings.TrimSpace(key.String())
  164. str = strings.TrimSpace(str)
  165. header.Add(prefix+keyStr, str)
  166. }
  167. return nil
  168. }
  169. func buildURI(u *url.URL, v reflect.Value, name string, tag reflect.StructTag) error {
  170. value, err := convertType(v, tag)
  171. if err == errValueNotSet {
  172. return nil
  173. } else if err != nil {
  174. return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
  175. }
  176. u.Path = strings.Replace(u.Path, "{"+name+"}", value, -1)
  177. u.Path = strings.Replace(u.Path, "{"+name+"+}", value, -1)
  178. u.RawPath = strings.Replace(u.RawPath, "{"+name+"}", EscapePath(value, true), -1)
  179. u.RawPath = strings.Replace(u.RawPath, "{"+name+"+}", EscapePath(value, false), -1)
  180. return nil
  181. }
  182. func buildQueryString(query url.Values, v reflect.Value, name string, tag reflect.StructTag) error {
  183. switch value := v.Interface().(type) {
  184. case []*string:
  185. for _, item := range value {
  186. query.Add(name, *item)
  187. }
  188. case map[string]*string:
  189. for key, item := range value {
  190. query.Add(key, *item)
  191. }
  192. case map[string][]*string:
  193. for key, items := range value {
  194. for _, item := range items {
  195. query.Add(key, *item)
  196. }
  197. }
  198. default:
  199. str, err := convertType(v, tag)
  200. if err == errValueNotSet {
  201. return nil
  202. } else if err != nil {
  203. return awserr.New(request.ErrCodeSerialization, "failed to encode REST request", err)
  204. }
  205. query.Set(name, str)
  206. }
  207. return nil
  208. }
  209. func cleanPath(u *url.URL) {
  210. hasSlash := strings.HasSuffix(u.Path, "/")
  211. // clean up path, removing duplicate `/`
  212. u.Path = path.Clean(u.Path)
  213. u.RawPath = path.Clean(u.RawPath)
  214. if hasSlash && !strings.HasSuffix(u.Path, "/") {
  215. u.Path += "/"
  216. u.RawPath += "/"
  217. }
  218. }
  219. // EscapePath escapes part of a URL path in Amazon style
  220. func EscapePath(path string, encodeSep bool) string {
  221. var buf bytes.Buffer
  222. for i := 0; i < len(path); i++ {
  223. c := path[i]
  224. if noEscape[c] || (c == '/' && !encodeSep) {
  225. buf.WriteByte(c)
  226. } else {
  227. fmt.Fprintf(&buf, "%%%02X", c)
  228. }
  229. }
  230. return buf.String()
  231. }
  232. func convertType(v reflect.Value, tag reflect.StructTag) (str string, err error) {
  233. v = reflect.Indirect(v)
  234. if !v.IsValid() {
  235. return "", errValueNotSet
  236. }
  237. switch value := v.Interface().(type) {
  238. case string:
  239. str = value
  240. case []byte:
  241. str = base64.StdEncoding.EncodeToString(value)
  242. case bool:
  243. str = strconv.FormatBool(value)
  244. case int64:
  245. str = strconv.FormatInt(value, 10)
  246. case float64:
  247. str = strconv.FormatFloat(value, 'f', -1, 64)
  248. case time.Time:
  249. format := tag.Get("timestampFormat")
  250. if len(format) == 0 {
  251. format = protocol.RFC822TimeFormatName
  252. if tag.Get("location") == "querystring" {
  253. format = protocol.ISO8601TimeFormatName
  254. }
  255. }
  256. str = protocol.FormatTime(format, value)
  257. case aws.JSONValue:
  258. if len(value) == 0 {
  259. return "", errValueNotSet
  260. }
  261. escaping := protocol.NoEscape
  262. if tag.Get("location") == "header" {
  263. escaping = protocol.Base64Escape
  264. }
  265. str, err = protocol.EncodeJSONValue(value, escaping)
  266. if err != nil {
  267. return "", fmt.Errorf("unable to encode JSONValue, %v", err)
  268. }
  269. default:
  270. err := fmt.Errorf("unsupported value for param %v (%s)", v.Interface(), v.Type())
  271. return "", err
  272. }
  273. return str, nil
  274. }