From 873ca6009fa0d2f69fdf4f2856f78c2471b7ea4b Mon Sep 17 00:00:00 2001 From: willard Date: Sun, 8 Oct 2023 22:29:10 +0800 Subject: [PATCH] Update the Host field in the request header This update checks for the 'Host' value in the request header, specifically assigning it to 'u.Host' if no value is found. This same logic is applied to 'cfg.ServerName'. The aim of this change is to ensure a correct 'Host' value is used throughout, addressing a bug related to setting the 'Host' field of the header. --- client.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 815b0ca..ad5dec9 100644 --- a/client.go +++ b/client.go @@ -189,6 +189,10 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h return nil, nil, errMalformedURL } + host := requestHeader.Get("Host") + if host == "" { + host = u.Host + } req := &http.Request{ Method: http.MethodGet, URL: u, @@ -196,7 +200,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h ProtoMajor: 1, ProtoMinor: 1, Header: make(http.Header), - Host: u.Host, + Host: host, } req = req.WithContext(ctx) @@ -347,7 +351,11 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h cfg := cloneTLSConfig(d.TLSClientConfig) if cfg.ServerName == "" { - cfg.ServerName = hostNoPort + if host != "" { + cfg.ServerName = host + } else { + cfg.ServerName = hostNoPort + } } tlsConn := tls.Client(netConn, cfg) netConn = tlsConn