-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
383 lines (336 loc) · 11.2 KB
/
http.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package http
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
// ExecuteResponse represents the JSON returned by /db/execute.
type ExecuteResponse struct {
Results []ExecuteResult `json:"results"`
Time float64 `json:"time"`
SequenceNumber int64 `json:"sequence_number,omitempty"`
}
// ExecuteResult is an element of ExecuteResponse.Results.
type ExecuteResult struct {
LastInsertID int64 `json:"last_insert_id,omitempty"`
RowsAffected int64 `json:"rows_affected,omitempty"`
Time float64 `json:"time,omitempty"`
Error string `json:"error,omitempty"`
}
// QueryResponse represents the JSON returned by /db/query in the default (non-associative) form.
type QueryResponse struct {
Results []QueryResult `json:"results"`
Time float64 `json:"time"`
}
// QueryResult is an element of QueryResponse.Results.
type QueryResult struct {
Columns []string `json:"columns,omitempty"`
Types []string `json:"types,omitempty"`
Values [][]any `json:"values,omitempty"`
Time float64 `json:"time,omitempty"`
Error string `json:"error,omitempty"`
}
// RequestResponse represents the JSON returned by /db/request.
type RequestResponse struct {
Results []RequestResult `json:"results"`
Time float64 `json:"time"`
}
// RequestResult is an element of RequestResponse.Results.
// It may include either Query-like results or Execute-like results, or an error.
type RequestResult struct {
// Same fields as QueryResult plus ExecuteResult fields.
// If read-only, LastInsertID and RowsAffected would be empty;
// if write-only, Columns and Values would be empty.
Columns []string `json:"columns,omitempty"`
Types []string `json:"types,omitempty"`
Values [][]any `json:"values,omitempty"`
LastInsertID *int64 `json:"last_insert_id,omitempty"`
RowsAffected *int64 `json:"rows_affected,omitempty"`
Error string `json:"error,omitempty"`
Time float64 `json:"time,omitempty"`
}
// Client is the main type through which rqlite is accessed.
type Client struct {
httpClient *http.Client
executeURL string
queryURL string
requestURL string
backupURL string
loadURL string
bootURL string
statusURL string
expvarURL string
nodesURL string
readyURL string
basicAuthUser string
basicAuthPass string
}
// NewClient creates a new Client with default settings. If httpClient is nil,
// the the default client is used.
func NewClient(baseURL string, httpClient *http.Client) *Client {
cl := &Client{
httpClient: httpClient,
executeURL: baseURL + "/db/execute",
queryURL: baseURL + "/db/query",
requestURL: baseURL + "/db/request",
backupURL: baseURL + "/db/backup",
loadURL: baseURL + "/db/load",
bootURL: baseURL + "/boot",
statusURL: baseURL + "/status",
expvarURL: baseURL + "/debug/vars",
nodesURL: baseURL + "/nodes",
readyURL: baseURL + "/readyz",
}
if cl.httpClient == nil {
cl.httpClient = DefaultClient()
}
return cl
}
// SetBasicAuth configures the client to use Basic Auth for all subsequent requests.
// Pass empty strings to disable Basic Auth.
func (c *Client) SetBasicAuth(username, password string) {
c.basicAuthUser = username
c.basicAuthPass = password
}
// ExecuteSingle performs a single write operation (INSERT, UPDATE, DELETE) using /db/execute.
func (c *Client) ExecuteSingle(ctx context.Context, statement string) (*ExecuteResponse, error) {
statements := SQLStatements{
{SQL: statement},
}
return c.Execute(ctx, statements, nil)
}
// Execute executes one or more SQL statements (INSERT, UPDATE, DELETE) using /db/execute.
func (c *Client) Execute(ctx context.Context, statements SQLStatements, opts *ExecuteOptions) (*ExecuteResponse, error) {
body, err := statements.MarshalJSON()
if err != nil {
return nil, err
}
queryParams, err := MakeURLValues(opts)
if err != nil {
return nil, err
}
resp, err := c.doJSONPostRequest(ctx, c.executeURL, queryParams, bytes.NewReader(body))
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, respBody)
}
var executeResp ExecuteResponse
if err := json.Unmarshal(respBody, &executeResp); err != nil {
return nil, err
}
return &executeResp, nil
}
// QuerySingle performs a single read operation (SELECT) using /db/query.
func (c *Client) QuerySingle(ctx context.Context, statement string) (*QueryResponse, error) {
statements := SQLStatements{
{SQL: statement},
}
return c.Query(ctx, statements, nil)
}
// Query performs a read operation (SELECT) using /db/query.
func (c *Client) Query(ctx context.Context, statements SQLStatements, opts *QueryOptions) (*QueryResponse, error) {
body, err := statements.MarshalJSON()
if err != nil {
return nil, err
}
queryParams, err := MakeURLValues(opts)
if err != nil {
return nil, err
}
resp, err := c.doJSONPostRequest(ctx, c.queryURL, queryParams, bytes.NewReader(body))
if err != nil {
return nil, err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, respBody)
}
var queryResponse QueryResponse
if err := json.Unmarshal(respBody, &queryResponse); err != nil {
return nil, err
}
return &queryResponse, nil
}
// RequestSingle sends a single statement using /db/request.
func (c *Client) RequestSingle(ctx context.Context, statement string) (*RequestResponse, error) {
statements := SQLStatements{
{SQL: statement},
}
return c.Request(ctx, statements, nil)
}
// Request sends both read and write statements in a single request using /db/request.
// This method determines read vs. write by inspecting the statements.
func (c *Client) Request(ctx context.Context, statements SQLStatements, opts *RequestOptions) (*RequestResponse, error) {
body, err := statements.MarshalJSON()
if err != nil {
return nil, err
}
reqParams, err := MakeURLValues(opts)
if err != nil {
return nil, err
}
resp, err := c.doJSONPostRequest(ctx, c.requestURL, reqParams, bytes.NewReader(body))
if err != nil {
return nil, err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, respBody)
}
var reqResp RequestResponse
if err := json.Unmarshal(respBody, &reqResp); err != nil {
return nil, err
}
return &reqResp, nil
}
// Backup requests a copy of the SQLite database from the node. The caller must close the
// returned ReadCloser when done, regardless of any error.
func (c *Client) Backup(ctx context.Context, opts BackupOptions) (io.ReadCloser, error) {
reqParams, err := MakeURLValues(opts)
if err != nil {
return nil, err
}
resp, err := c.doGetRequest(ctx, c.backupURL, reqParams)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return resp.Body, nil
}
// Load streams data from r into the node, to load or restore data. Load automatically
// detects the format of the data, and can handle both plain text and SQLite binary data.
func (c *Client) Load(ctx context.Context, r io.Reader, opts LoadOptions) error {
params, err := MakeURLValues(opts)
if err != nil {
return err
}
_ = params
first13 := make([]byte, 13)
_, err = r.Read(first13)
if err != nil {
return err
}
if validSQLiteData(first13) {
_, err = c.doOctetStreamPostRequest(ctx, c.loadURL, params, io.MultiReader(bytes.NewReader(first13), r))
} else {
_, err = c.doPlainPostRequest(ctx, c.loadURL, params, io.MultiReader(bytes.NewReader(first13), r))
}
return err
}
// Boot streams a raw SQLite file into a single-node system, effectively initializing
// the underlying SQLite store from scratch. This is done via a POST to /boot.
func (c *Client) Boot(ctx context.Context, r io.Reader) error {
_, err := c.doOctetStreamPostRequest(ctx, c.bootURL, nil, r)
return err
}
// Status returns the status of the node.
func (c *Client) Status(ctx context.Context) (json.RawMessage, error) {
resp, err := c.doGetRequest(ctx, c.statusURL, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return json.RawMessage(b), nil
}
// Expvar returns the expvar data from the node.
func (c *Client) Expvar(ctx context.Context) (json.RawMessage, error) {
resp, err := c.doGetRequest(ctx, c.expvarURL, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return json.RawMessage(b), nil
}
// Nodes returns the list of known nodes in the cluster.
func (c *Client) Nodes(ctx context.Context) (json.RawMessage, error) {
resp, err := c.doGetRequest(ctx, c.nodesURL, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return json.RawMessage(b), nil
}
// Ready returns the readiness of the node. The caller must close the returned ReadCloser
// when done, regardless of any error.
func (c *Client) Ready(ctx context.Context) (io.ReadCloser, error) {
resp, err := c.doGetRequest(ctx, c.readyURL, nil)
if err != nil {
return nil, err
}
return resp.Body, nil
}
// Close can clean up any long-lived resources owned by the Client, if needed.
func (c *Client) Close() error {
return nil
}
func (c *Client) doGetRequest(ctx context.Context, url string, values url.Values) (*http.Response, error) {
return c.doRequest(ctx, "GET", url, "", values, nil)
}
func (c *Client) doJSONPostRequest(ctx context.Context, url string, values url.Values, body io.Reader) (*http.Response, error) {
return c.doRequest(ctx, "POST", url, "application/json", values, body)
}
func (c *Client) doOctetStreamPostRequest(ctx context.Context, url string, values url.Values, body io.Reader) (*http.Response, error) {
return c.doRequest(ctx, "POST", url, "application/octet-stream", values, body)
}
func (c *Client) doPlainPostRequest(ctx context.Context, url string, values url.Values, body io.Reader) (*http.Response, error) {
return c.doRequest(ctx, "POST", url, "text/plain", values, body)
}
// doRequest builds and executes an HTTP request, returning the response.
func (c *Client) doRequest(ctx context.Context, method, url string, contentTpe string, values url.Values, body io.Reader) (*http.Response, error) {
fullURL := url
if values != nil {
fullURL += "?" + values.Encode()
}
req, err := http.NewRequestWithContext(ctx, method, fullURL, body)
if err != nil {
return nil, err
}
if contentTpe != "" {
req.Header.Set("Content-Type", contentTpe)
}
// If Basic Auth is configured, add an Authorization header
if c.basicAuthUser != "" || c.basicAuthPass != "" {
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", c.basicAuthUser, c.basicAuthPass)))
req.Header.Set("Authorization", "Basic "+auth)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func validSQLiteData(b []byte) bool {
return len(b) > 13 && string(b[0:13]) == "SQLite format"
}