1
|
|
|
package http |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
"io" |
7
|
|
|
"io/ioutil" |
8
|
|
|
"net" |
9
|
|
|
"net/http" |
10
|
|
|
"net/url" |
11
|
|
|
"strings" |
12
|
|
|
"time" |
13
|
|
|
|
14
|
|
|
"github.com/alibabacloud-go/debug/debug" |
15
|
|
|
"github.com/aliyun/credentials-go/credentials/internal/utils" |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
type Request struct { |
19
|
|
|
Method string // http request method |
20
|
|
|
Protocol string // http or https |
21
|
|
|
Host string // http host |
22
|
|
|
ReadTimeout time.Duration |
23
|
|
|
ConnectTimeout time.Duration |
24
|
|
|
Proxy string // http proxy |
25
|
|
|
Form map[string]string // http form |
26
|
|
|
Body []byte // request body for JSON or stream |
27
|
|
|
Path string |
28
|
|
|
Queries map[string]string |
29
|
|
|
Headers map[string]string |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
func (req *Request) BuildRequestURL() string { |
33
|
|
|
httpUrl := fmt.Sprintf("%s://%s%s", req.Protocol, req.Host, req.Path) |
34
|
|
|
|
35
|
|
|
querystring := utils.GetURLFormedMap(req.Queries) |
36
|
|
|
if querystring != "" { |
37
|
|
|
httpUrl = httpUrl + "?" + querystring |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return fmt.Sprintf("%s %s", req.Method, httpUrl) |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
type Response struct { |
44
|
|
|
StatusCode int |
45
|
|
|
Headers map[string]string |
46
|
|
|
Body []byte |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
var newRequest = http.NewRequest |
50
|
|
|
|
51
|
|
|
type do func(req *http.Request) (*http.Response, error) |
52
|
|
|
|
53
|
|
|
var hookDo = func(fn do) do { |
54
|
|
|
return fn |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
var debuglog = debug.Init("credential") |
58
|
|
|
|
59
|
|
|
func Do(req *Request) (res *Response, err error) { |
60
|
|
|
querystring := utils.GetURLFormedMap(req.Queries) |
61
|
|
|
// do request |
62
|
|
|
httpUrl := fmt.Sprintf("%s://%s%s?%s", req.Protocol, req.Host, req.Path, querystring) |
63
|
|
|
|
64
|
|
|
var body io.Reader |
65
|
|
|
if req.Method == "GET" { |
66
|
|
|
body = strings.NewReader("") |
67
|
|
|
} else { |
68
|
|
|
body = strings.NewReader(utils.GetURLFormedMap(req.Form)) |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
httpRequest, err := newRequest(req.Method, httpUrl, body) |
72
|
|
|
if err != nil { |
73
|
|
|
return |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if req.Form != nil { |
77
|
|
|
httpRequest.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
for key, value := range req.Headers { |
81
|
|
|
if value != "" { |
82
|
|
|
debuglog("> %s: %s", key, value) |
83
|
|
|
httpRequest.Header.Set(key, value) |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
httpClient := &http.Client{} |
88
|
|
|
|
89
|
|
|
if req.ReadTimeout != 0 { |
90
|
|
|
httpClient.Timeout = req.ReadTimeout |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
transport := &http.Transport{} |
94
|
|
|
if req.Proxy != "" { |
95
|
|
|
var proxy *url.URL |
96
|
|
|
proxy, err = url.Parse(req.Proxy) |
97
|
|
|
if err != nil { |
98
|
|
|
return |
99
|
|
|
} |
100
|
|
|
transport.Proxy = http.ProxyURL(proxy) |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if req.ConnectTimeout != 0 { |
104
|
|
|
transport.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) { |
105
|
|
|
return (&net.Dialer{ |
106
|
|
|
Timeout: req.ConnectTimeout, |
107
|
|
|
DualStack: true, |
108
|
|
|
}).DialContext(ctx, network, address) |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
httpClient.Transport = transport |
113
|
|
|
|
114
|
|
|
httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
115
|
|
|
if err != nil { |
116
|
|
|
return |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
defer httpResponse.Body.Close() |
120
|
|
|
|
121
|
|
|
responseBody, err := ioutil.ReadAll(httpResponse.Body) |
122
|
|
|
if err != nil { |
123
|
|
|
return |
124
|
|
|
} |
125
|
|
|
res = &Response{ |
126
|
|
|
StatusCode: httpResponse.StatusCode, |
127
|
|
|
Headers: make(map[string]string), |
128
|
|
|
Body: responseBody, |
129
|
|
|
} |
130
|
|
|
for key, v := range httpResponse.Header { |
131
|
|
|
res.Headers[key] = v[0] |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return |
135
|
|
|
} |
136
|
|
|
|