@@ -119,6 +119,47 @@ 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 (ctx context.Context , req * http.Request , opt * Options ) error {
124+ jwt := queryOptionsJWT (ctx )
125+ useJWT := jwt != "" || useJWTAuth (opt )
126+
127+ if opt .TLS != nil && useJWT {
128+ if jwt == "" {
129+ var err error
130+ jwt , err = opt .GetJWT (ctx )
131+ if err != nil {
132+ return fmt .Errorf ("failed to get JWT: %w" , err )
133+ }
134+ }
135+
136+ req .Header .Set ("Authorization" , "Bearer " + jwt )
137+ } else if opt .TLS != nil && len (opt .Auth .Username ) > 0 {
138+ req .Header .Set ("X-ClickHouse-User" , opt .Auth .Username )
139+ if len (opt .Auth .Password ) > 0 {
140+ req .Header .Set ("X-ClickHouse-Key" , opt .Auth .Password )
141+ req .Header .Set ("X-ClickHouse-SSL-Certificate-Auth" , "off" )
142+ } else {
143+ req .Header .Set ("X-ClickHouse-SSL-Certificate-Auth" , "on" )
144+ }
145+ } else if opt .TLS == nil && len (opt .Auth .Username ) > 0 {
146+ if len (opt .Auth .Password ) > 0 {
147+ req .URL .User = url .UserPassword (opt .Auth .Username , opt .Auth .Password )
148+
149+ } else {
150+ req .URL .User = url .User (opt .Auth .Username )
151+ }
152+ }
153+
154+ req .Header .Set ("User-Agent" , opt .ClientInfo .String ())
155+
156+ for k , v := range opt .HttpHeaders {
157+ req .Header .Set (k , v )
158+ }
159+
160+ return nil
161+ }
162+
122163func dialHttp (ctx context.Context , addr string , num int , opt * Options ) (* httpConnect , error ) {
123164 var debugf = func (format string , v ... any ) {}
124165 if opt .Debug {
@@ -151,29 +192,6 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
151192 Path : opt .HttpUrlPath ,
152193 }
153194
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-
177195 query := u .Query ()
178196 if len (opt .Auth .Database ) > 0 {
179197 query .Set ("database" , opt .Auth .Database )
@@ -225,6 +243,7 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
225243 }
226244
227245 conn := & httpConnect {
246+ opt : opt ,
228247 client : & http.Client {
229248 Transport : t ,
230249 },
@@ -234,7 +253,6 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
234253 blockCompressor : compress .NewWriter (compress .Level (opt .Compression .Level ), compress .Method (opt .Compression .Method )),
235254 compressionPool : compressionPool ,
236255 blockBufferSize : opt .BlockBufferSize ,
237- headers : headers ,
238256 }
239257 location , err := conn .readTimeZone (ctx )
240258 if err != nil {
@@ -251,6 +269,7 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
251269 }
252270
253271 return & httpConnect {
272+ opt : opt ,
254273 client : & http.Client {
255274 Transport : t ,
256275 },
@@ -261,11 +280,11 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
261280 compressionPool : compressionPool ,
262281 location : location ,
263282 blockBufferSize : opt .BlockBufferSize ,
264- headers : headers ,
265283 }, nil
266284}
267285
268286type httpConnect struct {
287+ opt * Options
269288 url * url.URL
270289 client * http.Client
271290 location * time.Location
@@ -274,7 +293,6 @@ type httpConnect struct {
274293 blockCompressor * compress.Writer
275294 compressionPool Pool [HTTPReaderWriter ]
276295 blockBufferSize uint8
277- headers map [string ]string
278296}
279297
280298func (h * httpConnect ) isBad () bool {
@@ -456,9 +474,16 @@ func (h *httpConnect) createRequest(ctx context.Context, requestUrl string, read
456474 if err != nil {
457475 return nil , err
458476 }
477+
478+ err = applyOptionsToRequest (ctx , req , h .opt )
479+ if err != nil {
480+ return nil , err
481+ }
482+
459483 for k , v := range headers {
460484 req .Header .Add (k , v )
461485 }
486+
462487 var query url.Values
463488 if options != nil {
464489 query = req .URL .Query ()
0 commit comments