route/vendor/github.com/lucas-clemente/quic-go/h2quic/roundtrip.go

169 lines
4.7 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package h2quic
import (
"crypto/tls"
"errors"
"fmt"
2018-01-03 19:19:49 +00:00
"io"
2017-12-12 02:51:45 +00:00
"net/http"
"strings"
"sync"
2018-01-03 19:19:49 +00:00
quic "github.com/lucas-clemente/quic-go"
2017-12-12 02:51:45 +00:00
"golang.org/x/net/lex/httplex"
)
2018-01-03 19:19:49 +00:00
type roundTripCloser interface {
http.RoundTripper
io.Closer
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
// RoundTripper implements the http.RoundTripper interface
type RoundTripper struct {
2017-12-12 02:51:45 +00:00
mutex sync.Mutex
// DisableCompression, if true, prevents the Transport from
// requesting compression with an "Accept-Encoding: gzip"
// request header when the Request contains no existing
// Accept-Encoding value. If the Transport requests gzip on
// its own and gets a gzipped response, it's transparently
// decoded in the Response.Body. However, if the user
// explicitly requested gzip it is not automatically
// uncompressed.
DisableCompression bool
// TLSClientConfig specifies the TLS configuration to use with
// tls.Client. If nil, the default configuration is used.
TLSClientConfig *tls.Config
2018-01-03 19:19:49 +00:00
// QuicConfig is the quic.Config used for dialing new connections.
// If nil, reasonable default values will be used.
QuicConfig *quic.Config
clients map[string]roundTripCloser
}
// RoundTripOpt are options for the Transport.RoundTripOpt method.
type RoundTripOpt struct {
// OnlyCachedConn controls whether the RoundTripper may
// create a new QUIC connection. If set true and
// no cached connection is available, RoundTrip
// will return ErrNoCachedConn.
OnlyCachedConn bool
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
var _ roundTripCloser = &RoundTripper{}
// ErrNoCachedConn is returned when RoundTripper.OnlyCachedConn is set
var ErrNoCachedConn = errors.New("h2quic: no cached connection was available")
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
// RoundTripOpt is like RoundTrip, but takes options.
func (r *RoundTripper) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) {
2017-12-12 02:51:45 +00:00
if req.URL == nil {
closeRequestBody(req)
return nil, errors.New("quic: nil Request.URL")
}
if req.URL.Host == "" {
closeRequestBody(req)
return nil, errors.New("quic: no Host in request URL")
}
if req.Header == nil {
closeRequestBody(req)
return nil, errors.New("quic: nil Request.Header")
}
if req.URL.Scheme == "https" {
for k, vv := range req.Header {
if !httplex.ValidHeaderFieldName(k) {
return nil, fmt.Errorf("quic: invalid http header field name %q", k)
}
for _, v := range vv {
if !httplex.ValidHeaderFieldValue(v) {
return nil, fmt.Errorf("quic: invalid http header field value %q for key %v", v, k)
}
}
}
} else {
closeRequestBody(req)
return nil, fmt.Errorf("quic: unsupported protocol scheme: %s", req.URL.Scheme)
}
if req.Method != "" && !validMethod(req.Method) {
closeRequestBody(req)
return nil, fmt.Errorf("quic: invalid method %q", req.Method)
}
hostname := authorityAddr("https", hostnameFromRequest(req))
2018-01-03 19:19:49 +00:00
cl, err := r.getClient(hostname, opt.OnlyCachedConn)
2017-12-12 02:51:45 +00:00
if err != nil {
return nil, err
}
2018-01-03 19:19:49 +00:00
return cl.RoundTrip(req)
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
// RoundTrip does a round trip.
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return r.RoundTripOpt(req, RoundTripOpt{})
}
func (r *RoundTripper) getClient(hostname string, onlyCached bool) (http.RoundTripper, error) {
2017-12-12 02:51:45 +00:00
r.mutex.Lock()
defer r.mutex.Unlock()
if r.clients == nil {
2018-01-03 19:19:49 +00:00
r.clients = make(map[string]roundTripCloser)
2017-12-12 02:51:45 +00:00
}
client, ok := r.clients[hostname]
if !ok {
2018-01-03 19:19:49 +00:00
if onlyCached {
return nil, ErrNoCachedConn
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
client = newClient(hostname, r.TLSClientConfig, &roundTripperOpts{DisableCompression: r.DisableCompression}, r.QuicConfig)
2017-12-12 02:51:45 +00:00
r.clients[hostname] = client
}
return client, nil
}
2018-01-03 19:19:49 +00:00
// Close closes the QUIC connections that this RoundTripper has used
func (r *RoundTripper) Close() error {
r.mutex.Lock()
defer r.mutex.Unlock()
for _, client := range r.clients {
if err := client.Close(); err != nil {
return err
}
}
r.clients = nil
return nil
2017-12-12 02:51:45 +00:00
}
func closeRequestBody(req *http.Request) {
if req.Body != nil {
req.Body.Close()
}
}
func validMethod(method string) bool {
/*
Method = "OPTIONS" ; Section 9.2
| "GET" ; Section 9.3
| "HEAD" ; Section 9.4
| "POST" ; Section 9.5
| "PUT" ; Section 9.6
| "DELETE" ; Section 9.7
| "TRACE" ; Section 9.8
| "CONNECT" ; Section 9.9
| extension-method
extension-method = token
token = 1*<any CHAR except CTLs or separators>
*/
return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1
}
// copied from net/http/http.go
func isNotToken(r rune) bool {
return !httplex.IsTokenRune(r)
}