build.go 7.4 KB

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