client.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package client
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "net/http/httptrace"
  11. "net/http/httputil"
  12. "net/url"
  13. "runtime"
  14. "strings"
  15. "github.com/qiniu/go-sdk/v7/auth"
  16. "github.com/qiniu/go-sdk/v7/conf"
  17. "github.com/qiniu/go-sdk/v7/internal/log"
  18. "github.com/qiniu/go-sdk/v7/reqid"
  19. )
  20. var UserAgent = "Golang qiniu/client package"
  21. var DefaultClient = Client{&http.Client{Transport: http.DefaultTransport}}
  22. // 用来打印调试信息
  23. var DebugMode = false
  24. var DeepDebugInfo = false
  25. // --------------------------------------------------------------------
  26. // Client 负责发送HTTP请求到七牛接口服务器
  27. type Client struct {
  28. *http.Client
  29. }
  30. // TurnOnDebug 开启Debug模式
  31. func TurnOnDebug() {
  32. DebugMode = true
  33. }
  34. // userApp should be [A-Za-z0-9_\ \-\.]*
  35. func SetAppName(userApp string) error {
  36. UserAgent = fmt.Sprintf(
  37. "QiniuGo/%s (%s; %s; %s) %s", conf.Version, runtime.GOOS, runtime.GOARCH, userApp, runtime.Version())
  38. return nil
  39. }
  40. // --------------------------------------------------------------------
  41. func newRequest(ctx context.Context, method, reqUrl string, headers http.Header, body io.Reader) (req *http.Request, err error) {
  42. req, err = http.NewRequest(method, reqUrl, body)
  43. if err != nil {
  44. return
  45. }
  46. if headers == nil {
  47. headers = http.Header{}
  48. }
  49. err = addDefaultHeader(headers)
  50. if err != nil {
  51. return
  52. }
  53. req.Header = headers
  54. req = req.WithContext(ctx)
  55. //check access token
  56. mac, t, ok := auth.CredentialsFromContext(ctx)
  57. if ok {
  58. err = mac.AddToken(t, req)
  59. if err != nil {
  60. return
  61. }
  62. }
  63. if DebugMode {
  64. trace := &httptrace.ClientTrace{
  65. GotConn: func(connInfo httptrace.GotConnInfo) {
  66. remoteAddr := connInfo.Conn.RemoteAddr()
  67. log.Debug(fmt.Sprintf("Network: %s, Remote ip:%s, URL: %s", remoteAddr.Network(), remoteAddr.String(), req.URL))
  68. },
  69. }
  70. req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
  71. bs, bErr := httputil.DumpRequest(req, DeepDebugInfo)
  72. if bErr != nil {
  73. err = bErr
  74. return
  75. }
  76. log.Debug(string(bs))
  77. }
  78. return
  79. }
  80. func (r Client) DoRequest(ctx context.Context, method, reqUrl string, headers http.Header) (resp *http.Response, err error) {
  81. req, err := newRequest(ctx, method, reqUrl, headers, nil)
  82. if err != nil {
  83. return
  84. }
  85. return r.Do(ctx, req)
  86. }
  87. func (r Client) DoRequestWith(ctx context.Context, method, reqUrl string, headers http.Header, body io.Reader,
  88. bodyLength int) (resp *http.Response, err error) {
  89. req, err := newRequest(ctx, method, reqUrl, headers, body)
  90. if err != nil {
  91. return
  92. }
  93. req.ContentLength = int64(bodyLength)
  94. return r.Do(ctx, req)
  95. }
  96. func (r Client) DoRequestWith64(ctx context.Context, method, reqUrl string, headers http.Header, body io.Reader,
  97. bodyLength int64) (resp *http.Response, err error) {
  98. req, err := newRequest(ctx, method, reqUrl, headers, body)
  99. if err != nil {
  100. return
  101. }
  102. req.ContentLength = bodyLength
  103. return r.Do(ctx, req)
  104. }
  105. func (r Client) DoRequestWithBodyGetter(ctx context.Context, method, reqUrl string, headers http.Header, body io.Reader,
  106. getBody func() (io.ReadCloser, error), bodyLength int64) (resp *http.Response, err error) {
  107. req, err := newRequest(ctx, method, reqUrl, headers, body)
  108. if err != nil {
  109. return
  110. }
  111. req.ContentLength = bodyLength
  112. req.GetBody = getBody
  113. return r.Do(ctx, req)
  114. }
  115. func (r Client) DoRequestWithForm(ctx context.Context, method, reqUrl string, headers http.Header,
  116. data map[string][]string) (resp *http.Response, err error) {
  117. if headers == nil {
  118. headers = http.Header{}
  119. }
  120. headers.Add("Content-Type", "application/x-www-form-urlencoded")
  121. requestData := url.Values(data).Encode()
  122. if method == "GET" || method == "HEAD" || method == "DELETE" {
  123. if strings.ContainsRune(reqUrl, '?') {
  124. reqUrl += "&"
  125. } else {
  126. reqUrl += "?"
  127. }
  128. return r.DoRequest(ctx, method, reqUrl+requestData, headers)
  129. }
  130. return r.DoRequestWith(ctx, method, reqUrl, headers, strings.NewReader(requestData), len(requestData))
  131. }
  132. func (r Client) DoRequestWithJson(ctx context.Context, method, reqUrl string, headers http.Header,
  133. data interface{}) (resp *http.Response, err error) {
  134. reqBody, err := json.Marshal(data)
  135. if err != nil {
  136. return
  137. }
  138. if headers == nil {
  139. headers = http.Header{}
  140. }
  141. headers.Add("Content-Type", "application/json")
  142. return r.DoRequestWith(ctx, method, reqUrl, headers, bytes.NewReader(reqBody), len(reqBody))
  143. }
  144. func (r Client) Do(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
  145. reqctx := req.Context()
  146. if reqId, ok := reqid.ReqidFromContext(ctx); ok {
  147. req.Header.Set("X-Reqid", reqId)
  148. } else if reqId, ok = reqid.ReqidFromContext(reqctx); ok {
  149. req.Header.Set("X-Reqid", reqId)
  150. }
  151. if _, ok := req.Header["User-Agent"]; !ok {
  152. req.Header.Set("User-Agent", UserAgent)
  153. }
  154. resp, err = r.Client.Do(req)
  155. return
  156. }
  157. // --------------------------------------------------------------------
  158. type ErrorInfo struct {
  159. Err string `json:"error,omitempty"`
  160. Key string `json:"key,omitempty"`
  161. Reqid string `json:"reqid,omitempty"`
  162. Errno int `json:"errno,omitempty"`
  163. Code int `json:"code"`
  164. }
  165. func (r *ErrorInfo) ErrorDetail() string {
  166. msg, _ := json.Marshal(r)
  167. return string(msg)
  168. }
  169. func (r *ErrorInfo) Error() string {
  170. return r.Err
  171. }
  172. func (r *ErrorInfo) RpcError() (code, errno int, key, err string) {
  173. return r.Code, r.Errno, r.Key, r.Err
  174. }
  175. func (r *ErrorInfo) HttpCode() int {
  176. return r.Code
  177. }
  178. // --------------------------------------------------------------------
  179. func parseError(e *ErrorInfo, r io.Reader) {
  180. body, err1 := ioutil.ReadAll(r)
  181. if err1 != nil {
  182. e.Err = err1.Error()
  183. return
  184. }
  185. var ret struct {
  186. Err string `json:"error"`
  187. Key string `json:"key"`
  188. Errno int `json:"errno"`
  189. }
  190. if decodeJsonFromData(body, &ret) == nil && ret.Err != "" {
  191. // qiniu error msg style returns here
  192. e.Err, e.Key, e.Errno = ret.Err, ret.Key, ret.Errno
  193. return
  194. }
  195. e.Err = string(body)
  196. }
  197. func ResponseError(resp *http.Response) (err error) {
  198. e := &ErrorInfo{
  199. Reqid: resp.Header.Get("X-Reqid"),
  200. Code: resp.StatusCode,
  201. }
  202. if resp.StatusCode > 299 {
  203. if resp.ContentLength != 0 {
  204. ct, ok := resp.Header["Content-Type"]
  205. if ok && strings.HasPrefix(ct[0], "application/json") {
  206. parseError(e, resp.Body)
  207. } else {
  208. bs, rErr := ioutil.ReadAll(resp.Body)
  209. if rErr != nil {
  210. err = rErr
  211. }
  212. e.Err = strings.TrimRight(string(bs), "\n")
  213. }
  214. }
  215. }
  216. return e
  217. }
  218. func CallRet(ctx context.Context, ret interface{}, resp *http.Response) (err error) {
  219. defer func() {
  220. io.Copy(ioutil.Discard, resp.Body)
  221. resp.Body.Close()
  222. }()
  223. if DebugMode {
  224. bs, dErr := httputil.DumpResponse(resp, DeepDebugInfo)
  225. if dErr != nil {
  226. err = dErr
  227. return
  228. }
  229. log.Debug(string(bs))
  230. }
  231. if resp.StatusCode/100 == 2 {
  232. if ret != nil && resp.ContentLength != 0 {
  233. err = decodeJsonFromReader(resp.Body, ret)
  234. if err != nil {
  235. return
  236. }
  237. }
  238. return nil
  239. }
  240. return ResponseError(resp)
  241. }
  242. func (r Client) CallWithForm(ctx context.Context, ret interface{}, method, reqUrl string, headers http.Header,
  243. param map[string][]string) (err error) {
  244. resp, err := r.DoRequestWithForm(ctx, method, reqUrl, headers, param)
  245. if err != nil {
  246. return err
  247. }
  248. return CallRet(ctx, ret, resp)
  249. }
  250. func (r Client) CallWithJson(ctx context.Context, ret interface{}, method, reqUrl string, headers http.Header,
  251. param interface{}) (err error) {
  252. resp, err := r.DoRequestWithJson(ctx, method, reqUrl, headers, param)
  253. if err != nil {
  254. return err
  255. }
  256. return CallRet(ctx, ret, resp)
  257. }
  258. func (r Client) CallWith(ctx context.Context, ret interface{}, method, reqUrl string, headers http.Header, body io.Reader,
  259. bodyLength int) (err error) {
  260. resp, err := r.DoRequestWith(ctx, method, reqUrl, headers, body, bodyLength)
  261. if err != nil {
  262. return err
  263. }
  264. return CallRet(ctx, ret, resp)
  265. }
  266. func (r Client) CallWith64(ctx context.Context, ret interface{}, method, reqUrl string, headers http.Header, body io.Reader,
  267. bodyLength int64) (err error) {
  268. resp, err := r.DoRequestWith64(ctx, method, reqUrl, headers, body, bodyLength)
  269. if err != nil {
  270. return err
  271. }
  272. return CallRet(ctx, ret, resp)
  273. }
  274. func (r Client) CallWithBodyGetter(ctx context.Context, ret interface{}, method, reqUrl string, headers http.Header, body io.Reader,
  275. getBody func() (io.ReadCloser, error), bodyLength int64) (err error) {
  276. resp, err := r.DoRequestWithBodyGetter(ctx, method, reqUrl, headers, body, getBody, bodyLength)
  277. if err != nil {
  278. return err
  279. }
  280. return CallRet(ctx, ret, resp)
  281. }
  282. func (r Client) Call(ctx context.Context, ret interface{}, method, reqUrl string, headers http.Header) (err error) {
  283. resp, err := r.DoRequestWith(ctx, method, reqUrl, headers, nil, 0)
  284. if err != nil {
  285. return err
  286. }
  287. return CallRet(ctx, ret, resp)
  288. }
  289. func (r Client) CredentialedCallWithForm(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ret interface{},
  290. method, reqUrl string, headers http.Header, param map[string][]string) error {
  291. ctx = auth.WithCredentialsType(ctx, cred, tokenType)
  292. return r.CallWithForm(ctx, ret, method, reqUrl, headers, param)
  293. }
  294. func (r Client) CredentialedCallWithJson(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ret interface{},
  295. method, reqUrl string, headers http.Header, param interface{}) error {
  296. ctx = auth.WithCredentialsType(ctx, cred, tokenType)
  297. return r.CallWithJson(ctx, ret, method, reqUrl, headers, param)
  298. }
  299. func (r Client) CredentialedCallWith(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ret interface{},
  300. method, reqUrl string, headers http.Header, body io.Reader, bodyLength int) error {
  301. ctx = auth.WithCredentialsType(ctx, cred, tokenType)
  302. return r.CallWith(ctx, ret, method, reqUrl, headers, body, bodyLength)
  303. }
  304. func (r Client) CredentialedCallWith64(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ret interface{},
  305. method, reqUrl string, headers http.Header, body io.Reader, bodyLength int64) error {
  306. ctx = auth.WithCredentialsType(ctx, cred, tokenType)
  307. return r.CallWith64(ctx, ret, method, reqUrl, headers, body, bodyLength)
  308. }
  309. func (r Client) CredentialedCall(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ret interface{},
  310. method, reqUrl string, headers http.Header) error {
  311. ctx = auth.WithCredentialsType(ctx, cred, tokenType)
  312. return r.Call(ctx, ret, method, reqUrl, headers)
  313. }