1
|
|
|
package providers |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"fmt" |
6
|
|
|
"io/ioutil" |
7
|
|
|
"net/http" |
8
|
|
|
"net/url" |
9
|
|
|
"strconv" |
10
|
|
|
"strings" |
11
|
|
|
"time" |
12
|
|
|
|
13
|
|
|
"github.com/alibabacloud-go/debug/debug" |
14
|
|
|
"github.com/aliyun/credentials-go/credentials/internal/utils" |
15
|
|
|
"github.com/aliyun/credentials-go/credentials/request" |
16
|
|
|
"github.com/aliyun/credentials-go/credentials/response" |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
var debuglog = debug.Init("credential") |
20
|
|
|
|
21
|
|
|
func mockResponse(statusCode int, content string) (res *http.Response) { |
22
|
|
|
status := strconv.Itoa(statusCode) |
23
|
|
|
res = &http.Response{ |
24
|
|
|
Proto: "HTTP/1.1", |
25
|
|
|
ProtoMajor: 1, |
26
|
|
|
Header: map[string][]string{"sdk": {"test"}}, |
27
|
|
|
StatusCode: statusCode, |
28
|
|
|
Status: status + " " + http.StatusText(statusCode), |
29
|
|
|
} |
30
|
|
|
res.Body = ioutil.NopCloser(bytes.NewReader([]byte(content))) |
31
|
|
|
return |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content []byte, err error) { |
35
|
|
|
var urlEncoded string |
36
|
|
|
if request.BodyParams != nil { |
37
|
|
|
urlEncoded = utils.GetURLFormedMap(request.BodyParams) |
38
|
|
|
} |
39
|
|
|
httpRequest, err := http.NewRequest(request.Method, request.URL, strings.NewReader(urlEncoded)) |
40
|
|
|
if err != nil { |
41
|
|
|
return |
42
|
|
|
} |
43
|
|
|
httpRequest.Proto = "HTTP/1.1" |
44
|
|
|
httpRequest.Host = request.Domain |
45
|
|
|
debuglog("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto) |
46
|
|
|
debuglog("> Host: %s", httpRequest.Host) |
47
|
|
|
for key, value := range request.Headers { |
48
|
|
|
if value != "" { |
49
|
|
|
debuglog("> %s: %s", key, value) |
50
|
|
|
httpRequest.Header[key] = []string{value} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
debuglog(">") |
54
|
|
|
httpClient := &http.Client{} |
55
|
|
|
httpClient.Timeout = time.Duration(runtime.ReadTimeout) * time.Second |
56
|
|
|
proxy := &url.URL{} |
57
|
|
|
if runtime.Proxy != "" { |
58
|
|
|
proxy, err = url.Parse(runtime.Proxy) |
59
|
|
|
if err != nil { |
60
|
|
|
return |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
trans := &http.Transport{} |
64
|
|
|
if proxy != nil && runtime.Proxy != "" { |
65
|
|
|
trans.Proxy = http.ProxyURL(proxy) |
66
|
|
|
} |
67
|
|
|
trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second) |
68
|
|
|
httpClient.Transport = trans |
69
|
|
|
httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
70
|
|
|
if err != nil { |
71
|
|
|
return |
72
|
|
|
} |
73
|
|
|
debuglog("< %s %s", httpResponse.Proto, httpResponse.Status) |
74
|
|
|
for key, value := range httpResponse.Header { |
75
|
|
|
debuglog("< %s: %v", key, strings.Join(value, "")) |
76
|
|
|
} |
77
|
|
|
debuglog("<") |
78
|
|
|
|
79
|
|
|
resp := &response.CommonResponse{} |
80
|
|
|
err = hookParse(resp.ParseFromHTTPResponse(httpResponse)) |
81
|
|
|
if err != nil { |
82
|
|
|
return |
83
|
|
|
} |
84
|
|
|
debuglog("%s", resp.GetHTTPContentString()) |
85
|
|
|
if resp.GetHTTPStatus() != http.StatusOK { |
86
|
|
|
err = fmt.Errorf("httpStatus: %d, message = %s", resp.GetHTTPStatus(), resp.GetHTTPContentString()) |
87
|
|
|
return |
88
|
|
|
} |
89
|
|
|
return resp.GetHTTPContentBytes(), nil |
90
|
|
|
} |
91
|
|
|
|