Skip to content

Commit

Permalink
use headers consisently for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerwizard committed Nov 19, 2022
1 parent 2588a36 commit 9c21581
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
52 changes: 26 additions & 26 deletions conn_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
u.User = url.User(opt.Auth.Username)
}
} else if opt.TLS != nil && len(opt.Auth.Username) > 0 {
headers["X-Clickhouse-User"] = opt.Auth.Username
headers["X-ClickHouse-User"] = opt.Auth.Username
if len(opt.Auth.Password) > 0 {
headers["X-Clickhouse-Key"] = opt.Auth.Password
headers["X-ClickHouse-Key"] = opt.Auth.Password
}
}

Expand Down Expand Up @@ -205,13 +205,13 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
client: &http.Client{
Transport: t,
},
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
blockBufferSize: opt.BlockBufferSize,
additionalHttpHeaders: headers,
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
blockBufferSize: opt.BlockBufferSize,
headers: headers,
}
location, err := conn.readTimeZone(ctx)
if err != nil {
Expand All @@ -231,27 +231,27 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
client: &http.Client{
Transport: t,
},
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
location: location,
blockBufferSize: opt.BlockBufferSize,
additionalHttpHeaders: headers,
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
location: location,
blockBufferSize: opt.BlockBufferSize,
headers: headers,
}, nil
}

type httpConnect struct {
url *url.URL
client *http.Client
location *time.Location
buffer *chproto.Buffer
compression CompressionMethod
blockCompressor *compress.Writer
compressionPool Pool[HTTPReaderWriter]
blockBufferSize uint8
additionalHttpHeaders map[string]string
url *url.URL
client *http.Client
location *time.Location
buffer *chproto.Buffer
compression CompressionMethod
blockCompressor *compress.Writer
compressionPool Pool[HTTPReaderWriter]
blockBufferSize uint8
headers map[string]string
}

func (h *httpConnect) isBad() bool {
Expand Down
2 changes: 1 addition & 1 deletion conn_http_async_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (h *httpConnect) asyncInsert(ctx context.Context, query string, wait bool)
if wait {
options.settings["wait_for_async_insert"] = 1
}
res, err := h.sendQuery(ctx, strings.NewReader(query), &options, nil)
res, err := h.sendQuery(ctx, strings.NewReader(query), &options, h.headers)
if res != nil {
defer res.Body.Close()
// we don't care about result, so just discard it to reuse connection
Expand Down
3 changes: 3 additions & 0 deletions conn_http_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func (b *httpBatch) Send() (err error) {

options.settings["query"] = b.query
headers["Content-Type"] = "application/octet-stream"
for k, v := range b.conn.headers {
headers[k] = v
}
res, err := b.conn.sendQuery(b.ctx, r, &options, headers)

if res != nil {
Expand Down
2 changes: 1 addition & 1 deletion conn_http_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (h *httpConnect) exec(ctx context.Context, query string, args ...interface{

options := queryOptions(ctx)

res, err := h.sendQuery(ctx, strings.NewReader(query), &options, nil)
res, err := h.sendQuery(ctx, strings.NewReader(query), &options, h.headers)
if res != nil {
defer res.Body.Close()
// we don't care about result, so just discard it to reuse connection
Expand Down
2 changes: 1 addition & 1 deletion conn_http_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *httpConnect) query(ctx context.Context, release func(*connect, error),
headers["Accept-Encoding"] = h.compression.String()
}

for k, v := range h.additionalHttpHeaders {
for k, v := range h.headers {
headers[k] = v
}

Expand Down

0 comments on commit 9c21581

Please sign in to comment.