| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- package http3
- import (
- "bytes"
- "context"
- "crypto/tls"
- "errors"
- "fmt"
- "io"
- "net/http"
- "strconv"
- "sync"
- "github.com/Psiphon-Labs/quic-go"
- "github.com/Psiphon-Labs/quic-go/internal/protocol"
- "github.com/Psiphon-Labs/quic-go/internal/qtls"
- "github.com/Psiphon-Labs/quic-go/internal/utils"
- "github.com/Psiphon-Labs/quic-go/quicvarint"
- "github.com/marten-seemann/qpack"
- )
- // MethodGet0RTT allows a GET request to be sent using 0-RTT.
- // Note that 0-RTT data doesn't provide replay protection.
- const MethodGet0RTT = "GET_0RTT"
- const (
- defaultUserAgent = "quic-go HTTP/3"
- defaultMaxResponseHeaderBytes = 10 * 1 << 20 // 10 MB
- )
- var defaultQuicConfig = &quic.Config{
- MaxIncomingStreams: -1, // don't allow the server to create bidirectional streams
- KeepAlive: true,
- Versions: []protocol.VersionNumber{protocol.VersionTLS},
- }
- var dialAddr = quic.DialAddrEarly
- type roundTripperOpts struct {
- DisableCompression bool
- EnableDatagram bool
- MaxHeaderBytes int64
- }
- // client is a HTTP3 client doing requests
- type client struct {
- tlsConf *tls.Config
- config *quic.Config
- opts *roundTripperOpts
- dialOnce sync.Once
- dialer func(network, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error)
- handshakeErr error
- requestWriter *requestWriter
- decoder *qpack.Decoder
- hostname string
- // [Psiphon]
- // Enable Close to be called concurrently with dial.
- sessionMutex sync.Mutex
- closed bool
- session quic.EarlySession
- logger utils.Logger
- }
- func newClient(
- hostname string,
- tlsConf *tls.Config,
- opts *roundTripperOpts,
- quicConfig *quic.Config,
- dialer func(network, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error),
- ) (*client, error) {
- if quicConfig == nil {
- quicConfig = defaultQuicConfig.Clone()
- } else if len(quicConfig.Versions) == 0 {
- quicConfig = quicConfig.Clone()
- quicConfig.Versions = []quic.VersionNumber{defaultQuicConfig.Versions[0]}
- }
- if len(quicConfig.Versions) != 1 {
- return nil, errors.New("can only use a single QUIC version for dialing a HTTP/3 connection")
- }
- quicConfig.MaxIncomingStreams = -1 // don't allow any bidirectional streams
- quicConfig.EnableDatagrams = opts.EnableDatagram
- logger := utils.DefaultLogger.WithPrefix("h3 client")
- if tlsConf == nil {
- tlsConf = &tls.Config{}
- } else {
- tlsConf = tlsConf.Clone()
- }
- // Replace existing ALPNs by H3
- tlsConf.NextProtos = []string{versionToALPN(quicConfig.Versions[0])}
- return &client{
- hostname: authorityAddr("https", hostname),
- tlsConf: tlsConf,
- requestWriter: newRequestWriter(logger),
- decoder: qpack.NewDecoder(func(hf qpack.HeaderField) {}),
- config: quicConfig,
- opts: opts,
- dialer: dialer,
- logger: logger,
- }, nil
- }
- func (c *client) dial() error {
- var err error
- var session quic.EarlySession
- if c.dialer != nil {
- session, err = c.dialer("udp", c.hostname, c.tlsConf, c.config)
- } else {
- session, err = dialAddr(c.hostname, c.tlsConf, c.config)
- }
- if err != nil {
- return err
- }
- // [Psiphon]
- c.sessionMutex.Lock()
- if c.closed {
- session.CloseWithError(quic.ApplicationErrorCode(errorNoError), "")
- err = errors.New("closed while dialing")
- } else {
- c.session = session
- }
- c.sessionMutex.Unlock()
- // send the SETTINGs frame, using 0-RTT data, if possible
- go func() {
- if err := c.setupSession(); err != nil {
- c.logger.Debugf("Setting up session failed: %s", err)
- c.session.CloseWithError(quic.ApplicationErrorCode(errorInternalError), "")
- }
- }()
- go c.handleUnidirectionalStreams()
- return nil
- }
- func (c *client) setupSession() error {
- // open the control stream
- str, err := c.session.OpenUniStream()
- if err != nil {
- return err
- }
- buf := &bytes.Buffer{}
- quicvarint.Write(buf, streamTypeControlStream)
- // send the SETTINGS frame
- (&settingsFrame{Datagram: c.opts.EnableDatagram}).Write(buf)
- _, err = str.Write(buf.Bytes())
- return err
- }
- func (c *client) handleUnidirectionalStreams() {
- for {
- str, err := c.session.AcceptUniStream(context.Background())
- if err != nil {
- c.logger.Debugf("accepting unidirectional stream failed: %s", err)
- return
- }
- go func() {
- streamType, err := quicvarint.Read(quicvarint.NewReader(str))
- if err != nil {
- c.logger.Debugf("reading stream type on stream %d failed: %s", str.StreamID(), err)
- return
- }
- // We're only interested in the control stream here.
- switch streamType {
- case streamTypeControlStream:
- case streamTypeQPACKEncoderStream, streamTypeQPACKDecoderStream:
- // Our QPACK implementation doesn't use the dynamic table yet.
- // TODO: check that only one stream of each type is opened.
- return
- case streamTypePushStream:
- // We never increased the Push ID, so we don't expect any push streams.
- c.session.CloseWithError(quic.ApplicationErrorCode(errorIDError), "")
- return
- default:
- str.CancelRead(quic.StreamErrorCode(errorStreamCreationError))
- return
- }
- f, err := parseNextFrame(str)
- if err != nil {
- c.session.CloseWithError(quic.ApplicationErrorCode(errorFrameError), "")
- return
- }
- sf, ok := f.(*settingsFrame)
- if !ok {
- c.session.CloseWithError(quic.ApplicationErrorCode(errorMissingSettings), "")
- return
- }
- if !sf.Datagram {
- return
- }
- // If datagram support was enabled on our side as well as on the server side,
- // we can expect it to have been negotiated both on the transport and on the HTTP/3 layer.
- // Note: ConnectionState() will block until the handshake is complete (relevant when using 0-RTT).
- if c.opts.EnableDatagram && !c.session.ConnectionState().SupportsDatagrams {
- c.session.CloseWithError(quic.ApplicationErrorCode(errorSettingsError), "missing QUIC Datagram support")
- }
- }()
- }
- }
- func (c *client) Close() error {
- // [Psiphon]
- c.sessionMutex.Lock()
- session := c.session
- c.closed = true
- c.sessionMutex.Unlock()
- if session == nil {
- return nil
- }
- return session.CloseWithError(quic.ApplicationErrorCode(errorNoError), "")
- }
- func (c *client) maxHeaderBytes() uint64 {
- if c.opts.MaxHeaderBytes <= 0 {
- return defaultMaxResponseHeaderBytes
- }
- return uint64(c.opts.MaxHeaderBytes)
- }
- // RoundTrip executes a request and returns a response
- func (c *client) RoundTrip(req *http.Request) (*http.Response, error) {
- if authorityAddr("https", hostnameFromRequest(req)) != c.hostname {
- return nil, fmt.Errorf("http3 client BUG: RoundTrip called for the wrong client (expected %s, got %s)", c.hostname, req.Host)
- }
- c.dialOnce.Do(func() {
- c.handshakeErr = c.dial()
- })
- if c.handshakeErr != nil {
- return nil, c.handshakeErr
- }
- // Immediately send out this request, if this is a 0-RTT request.
- if req.Method == MethodGet0RTT {
- req.Method = http.MethodGet
- } else {
- // wait for the handshake to complete
- select {
- case <-c.session.HandshakeComplete().Done():
- case <-req.Context().Done():
- return nil, req.Context().Err()
- }
- }
- str, err := c.session.OpenStreamSync(req.Context())
- if err != nil {
- return nil, err
- }
- // Request Cancellation:
- // This go routine keeps running even after RoundTrip() returns.
- // It is shut down when the application is done processing the body.
- reqDone := make(chan struct{})
- go func() {
- select {
- case <-req.Context().Done():
- str.CancelWrite(quic.StreamErrorCode(errorRequestCanceled))
- str.CancelRead(quic.StreamErrorCode(errorRequestCanceled))
- case <-reqDone:
- }
- }()
- rsp, rerr := c.doRequest(req, str, reqDone)
- if rerr.err != nil { // if any error occurred
- close(reqDone)
- if rerr.streamErr != 0 { // if it was a stream error
- str.CancelWrite(quic.StreamErrorCode(rerr.streamErr))
- }
- if rerr.connErr != 0 { // if it was a connection error
- var reason string
- if rerr.err != nil {
- reason = rerr.err.Error()
- }
- c.session.CloseWithError(quic.ApplicationErrorCode(rerr.connErr), reason)
- }
- }
- return rsp, rerr.err
- }
- func (c *client) doRequest(
- req *http.Request,
- str quic.Stream,
- reqDone chan struct{},
- ) (*http.Response, requestError) {
- var requestGzip bool
- if !c.opts.DisableCompression && req.Method != "HEAD" && req.Header.Get("Accept-Encoding") == "" && req.Header.Get("Range") == "" {
- requestGzip = true
- }
- if err := c.requestWriter.WriteRequest(str, req, requestGzip); err != nil {
- return nil, newStreamError(errorInternalError, err)
- }
- frame, err := parseNextFrame(str)
- if err != nil {
- return nil, newStreamError(errorFrameError, err)
- }
- hf, ok := frame.(*headersFrame)
- if !ok {
- return nil, newConnError(errorFrameUnexpected, errors.New("expected first frame to be a HEADERS frame"))
- }
- if hf.Length > c.maxHeaderBytes() {
- return nil, newStreamError(errorFrameError, fmt.Errorf("HEADERS frame too large: %d bytes (max: %d)", hf.Length, c.maxHeaderBytes()))
- }
- headerBlock := make([]byte, hf.Length)
- if _, err := io.ReadFull(str, headerBlock); err != nil {
- return nil, newStreamError(errorRequestIncomplete, err)
- }
- hfs, err := c.decoder.DecodeFull(headerBlock)
- if err != nil {
- // TODO: use the right error code
- return nil, newConnError(errorGeneralProtocolError, err)
- }
- connState := qtls.ToTLSConnectionState(c.session.ConnectionState().TLS)
- res := &http.Response{
- Proto: "HTTP/3",
- ProtoMajor: 3,
- Header: http.Header{},
- TLS: &connState,
- }
- for _, hf := range hfs {
- switch hf.Name {
- case ":status":
- status, err := strconv.Atoi(hf.Value)
- if err != nil {
- return nil, newStreamError(errorGeneralProtocolError, errors.New("malformed non-numeric status pseudo header"))
- }
- res.StatusCode = status
- res.Status = hf.Value + " " + http.StatusText(status)
- default:
- res.Header.Add(hf.Name, hf.Value)
- }
- }
- respBody := newResponseBody(str, reqDone, func() {
- c.session.CloseWithError(quic.ApplicationErrorCode(errorFrameUnexpected), "")
- })
- // Rules for when to set Content-Length are defined in https://tools.ietf.org/html/rfc7230#section-3.3.2.
- _, hasTransferEncoding := res.Header["Transfer-Encoding"]
- isInformational := res.StatusCode >= 100 && res.StatusCode < 200
- isNoContent := res.StatusCode == 204
- isSuccessfulConnect := req.Method == http.MethodConnect && res.StatusCode >= 200 && res.StatusCode < 300
- if !hasTransferEncoding && !isInformational && !isNoContent && !isSuccessfulConnect {
- res.ContentLength = -1
- if clens, ok := res.Header["Content-Length"]; ok && len(clens) == 1 {
- if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil {
- res.ContentLength = clen64
- }
- }
- }
- if requestGzip && res.Header.Get("Content-Encoding") == "gzip" {
- res.Header.Del("Content-Encoding")
- res.Header.Del("Content-Length")
- res.ContentLength = -1
- res.Body = newGzipReader(respBody)
- res.Uncompressed = true
- } else {
- res.Body = respBody
- }
- return res, requestError{}
- }
|