-
Notifications
You must be signed in to change notification settings - Fork 80
/
upgrader.go
46 lines (36 loc) · 1.17 KB
/
upgrader.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
package webtransport
import (
"errors"
"fmt"
"net/http"
"github.com/quic-go/quic-go/http3"
)
type Upgrader struct {
CheckOrigin func(r *http.Request) bool
}
func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (http3.HTTPStreamer, error) {
if r.Method != http.MethodConnect {
return nil, fmt.Errorf("expected CONNECT request, got %s", r.Method)
}
if r.Proto != protocolHeader {
return nil, fmt.Errorf("unexpected protocol: %s", r.Proto)
}
if v, ok := r.Header[webTransportDraftOfferHeaderKey]; !ok || len(v) != 1 || v[0] != "1" {
return nil, fmt.Errorf("missing or invalid %s header", webTransportDraftOfferHeaderKey)
}
checkOrigin := u.CheckOrigin
if checkOrigin == nil {
checkOrigin = checkSameOrigin
}
if !u.CheckOrigin(r) {
return nil, errors.New("request origin not allowed")
}
w.Header().Add(webTransportDraftHeaderKey, webTransportDraftHeaderValue)
w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush()
httpStreamer, ok := r.Body.(http3.HTTPStreamer)
if !ok { // should never happen, unless quic-go changed the API
return nil, errors.New("failed to take over HTTP qStream")
}
return httpStreamer, nil
}