-
Notifications
You must be signed in to change notification settings - Fork 259
/
utils.go
320 lines (276 loc) · 7.16 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"bytes"
"compress/gzip"
"fmt"
"hash/fnv"
"io"
"net/http"
"sort"
"strconv"
"strings"
"github.com/contentsquare/chproxy/chdecompressor"
"github.com/contentsquare/chproxy/log"
)
func respondWith(rw http.ResponseWriter, err error, status int) {
log.ErrorWithCallDepth(err, 1)
rw.WriteHeader(status)
fmt.Fprintf(rw, "%s\n", err)
}
var defaultUser = "default"
// getAuth retrieves auth credentials from request
// according to CH documentation @see "https://clickhouse.yandex/docs/en/interfaces/http/"
func getAuth(req *http.Request) (string, string) {
// check X-ClickHouse- headers
name := req.Header.Get("X-ClickHouse-User")
pass := req.Header.Get("X-ClickHouse-Key")
if name != "" {
return name, pass
}
// if header is empty - check basicAuth
if name, pass, ok := req.BasicAuth(); ok {
return name, pass
}
// if basicAuth is empty - check URL params `user` and `password`
params := req.URL.Query()
if name := params.Get("user"); name != "" {
pass := params.Get("password")
return name, pass
}
// if still no credentials - treat it as `default` user request
return defaultUser, ""
}
// getSessionId retrieves session id
func getSessionId(req *http.Request) string {
params := req.URL.Query()
sessionId := params.Get("session_id")
return sessionId
}
// getSessionId retrieves session id
func getSessionTimeout(req *http.Request) int {
params := req.URL.Query()
sessionTimeout, err := strconv.Atoi(params.Get("session_timeout"))
if err == nil && sessionTimeout > 0 {
return sessionTimeout
}
return 60
}
// getQuerySnippet returns query snippet.
//
// getQuerySnippet must be called only for error reporting.
func getQuerySnippet(req *http.Request) string {
query := req.URL.Query().Get("query")
body := getQuerySnippetFromBody(req)
if len(query) != 0 && len(body) != 0 {
query += "\n"
}
return query + body
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func getQuerySnippetFromBody(req *http.Request) string {
if req.Body == nil {
return ""
}
crc, ok := req.Body.(*cachedReadCloser)
if !ok {
crc = &cachedReadCloser{
ReadCloser: req.Body,
}
}
// 'read' request body, so it traps into to crc.
// Ignore any errors, since getQuerySnippet is called only
// during error reporting.
// Temporary solution: Quick and dirty way to work with the request body.
// TODO: Create an original copy of req.Body and work with the copy to avoid altering the original request.
// This current approach consumes the req.Body content with io.Copy(io.Discard, crc) to reset the internal state of crc.
// However, it is not the most efficient or safest method, as it modifies the original req.Body.
io.Copy(io.Discard, crc) // nolint
data := crc.String()
// Here, we attempt to restore req.Body by wrapping the string data in a ReadCloser.
// This is part of the temporary solution and should be replaced with a more robust method that does not consume the original req.Body.
req.Body = io.NopCloser(strings.NewReader(data))
u := getDecompressor(req)
if u == nil {
return data
}
bs := bytes.NewBufferString(data)
b, err := u.decompress(bs)
if err == nil {
return string(b)
}
// It is better to return partially decompressed data instead of an empty string.
if len(b) > 0 {
return string(b)
}
// The data failed to be decompressed. Return compressed data
// instead of an empty string.
return data
}
// getFullQuery returns full query from req.
func getFullQuery(req *http.Request) ([]byte, error) {
var result bytes.Buffer
if req.URL.Query().Get("query") != "" {
result.WriteString(req.URL.Query().Get("query"))
}
body, err := getFullQueryFromBody(req)
if err != nil {
return nil, err
}
if result.Len() != 0 && len(body) != 0 {
result.WriteByte('\n')
}
result.Write(body)
return result.Bytes(), nil
}
func getFullQueryFromBody(req *http.Request) ([]byte, error) {
if req.Body == nil {
return nil, nil
}
data, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
// restore body for further reading
req.Body = io.NopCloser(bytes.NewBuffer(data))
u := getDecompressor(req)
if u == nil {
return data, nil
}
br := bytes.NewReader(data)
b, err := u.decompress(br)
if err != nil {
return nil, fmt.Errorf("cannot uncompress query: %w", err)
}
return b, nil
}
var cachableStatements = []string{"SELECT", "WITH"}
// canCacheQuery returns true if q can be cached.
func canCacheQuery(q []byte) bool {
q = skipLeadingComments(q)
for _, statement := range cachableStatements {
if len(q) < len(statement) {
continue
}
l := bytes.ToUpper(q[:len(statement)])
if bytes.HasPrefix(l, []byte(statement)) {
return true
}
}
return false
}
//nolint:cyclop // No clean way to split this.
func skipLeadingComments(q []byte) []byte {
for len(q) > 0 {
switch q[0] {
case '\t', '\n', '\v', '\f', '\r', ' ':
q = q[1:]
case '-':
if len(q) < 2 || q[1] != '-' {
return q
}
// skip `-- comment`
n := bytes.IndexByte(q, '\n')
if n < 0 {
return nil
}
q = q[n+1:]
case '/':
if len(q) < 2 || q[1] != '*' {
return q
}
// skip `/* comment */`
for {
n := bytes.IndexByte(q, '*')
if n < 0 {
return nil
}
q = q[n+1:]
if len(q) == 0 {
return nil
}
if q[0] == '/' {
q = q[1:]
break
}
}
default:
return q
}
}
return nil
}
// splits header string in sorted slice
func sortHeader(header string) string {
h := strings.Split(header, ",")
for i, v := range h {
h[i] = strings.TrimSpace(v)
}
sort.Strings(h)
return strings.Join(h, ",")
}
func getDecompressor(req *http.Request) decompressor {
if req.Header.Get("Content-Encoding") == "gzip" {
return gzipDecompressor{}
}
if req.URL.Query().Get("decompress") == "1" {
return chDecompressor{}
}
return nil
}
type decompressor interface {
decompress(r io.Reader) ([]byte, error)
}
type gzipDecompressor struct{}
func (dc gzipDecompressor) decompress(r io.Reader) ([]byte, error) {
gr, err := gzip.NewReader(r)
if err != nil {
return nil, fmt.Errorf("cannot ungzip query: %w", err)
}
return io.ReadAll(gr)
}
type chDecompressor struct{}
func (dc chDecompressor) decompress(r io.Reader) ([]byte, error) {
lr := chdecompressor.NewReader(r)
return io.ReadAll(lr)
}
func calcMapHash(m map[string]string) (uint32, error) {
if len(m) == 0 {
return 0, nil
}
var keys []string
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
h := fnv.New32a()
for _, k := range keys {
str := fmt.Sprintf("%s=%s&", k, m[k])
_, err := h.Write([]byte(str))
if err != nil {
return 0, err
}
}
return h.Sum32(), nil
}
func calcCredentialHash(user string, pwd string) (uint32, error) {
h := fnv.New32a()
_, err := h.Write([]byte(user + pwd))
return h.Sum32(), err
}
// Function to read the request body and return it as a byte slice.
// It also restores the req.Body to be used again.
func readAndRestoreRequestBody(req *http.Request) ([]byte, error) {
// Read the entire request body.
body, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
// Restore the req.Body with a new reader for the original content.
req.Body = io.NopCloser(bytes.NewReader(body))
// Return the read body.
return body, nil
}