client.go 25 KB

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