Repository navigation
Expand file tree
/
Copy pathtransport.go
More file actions
123 lines (112 loc) · 3.65 KB
/
Copy pathtransport.go
File metadata and controls
123 lines (112 loc) · 3.65 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
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
package simple
import (
"context"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// HTTPResponse is the transport-neutral result of one HTTP exchange.
// Header keys are lowercase.
type HTTPResponse struct {
StatusCode int
Body string
Headers map[string]string
Err error
}
// OK reports a transport-level success with a 2xx status.
func (r HTTPResponse) OK() bool {
return r.Err == nil && r.StatusCode >= 200 && r.StatusCode < 300
}
// Header returns a header value case-insensitively, or "" when absent.
func (r HTTPResponse) Header(name string) string {
return r.Headers[strings.ToLower(name)]
}
// HTTPClient abstracts the HTTP operations the protocol uses. Inject a fake
// in tests.
type HTTPClient interface {
PostForm(ctx context.Context, rawURL string, fields url.Values) HTTPResponse
Get(ctx context.Context, rawURL string, headers map[string]string) HTTPResponse
}
// DefaultHTTPClient performs real requests with net/http.
type DefaultHTTPClient struct {
HTTP *http.Client
Agent string
}
// NewDefaultHTTPClient builds a DefaultHTTPClient with the given timeout and
// user agent.
func NewDefaultHTTPClient(timeout time.Duration, userAgent string) *DefaultHTTPClient {
return &DefaultHTTPClient{
HTTP: &http.Client{Timeout: timeout, CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
}},
Agent: userAgent,
}
}
// PostForm sends a form-urlencoded POST.
func (d *DefaultHTTPClient) PostForm(ctx context.Context, rawURL string, fields url.Values) HTTPResponse {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, rawURL, strings.NewReader(fields.Encode()))
if err != nil {
return HTTPResponse{Err: err}
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if d.Agent != "" {
req.Header.Set("User-Agent", d.Agent)
}
resp, err := d.HTTP.Do(req)
if err != nil {
return HTTPResponse{Err: err}
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 1024*1024+1))
if err != nil {
return HTTPResponse{StatusCode: resp.StatusCode, Err: err}
}
if len(body) > 1024*1024 {
return HTTPResponse{StatusCode: resp.StatusCode, Err: io.ErrUnexpectedEOF}
}
headers := make(map[string]string, len(resp.Header))
for name, values := range resp.Header {
if len(values) > 0 {
headers[strings.ToLower(name)] = values[0]
}
}
return HTTPResponse{StatusCode: resp.StatusCode, Body: string(body), Headers: headers}
}
// Get sends a GET with the supplied headers. The Simple protocol itself is
// POST-only; GET exists for the Invisible Folder module.
func (d *DefaultHTTPClient) Get(ctx context.Context, rawURL string, headers map[string]string) HTTPResponse {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return HTTPResponse{Err: err}
}
for name, value := range headers {
req.Header.Set(name, value)
}
if d.Agent != "" {
req.Header.Set("User-Agent", d.Agent)
}
resp, err := d.HTTP.Do(req)
if err != nil {
return HTTPResponse{Err: err}
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 1024*1024+1))
if err != nil {
return HTTPResponse{StatusCode: resp.StatusCode, Err: err}
}
if len(body) > 1024*1024 {
return HTTPResponse{StatusCode: resp.StatusCode, Err: io.ErrUnexpectedEOF}
}
responseHeaders := make(map[string]string, len(resp.Header))
for name, values := range resp.Header {
if len(values) > 0 {
responseHeaders[strings.ToLower(name)] = values[0]
}
}
return HTTPResponse{StatusCode: resp.StatusCode, Body: string(body), Headers: responseHeaders}
}
var _ HTTPClient = (*DefaultHTTPClient)(nil)
func itoa(value int) string { return strconv.Itoa(value) }