forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_transp.go
196 lines (162 loc) · 3.84 KB
/
simple_transp.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package http
import (
"bufio"
"compress/gzip"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"github.com/elastic/beats/libbeat/outputs/transport"
)
const (
gzipEncoding = "gzip"
urlSchemaHTTP = "http"
urlSchemaHTTPS = "https"
)
// SimpleTransport contains the dialer and read/write callbacks
type SimpleTransport struct {
Dialer transport.Dialer
DisableCompression bool
OnStartWrite func()
OnEndWrite func()
OnStartRead func()
}
func (t *SimpleTransport) checkRequest(req *http.Request) error {
if req.URL == nil {
return errors.New("http: missing request URL")
}
if req.Header == nil {
return errors.New("http: missing request headers")
}
scheme := req.URL.Scheme
isHTTP := scheme == urlSchemaHTTP || scheme == urlSchemaHTTPS
if !isHTTP {
return fmt.Errorf("http: unsupported scheme %v", scheme)
}
if req.URL.Host == "" {
return errors.New("http: no host in URL")
}
return nil
}
// RoundTrip sets up goroutines to write the request and read the responses
func (t *SimpleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
type readReturn struct {
resp *http.Response
err error
}
defer reqCloseBody(req)
if err := t.checkRequest(req); err != nil {
return nil, err
}
conn, err := t.Dialer.Dial("tcp", canonicalAddr(req.URL))
if err != nil {
return nil, err
}
defer conn.Close()
requestedGzip := false
if t.DisableCompression &&
req.Header.Get("Accept-Encoding") == "" &&
req.Header.Get("Range") == "" &&
req.Method != "HEAD" {
requestedGzip = true
req.Header.Add("Accept-Encoding", gzipEncoding)
defer req.Header.Del("Accept-Encoding")
}
done := req.Context().Done()
readerDone := make(chan readReturn, 1)
writerDone := make(chan error, 1)
// write request
go func() {
writerDone <- t.writeRequest(conn, req)
}()
// read response
go func() {
resp, err := t.readResponse(conn, req, requestedGzip)
readerDone <- readReturn{resp, err}
}()
select {
case err := <-writerDone:
if err != nil {
return nil, err
}
case <-done:
return nil, errors.New("http: request timed out before writing finished")
}
close(writerDone)
var ret readReturn
select {
case ret = <-readerDone:
break
case <-done:
return nil, errors.New("http: request timed out while waiting for response")
}
close(readerDone)
return ret.resp, ret.err
}
func (t *SimpleTransport) writeRequest(conn net.Conn, req *http.Request) error {
writer := bufio.NewWriter(conn)
t.sigStartWrite()
err := req.Write(writer)
if err == nil {
err = writer.Flush()
}
t.sigEndWrite()
return err
}
func (t *SimpleTransport) readResponse(
conn net.Conn,
req *http.Request,
requestedGzip bool,
) (*http.Response, error) {
reader := bufio.NewReader(conn)
resp, err := http.ReadResponse(reader, req)
if err != nil {
return nil, err
}
t.sigStartRead()
if requestedGzip && resp.Header.Get("Content-Encoding") == gzipEncoding {
resp.Header.Del("Content-Encoding")
resp.Header.Del("Content-Length")
resp.ContentLength = -1
unzipper, err := gzip.NewReader(resp.Body)
if err != nil {
resp.Body.Close()
return nil, err
}
resp.Body = struct {
io.Reader
io.Closer
}{unzipper, resp.Body}
}
return resp, nil
}
func (t *SimpleTransport) sigStartRead() { call(t.OnStartRead) }
func (t *SimpleTransport) sigStartWrite() { call(t.OnStartWrite) }
func (t *SimpleTransport) sigEndWrite() { call(t.OnEndWrite) }
func call(f func()) {
if f != nil {
f()
}
}
func reqCloseBody(req *http.Request) {
if req.Body != nil {
req.Body.Close()
}
}
func canonicalAddr(url *url.URL) string {
scheme, addr := url.Scheme, url.Host
if !hasPort(addr) {
portmap := map[string]string{
"http": "80",
"https": "443",
}
addr = net.JoinHostPort(addr, portmap[scheme])
}
return addr
}
func hasPort(s string) bool {
return strings.LastIndex(s, ":") > strings.LastIndex(s, "]")
}