privatize the h2quic client

This commit is contained in:
Marten Seemann
2017-05-26 17:47:49 +08:00
parent a025e89f03
commit 4c3d4960bb
3 changed files with 21 additions and 21 deletions

View File

@@ -20,8 +20,8 @@ import (
"github.com/lucas-clemente/quic-go/utils" "github.com/lucas-clemente/quic-go/utils"
) )
// Client is a HTTP2 client doing QUIC requests // client is a HTTP2 client doing QUIC requests
type Client struct { type client struct {
mutex sync.RWMutex mutex sync.RWMutex
dialAddr func(hostname string, config *quic.Config) (quic.Session, error) dialAddr func(hostname string, config *quic.Config) (quic.Session, error)
@@ -42,11 +42,11 @@ type Client struct {
responses map[protocol.StreamID]chan *http.Response responses map[protocol.StreamID]chan *http.Response
} }
var _ h2quicClient = &Client{} var _ h2quicClient = &client{}
// NewClient creates a new client // newClient creates a new client
func NewClient(t *QuicRoundTripper, tlsConfig *tls.Config, hostname string) *Client { func newClient(t *QuicRoundTripper, tlsConfig *tls.Config, hostname string) *client {
return &Client{ return &client{
t: t, t: t,
dialAddr: quic.DialAddr, dialAddr: quic.DialAddr,
hostname: authorityAddr("https", hostname), hostname: authorityAddr("https", hostname),
@@ -61,7 +61,7 @@ func NewClient(t *QuicRoundTripper, tlsConfig *tls.Config, hostname string) *Cli
} }
// Dial dials the connection // Dial dials the connection
func (c *Client) Dial() (err error) { func (c *client) Dial() (err error) {
defer func() { defer func() {
c.handshakeErr = err c.handshakeErr = err
close(c.dialChan) close(c.dialChan)
@@ -85,7 +85,7 @@ func (c *Client) Dial() (err error) {
return return
} }
func (c *Client) handleHeaderStream() { func (c *client) handleHeaderStream() {
decoder := hpack.NewDecoder(4096, func(hf hpack.HeaderField) {}) decoder := hpack.NewDecoder(4096, func(hf hpack.HeaderField) {})
h2framer := http2.NewFramer(nil, c.headerStream) h2framer := http2.NewFramer(nil, c.headerStream)
@@ -135,7 +135,7 @@ func (c *Client) handleHeaderStream() {
} }
// Do executes a request and returns a response // 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 // TODO: add port to address, if it doesn't have one
if req.URL.Scheme != "https" { if req.URL.Scheme != "https" {
return nil, errors.New("quic http2: unsupported scheme") 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 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() { defer func() {
cerr := body.Close() cerr := body.Close()
if err == nil { if err == nil {
@@ -252,7 +252,7 @@ func (c *Client) writeRequestBody(dataStream quic.Stream, body io.ReadCloser) (e
} }
// Close closes the client // Close closes the client
func (c *Client) Close(e error) { func (c *client) Close(e error) {
_ = c.session.Close(e) _ = c.session.Close(e)
} }

View File

@@ -20,7 +20,7 @@ import (
var _ = Describe("Client", func() { var _ = Describe("Client", func() {
var ( var (
client *Client client *client
session *mockSession session *mockSession
headerStream *mockStream headerStream *mockStream
quicTransport *QuicRoundTripper quicTransport *QuicRoundTripper
@@ -29,7 +29,7 @@ var _ = Describe("Client", func() {
BeforeEach(func() { BeforeEach(func() {
quicTransport = &QuicRoundTripper{} quicTransport = &QuicRoundTripper{}
hostname := "quic.clemente.io:1337" hostname := "quic.clemente.io:1337"
client = NewClient(quicTransport, nil, hostname) client = newClient(quicTransport, nil, hostname)
Expect(client.hostname).To(Equal(hostname)) Expect(client.hostname).To(Equal(hostname))
session = &mockSession{} session = &mockSession{}
client.session = session client.session = session
@@ -41,17 +41,17 @@ var _ = Describe("Client", func() {
It("saves the TLS config", func() { It("saves the TLS config", func() {
tlsConf := &tls.Config{InsecureSkipVerify: true} tlsConf := &tls.Config{InsecureSkipVerify: true}
client = NewClient(&QuicRoundTripper{}, tlsConf, "") client = newClient(&QuicRoundTripper{}, tlsConf, "")
Expect(client.config.TLSConfig).To(Equal(tlsConf)) Expect(client.config.TLSConfig).To(Equal(tlsConf))
}) })
It("adds the port to the hostname, if none is given", func() { 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")) Expect(client.hostname).To(Equal("quic.clemente.io:443"))
}) })
It("dials", func() { It("dials", func() {
client = NewClient(quicTransport, nil, "localhost") client = newClient(quicTransport, nil, "localhost")
session.streamToOpen = &mockStream{id: 3} session.streamToOpen = &mockStream{id: 3}
client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) { client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
return session, nil return session, nil
@@ -63,7 +63,7 @@ var _ = Describe("Client", func() {
It("errors when dialing fails", func() { It("errors when dialing fails", func() {
testErr := errors.New("handshake error") 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) { client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
return nil, testErr return nil, testErr
} }
@@ -72,7 +72,7 @@ var _ = Describe("Client", func() {
}) })
It("errors if the header stream has the wrong stream ID", 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} session.streamToOpen = &mockStream{id: 2}
client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) { client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
return session, nil return session, nil
@@ -83,7 +83,7 @@ var _ = Describe("Client", func() {
It("errors if it can't open a stream", func() { It("errors if it can't open a stream", func() {
testErr := errors.New("you shall not pass") testErr := errors.New("you shall not pass")
client = NewClient(quicTransport, nil, "localhost") client = newClient(quicTransport, nil, "localhost")
session.streamOpenErr = testErr session.streamOpenErr = testErr
client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) { client.dialAddr = func(hostname string, conf *quic.Config) (quic.Session, error) {
return session, nil return session, nil
@@ -226,7 +226,7 @@ var _ = Describe("Client", func() {
It("adds the port for request URLs without one", func(done Done) { It("adds the port for request URLs without one", func(done Done) {
var err error 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) req, err := http.NewRequest("https", "https://quic.clemente.io/foobar.html", nil)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())

View File

@@ -93,7 +93,7 @@ func (r *QuicRoundTripper) getClient(hostname string) (h2quicClient, error) {
client, ok := r.clients[hostname] client, ok := r.clients[hostname]
if !ok { if !ok {
client = NewClient(r, r.TLSClientConfig, hostname) client = newClient(r, r.TLSClientConfig, hostname)
err := client.Dial() err := client.Dial()
if err != nil { if err != nil {
return nil, err return nil, err