httpcaller.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. Copyright 1999-2017 Alibaba Group Holding Ltd.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. CSB-HTTP-SDK based on GO language.
  13. */
  14. package csbhttp
  15. import (
  16. "fmt"
  17. //"crypto/tls"
  18. "bytes"
  19. "encoding/json"
  20. "io/ioutil"
  21. "net"
  22. "net/http"
  23. "net/url"
  24. "strings"
  25. "sync"
  26. "time"
  27. )
  28. var settingMutex sync.Mutex
  29. const (
  30. CSB_SDK_VERSION = "1.1.0"
  31. API_NAME_KEY = "_api_name"
  32. VERSION_KEY = "_api_version"
  33. ACCESS_KEY = "_api_access_key"
  34. SECRET_KEY = "_api_secret_key"
  35. SIGNATURE_KEY = "_api_signature"
  36. TIMESTAMP_KEY = "_api_timestamp"
  37. RESTFUL_PATH_SIGNATURE_KEY = "csb_restful_path_signature_key" //TODO: fix the terrible key name!
  38. )
  39. /**
  40. CSBHttp的基本设置结构
  41. */
  42. type CSBHTTPSettings struct {
  43. ShowDebug bool // "运行时是否显示调试信息"
  44. UserAgent string // "调用CSB服务的客户端代理, 默认为 csbBroker"
  45. ConnectTimeout time.Duration // "连接超时时间"
  46. ReadWriteTimeout time.Duration // "读写超时时间"
  47. Retries int // if set to -1 means will retry forever
  48. CareResponseHttpHeader bool // if return the response http headers
  49. SignPath bool
  50. /* TODO:support the following fields
  51. TLSClientConfig *tls.Config
  52. Proxy func(*http.Request) (*url.URL, error)
  53. Transport http.RoundTripper
  54. CheckRedirect func(req *http.Request, via []*http.Request) error
  55. EnableCookie bool
  56. */
  57. }
  58. var defaultSetting = CSBHTTPSettings{
  59. ShowDebug: true,
  60. UserAgent: "csbBroker",
  61. ConnectTimeout: 60 * time.Second,
  62. ReadWriteTimeout: 60 * time.Second,
  63. CareResponseHttpHeader: true,
  64. }
  65. /**
  66. 定义自己的http属性的结构来覆盖默认的设置
  67. */
  68. func SetDefaultSetting(setting CSBHTTPSettings) {
  69. settingMutex.Lock()
  70. defer settingMutex.Unlock()
  71. defaultSetting = setting
  72. }
  73. /**
  74. 内部方法: 拼接请求参数
  75. */
  76. func appendParams(reqUrl string, params string) string {
  77. if strings.Contains(reqUrl, "?") {
  78. return reqUrl + "&" + params
  79. } else {
  80. return reqUrl + "?" + params
  81. }
  82. }
  83. /**
  84. 内部方法: 将请求串中的请求参数装换为map
  85. */
  86. func parseUrlParamsMap(reqUrl string) (params map[string]string, err *HttpCallerException) {
  87. params = make(map[string]string) //must init the map
  88. if strings.Contains(reqUrl, "?") {
  89. i := strings.Index(reqUrl, "?")
  90. reqUrl = string(reqUrl[i+1:])
  91. fmt.Println(reqUrl)
  92. kvs := strings.Split(reqUrl, "&")
  93. if len(kvs) > 0 {
  94. for _, kv := range kvs {
  95. i = strings.Index(kv, "=")
  96. if i >= 0 {
  97. params[string(kv[0:i])] = string(kv[i+1:])
  98. } else {
  99. //TODO: write or throw exception
  100. fmt.Errorf("bad kv pair:", kv)
  101. }
  102. }
  103. }
  104. }
  105. return params, nil
  106. }
  107. /**
  108. 内部方法: 进行参数的签名处理
  109. */
  110. func signParams(params map[string]string, api string, version string, ak string, sk string, time_stamp string) (headMaps map[string]string) {
  111. headMaps = make(map[string]string)
  112. params[API_NAME_KEY] = api
  113. headMaps[API_NAME_KEY] = api
  114. params[VERSION_KEY] = version
  115. headMaps[VERSION_KEY] = version
  116. //https://currentmillis.com/ calc current time with varies languages
  117. //v := time.Now().UnixNano() / 1000000
  118. params[TIMESTAMP_KEY] = time_stamp
  119. headMaps[TIMESTAMP_KEY] = time_stamp
  120. if ak != "" {
  121. params[ACCESS_KEY] = ak
  122. headMaps[ACCESS_KEY] = ak
  123. delete(params, SECRET_KEY)
  124. delete(params, SIGNATURE_KEY)
  125. signValue := doSign(params, sk)
  126. headMaps[SIGNATURE_KEY] = signValue
  127. }
  128. return headMaps
  129. }
  130. /**
  131. 调用CSB开放出来的服务(后者CSB控制台的Open API),并放回结果
  132. 请求参数的内容根据 HttpParams 的定义进行设置
  133. 返回的结果包含: 调用fa返回结果串, 返回的httpheaders 和 异常
  134. 当处理正常时,异常为nil
  135. */
  136. func Invoke(params HttpParams, time_stamp string) (str string, rtnHeaders map[string][]string, hcError *HttpCallerException) {
  137. //init rtnHeaders
  138. rtnHeaders = make(map[string][]string)
  139. hcError = params.Validate()
  140. if hcError != nil {
  141. return str, rtnHeaders, hcError
  142. }
  143. _, err := url.Parse(params.requestUrl)
  144. if err != nil {
  145. return str, rtnHeaders, &HttpCallerException{CauseErr: err}
  146. }
  147. client := &http.Client{
  148. Transport: &http.Transport{
  149. Dial: func(netw, addr string) (net.Conn, error) {
  150. conn, err := net.DialTimeout(netw, addr, defaultSetting.ConnectTimeout) //设置建立连接超时
  151. if err != nil {
  152. return nil, err
  153. }
  154. conn.SetDeadline(time.Now().Add(defaultSetting.ReadWriteTimeout)) //设置发送接受数据超时
  155. return conn, nil
  156. },
  157. ResponseHeaderTimeout: time.Second * 60,
  158. },
  159. }
  160. //resp := &http.Response{}
  161. //for request parmas
  162. data := url.Values{}
  163. if params.params != nil {
  164. for k, v := range params.params {
  165. //strParams += fmt.Sprintf("%s=%s", k, url.QueryEscape())
  166. data.Set(k, v)
  167. }
  168. }
  169. var reqStr string
  170. var method string
  171. //method := "GET"
  172. if params.method == "post" {
  173. //genSignHeaders
  174. method = "POST"
  175. } else {
  176. method = "GET"
  177. }
  178. reqUrl := params.requestUrl
  179. defaultContentType := "application/json;charset=utf-8"
  180. if params.ct.jsonBody != "" {
  181. json.Marshal(params.ct.jsonBody)
  182. //if err == nil {
  183. // return str, rtnHeaders, &HttpCallerException{Message: "failed to bad content type json string", CauseErr: err}
  184. //}
  185. reqStr = params.ct.jsonBody
  186. defaultContentType = params.ct.contentType
  187. reqUrl = appendParams(reqUrl, data.Encode())
  188. } else if params.ct.bytesBody != nil {
  189. reqStr = string(params.ct.bytesBody)
  190. defaultContentType = params.ct.contentType
  191. reqUrl = appendParams(reqUrl, data.Encode())
  192. } else {
  193. reqStr = data.Encode()
  194. }
  195. req, err := http.NewRequest(method, reqUrl, bytes.NewBufferString(reqStr))
  196. if err != nil {
  197. return str, rtnHeaders, &HttpCallerException{Message: "failed to construct http post request", CauseErr: err}
  198. }
  199. //set signature related headers
  200. urlParams, hcError := parseUrlParamsMap(params.requestUrl)
  201. if err != nil {
  202. return str, rtnHeaders, hcError
  203. }
  204. printDebug("urlparams", urlParams)
  205. mergeTwoMaps(urlParams, params.params)
  206. printDebug("merged urlparams", urlParams)
  207. signHeaders := signParams(urlParams, params.api, params.version, params.ak, params.sk, time_stamp)
  208. printDebug("signHeaders", signHeaders)
  209. req.Header.Add("Content-Type", defaultContentType)
  210. if params.headers != nil {
  211. for k, v := range params.headers {
  212. req.Header.Add(k, v)
  213. }
  214. }
  215. //add sign related headers
  216. if signHeaders != nil {
  217. for k, v := range signHeaders {
  218. req.Header.Add(k, v)
  219. }
  220. }
  221. //client.Timeout
  222. resp, err := client.Do(req)
  223. if err != nil {
  224. return str, rtnHeaders, &HttpCallerException{Message: "failed to invoke http post", CauseErr: err}
  225. }
  226. if err != nil {
  227. fmt.Println(err)
  228. }
  229. defer resp.Body.Close()
  230. body, err := ioutil.ReadAll(resp.Body)
  231. if err != nil {
  232. fmt.Println(err)
  233. }
  234. str = string(body)
  235. if defaultSetting.CareResponseHttpHeader {
  236. rtnHeaders = resp.Header
  237. }
  238. /*
  239. fmt.Println("jsonStr", jsonStr)
  240. var dat map[string]string
  241. if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
  242. fmt.Println("token", dat["token"])
  243. } else {
  244. fmt.Println("json str to struct error")
  245. }
  246. */
  247. return str, rtnHeaders, nil
  248. }