save correct hostname in quic Client

This commit is contained in:
Marten Seemann 2016-12-16 23:15:21 +07:00
parent 3458514744
commit b9f9c2e429
No known key found for this signature in database
GPG key ID: 3603F40B121FCDEA
2 changed files with 13 additions and 13 deletions

View file

@ -5,7 +5,6 @@ import (
"errors"
"math/rand"
"net"
"net/url"
"strings"
"time"
@ -41,18 +40,7 @@ var (
)
// NewClient makes a new client
func NewClient(addr string, cryptoChangeCallback CryptoChangeCallback, versionNegotiateCallback VersionNegotiateCallback) (*Client, error) {
hostname, err := utils.HostnameFromAddr(addr)
if err != nil || len(hostname) == 0 {
return nil, errHostname
}
p, err := url.Parse(addr)
if err != nil {
return nil, err
}
host := p.Host
func NewClient(host string, cryptoChangeCallback CryptoChangeCallback, versionNegotiateCallback VersionNegotiateCallback) (*Client, error) {
udpAddr, err := net.ResolveUDPAddr("udp", host)
if err != nil {
return nil, err
@ -67,6 +55,11 @@ func NewClient(addr string, cryptoChangeCallback CryptoChangeCallback, versionNe
rand.Seed(time.Now().UTC().UnixNano())
connectionID := protocol.ConnectionID(rand.Int63())
hostname, _, err := net.SplitHostPort(host)
if err != nil {
return nil, err
}
client := &Client{
addr: udpAddr,
conn: conn,

View file

@ -41,6 +41,13 @@ var _ = Describe("Client", func() {
Expect(err).NotTo(HaveOccurred())
}
It("sets the correct hostname for new clients", func() {
var err error
client, err = NewClient("quic.clemente.io:1337", nil, nil)
Expect(err).ToNot(HaveOccurred())
Expect(client.hostname).To(Equal("quic.clemente.io"))
})
It("errors on invalid public header", func() {
err := client.handlePacket(nil)
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.InvalidPacketHeader))