@@ -119,6 +119,34 @@ func (rw *HTTPReaderWriter) reset(pw *io.PipeWriter) io.WriteCloser {
119119 }
120120}
121121
122+ // applyOptionsToRequest applies the client Options (such as auth, headers, client info) to the given http.Request
123+ func applyOptionsToRequest (req * http.Request , opt * Options ) {
124+ if opt .TLS != nil && len (opt .Auth .JWT ) > 0 {
125+ req .Header .Set ("Authorization" , "Bearer " + opt .Auth .JWT )
126+ } else if opt .TLS != nil && len (opt .Auth .Username ) > 0 {
127+ req .Header .Set ("X-ClickHouse-User" , opt .Auth .Username )
128+ if len (opt .Auth .Password ) > 0 {
129+ req .Header .Set ("X-ClickHouse-Key" , opt .Auth .Password )
130+ req .Header .Set ("X-ClickHouse-SSL-Certificate-Auth" , "off" )
131+ } else {
132+ req .Header .Set ("X-ClickHouse-SSL-Certificate-Auth" , "on" )
133+ }
134+ } else if opt .TLS == nil && len (opt .Auth .Username ) > 0 {
135+ if len (opt .Auth .Password ) > 0 {
136+ req .URL .User = url .UserPassword (opt .Auth .Username , opt .Auth .Password )
137+
138+ } else {
139+ req .URL .User = url .User (opt .Auth .Username )
140+ }
141+ }
142+
143+ req .Header .Set ("User-Agent" , opt .ClientInfo .String ())
144+
145+ for k , v := range opt .HttpHeaders {
146+ req .Header .Set (k , v )
147+ }
148+ }
149+
122150func dialHttp (ctx context.Context , addr string , num int , opt * Options ) (* httpConnect , error ) {
123151 var debugf = func (format string , v ... any ) {}
124152 if opt .Debug {
@@ -151,29 +179,6 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
151179 Path : opt .HttpUrlPath ,
152180 }
153181
154- headers := make (map [string ]string )
155- for k , v := range opt .HttpHeaders {
156- headers [k ] = v
157- }
158-
159- if opt .TLS == nil && len (opt .Auth .Username ) > 0 {
160- if len (opt .Auth .Password ) > 0 {
161- u .User = url .UserPassword (opt .Auth .Username , opt .Auth .Password )
162- } else {
163- u .User = url .User (opt .Auth .Username )
164- }
165- } else if opt .TLS != nil && len (opt .Auth .Username ) > 0 {
166- headers ["X-ClickHouse-User" ] = opt .Auth .Username
167- if len (opt .Auth .Password ) > 0 {
168- headers ["X-ClickHouse-Key" ] = opt .Auth .Password
169- headers ["X-ClickHouse-SSL-Certificate-Auth" ] = "off"
170- } else {
171- headers ["X-ClickHouse-SSL-Certificate-Auth" ] = "on"
172- }
173- }
174-
175- headers ["User-Agent" ] = opt .ClientInfo .String ()
176-
177182 query := u .Query ()
178183 if len (opt .Auth .Database ) > 0 {
179184 query .Set ("database" , opt .Auth .Database )
@@ -225,6 +230,7 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
225230 }
226231
227232 conn := & httpConnect {
233+ opt : opt ,
228234 client : & http.Client {
229235 Transport : t ,
230236 },
@@ -234,7 +240,6 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
234240 blockCompressor : compress .NewWriter (compress .Level (opt .Compression .Level ), compress .Method (opt .Compression .Method )),
235241 compressionPool : compressionPool ,
236242 blockBufferSize : opt .BlockBufferSize ,
237- headers : headers ,
238243 }
239244 location , err := conn .readTimeZone (ctx )
240245 if err != nil {
@@ -251,6 +256,7 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
251256 }
252257
253258 return & httpConnect {
259+ opt : opt ,
254260 client : & http.Client {
255261 Transport : t ,
256262 },
@@ -261,11 +267,11 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
261267 compressionPool : compressionPool ,
262268 location : location ,
263269 blockBufferSize : opt .BlockBufferSize ,
264- headers : headers ,
265270 }, nil
266271}
267272
268273type httpConnect struct {
274+ opt * Options
269275 url * url.URL
270276 client * http.Client
271277 location * time.Location
@@ -274,7 +280,6 @@ type httpConnect struct {
274280 blockCompressor * compress.Writer
275281 compressionPool Pool [HTTPReaderWriter ]
276282 blockBufferSize uint8
277- headers map [string ]string
278283}
279284
280285func (h * httpConnect ) isBad () bool {
@@ -456,9 +461,12 @@ func (h *httpConnect) createRequest(ctx context.Context, requestUrl string, read
456461 if err != nil {
457462 return nil , err
458463 }
464+
465+ applyOptionsToRequest (req , h .opt )
459466 for k , v := range headers {
460467 req .Header .Add (k , v )
461468 }
469+
462470 var query url.Values
463471 if options != nil {
464472 query = req .URL .Query ()
0 commit comments