forked from chinawebeye/http2
/
upgrade.go
283 lines (254 loc) · 7.05 KB
/
upgrade.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Adds HTTP/1.1 Upgrade mode to github.com/bradfitz/http2
//
// Upgrade mode works similarly to http2 in tls npn or alpn modes.
// Use the UpgradeServer() function instead of ConfigureServer to
// make your standard net/http.Server's http/2 compatible (via
// the "upgrade" mechanism).
package http2
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
)
var (
http2SettingsHeader = http.CanonicalHeaderKey("HTTP2-Settings")
ignoreUpgradeHeaders = map[string]struct{}{
"upgrade": struct{}{},
"connection": struct{}{},
"http2-settings": struct{}{},
}
)
type upgradeMux struct {
handler http.Handler
srv *http.Server
conf *Server
}
type mrcloser struct {
io.Reader
closers []io.Closer
}
type hdr struct {
sc *serverConn
key, val string
}
func (h hdr) String() string {
return fmt.Sprintf("{Name:%v, Value:%v}", h.sc.canonicalHeader(h.key), h.val)
}
func decodeSettings(b []byte) ([]Setting, error) {
dst := make([]byte, base64.URLEncoding.DecodedLen(len(b)))
n, err := base64.URLEncoding.Decode(dst, b)
if err != nil {
return nil, err
}
fh := FrameHeader{
valid: true,
Type: FrameSettings,
Length: uint32(n),
}
f, err := parseSettingsFrame(fh, dst[:n])
if err == nil {
settings := make([]Setting, 0, 4)
f.(*SettingsFrame).ForeachSetting(func(s Setting) error {
settings = append(settings, s)
return nil
})
return settings, nil
}
return nil, err
}
func (mrc *mrcloser) Close() (err error) {
for _, c := range mrc.closers {
defer func(c io.Closer) {
if e := c.Close(); e != nil && err == nil {
err = e
}
}(c)
}
return
}
func multiReadCloser(readers ...io.Reader) io.ReadCloser {
mrc := &mrcloser{
Reader: io.MultiReader(readers...),
closers: make([]io.Closer, 0, len(readers)),
}
for _, r := range readers {
if c, ok := r.(io.Closer); ok {
mrc.closers = append(mrc.closers, c)
}
}
return mrc
}
func (m *upgradeMux) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if upg := r.Header.Get("Upgrade"); upg == "h2c-14" || upg == "h2c" {
hj, ok := rw.(http.Hijacker)
if !ok {
panic("cannot hijack this server -- upgrade impossible")
}
h2sh := r.Header.Get(http2SettingsHeader)
if h2sh == "" {
http.Error(rw, "missing HTTP2-Settings header", 501)
return
}
settings, err := decodeSettings(bytes.TrimSpace([]byte(h2sh)))
if err != nil {
http.Error(rw, "invalid HTTP2-Settings header", 501)
return
}
for _, connH := range strings.Split(r.Header.Get("Connection"), ",") {
connH = strings.TrimSpace(connH)
if _, ok := r.Header[http.CanonicalHeaderKey(connH)]; !ok {
http.Error(rw, "invalid connection header", 501)
return
}
}
data := []byte{0}
n, err := r.Body.Read(data)
if err != nil && err != io.EOF {
http.Error(rw, err.Error(), 501)
return
}
// Currently, request bodies in the upgrade request are not supported,
// so hand this off to http/1.1 if there's one or more bytes pending.
if n > 0 {
r.Body = multiReadCloser(bytes.NewBuffer(data), r.Body)
m.handler.ServeHTTP(rw, r)
return
}
r.Body.Close()
h := rw.Header()
for k, _ := range h {
h.Del(k)
}
h.Set("Connection", "Upgrade")
h.Set("Upgrade", upg)
rw.WriteHeader(101)
if flusher, ok := rw.(http.Flusher); ok {
flusher.Flush()
}
c, _, err := hj.Hijack()
if err != nil {
panic(err.Error())
}
defer c.Close()
var sc *serverConn
sc, err = m.conf.newConn(m.srv, c, m.handler, settings, func() {
defer close(sc.upgradeCh)
if testHookOnConn != nil {
testHookOnConn()
}
if sc.maxStreamID < 1 {
sc.maxStreamID = 1
}
st := &stream{
id: 1,
state: stateHalfClosedRemote,
}
st.cw.Init()
st.flow.conn = &sc.flow
st.flow.add(sc.initialWindowSize)
st.inflow.conn = &sc.inflow
st.inflow.add(initialWindowSize)
sc.streams[1] = st
sc.curOpenStreams++
sc.req = requestParam{
stream: st,
method: r.Method,
path: r.URL.RequestURI(),
scheme: r.URL.Scheme,
authority: r.Host,
sawRegularHeader: true,
header: make(http.Header),
}
if sc.req.scheme == "" {
sc.req.scheme = "http"
}
if VerboseLogs {
sc.vlogf("got header field %+v", &hdr{sc, ":scheme", sc.req.scheme})
if sc.req.method != "" {
sc.vlogf("got header field %+v", &hdr{sc, ":method", sc.req.method})
}
if sc.req.path != "" {
sc.vlogf("got header field %+v", &hdr{sc, ":path", sc.req.path})
}
if sc.req.authority != "" {
sc.vlogf("got header field %+v", &hdr{sc, ":authority", sc.req.authority})
}
}
for k, vals := range r.Header {
if k == "Cookie" {
sc.req.header.Set(sc.canonicalHeader(k), strings.Join(vals, "; "))
continue
}
if _, ok := ignoreUpgradeHeaders[lowerHeader(k)]; !ok {
for _, v := range vals {
if VerboseLogs {
sc.vlogf("got header field %+v", &hdr{sc, k, v})
}
sc.req.header.Add(sc.canonicalHeader(k), v)
}
}
}
if err := sc.processHeaderBlockFragment(st, nil, true); err != nil {
sc.vlogf("error processing header %v", err)
}
})
if err == nil {
m.conf.handleConn(m.srv, c, m.handler, sc)
}
} else {
m.handler.ServeHTTP(rw, r)
}
}
// UpgradeServer operates similarly to ConfigureServer() with the exception that
// it returns a clone copy of the original server that will be used to
// exclusively serve HTTP/1.1 and HTTP/1.0 requests. The "HTTP/2 Configured"
// server passed to UpgradeServer() should not be modified once this call has
// been made, nor should it actively be listening for new connections.
//
// When an existing HTTP/1.1 connection requests an upgrade to h2c or h2c-14 mode,
// the connection will be hijacked from the "clone server" (as returned by
// UpgradeServer() and handed off to the internal http2 configured server.
//
// Ex usage:
//
// http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
// rw.Header().Set("Content-Type", "text/plain")
// fmt.Fprintf(rw, "response to %v\n", r)
// })
//
// s := http2.UpgradeServer(&http.Server{Addr:":8080"},nil)
// s.ListenAndServe()
//
// NB: Do *not* alter the returned server's Handler, as it is hooked in order to
// perform the protocol switch. Any changes to uri handling must be made in the
// "inner" server *before* it is passed to UpgradeServer(). The standard
// http.DefaultServeMux will always be used as a last resort.
//
// NB: Note that HTTP/1.1 requests with bodies (i.e. HTTP POST) are not
// supported in the initial Upgrade request but will work once the connection
// is fully HTTP/2. Requests that have content entities will automatically
// fallback to HTTP/1.1 (the upgrade silently fails but the normal handler
// is invoked).
func UpgradeServer(s *http.Server, conf *Server) *http.Server {
if conf == nil {
conf = new(Server)
}
handler := s.Handler
if handler == nil {
handler = http.DefaultServeMux
}
h1s := new(http.Server)
*h1s = *s
h1s.TLSConfig = nil
h1s.TLSNextProto = nil
h1s.Handler = &upgradeMux{
handler: handler,
srv: s,
conf: conf,
}
ConfigureServer(s, conf)
return h1s
}