-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
173 lines (140 loc) · 3.46 KB
/
transport.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
package clickhouse
import (
"bytes"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
)
const (
httpTransportBodyType = "text/plain"
)
// Transport interface, Conn store this interface inside.
//
// query.Exec -> conn.Exec -> transport.Exec -> Clickhouse Http
type Transport interface {
Exec(host, params string, q Query, readOnly bool) (res string, err error)
}
// HttpTransport use http.Client for connections
type HttpTransport struct {
client *http.Client
}
// NewHttpTransport creates default http transport with 30sec timeout
func NewHttpTransport() HttpTransport {
default_client := &http.Client{
Timeout: 30 * time.Second,
}
return NewCustomTransport(default_client)
}
// NewCustomTransport creates HttpTransport with custom client. Usefull for special timeouts, tls config etc.
func NewCustomTransport(client *http.Client) HttpTransport {
return HttpTransport{
client: client,
}
}
// Exec make http request with all params. readOnly param controls GET/POST request
func (t HttpTransport) Exec(host, params string, q Query, readOnly bool) (res string, err error) {
var resp *http.Response
query := prepareHttp(q.Stmt, q.args)
if readOnly {
if len(query) > 0 {
query = "?query=" + query
}
if len(params) > 0 {
if len(query) > 0 {
query += "&" + params
} else {
query += "?" + params
}
}
resp, err = t.client.Get(host + query)
} else {
var req *http.Request
// Set global parameters for query, like: user, password, max_memory_limit, etc.
// But it skips already defined params.
req, err = prepareExecPostRequest(host, params, q)
if err != nil {
return "", err
}
resp, err = t.client.Do(req)
}
if err != nil {
return "", err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
return buf.String(), err
}
func prepareExecPostRequest(host, paramsCon string, q Query) (*http.Request, error) {
query := prepareHttp(q.Stmt, q.args)
var req *http.Request
var err error = nil
if len(q.externals) > 0 {
if len(query) > 0 {
query = "?query=" + url.QueryEscape(query)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, ext := range q.externals {
query = query + "&" + ext.Name + "_structure=" + url.QueryEscape(ext.Structure)
part, err := writer.CreateFormFile(ext.Name, ext.Name)
if err != nil {
return nil, err
}
_, err = part.Write(ext.Data)
if err != nil {
return nil, err
}
}
params := q.params.Encode()
if len(params) > 0 {
query += "&" + params
}
if len(paramsCon) > 0 {
query += "&" + paramsCon
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err = http.NewRequest("POST", host+query, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
} else {
req, err = http.NewRequest("POST", host+"?"+paramsCon, strings.NewReader(query))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", httpTransportBodyType)
}
return req, err
}
func prepareHttp(stmt string, args []interface{}) string {
if len(args) == 0 {
return stmt
}
var res []byte
buf := []byte(stmt)
res = make([]byte, 0)
k := 0
skip_to := -1
for key, ch := range buf {
if skip_to != -1 && key < skip_to {
continue
} else {
skip_to = -1
}
if ch == ':' && stmt[key:key+7] == ":value:" {
res = append(res, []byte(marshal(args[k]))...)
k++
skip_to = key + 7
} else {
res = append(res, ch)
}
}
return string(res)
}