-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_windows.go
172 lines (138 loc) · 3.46 KB
/
client_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//go:build windows
package wininet
import (
"bytes"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/mjwhitta/errors"
w32 "github.com/mjwhitta/win/api"
)
// Client is a struct containing relevant metadata to make HTTP
// requests.
type Client struct {
Debug bool
Jar http.CookieJar
Timeout time.Duration
Transport *http.Transport
hndl uintptr
ua string
}
// NewClient will return a pointer to a new Client instance that
// simply wraps the net/http.Client type.
func NewClient(ua ...string) (*Client, error) {
var c *Client = &Client{}
var e error
if len(ua) == 0 {
ua = []string{"Go-http-client/1.1"}
}
// Store User-Agent
c.ua = ua[0]
// Create session
c.hndl, e = w32.InternetOpenW(
strings.Join(ua, " "),
w32.Wininet.InternetOpenTypePreconfig,
"",
"",
0,
)
if e != nil {
return nil, errors.Newf("failed to create session: %w", e)
}
return c, nil
}
// Do will send the HTTP request and return an HTTP response.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
var b []byte
var e error
var reqHndl uintptr
var res *http.Response
var trans *http.Transport = c.Transport
if trans == nil {
if t, ok := http.DefaultTransport.(*http.Transport); ok {
trans = t
}
}
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", c.ua)
}
if reqHndl, e = buildRequest(c.hndl, req, c.Timeout); e != nil {
return nil, e
}
if (trans != nil) && (trans.TLSClientConfig != nil) {
if trans.TLSClientConfig.InsecureSkipVerify {
if e = disableTLS(reqHndl); e != nil {
return nil, e
}
}
}
if c.Debug {
// Only needed for debugging b/c WinINet will do this for you
loadCookies(c.Jar, req)
if b, e = httputil.DumpRequestOut(req, true); e == nil {
println(string(b))
}
}
if res, e = sendRequest(reqHndl, req); e != nil {
return nil, e
}
if c.Debug {
if b, e = httputil.DumpResponse(res, true); e == nil {
println(string(b))
}
}
return res, storeCookies(c.Jar, req.URL, res.Cookies())
}
// Get will make a GET request using WinINet.dll.
func (c *Client) Get(url string) (*http.Response, error) {
var e error
var req *http.Request
if req, e = http.NewRequest(http.MethodGet, url, nil); e != nil {
return nil, errors.Newf("failed to create request: %w", e)
}
return c.Do(req)
}
// Head will make a HEAD request using WinINet.dll.
func (c *Client) Head(url string) (*http.Response, error) {
var e error
var req *http.Request
if req, e = http.NewRequest(http.MethodHead, url, nil); e != nil {
return nil, errors.Newf("failed to create request: %w", e)
}
return c.Do(req)
}
// Post will make a POST request using WinINet.dll.
func (c *Client) Post(
url string, contentType string, body io.Reader,
) (*http.Response, error) {
var e error
var req *http.Request
req, e = http.NewRequest(http.MethodPost, url, body)
if e != nil {
return nil, errors.Newf("failed to create request: %w", e)
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
return c.Do(req)
}
// PostForm will make a POST request using WinINet.dll.
func (c *Client) PostForm(
url string, data url.Values,
) (*http.Response, error) {
var body io.Reader = bytes.NewReader([]byte(data.Encode()))
var e error
var req *http.Request
req, e = http.NewRequest(http.MethodPost, url, body)
if e != nil {
return nil, errors.Newf("failed to create request: %w", e)
}
req.Header.Set(
"Content-Type",
"application/x-www-form-urlencoded",
)
return c.Do(req)
}