Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional HTTP headers; For HTTPS connections, pass auth as headers #811

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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