responder.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package autorest
  2. // Copyright 2017 Microsoft Corporation
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "encoding/xml"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "net/http"
  23. "strings"
  24. )
  25. // Responder is the interface that wraps the Respond method.
  26. //
  27. // Respond accepts and reacts to an http.Response. Implementations must ensure to not share or hold
  28. // state since Responders may be shared and re-used.
  29. type Responder interface {
  30. Respond(*http.Response) error
  31. }
  32. // ResponderFunc is a method that implements the Responder interface.
  33. type ResponderFunc func(*http.Response) error
  34. // Respond implements the Responder interface on ResponderFunc.
  35. func (rf ResponderFunc) Respond(r *http.Response) error {
  36. return rf(r)
  37. }
  38. // RespondDecorator takes and possibly decorates, by wrapping, a Responder. Decorators may react to
  39. // the http.Response and pass it along or, first, pass the http.Response along then react.
  40. type RespondDecorator func(Responder) Responder
  41. // CreateResponder creates, decorates, and returns a Responder. Without decorators, the returned
  42. // Responder returns the passed http.Response unmodified. Responders may or may not be safe to share
  43. // and re-used: It depends on the applied decorators. For example, a standard decorator that closes
  44. // the response body is fine to share whereas a decorator that reads the body into a passed struct
  45. // is not.
  46. //
  47. // To prevent memory leaks, ensure that at least one Responder closes the response body.
  48. func CreateResponder(decorators ...RespondDecorator) Responder {
  49. return DecorateResponder(
  50. Responder(ResponderFunc(func(r *http.Response) error { return nil })),
  51. decorators...)
  52. }
  53. // DecorateResponder accepts a Responder and a, possibly empty, set of RespondDecorators, which it
  54. // applies to the Responder. Decorators are applied in the order received, but their affect upon the
  55. // request depends on whether they are a pre-decorator (react to the http.Response and then pass it
  56. // along) or a post-decorator (pass the http.Response along and then react).
  57. func DecorateResponder(r Responder, decorators ...RespondDecorator) Responder {
  58. for _, decorate := range decorators {
  59. r = decorate(r)
  60. }
  61. return r
  62. }
  63. // Respond accepts an http.Response and a, possibly empty, set of RespondDecorators.
  64. // It creates a Responder from the decorators it then applies to the passed http.Response.
  65. func Respond(r *http.Response, decorators ...RespondDecorator) error {
  66. if r == nil {
  67. return nil
  68. }
  69. return CreateResponder(decorators...).Respond(r)
  70. }
  71. // ByIgnoring returns a RespondDecorator that ignores the passed http.Response passing it unexamined
  72. // to the next RespondDecorator.
  73. func ByIgnoring() RespondDecorator {
  74. return func(r Responder) Responder {
  75. return ResponderFunc(func(resp *http.Response) error {
  76. return r.Respond(resp)
  77. })
  78. }
  79. }
  80. // ByCopying copies the contents of the http.Response Body into the passed bytes.Buffer as
  81. // the Body is read.
  82. func ByCopying(b *bytes.Buffer) RespondDecorator {
  83. return func(r Responder) Responder {
  84. return ResponderFunc(func(resp *http.Response) error {
  85. err := r.Respond(resp)
  86. if err == nil && resp != nil && resp.Body != nil {
  87. resp.Body = TeeReadCloser(resp.Body, b)
  88. }
  89. return err
  90. })
  91. }
  92. }
  93. // ByDiscardingBody returns a RespondDecorator that first invokes the passed Responder after which
  94. // it copies the remaining bytes (if any) in the response body to ioutil.Discard. Since the passed
  95. // Responder is invoked prior to discarding the response body, the decorator may occur anywhere
  96. // within the set.
  97. func ByDiscardingBody() RespondDecorator {
  98. return func(r Responder) Responder {
  99. return ResponderFunc(func(resp *http.Response) error {
  100. err := r.Respond(resp)
  101. if err == nil && resp != nil && resp.Body != nil {
  102. if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
  103. return fmt.Errorf("Error discarding the response body: %v", err)
  104. }
  105. }
  106. return err
  107. })
  108. }
  109. }
  110. // ByClosing returns a RespondDecorator that first invokes the passed Responder after which it
  111. // closes the response body. Since the passed Responder is invoked prior to closing the response
  112. // body, the decorator may occur anywhere within the set.
  113. func ByClosing() RespondDecorator {
  114. return func(r Responder) Responder {
  115. return ResponderFunc(func(resp *http.Response) error {
  116. err := r.Respond(resp)
  117. if resp != nil && resp.Body != nil {
  118. if err := resp.Body.Close(); err != nil {
  119. return fmt.Errorf("Error closing the response body: %v", err)
  120. }
  121. }
  122. return err
  123. })
  124. }
  125. }
  126. // ByClosingIfError returns a RespondDecorator that first invokes the passed Responder after which
  127. // it closes the response if the passed Responder returns an error and the response body exists.
  128. func ByClosingIfError() RespondDecorator {
  129. return func(r Responder) Responder {
  130. return ResponderFunc(func(resp *http.Response) error {
  131. err := r.Respond(resp)
  132. if err != nil && resp != nil && resp.Body != nil {
  133. if err := resp.Body.Close(); err != nil {
  134. return fmt.Errorf("Error closing the response body: %v", err)
  135. }
  136. }
  137. return err
  138. })
  139. }
  140. }
  141. // ByUnmarshallingJSON returns a RespondDecorator that decodes a JSON document returned in the
  142. // response Body into the value pointed to by v.
  143. func ByUnmarshallingJSON(v interface{}) RespondDecorator {
  144. return func(r Responder) Responder {
  145. return ResponderFunc(func(resp *http.Response) error {
  146. err := r.Respond(resp)
  147. if err == nil {
  148. b, errInner := ioutil.ReadAll(resp.Body)
  149. // Some responses might include a BOM, remove for successful unmarshalling
  150. b = bytes.TrimPrefix(b, []byte("\xef\xbb\xbf"))
  151. if errInner != nil {
  152. err = fmt.Errorf("Error occurred reading http.Response#Body - Error = '%v'", errInner)
  153. } else if len(strings.Trim(string(b), " ")) > 0 {
  154. errInner = json.Unmarshal(b, v)
  155. if errInner != nil {
  156. err = fmt.Errorf("Error occurred unmarshalling JSON - Error = '%v' JSON = '%s'", errInner, string(b))
  157. }
  158. }
  159. }
  160. return err
  161. })
  162. }
  163. }
  164. // ByUnmarshallingXML returns a RespondDecorator that decodes a XML document returned in the
  165. // response Body into the value pointed to by v.
  166. func ByUnmarshallingXML(v interface{}) RespondDecorator {
  167. return func(r Responder) Responder {
  168. return ResponderFunc(func(resp *http.Response) error {
  169. err := r.Respond(resp)
  170. if err == nil {
  171. b, errInner := ioutil.ReadAll(resp.Body)
  172. if errInner != nil {
  173. err = fmt.Errorf("Error occurred reading http.Response#Body - Error = '%v'", errInner)
  174. } else {
  175. errInner = xml.Unmarshal(b, v)
  176. if errInner != nil {
  177. err = fmt.Errorf("Error occurred unmarshalling Xml - Error = '%v' Xml = '%s'", errInner, string(b))
  178. }
  179. }
  180. }
  181. return err
  182. })
  183. }
  184. }
  185. // WithErrorUnlessStatusCode returns a RespondDecorator that emits an error unless the response
  186. // StatusCode is among the set passed. On error, response body is fully read into a buffer and
  187. // presented in the returned error, as well as in the response body.
  188. func WithErrorUnlessStatusCode(codes ...int) RespondDecorator {
  189. return func(r Responder) Responder {
  190. return ResponderFunc(func(resp *http.Response) error {
  191. err := r.Respond(resp)
  192. if err == nil && !ResponseHasStatusCode(resp, codes...) {
  193. derr := NewErrorWithResponse("autorest", "WithErrorUnlessStatusCode", resp, "%v %v failed with %s",
  194. resp.Request.Method,
  195. resp.Request.URL,
  196. resp.Status)
  197. if resp.Body != nil {
  198. defer resp.Body.Close()
  199. b, _ := ioutil.ReadAll(resp.Body)
  200. derr.ServiceError = b
  201. resp.Body = ioutil.NopCloser(bytes.NewReader(b))
  202. }
  203. err = derr
  204. }
  205. return err
  206. })
  207. }
  208. }
  209. // WithErrorUnlessOK returns a RespondDecorator that emits an error if the response StatusCode is
  210. // anything other than HTTP 200.
  211. func WithErrorUnlessOK() RespondDecorator {
  212. return WithErrorUnlessStatusCode(http.StatusOK)
  213. }
  214. // ExtractHeader extracts all values of the specified header from the http.Response. It returns an
  215. // empty string slice if the passed http.Response is nil or the header does not exist.
  216. func ExtractHeader(header string, resp *http.Response) []string {
  217. if resp != nil && resp.Header != nil {
  218. return resp.Header[http.CanonicalHeaderKey(header)]
  219. }
  220. return nil
  221. }
  222. // ExtractHeaderValue extracts the first value of the specified header from the http.Response. It
  223. // returns an empty string if the passed http.Response is nil or the header does not exist.
  224. func ExtractHeaderValue(header string, resp *http.Response) string {
  225. h := ExtractHeader(header, resp)
  226. if len(h) > 0 {
  227. return h[0]
  228. }
  229. return ""
  230. }