-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_windows.go
More file actions
74 lines (59 loc) · 1.66 KB
/
Copy pathclient_windows.go
File metadata and controls
74 lines (59 loc) · 1.66 KB
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
//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 _, ok := defaultClients[backend]; !ok {
return errors.Newf("unsupported backend %d", backend)
}
useBackend = backend
DefaultClient = defaultClients[useBackend]
return nil
}
func init() {
var c1 *winhttp.Client
var c2 *wininet.Client
var e error
if c1, e = winhttp.NewClient(); e != nil {
panic(errors.Newf("failed to create client: %w", e))
}
if c2, e = wininet.NewClient(); e != nil {
panic(errors.Newf("failed to create client: %w", e))
}
defaultClients = map[int]Client{
HTTPBackend: &HTTPClient{},
WinHTTPBackend: &WinHTTPClient{*c1},
WinINetBackend: &WinINetClient{*c2},
}
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:
if len(ua) > 0 {
return &HTTPClient{ua: ua[0]}, nil
}
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
}
}