@@ -59,11 +59,15 @@ var clientConnectionCounter uint64
5959
6060// http2Client implements the ClientTransport interface with HTTP2.
6161type http2Client struct {
62- lastRead int64 // Keep this field 64-bit aligned. Accessed atomically.
63- ctx context.Context
64- cancel context.CancelFunc
65- ctxDone <- chan struct {} // Cache the ctx.Done() chan.
66- userAgent string
62+ lastRead int64 // Keep this field 64-bit aligned. Accessed atomically.
63+ ctx context.Context
64+ cancel context.CancelFunc
65+ ctxDone <- chan struct {} // Cache the ctx.Done() chan.
66+ userAgent string
67+ // address contains the resolver returned address for this transport.
68+ // If the `ServerName` field is set, it takes precedence over `CallHdr.Host`
69+ // passed to `NewStream`, when determining the :authority header.
70+ address resolver.Address
6771 md metadata.MD
6872 conn net.Conn // underlying communication channel
6973 loopy * loopyWriter
@@ -314,6 +318,7 @@ func newHTTP2Client(connectCtx, ctx context.Context, addr resolver.Address, opts
314318 cancel : cancel ,
315319 userAgent : opts .UserAgent ,
316320 registeredCompressors : grpcutil .RegisteredCompressors (),
321+ address : addr ,
317322 conn : conn ,
318323 remoteAddr : conn .RemoteAddr (),
319324 localAddr : conn .LocalAddr (),
@@ -702,6 +707,18 @@ func (e NewStreamError) Error() string {
702707// streams. All non-nil errors returned will be *NewStreamError.
703708func (t * http2Client ) NewStream (ctx context.Context , callHdr * CallHdr ) (* Stream , error ) {
704709 ctx = peer .NewContext (ctx , t .getPeer ())
710+
711+ // ServerName field of the resolver returned address takes precedence over
712+ // Host field of CallHdr to determine the :authority header. This is because,
713+ // the ServerName field takes precedence for server authentication during
714+ // TLS handshake, and the :authority header should match the value used
715+ // for server authentication.
716+ if t .address .ServerName != "" {
717+ newCallHdr := * callHdr
718+ newCallHdr .Host = t .address .ServerName
719+ callHdr = & newCallHdr
720+ }
721+
705722 headerFields , err := t .createHeaderFields (ctx , callHdr )
706723 if err != nil {
707724 return nil , & NewStreamError {Err : err , AllowTransparentRetry : false }
0 commit comments