forked from quic-go/quic-go
privatize the h2quic client
This commit is contained in:
@@ -20,8 +20,8 @@ import (
|
||||
"github.com/lucas-clemente/quic-go/utils"
|
||||
)
|
||||
|
||||
// Client is a HTTP2 client doing QUIC requests
|
||||
type Client struct {
|
||||
// client is a HTTP2 client doing QUIC requests
|
||||
type client struct {
|
||||
mutex sync.RWMutex
|
||||
|
||||
dialAddr func(hostname string, config *quic.Config) (quic.Session, error)
|
||||
@@ -42,11 +42,11 @@ type Client struct {
|
||||
responses map[protocol.StreamID]chan *http.Response
|
||||
}
|
||||
|
||||
var _ h2quicClient = &Client{}
|
||||
var _ h2quicClient = &client{}
|
||||
|
||||
// NewClient creates a new client
|
||||
func NewClient(t *QuicRoundTripper, tlsConfig *tls.Config, hostname string) *Client {
|
||||
return &Client{
|
||||
// newClient creates a new client
|
||||
func newClient(t *QuicRoundTripper, tlsConfig *tls.Config, hostname string) *client {
|
||||
return &client{
|
||||
t: t,
|
||||
dialAddr: quic.DialAddr,
|
||||
hostname: authorityAddr("https", hostname),
|
||||
@@ -61,7 +61,7 @@ func NewClient(t *QuicRoundTripper, tlsConfig *tls.Config, hostname string) *Cli
|
||||
}
|
||||
|
||||
// Dial dials the connection
|
||||
func (c *Client) Dial() (err error) {
|
||||
func (c *client) Dial() (err error) {
|
||||
defer func() {
|
||||
c.handshakeErr = err
|
||||
close(c.dialChan)
|
||||
@@ -85,7 +85,7 @@ func (c *Client) Dial() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) handleHeaderStream() {
|
||||
func (c *client) handleHeaderStream() {
|
||||
decoder := hpack.NewDecoder(4096, func(hf hpack.HeaderField) {})
|
||||
h2framer := http2.NewFramer(nil, c.headerStream)
|
||||
|
||||
@@ -135,7 +135,7 @@ func (c *Client) handleHeaderStream() {
|
||||
}
|
||||
|
||||
// Do executes a request and returns a response
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
func (c *client) Do(req *http.Request) (*http.Response, error) {
|
||||
// TODO: add port to address, if it doesn't have one
|
||||
if req.URL.Scheme != "https" {
|
||||
return nil, errors.New("quic http2: unsupported scheme")
|
||||
@@ -234,7 +234,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) writeRequestBody(dataStream quic.Stream, body io.ReadCloser) (err error) {
|
||||
func (c *client) writeRequestBody(dataStream quic.Stream, body io.ReadCloser) (err error) {
|
||||
defer func() {
|
||||
cerr := body.Close()
|
||||
if err == nil {
|
||||
@@ -252,7 +252,7 @@ func (c *Client) writeRequestBody(dataStream quic.Stream, body io.ReadCloser) (e
|
||||
}
|
||||
|
||||
// Close closes the client
|
||||
func (c *Client) Close(e error) {
|
||||
func (c *client) Close(e error) {
|
||||
_ = c.session.Close(e)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
|
||||
var _ = Describe("Client", func() {
|
||||
var (
|
||||
client *Client
|
||||
client *client
|
||||
session *mockSession
|
||||
headerStream *mockStream
|
||||
quicTransport *QuicRoundTripper
|
||||
@@ -29,7 +29,7 @@ var _ = Describe("Client", func() {
|
||||
BeforeEach(func() {
|
||||
quicTransport = &QuicRoundTripper{}
|
||||
hostname := "quic.clemente.io:1337"
|
||||
client = NewClient(quicTransport, nil, hostname)
|
||||
client = newClient(quicTransport, nil, hostname)
|
||||
Expect(client.hostname).To(Equal(hostname))
|
||||
session = &mockSession{}
|
||||
client.session = session
|
||||
@@ -41,17 +41,17 @@ var _ = Describe("Client", func() {
|
||||
|
||||
It("saves the TLS config", func() {
|
||||
tlsConf := &tls.Config{InsecureSkipVerify: true}
|
||||
client = NewClient(&QuicRoundTripper{}, tlsConf, "")
|
||||
client = newClient(&QuicRoundTripper{}, tlsConf, "")
|
||||
Expect(client.config.TLSConfig).To(Equal(tlsConf))
|
||||
})
|
||||
|
||||
It("adds the port to the hostname, if none is given", func() {
|
||||
client = NewClient(quicTransport, nil, "quic.clemente.io")
|
||||
client = newClient(quicTransport, nil, "quic.clemente.io")
|
||||
Expect(client.hostname).To(Equal("quic.clemente.io:443"))
|
||||
})
|
||||
|
||||
It("dials", func() {
|
||||
client = NewClient(quicTransport, nil, "localhost")
|
||||
client = newClient(quicTransport, nil, "localhost")
|
||||
session.streamToOpen = &mockStream{id: 3}
|
||||
client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
|
||||
return session, nil
|
||||
@@ -63,7 +63,7 @@ var _ = Describe("Client", func() {
|
||||
|
||||
It("errors when dialing fails", func() {
|
||||
testErr := errors.New("handshake error")
|
||||
client = NewClient(quicTransport, nil, "localhost")
|
||||
client = newClient(quicTransport, nil, "localhost")
|
||||
client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
|
||||
return nil, testErr
|
||||
}
|
||||
@@ -72,7 +72,7 @@ var _ = Describe("Client", func() {
|
||||
})
|
||||
|
||||
It("errors if the header stream has the wrong stream ID", func() {
|
||||
client = NewClient(quicTransport, nil, "localhost")
|
||||
client = newClient(quicTransport, nil, "localhost")
|
||||
session.streamToOpen = &mockStream{id: 2}
|
||||
client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
|
||||
return session, nil
|
||||
@@ -83,7 +83,7 @@ var _ = Describe("Client", func() {
|
||||
|
||||
It("errors if it can't open a stream", func() {
|
||||
testErr := errors.New("you shall not pass")
|
||||
client = NewClient(quicTransport, nil, "localhost")
|
||||
client = newClient(quicTransport, nil, "localhost")
|
||||
session.streamOpenErr = testErr
|
||||
client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
|
||||
return session, nil
|
||||
@@ -226,7 +226,7 @@ var _ = Describe("Client", func() {
|
||||
|
||||
It("adds the port for request URLs without one", func(done Done) {
|
||||
var err error
|
||||
client = NewClient(quicTransport, nil, "quic.clemente.io")
|
||||
client = newClient(quicTransport, nil, "quic.clemente.io")
|
||||
req, err := http.NewRequest("https", "https://quic.clemente.io/foobar.html", nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ func (r *QuicRoundTripper) getClient(hostname string) (h2quicClient, error) {
|
||||
|
||||
client, ok := r.clients[hostname]
|
||||
if !ok {
|
||||
client = NewClient(r, r.TLSClientConfig, hostname)
|
||||
client = newClient(r, r.TLSClientConfig, hostname)
|
||||
err := client.Dial()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user