Skip to content

Commit

Permalink
Remove host argument from NewClient
Browse files Browse the repository at this point in the history
Users can pass it via tls.Config in Client.StartTLS.
  • Loading branch information
emersion committed Dec 6, 2023
1 parent 7f9654d commit 7c803aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
27 changes: 17 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ func Dial(addr string) (*Client, error) {
if err != nil {
return nil, err
}
host, _, _ := net.SplitHostPort(addr)
return NewClient(conn, host)
client, err := NewClient(conn)
if err != nil {
return nil, err
}
client.serverName, _, _ = net.SplitHostPort(addr)
return client, nil
}

// DialTLS returns a new Client connected to an SMTP server via TLS at addr.
Expand All @@ -79,16 +83,19 @@ func DialTLS(addr string, tlsConfig *tls.Config) (*Client, error) {
if err != nil {
return nil, err
}
host, _, _ := net.SplitHostPort(addr)
return NewClient(conn, host)
client, err := NewClient(conn)
if err != nil {
return nil, err
}
client.serverName, _, _ = net.SplitHostPort(addr)
return client, nil
}

// NewClient returns a new Client using an existing connection and host as a
// server name to be used when authenticating.
func NewClient(conn net.Conn, host string) (*Client, error) {
func NewClient(conn net.Conn) (*Client, error) {
c := &Client{
serverName: host,
localName: "localhost",
localName: "localhost",
// As recommended by RFC 5321. For DATA command reply (3xx one) RFC
// recommends a slightly shorter timeout but we do not bother
// differentiating these.
Expand Down Expand Up @@ -118,8 +125,8 @@ func NewClient(conn net.Conn, host string) (*Client, error) {

// NewClientLMTP returns a new LMTP Client (as defined in RFC 2033) using an
// existing connection and host as a server name to be used when authenticating.
func NewClientLMTP(conn net.Conn, host string) (*Client, error) {
c, err := NewClient(conn, host)
func NewClientLMTP(conn net.Conn) (*Client, error) {
c, err := NewClient(conn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -273,7 +280,7 @@ func (c *Client) StartTLS(config *tls.Config) error {
if config == nil {
config = &tls.Config{}
}
if config.ServerName == "" {
if config.ServerName == "" && c.serverName != "" {
// Make a copy to avoid polluting argument
config = config.Clone()
config.ServerName = c.serverName
Expand Down
19 changes: 10 additions & 9 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestClientAuthTrimSpace(t *testing.T) {
strings.NewReader(server),
&wrote,
}
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestBasic_SMTPError(t *testing.T) {
strings.NewReader(faultyServer),
&wrote,
}
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestClient_TooLongLine(t *testing.T) {
pr,
&wrote,
}
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestNewClient(t *testing.T) {
}
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient: %v\n(after %v)", err, out())
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestNewClient2(t *testing.T) {
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
Expand Down Expand Up @@ -422,11 +422,12 @@ func TestHello(t *testing.T) {
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
defer c.Close()
c.serverName = "fake.host"
c.localName = "customhost"
err = nil

Expand Down Expand Up @@ -552,7 +553,7 @@ func TestAuthFailed(t *testing.T) {
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
Expand Down Expand Up @@ -950,7 +951,7 @@ func TestClientXtext(t *testing.T) {
strings.NewReader(server),
&wrote,
}
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
Expand Down Expand Up @@ -1000,7 +1001,7 @@ func TestClientDSN(t *testing.T) {
strings.NewReader(server),
&wrote,
}
c, err := NewClient(fake, "fake.host")
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
Expand Down

0 comments on commit 7c803aa

Please sign in to comment.