build.go 7.4 KB

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