-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_windows.go
66 lines (53 loc) · 1.45 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
//go:build windows
package inet
import (
"github.com/mjwhitta/errors"
"github.com/mjwhitta/win/winhttp"
"github.com/mjwhitta/win/wininet"
)
// Backend is used to track the preferred backend HTTP client. Windows
// allows for net/http, WinHTTP, and WinINet. The default is WinINet.
func Backend(backend int) error {
if e := isValidBackend(backend); e != nil {
return e
}
useBackend = backend
DefaultClient = defaultClients[useBackend]
return nil
}
func init() {
defaultClients = map[int]Client{HTTPBackend: &HTTPClient{}}
if tmp, e := winhttp.NewClient(); e != nil {
panic(e)
} else {
defaultClients[WinHTTPBackend] = &WinHTTPClient{*tmp}
}
if tmp, e := wininet.NewClient(); e != nil {
panic(e)
} else {
defaultClients[WinINetBackend] = &WinINetClient{*tmp}
}
useBackend = WinINetBackend
DefaultClient = defaultClients[useBackend]
}
// NewClient will return a new Client for the current Backend. An
// optional User-Agent can be provided.
func NewClient(ua ...string) (Client, error) {
var c1 *winhttp.Client
var c2 *wininet.Client
var e error
switch useBackend {
case HTTPBackend:
return &HTTPClient{}, nil
case WinHTTPBackend:
if c1, e = winhttp.NewClient(ua...); e != nil {
return nil, errors.Newf("failed to create client: %w", e)
}
return &WinHTTPClient{*c1}, nil
default:
if c2, e = wininet.NewClient(ua...); e != nil {
return nil, errors.Newf("failed to create client: %w", e)
}
return &WinINetClient{*c2}, nil
}
}