-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
59 lines (50 loc) · 1.05 KB
/
client.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
package sendremotefile
import (
"context"
"net"
"net/http"
"time"
)
type client struct {
inner *http.Client
url string
}
type connection struct {
net.Conn
timeout time.Duration
}
func NewClient(url string, timeout time.Duration) *client {
return &client{
inner: &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := (&net.Dialer{Timeout: timeout}).DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
return &connection{
Conn: conn,
timeout: timeout,
}, nil
},
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
},
},
url: url,
}
}
func (c *client) Request() (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, c.url, nil)
if err != nil {
return nil, err
}
return c.inner.Do(req)
}
func (c *connection) Read(b []byte) (int, error) {
err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout))
if err != nil {
return 0, err
}
return c.Conn.Read(b)
}