client.go 24 KB

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