Skip to content

Commit dcd2661

Browse files
authored
Merge pull request #1064 from rodaine/set-authority
Add DialOption to overwrite :authority pseudo-header
2 parents 4acc910 + 4ad16bc commit dcd2661

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Diff for: clientconn.go

+11
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ func WithStreamInterceptor(f StreamClientInterceptor) DialOption {
263263
}
264264
}
265265

266+
// WithAuthority returns a DialOption that specifies the value to be used as
267+
// the :authority pseudo-header. This value only works with WithInsecure and
268+
// has no effect if TransportCredentials are present.
269+
func WithAuthority(a string) DialOption {
270+
return func(o *dialOptions) {
271+
o.copts.Authority = a
272+
}
273+
}
274+
266275
// Dial creates a client connection to the given target.
267276
func Dial(target string, opts ...DialOption) (*ClientConn, error) {
268277
return DialContext(context.Background(), target, opts...)
@@ -321,6 +330,8 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
321330
creds := cc.dopts.copts.TransportCredentials
322331
if creds != nil && creds.Info().ServerName != "" {
323332
cc.authority = creds.Info().ServerName
333+
} else if cc.dopts.insecure && cc.dopts.copts.Authority != "" {
334+
cc.authority = cc.dopts.copts.Authority
324335
} else {
325336
colonPos := strings.LastIndex(target, ":")
326337
if colonPos == -1 {

Diff for: clientconn_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ func TestTLSServerNameOverwrite(t *testing.T) {
8585
}
8686
}
8787

88+
func TestWithAuthority(t *testing.T) {
89+
overwriteServerName := "over.write.server.name"
90+
conn, err := Dial("Non-Existent.Server:80", WithInsecure(), WithAuthority(overwriteServerName))
91+
if err != nil {
92+
t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
93+
}
94+
conn.Close()
95+
if conn.authority != overwriteServerName {
96+
t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
97+
}
98+
}
99+
100+
func TestWithAuthorityAndTLS(t *testing.T) {
101+
overwriteServerName := "over.write.server.name"
102+
creds, err := credentials.NewClientTLSFromFile(tlsDir+"ca.pem", overwriteServerName)
103+
if err != nil {
104+
t.Fatalf("Failed to create credentials %v", err)
105+
}
106+
conn, err := Dial("Non-Existent.Server:80", WithTransportCredentials(creds), WithAuthority("no.effect.authority"))
107+
if err != nil {
108+
t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
109+
}
110+
conn.Close()
111+
if conn.authority != overwriteServerName {
112+
t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
113+
}
114+
}
115+
88116
func TestDialContextCancel(t *testing.T) {
89117
ctx, cancel := context.WithCancel(context.Background())
90118
cancel()

Diff for: transport/transport.go

+3
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ func NewServerTransport(protocol string, conn net.Conn, config *ServerConfig) (S
374374
type ConnectOptions struct {
375375
// UserAgent is the application user agent.
376376
UserAgent string
377+
// Authority is the :authority pseudo-header to use. This field has no effect if
378+
// TransportCredentials is set.
379+
Authority string
377380
// Dialer specifies how to dial a network address.
378381
Dialer func(context.Context, string) (net.Conn, error)
379382
// FailOnNonTempDialError specifies if gRPC fails on non-temporary dial errors.

0 commit comments

Comments
 (0)