Skip to content

Commit

Permalink
Merge pull request #811 from tonickkozlov/main
Browse files Browse the repository at this point in the history
Allow optional HTTP headers; For HTTPS connections, pass auth as headers
  • Loading branch information
gingerwizard authored Nov 17, 2022
2 parents bfd9f33 + 2bc5866 commit 2588a36
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
3 changes: 2 additions & 1 deletion clickhouse_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ type Options struct {
MaxIdleConns int // default 5
ConnMaxLifetime time.Duration // default 1 hour
ConnOpenStrategy ConnOpenStrategy
BlockBufferSize uint8 // default 2 - can be overwritten on query
HttpHeaders map[string]string // set additional headers on HTTP requests
BlockBufferSize uint8 // default 2 - can be overwritten on query

scheme string
ReadTimeout time.Duration
Expand Down
57 changes: 35 additions & 22 deletions conn_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,22 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
Host: addr,
}

if len(opt.Auth.Username) > 0 {
headers := make(map[string]string)
for k, v := range opt.HttpHeaders {
headers[k] = v
}

if opt.TLS == nil && len(opt.Auth.Username) > 0 {
if len(opt.Auth.Password) > 0 {
u.User = url.UserPassword(opt.Auth.Username, opt.Auth.Password)
} else {
u.User = url.User(opt.Auth.Username)
}
} else if opt.TLS != nil && len(opt.Auth.Username) > 0 {
headers["X-Clickhouse-User"] = opt.Auth.Username
if len(opt.Auth.Password) > 0 {
headers["X-Clickhouse-Key"] = opt.Auth.Password
}
}

query := u.Query()
Expand Down Expand Up @@ -195,12 +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,
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
blockBufferSize: opt.BlockBufferSize,
additionalHttpHeaders: headers,
}
location, err := conn.readTimeZone(ctx)
if err != nil {
Expand All @@ -220,25 +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,
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
location: location,
blockBufferSize: opt.BlockBufferSize,
additionalHttpHeaders: 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
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
}

func (h *httpConnect) isBad() bool {
Expand Down
9 changes: 7 additions & 2 deletions conn_http_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"bytes"
"context"
"errors"
chproto "github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
"io"
"strings"

chproto "github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
)

// release is ignored, because http used by std with empty release function
Expand All @@ -43,6 +44,10 @@ func (h *httpConnect) query(ctx context.Context, release func(*connect, error),
headers["Accept-Encoding"] = h.compression.String()
}

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

res, err := h.sendQuery(ctx, strings.NewReader(query), &options, headers)
if err != nil {
return nil, err
Expand Down

0 comments on commit 2588a36

Please sign in to comment.