client.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package sdk
  15. import (
  16. "context"
  17. "crypto/tls"
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "time"
  28. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
  29. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  30. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials/provider"
  31. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
  32. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
  33. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  34. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  35. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  36. )
  37. var debug utils.Debug
  38. func init() {
  39. debug = utils.Init("sdk")
  40. }
  41. // Version this value will be replaced while build: -ldflags="-X sdk.version=x.x.x"
  42. var Version = "0.0.1"
  43. var defaultConnectTimeout = 5 * time.Second
  44. var defaultReadTimeout = 10 * time.Second
  45. var DefaultUserAgent = fmt.Sprintf("AlibabaCloud (%s; %s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version)
  46. var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
  47. return fn
  48. }
  49. // Client the type Client
  50. type Client struct {
  51. isInsecure bool
  52. regionId string
  53. config *Config
  54. httpProxy string
  55. httpsProxy string
  56. noProxy string
  57. logger *Logger
  58. userAgent map[string]string
  59. signer auth.Signer
  60. httpClient *http.Client
  61. asyncTaskQueue chan func()
  62. readTimeout time.Duration
  63. connectTimeout time.Duration
  64. EndpointMap map[string]string
  65. EndpointType string
  66. Network string
  67. Domain string
  68. debug bool
  69. isRunning bool
  70. // void "panic(write to close channel)" cause of addAsync() after Shutdown()
  71. asyncChanLock *sync.RWMutex
  72. }
  73. func (client *Client) Init() (err error) {
  74. panic("not support yet")
  75. }
  76. func (client *Client) SetEndpointRules(endpointMap map[string]string, endpointType string, netWork string) {
  77. client.EndpointMap = endpointMap
  78. client.Network = netWork
  79. client.EndpointType = endpointType
  80. }
  81. func (client *Client) SetHTTPSInsecure(isInsecure bool) {
  82. client.isInsecure = isInsecure
  83. }
  84. func (client *Client) GetHTTPSInsecure() bool {
  85. return client.isInsecure
  86. }
  87. func (client *Client) SetHttpsProxy(httpsProxy string) {
  88. client.httpsProxy = httpsProxy
  89. }
  90. func (client *Client) GetHttpsProxy() string {
  91. return client.httpsProxy
  92. }
  93. func (client *Client) SetHttpProxy(httpProxy string) {
  94. client.httpProxy = httpProxy
  95. }
  96. func (client *Client) GetHttpProxy() string {
  97. return client.httpProxy
  98. }
  99. func (client *Client) SetNoProxy(noProxy string) {
  100. client.noProxy = noProxy
  101. }
  102. func (client *Client) GetNoProxy() string {
  103. return client.noProxy
  104. }
  105. func (client *Client) SetTransport(transport http.RoundTripper) {
  106. if client.httpClient == nil {
  107. client.httpClient = &http.Client{}
  108. }
  109. client.httpClient.Transport = transport
  110. }
  111. // InitWithProviderChain will get credential from the providerChain,
  112. // the RsaKeyPairCredential Only applicable to regionID `ap-northeast-1`,
  113. // if your providerChain may return a credential type with RsaKeyPairCredential,
  114. // please ensure your regionID is `ap-northeast-1`.
  115. func (client *Client) InitWithProviderChain(regionId string, provider provider.Provider) (err error) {
  116. config := client.InitClientConfig()
  117. credential, err := provider.Resolve()
  118. if err != nil {
  119. return
  120. }
  121. return client.InitWithOptions(regionId, config, credential)
  122. }
  123. func (client *Client) InitWithOptions(regionId string, config *Config, credential auth.Credential) (err error) {
  124. client.isRunning = true
  125. client.asyncChanLock = new(sync.RWMutex)
  126. client.regionId = regionId
  127. client.config = config
  128. client.httpClient = &http.Client{}
  129. if config.Transport != nil {
  130. client.httpClient.Transport = config.Transport
  131. } else if config.HttpTransport != nil {
  132. client.httpClient.Transport = config.HttpTransport
  133. }
  134. if config.Timeout > 0 {
  135. client.httpClient.Timeout = config.Timeout
  136. }
  137. if config.EnableAsync {
  138. client.EnableAsync(config.GoRoutinePoolSize, config.MaxTaskQueueSize)
  139. }
  140. client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner)
  141. return
  142. }
  143. func (client *Client) SetReadTimeout(readTimeout time.Duration) {
  144. client.readTimeout = readTimeout
  145. }
  146. func (client *Client) SetConnectTimeout(connectTimeout time.Duration) {
  147. client.connectTimeout = connectTimeout
  148. }
  149. func (client *Client) GetReadTimeout() time.Duration {
  150. return client.readTimeout
  151. }
  152. func (client *Client) GetConnectTimeout() time.Duration {
  153. return client.connectTimeout
  154. }
  155. func (client *Client) getHttpProxy(scheme string) (proxy *url.URL, err error) {
  156. if scheme == "https" {
  157. if client.GetHttpsProxy() != "" {
  158. proxy, err = url.Parse(client.httpsProxy)
  159. } else if rawurl := os.Getenv("HTTPS_PROXY"); rawurl != "" {
  160. proxy, err = url.Parse(rawurl)
  161. } else if rawurl := os.Getenv("https_proxy"); rawurl != "" {
  162. proxy, err = url.Parse(rawurl)
  163. }
  164. } else {
  165. if client.GetHttpProxy() != "" {
  166. proxy, err = url.Parse(client.httpProxy)
  167. } else if rawurl := os.Getenv("HTTP_PROXY"); rawurl != "" {
  168. proxy, err = url.Parse(rawurl)
  169. } else if rawurl := os.Getenv("http_proxy"); rawurl != "" {
  170. proxy, err = url.Parse(rawurl)
  171. }
  172. }
  173. return proxy, err
  174. }
  175. func (client *Client) getNoProxy(scheme string) []string {
  176. var urls []string
  177. if client.GetNoProxy() != "" {
  178. urls = strings.Split(client.noProxy, ",")
  179. } else if rawurl := os.Getenv("NO_PROXY"); rawurl != "" {
  180. urls = strings.Split(rawurl, ",")
  181. } else if rawurl := os.Getenv("no_proxy"); rawurl != "" {
  182. urls = strings.Split(rawurl, ",")
  183. }
  184. return urls
  185. }
  186. // EnableAsync enable the async task queue
  187. func (client *Client) EnableAsync(routinePoolSize, maxTaskQueueSize int) {
  188. client.asyncTaskQueue = make(chan func(), maxTaskQueueSize)
  189. for i := 0; i < routinePoolSize; i++ {
  190. go func() {
  191. for client.isRunning {
  192. select {
  193. case task, notClosed := <-client.asyncTaskQueue:
  194. if notClosed {
  195. task()
  196. }
  197. }
  198. }
  199. }()
  200. }
  201. }
  202. func (client *Client) InitWithAccessKey(regionId, accessKeyId, accessKeySecret string) (err error) {
  203. config := client.InitClientConfig()
  204. credential := &credentials.BaseCredential{
  205. AccessKeyId: accessKeyId,
  206. AccessKeySecret: accessKeySecret,
  207. }
  208. return client.InitWithOptions(regionId, config, credential)
  209. }
  210. func (client *Client) InitWithStsToken(regionId, accessKeyId, accessKeySecret, securityToken string) (err error) {
  211. config := client.InitClientConfig()
  212. credential := &credentials.StsTokenCredential{
  213. AccessKeyId: accessKeyId,
  214. AccessKeySecret: accessKeySecret,
  215. AccessKeyStsToken: securityToken,
  216. }
  217. return client.InitWithOptions(regionId, config, credential)
  218. }
  219. func (client *Client) InitWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (err error) {
  220. config := client.InitClientConfig()
  221. credential := &credentials.RamRoleArnCredential{
  222. AccessKeyId: accessKeyId,
  223. AccessKeySecret: accessKeySecret,
  224. RoleArn: roleArn,
  225. RoleSessionName: roleSessionName,
  226. }
  227. return client.InitWithOptions(regionId, config, credential)
  228. }
  229. func (client *Client) InitWithRamRoleArnAndPolicy(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName, policy string) (err error) {
  230. config := client.InitClientConfig()
  231. credential := &credentials.RamRoleArnCredential{
  232. AccessKeyId: accessKeyId,
  233. AccessKeySecret: accessKeySecret,
  234. RoleArn: roleArn,
  235. RoleSessionName: roleSessionName,
  236. Policy: policy,
  237. }
  238. return client.InitWithOptions(regionId, config, credential)
  239. }
  240. func (client *Client) InitWithRsaKeyPair(regionId, publicKeyId, privateKey string, sessionExpiration int) (err error) {
  241. config := client.InitClientConfig()
  242. credential := &credentials.RsaKeyPairCredential{
  243. PrivateKey: privateKey,
  244. PublicKeyId: publicKeyId,
  245. SessionExpiration: sessionExpiration,
  246. }
  247. return client.InitWithOptions(regionId, config, credential)
  248. }
  249. func (client *Client) InitWithEcsRamRole(regionId, roleName string) (err error) {
  250. config := client.InitClientConfig()
  251. credential := &credentials.EcsRamRoleCredential{
  252. RoleName: roleName,
  253. }
  254. return client.InitWithOptions(regionId, config, credential)
  255. }
  256. func (client *Client) InitWithBearerToken(regionId, bearerToken string) (err error) {
  257. config := client.InitClientConfig()
  258. credential := &credentials.BearerTokenCredential{
  259. BearerToken: bearerToken,
  260. }
  261. return client.InitWithOptions(regionId, config, credential)
  262. }
  263. func (client *Client) InitClientConfig() (config *Config) {
  264. if client.config != nil {
  265. return client.config
  266. } else {
  267. return NewConfig()
  268. }
  269. }
  270. func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) {
  271. return client.DoActionWithSigner(request, response, nil)
  272. }
  273. func (client *Client) GetEndpointRules(regionId string, product string) (endpointRaw string, err error) {
  274. if client.EndpointType == "regional" {
  275. if regionId == "" {
  276. err = fmt.Errorf("RegionId is empty, please set a valid RegionId.")
  277. return "", err
  278. }
  279. endpointRaw = strings.Replace("<product><network>.<region_id>.aliyuncs.com", "<region_id>", regionId, 1)
  280. } else {
  281. endpointRaw = "<product><network>.aliyuncs.com"
  282. }
  283. endpointRaw = strings.Replace(endpointRaw, "<product>", strings.ToLower(product), 1)
  284. if client.Network == "" || client.Network == "public" {
  285. endpointRaw = strings.Replace(endpointRaw, "<network>", "", 1)
  286. } else {
  287. endpointRaw = strings.Replace(endpointRaw, "<network>", "-"+client.Network, 1)
  288. }
  289. return endpointRaw, nil
  290. }
  291. func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) {
  292. // add clientVersion
  293. request.GetHeaders()["x-sdk-core-version"] = Version
  294. regionId := client.regionId
  295. if len(request.GetRegionId()) > 0 {
  296. regionId = request.GetRegionId()
  297. }
  298. // resolve endpoint
  299. endpoint := request.GetDomain()
  300. if endpoint == "" && client.Domain != "" {
  301. endpoint = client.Domain
  302. }
  303. if endpoint == "" {
  304. endpoint = endpoints.GetEndpointFromMap(regionId, request.GetProduct())
  305. }
  306. if endpoint == "" && client.EndpointType != "" && request.GetProduct() != "Sts" {
  307. if client.EndpointMap != nil && client.Network == "" || client.Network == "public" {
  308. endpoint = client.EndpointMap[regionId]
  309. }
  310. if endpoint == "" {
  311. endpoint, err = client.GetEndpointRules(regionId, request.GetProduct())
  312. if err != nil {
  313. return
  314. }
  315. }
  316. }
  317. if endpoint == "" {
  318. resolveParam := &endpoints.ResolveParam{
  319. Domain: request.GetDomain(),
  320. Product: request.GetProduct(),
  321. RegionId: regionId,
  322. LocationProduct: request.GetLocationServiceCode(),
  323. LocationEndpointType: request.GetLocationEndpointType(),
  324. CommonApi: client.ProcessCommonRequest,
  325. }
  326. endpoint, err = endpoints.Resolve(resolveParam)
  327. if err != nil {
  328. return
  329. }
  330. }
  331. request.SetDomain(endpoint)
  332. if request.GetScheme() == "" {
  333. request.SetScheme(client.config.Scheme)
  334. }
  335. // init request params
  336. err = requests.InitParams(request)
  337. if err != nil {
  338. return
  339. }
  340. // signature
  341. var finalSigner auth.Signer
  342. if signer != nil {
  343. finalSigner = signer
  344. } else {
  345. finalSigner = client.signer
  346. }
  347. httpRequest, err = buildHttpRequest(request, finalSigner, regionId)
  348. if err == nil {
  349. userAgent := DefaultUserAgent + getSendUserAgent(client.config.UserAgent, client.userAgent, request.GetUserAgent())
  350. httpRequest.Header.Set("User-Agent", userAgent)
  351. }
  352. return
  353. }
  354. func getSendUserAgent(configUserAgent string, clientUserAgent, requestUserAgent map[string]string) string {
  355. realUserAgent := ""
  356. for key1, value1 := range clientUserAgent {
  357. for key2, _ := range requestUserAgent {
  358. if key1 == key2 {
  359. key1 = ""
  360. }
  361. }
  362. if key1 != "" {
  363. realUserAgent += fmt.Sprintf(" %s/%s", key1, value1)
  364. }
  365. }
  366. for key, value := range requestUserAgent {
  367. realUserAgent += fmt.Sprintf(" %s/%s", key, value)
  368. }
  369. if configUserAgent != "" {
  370. return realUserAgent + fmt.Sprintf(" Extra/%s", configUserAgent)
  371. }
  372. return realUserAgent
  373. }
  374. func (client *Client) AppendUserAgent(key, value string) {
  375. newkey := true
  376. if client.userAgent == nil {
  377. client.userAgent = make(map[string]string)
  378. }
  379. if strings.ToLower(key) != "core" && strings.ToLower(key) != "go" {
  380. for tag, _ := range client.userAgent {
  381. if tag == key {
  382. client.userAgent[tag] = value
  383. newkey = false
  384. }
  385. }
  386. if newkey {
  387. client.userAgent[key] = value
  388. }
  389. }
  390. }
  391. func (client *Client) BuildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (err error) {
  392. _, err = client.buildRequestWithSigner(request, signer)
  393. return
  394. }
  395. func (client *Client) getTimeout(request requests.AcsRequest) (time.Duration, time.Duration) {
  396. readTimeout := defaultReadTimeout
  397. connectTimeout := defaultConnectTimeout
  398. reqReadTimeout := request.GetReadTimeout()
  399. reqConnectTimeout := request.GetConnectTimeout()
  400. if reqReadTimeout != 0*time.Millisecond {
  401. readTimeout = reqReadTimeout
  402. } else if client.readTimeout != 0*time.Millisecond {
  403. readTimeout = client.readTimeout
  404. } else if client.httpClient.Timeout != 0 {
  405. readTimeout = client.httpClient.Timeout
  406. } else if timeout, ok := getAPIMaxTimeout(request.GetProduct(), request.GetActionName()); ok {
  407. readTimeout = timeout
  408. }
  409. if reqConnectTimeout != 0*time.Millisecond {
  410. connectTimeout = reqConnectTimeout
  411. } else if client.connectTimeout != 0*time.Millisecond {
  412. connectTimeout = client.connectTimeout
  413. }
  414. return readTimeout, connectTimeout
  415. }
  416. func Timeout(connectTimeout time.Duration) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
  417. return func(ctx context.Context, network, address string) (net.Conn, error) {
  418. return (&net.Dialer{
  419. Timeout: connectTimeout,
  420. DualStack: true,
  421. }).DialContext(ctx, network, address)
  422. }
  423. }
  424. func (client *Client) setTimeout(request requests.AcsRequest) {
  425. readTimeout, connectTimeout := client.getTimeout(request)
  426. client.httpClient.Timeout = readTimeout
  427. if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
  428. trans.DialContext = Timeout(connectTimeout)
  429. client.httpClient.Transport = trans
  430. } else if client.httpClient.Transport == nil {
  431. client.httpClient.Transport = &http.Transport{
  432. DialContext: Timeout(connectTimeout),
  433. }
  434. }
  435. }
  436. func (client *Client) getHTTPSInsecure(request requests.AcsRequest) (insecure bool) {
  437. if request.GetHTTPSInsecure() != nil {
  438. insecure = *request.GetHTTPSInsecure()
  439. } else {
  440. insecure = client.GetHTTPSInsecure()
  441. }
  442. return insecure
  443. }
  444. func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
  445. fieldMap := make(map[string]string)
  446. initLogMsg(fieldMap)
  447. defer func() {
  448. client.printLog(fieldMap, err)
  449. }()
  450. httpRequest, err := client.buildRequestWithSigner(request, signer)
  451. if err != nil {
  452. return
  453. }
  454. client.setTimeout(request)
  455. proxy, err := client.getHttpProxy(httpRequest.URL.Scheme)
  456. if err != nil {
  457. return err
  458. }
  459. noProxy := client.getNoProxy(httpRequest.URL.Scheme)
  460. var flag bool
  461. for _, value := range noProxy {
  462. if value == httpRequest.Host {
  463. flag = true
  464. break
  465. }
  466. }
  467. // Set whether to ignore certificate validation.
  468. // Default InsecureSkipVerify is false.
  469. if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
  470. if trans.TLSClientConfig != nil {
  471. trans.TLSClientConfig.InsecureSkipVerify = client.getHTTPSInsecure(request)
  472. } else {
  473. trans.TLSClientConfig = &tls.Config{
  474. InsecureSkipVerify: client.getHTTPSInsecure(request),
  475. }
  476. }
  477. if proxy != nil && !flag {
  478. trans.Proxy = http.ProxyURL(proxy)
  479. }
  480. client.httpClient.Transport = trans
  481. }
  482. var httpResponse *http.Response
  483. for retryTimes := 0; retryTimes <= client.config.MaxRetryTime; retryTimes++ {
  484. if proxy != nil && proxy.User != nil {
  485. if password, passwordSet := proxy.User.Password(); passwordSet {
  486. httpRequest.SetBasicAuth(proxy.User.Username(), password)
  487. }
  488. }
  489. if retryTimes > 0 {
  490. client.printLog(fieldMap, err)
  491. initLogMsg(fieldMap)
  492. }
  493. putMsgToMap(fieldMap, httpRequest)
  494. debug("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto)
  495. debug("> Host: %s", httpRequest.Host)
  496. for key, value := range httpRequest.Header {
  497. debug("> %s: %v", key, strings.Join(value, ""))
  498. }
  499. debug(">")
  500. debug(" Retry Times: %d.", retryTimes)
  501. startTime := time.Now()
  502. fieldMap["{start_time}"] = startTime.Format("2006-01-02 15:04:05")
  503. httpResponse, err = hookDo(client.httpClient.Do)(httpRequest)
  504. fieldMap["{cost}"] = time.Now().Sub(startTime).String()
  505. if err == nil {
  506. fieldMap["{code}"] = strconv.Itoa(httpResponse.StatusCode)
  507. fieldMap["{res_headers}"] = TransToString(httpResponse.Header)
  508. debug("< %s %s", httpResponse.Proto, httpResponse.Status)
  509. for key, value := range httpResponse.Header {
  510. debug("< %s: %v", key, strings.Join(value, ""))
  511. }
  512. }
  513. debug("<")
  514. // receive error
  515. if err != nil {
  516. debug(" Error: %s.", err.Error())
  517. if !client.config.AutoRetry {
  518. return
  519. } else if retryTimes >= client.config.MaxRetryTime {
  520. // timeout but reached the max retry times, return
  521. times := strconv.Itoa(retryTimes + 1)
  522. timeoutErrorMsg := fmt.Sprintf(errors.TimeoutErrorMessage, times, times)
  523. if strings.Contains(err.Error(), "Client.Timeout") {
  524. timeoutErrorMsg += " Read timeout. Please set a valid ReadTimeout."
  525. } else {
  526. timeoutErrorMsg += " Connect timeout. Please set a valid ConnectTimeout."
  527. }
  528. err = errors.NewClientError(errors.TimeoutErrorCode, timeoutErrorMsg, err)
  529. return
  530. }
  531. }
  532. if isCertificateError(err) {
  533. return
  534. }
  535. // if status code >= 500 or timeout, will trigger retry
  536. if client.config.AutoRetry && (err != nil || isServerError(httpResponse)) {
  537. client.setTimeout(request)
  538. // rewrite signatureNonce and signature
  539. httpRequest, err = client.buildRequestWithSigner(request, signer)
  540. // buildHttpRequest(request, finalSigner, regionId)
  541. if err != nil {
  542. return
  543. }
  544. continue
  545. }
  546. break
  547. }
  548. err = responses.Unmarshal(response, httpResponse, request.GetAcceptFormat())
  549. fieldMap["{res_body}"] = response.GetHttpContentString()
  550. debug("%s", response.GetHttpContentString())
  551. // wrap server errors
  552. if serverErr, ok := err.(*errors.ServerError); ok {
  553. var wrapInfo = map[string]string{}
  554. wrapInfo["StringToSign"] = request.GetStringToSign()
  555. err = errors.WrapServerError(serverErr, wrapInfo)
  556. }
  557. return
  558. }
  559. func isCertificateError(err error) bool {
  560. if err != nil && strings.Contains(err.Error(), "x509: certificate signed by unknown authority") {
  561. return true
  562. }
  563. return false
  564. }
  565. func putMsgToMap(fieldMap map[string]string, request *http.Request) {
  566. fieldMap["{host}"] = request.Host
  567. fieldMap["{method}"] = request.Method
  568. fieldMap["{uri}"] = request.URL.RequestURI()
  569. fieldMap["{pid}"] = strconv.Itoa(os.Getpid())
  570. fieldMap["{version}"] = strings.Split(request.Proto, "/")[1]
  571. hostname, _ := os.Hostname()
  572. fieldMap["{hostname}"] = hostname
  573. fieldMap["{req_headers}"] = TransToString(request.Header)
  574. fieldMap["{target}"] = request.URL.Path + request.URL.RawQuery
  575. }
  576. func buildHttpRequest(request requests.AcsRequest, singer auth.Signer, regionId string) (httpRequest *http.Request, err error) {
  577. err = auth.Sign(request, singer, regionId)
  578. if err != nil {
  579. return
  580. }
  581. requestMethod := request.GetMethod()
  582. requestUrl := request.BuildUrl()
  583. body := request.GetBodyReader()
  584. httpRequest, err = http.NewRequest(requestMethod, requestUrl, body)
  585. if err != nil {
  586. return
  587. }
  588. for key, value := range request.GetHeaders() {
  589. httpRequest.Header[key] = []string{value}
  590. }
  591. // host is a special case
  592. if host, containsHost := request.GetHeaders()["Host"]; containsHost {
  593. httpRequest.Host = host
  594. }
  595. return
  596. }
  597. func isServerError(httpResponse *http.Response) bool {
  598. return httpResponse.StatusCode >= http.StatusInternalServerError
  599. }
  600. /**
  601. only block when any one of the following occurs:
  602. 1. the asyncTaskQueue is full, increase the queue size to avoid this
  603. 2. Shutdown() in progressing, the client is being closed
  604. **/
  605. func (client *Client) AddAsyncTask(task func()) (err error) {
  606. if client.asyncTaskQueue != nil {
  607. client.asyncChanLock.RLock()
  608. defer client.asyncChanLock.RUnlock()
  609. if client.isRunning {
  610. client.asyncTaskQueue <- task
  611. }
  612. } else {
  613. err = errors.NewClientError(errors.AsyncFunctionNotEnabledCode, errors.AsyncFunctionNotEnabledMessage, nil)
  614. }
  615. return
  616. }
  617. func (client *Client) GetConfig() *Config {
  618. return client.config
  619. }
  620. func (client *Client) GetSigner() auth.Signer {
  621. return client.signer
  622. }
  623. func (client *Client) SetSigner(signer auth.Signer) {
  624. client.signer = signer
  625. }
  626. func NewClient() (client *Client, err error) {
  627. client = &Client{}
  628. err = client.Init()
  629. return
  630. }
  631. func NewClientWithProvider(regionId string, providers ...provider.Provider) (client *Client, err error) {
  632. client = &Client{}
  633. var pc provider.Provider
  634. if len(providers) == 0 {
  635. pc = provider.DefaultChain
  636. } else {
  637. pc = provider.NewProviderChain(providers)
  638. }
  639. err = client.InitWithProviderChain(regionId, pc)
  640. return
  641. }
  642. func NewClientWithOptions(regionId string, config *Config, credential auth.Credential) (client *Client, err error) {
  643. client = &Client{}
  644. err = client.InitWithOptions(regionId, config, credential)
  645. return
  646. }
  647. func NewClientWithAccessKey(regionId, accessKeyId, accessKeySecret string) (client *Client, err error) {
  648. client = &Client{}
  649. err = client.InitWithAccessKey(regionId, accessKeyId, accessKeySecret)
  650. return
  651. }
  652. func NewClientWithStsToken(regionId, stsAccessKeyId, stsAccessKeySecret, stsToken string) (client *Client, err error) {
  653. client = &Client{}
  654. err = client.InitWithStsToken(regionId, stsAccessKeyId, stsAccessKeySecret, stsToken)
  655. return
  656. }
  657. func NewClientWithRamRoleArn(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
  658. client = &Client{}
  659. err = client.InitWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName)
  660. return
  661. }
  662. func NewClientWithRamRoleArnAndPolicy(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName, policy string) (client *Client, err error) {
  663. client = &Client{}
  664. err = client.InitWithRamRoleArnAndPolicy(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName, policy)
  665. return
  666. }
  667. func NewClientWithEcsRamRole(regionId string, roleName string) (client *Client, err error) {
  668. client = &Client{}
  669. err = client.InitWithEcsRamRole(regionId, roleName)
  670. return
  671. }
  672. func NewClientWithRsaKeyPair(regionId string, publicKeyId, privateKey string, sessionExpiration int) (client *Client, err error) {
  673. client = &Client{}
  674. err = client.InitWithRsaKeyPair(regionId, publicKeyId, privateKey, sessionExpiration)
  675. return
  676. }
  677. func NewClientWithBearerToken(regionId, bearerToken string) (client *Client, err error) {
  678. client = &Client{}
  679. err = client.InitWithBearerToken(regionId, bearerToken)
  680. return
  681. }
  682. func (client *Client) ProcessCommonRequest(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
  683. request.TransToAcsRequest()
  684. response = responses.NewCommonResponse()
  685. err = client.DoAction(request, response)
  686. return
  687. }
  688. func (client *Client) ProcessCommonRequestWithSigner(request *requests.CommonRequest, signerInterface interface{}) (response *responses.CommonResponse, err error) {
  689. if signer, isSigner := signerInterface.(auth.Signer); isSigner {
  690. request.TransToAcsRequest()
  691. response = responses.NewCommonResponse()
  692. err = client.DoActionWithSigner(request, response, signer)
  693. return
  694. }
  695. panic("should not be here")
  696. }
  697. func (client *Client) Shutdown() {
  698. // lock the addAsync()
  699. client.asyncChanLock.Lock()
  700. defer client.asyncChanLock.Unlock()
  701. if client.asyncTaskQueue != nil {
  702. close(client.asyncTaskQueue)
  703. }
  704. client.isRunning = false
  705. }
  706. // Deprecated: Use NewClientWithRamRoleArn in this package instead.
  707. func NewClientWithStsRoleArn(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
  708. return NewClientWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName)
  709. }
  710. // Deprecated: Use NewClientWithEcsRamRole in this package instead.
  711. func NewClientWithStsRoleNameOnEcs(regionId string, roleName string) (client *Client, err error) {
  712. return NewClientWithEcsRamRole(regionId, roleName)
  713. }