/
conn.go
150 lines (125 loc) · 3.25 KB
/
conn.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
package httpx
import (
"bufio"
"context"
"io"
"net"
"net/http"
"time"
"github.com/segmentio/netx"
)
// ConnTransport is a http.RoundTripper that works on a pre-established network
// connection.
type ConnTransport struct {
// Conn is the connection to use to send requests and receive responses.
Conn net.Conn
// Buffer may be set to a bufio.ReadWriter which will be used to buffer all
// I/O done on the connection.
Buffer *bufio.ReadWriter
// DialContext is used to open a connection when Conn is set to nil.
// If the function is nil the transport uses a default dialer.
DialContext func(context.Context, string, string) (net.Conn, error)
// ResponseHeaderTimeout, if non-zero, specifies the amount of time to wait
// for a server's response headers after fully writing the request (including
// its body, if any). This time does not include the time to read the response
// body.
ResponseHeaderTimeout time.Duration
// MaxResponseHeaderBytes specifies a limit on how many response bytes are
// allowed in the server's response header.
//
// Zero means to use a default limit.
MaxResponseHeaderBytes int
}
// the default dialer used by ConnTransport when neither Conn nor DialContext is
// set.
var dialer net.Dialer
// RoundTrip satisfies the http.RoundTripper interface.
func (t *ConnTransport) RoundTrip(req *http.Request) (res *http.Response, err error) {
var ctx = req.Context()
var conn net.Conn
var dial func(context.Context, string, string) (net.Conn, error)
if conn = t.Conn; conn == nil {
if dial = t.DialContext; dial == nil {
dial = dialer.DialContext
}
if conn, err = dial(ctx, "tcp", req.Host); err != nil {
return
}
}
var c = &connReader{Conn: conn, limit: -1}
var b = t.Buffer
var r *bufio.Reader
var w *bufio.Writer
if b != nil && b.Reader != nil {
r = b.Reader
r.Reset(c)
} else {
r = bufio.NewReader(c)
}
if b != nil && b.Writer != nil {
w = b.Writer
w.Reset(c)
} else {
w = bufio.NewWriter(c)
}
if err = req.Write(w); err != nil {
return
}
if err = w.Flush(); err != nil {
return
}
switch limit := t.MaxResponseHeaderBytes; {
case limit == 0:
c.limit = http.DefaultMaxHeaderBytes
case limit > 0:
c.limit = limit
}
if timeout := t.ResponseHeaderTimeout; timeout != 0 {
c.SetReadDeadline(time.Now().Add(timeout))
}
if res, err = http.ReadResponse(r, req); err != nil {
return
}
if dial != nil {
res.Body = struct {
io.Reader
io.Closer
}{
Reader: res.Body,
Closer: conn,
}
}
c.limit = -1
c.SetReadDeadline(time.Time{})
return
}
// connReader is a net.Conn wrappers used by the HTTP server to limit the size
// of the request header.
//
// A cancel function can also be set on the reader, it is expected to be used to
// cancel the associated request context to notify the handlers that a client is
// gone and the request can be aborted.
type connReader struct {
net.Conn
limit int
cancel context.CancelFunc
}
// Read satsifies the io.Reader interface.
func (c *connReader) Read(b []byte) (n int, err error) {
if c.limit == 0 {
err = io.EOF
return
}
n1 := len(b)
n2 := c.limit
if n2 > 0 && n1 > n2 {
n1 = n2
}
if n, err = c.Conn.Read(b[:n1]); n > 0 && n2 > 0 {
c.limit -= n
}
if err != nil && !netx.IsTemporary(err) {
c.cancel()
}
return
}