@@ -59,11 +59,15 @@ var clientConnectionCounter uint64
59
59
60
60
// http2Client implements the ClientTransport interface with HTTP2.
61
61
type 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
67
71
md metadata.MD
68
72
conn net.Conn // underlying communication channel
69
73
loopy * loopyWriter
@@ -314,6 +318,7 @@ func newHTTP2Client(connectCtx, ctx context.Context, addr resolver.Address, opts
314
318
cancel : cancel ,
315
319
userAgent : opts .UserAgent ,
316
320
registeredCompressors : grpcutil .RegisteredCompressors (),
321
+ address : addr ,
317
322
conn : conn ,
318
323
remoteAddr : conn .RemoteAddr (),
319
324
localAddr : conn .LocalAddr (),
@@ -702,6 +707,18 @@ func (e NewStreamError) Error() string {
702
707
// streams. All non-nil errors returned will be *NewStreamError.
703
708
func (t * http2Client ) NewStream (ctx context.Context , callHdr * CallHdr ) (* Stream , error ) {
704
709
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
+
705
722
headerFields , err := t .createHeaderFields (ctx , callHdr )
706
723
if err != nil {
707
724
return nil , & NewStreamError {Err : err , AllowTransparentRetry : false }
0 commit comments