forked from dgraph-io/dgraph
/
http.go
407 lines (347 loc) · 9.31 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/dgraph-io/dgraph/edgraph"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/protos/api"
"github.com/dgraph-io/dgraph/query"
"github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x"
)
func allowed(method string) bool {
return method == http.MethodPost || method == http.MethodPut
}
func extractStartTs(urlPath string) (uint64, error) {
params := strings.Split(strings.TrimPrefix(urlPath, "/"), "/")
switch l := len(params); l {
case 1:
// When startTs is not supplied. /query or /mutate
return 0, nil
case 2:
ts, err := strconv.ParseUint(params[1], 0, 64)
if err != nil {
return 0, fmt.Errorf("Error: %+v while parsing StartTs path parameter as uint64", err)
}
return ts, nil
default:
return 0, x.Errorf("Incorrect no. of path parameters. Expected 1 or 2. Got: %+v", l)
}
return 0, nil
}
// This method should just build the request and proxy it to the Query method of dgraph.Server.
// It can then encode the response as appropriate before sending it back to the user.
func queryHandler(w http.ResponseWriter, r *http.Request) {
x.AddCorsHeaders(w)
w.Header().Set("Content-Type", "application/json")
if r.Method == "OPTIONS" {
return
}
if !allowed(r.Method) {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}
req := api.Request{}
ts, err := extractStartTs(r.URL.Path)
if err != nil {
x.SetStatus(w, err.Error(), x.ErrorInvalidRequest)
return
}
req.StartTs = ts
linRead := r.Header.Get("X-Dgraph-LinRead")
if linRead != "" {
lr := make(map[uint32]uint64)
if err := json.Unmarshal([]byte(linRead), &lr); err != nil {
x.SetStatus(w, x.ErrorInvalidRequest,
"Error while unmarshalling LinRead header into map")
return
}
req.LinRead = &api.LinRead{
Ids: lr,
}
}
if vars := r.Header.Get("X-Dgraph-Vars"); vars != "" {
req.Vars = map[string]string{}
if err := json.Unmarshal([]byte(vars), &req.Vars); err != nil {
x.SetStatus(w, x.ErrorInvalidRequest,
"Error while unmarshalling Vars header into map")
return
}
}
defer r.Body.Close()
q, err := ioutil.ReadAll(r.Body)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
req.Query = string(q)
d := r.URL.Query().Get("debug")
ctx := context.WithValue(context.Background(), "debug", d)
resp, err := (&edgraph.Server{}).Query(ctx, &req)
if err != nil {
x.SetStatusWithData(w, x.ErrorInvalidRequest, err.Error())
return
}
response := map[string]interface{}{}
e := query.Extensions{
Txn: resp.Txn,
Latency: resp.Latency,
}
response["extensions"] = e
// User can either ask for schema or have a query.
if len(resp.Schema) > 0 {
sort.Slice(resp.Schema, func(i, j int) bool {
return resp.Schema[i].Predicate < resp.Schema[j].Predicate
})
js, err := json.Marshal(resp.Schema)
if err != nil {
x.SetStatusWithData(w, x.Error, "Unable to marshal schema")
return
}
mp := map[string]interface{}{}
mp["schema"] = json.RawMessage(string(js))
response["data"] = mp
} else {
response["data"] = json.RawMessage(string(resp.Json))
}
if js, err := json.Marshal(response); err == nil {
w.Write(js)
} else {
x.SetStatusWithData(w, x.Error, "Unable to marshal response")
}
}
func mutationHandler(w http.ResponseWriter, r *http.Request) {
x.AddCorsHeaders(w)
w.Header().Set("Content-Type", "application/json")
if r.Method == "OPTIONS" {
return
}
if !allowed(r.Method) {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}
defer r.Body.Close()
m, err := ioutil.ReadAll(r.Body)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
parseStart := time.Now()
mu, err := gql.ParseMutation(string(m))
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
parseEnd := time.Now()
// Maybe rename it so that default is CommitNow.
commit := r.Header.Get("X-Dgraph-CommitNow")
if commit != "" {
c, err := strconv.ParseBool(commit)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest,
"Error while parsing Commit header as bool")
return
}
mu.CommitNow = c
}
ts, err := extractStartTs(r.URL.Path)
if err != nil {
x.SetStatus(w, err.Error(), x.ErrorInvalidRequest)
return
}
mu.StartTs = ts
resp, err := (&edgraph.Server{}).Mutate(context.Background(), mu)
if err != nil {
x.SetStatusWithData(w, x.ErrorInvalidRequest, err.Error())
return
}
resp.Latency.ParsingNs = uint64(parseEnd.Sub(parseStart).Nanoseconds())
e := query.Extensions{
Txn: resp.Context,
Latency: resp.Latency,
}
// Don't send keys array which is part of txn context if its commit immediately.
if mu.CommitNow {
e.Txn.Keys = e.Txn.Keys[:0]
}
response := map[string]interface{}{}
response["extensions"] = e
mp := map[string]interface{}{}
mp["code"] = x.Success
mp["message"] = "Done"
mp["uids"] = resp.Uids
response["data"] = mp
js, err := json.Marshal(response)
if err != nil {
x.SetStatusWithData(w, x.Error, err.Error())
return
}
w.Write(js)
}
func commitHandler(w http.ResponseWriter, r *http.Request) {
x.AddCorsHeaders(w)
w.Header().Set("Content-Type", "application/json")
if r.Method == "OPTIONS" {
return
}
if !allowed(r.Method) {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}
resp := &api.Assigned{}
tc := &api.TxnContext{}
resp.Context = tc
ts, err := extractStartTs(r.URL.Path)
if err != nil {
x.SetStatus(w, err.Error(), x.ErrorInvalidRequest)
return
}
if ts == 0 {
x.SetStatus(w, x.ErrorInvalidRequest,
"StartTs path parameter is mandatory while trying to commit")
return
}
tc.StartTs = ts
// Keys are sent as an array in the body.
defer r.Body.Close()
keys, err := ioutil.ReadAll(r.Body)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
var encodedKeys []string
if err := json.Unmarshal([]byte(keys), &encodedKeys); err != nil {
x.SetStatus(w, x.ErrorInvalidRequest,
"Error while unmarshalling keys header into array")
return
}
tc.Keys = encodedKeys
cts, err := worker.CommitOverNetwork(context.Background(), tc)
if err != nil {
x.SetStatus(w, x.Error, err.Error())
return
}
resp.Context.CommitTs = cts
e := query.Extensions{
Txn: resp.Context,
}
e.Txn.Keys = e.Txn.Keys[:0]
response := map[string]interface{}{}
response["extensions"] = e
mp := map[string]interface{}{}
mp["code"] = x.Success
mp["message"] = "Done"
response["data"] = mp
js, err := json.Marshal(response)
if err != nil {
x.SetStatusWithData(w, x.Error, err.Error())
return
}
w.Write(js)
}
func abortHandler(w http.ResponseWriter, r *http.Request) {
x.AddCorsHeaders(w)
w.Header().Set("Content-Type", "application/json")
if r.Method == "OPTIONS" {
return
}
if !allowed(r.Method) {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}
resp := &api.Assigned{}
tc := &api.TxnContext{}
resp.Context = tc
ts, err := extractStartTs(r.URL.Path)
if err != nil {
x.SetStatus(w, err.Error(), x.ErrorInvalidRequest)
return
}
if ts == 0 {
x.SetStatus(w, x.ErrorInvalidRequest,
"StartTs path parameter is mandatory while trying to abort.")
return
}
tc.StartTs = ts
tc.Aborted = true
_, aerr := worker.CommitOverNetwork(context.Background(), tc)
if aerr != nil {
x.SetStatus(w, x.Error, aerr.Error())
return
}
response := map[string]interface{}{}
response["code"] = x.Success
response["message"] = "Done"
js, err := json.Marshal(response)
if err != nil {
x.SetStatusWithData(w, x.Error, err.Error())
return
}
w.Write(js)
}
func alterHandler(w http.ResponseWriter, r *http.Request) {
x.AddCorsHeaders(w)
w.Header().Set("Content-Type", "application/json")
if r.Method == "OPTIONS" {
return
}
if !allowed(r.Method) {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}
op := &api.Operation{}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
err = json.Unmarshal(b, &op)
if err != nil {
op.Schema = string(b)
}
_, err = (&edgraph.Server{}).Alter(context.Background(), op)
if err != nil {
x.SetStatus(w, x.Error, err.Error())
return
}
res := map[string]interface{}{}
data := map[string]interface{}{}
data["code"] = x.Success
data["message"] = "Done"
res["data"] = data
js, err := json.Marshal(res)
if err != nil {
x.SetStatus(w, x.Error, err.Error())
return
}
w.Write(js)
}