-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.go
522 lines (453 loc) · 13.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package dsync
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
format "github.com/ipfs/go-ipld-format"
coreiface "github.com/ipfs/interface-go-ipfs-core"
protocol "github.com/libp2p/go-libp2p-core/protocol"
"github.com/qri-io/dag"
)
const (
httpDsyncProtocolIDHeader = "dsync-version"
sidHeader = "sid"
)
const (
carMIMEType = "archive/car"
cborMIMEType = "application/cbor"
jsonMIMEType = "application/json"
binaryMIMEType = "application/octet-stream"
)
// HTTPClient is the request side of doing dsync over HTTP
type HTTPClient struct {
URL string
NodeGetter format.NodeGetter
BlockAPI coreiface.BlockAPI
remProtocolID protocol.ID
}
var (
// HTTPClient exists to satisfy the DaySyncable interface on the client side
// of a transfer
_ DagSyncable = (*HTTPClient)(nil)
_ DagStreamable = (*HTTPClient)(nil)
)
// NewReceiveSession initiates a session for pushing blocks to a remote.
// It sends a Manifest to a remote source over HTTP
func (rem *HTTPClient) NewReceiveSession(info *dag.Info, pinOnComplete bool, meta map[string]string) (sid string, diff *dag.Manifest, err error) {
buf := &bytes.Buffer{}
if err = json.NewEncoder(buf).Encode(info); err != nil {
return
}
u, err := url.Parse(rem.URL)
if err != nil {
return
}
q := u.Query()
q.Set("pin", fmt.Sprintf("%t", pinOnComplete))
for key, val := range meta {
q.Set(key, val)
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodPost, u.String(), buf)
if err != nil {
return
}
req.Header.Set("Content-Type", jsonMIMEType)
req.Header.Set("Accept", jsonMIMEType)
req.Header.Set(httpDsyncProtocolIDHeader, string(DsyncProtocolID))
res, err := http.DefaultClient.Do(req)
if err != nil {
return
}
if res.StatusCode != http.StatusOK {
var msg string
if data, err := ioutil.ReadAll(res.Body); err == nil {
msg = string(data)
}
err = fmt.Errorf("remote response: %d %s", res.StatusCode, msg)
return
}
sid = res.Header.Get("sid")
rem.remProtocolID = protocolIDFromHTTPData(req.URL, res.Header)
diff = &dag.Manifest{}
err = json.NewDecoder(res.Body).Decode(diff)
return
}
// ProtocolVersion indicates the version of dsync the remote speaks, only
// available after a handshake is established
func (rem *HTTPClient) ProtocolVersion() (protocol.ID, error) {
if string(rem.remProtocolID) == "" {
return "", ErrUnknownProtocolVersion
}
return rem.remProtocolID, nil
}
// ReceiveBlocks writes a block stream as an HTTP PUT request to the remote
func (rem *HTTPClient) ReceiveBlocks(ctx context.Context, sid string, r io.Reader) error {
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s?sid=%s", rem.URL, sid), r)
if err != nil {
log.Debugf("err creating %s HTTP request err=%q ", http.MethodPut, err)
return err
}
req.TransferEncoding = []string{"chunked"}
req.Header.Set("Content-Type", carMIMEType)
// response body is only used for error reporting
req.Header.Set("Accept", binaryMIMEType)
req.Header.Set(httpDsyncProtocolIDHeader, string(DsyncProtocolID))
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Debugf("err doing HTTP request. err=%q", err)
return err
}
if res.StatusCode != http.StatusOK {
var msg string
if data, err := ioutil.ReadAll(res.Body); err == nil {
msg = string(data)
}
log.Debugf("error response from remote. err=%q", msg)
return fmt.Errorf("remote response: %d %s", res.StatusCode, msg)
}
return nil
}
// ReceiveBlock asks a remote to receive a block over HTTP
func (rem *HTTPClient) ReceiveBlock(sid, hash string, data []byte) ReceiveResponse {
url := fmt.Sprintf("%s?sid=%s&hash=%s", rem.URL, sid, hash)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))
if err != nil {
log.Debugf("http client create request error=%s", err)
return ReceiveResponse{
Hash: hash,
Status: StatusErrored,
Err: err,
}
}
req.Header.Set("Content-Type", binaryMIMEType)
// response body is only used for error reporting
req.Header.Set("Accept", binaryMIMEType)
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Debugf("http client perform request error=%s", err)
return ReceiveResponse{
Hash: hash,
Status: StatusRetry,
Err: fmt.Errorf("performing HTTP PUT: %w", err),
}
}
if res.StatusCode != http.StatusOK {
var msg string
if data, err := ioutil.ReadAll(res.Body); err == nil {
msg = string(data)
}
return ReceiveResponse{
Hash: hash,
Status: StatusErrored,
Err: fmt.Errorf("remote error: %d %s", res.StatusCode, msg),
}
}
return ReceiveResponse{
Hash: hash,
Status: StatusOk,
}
}
// GetDagInfo fetches a manifest from a remote source over HTTP
func (rem *HTTPClient) GetDagInfo(ctx context.Context, id string, meta map[string]string) (info *dag.Info, err error) {
u, err := url.Parse(rem.URL)
if err != nil {
return
}
q := u.Query()
q.Set("manifest", id)
for key, val := range meta {
q.Set(key, val)
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", jsonMIMEType)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
rem.remProtocolID = protocolIDFromHTTPData(req.URL, res.Header)
if res.StatusCode != http.StatusOK {
var msg string
if data, err := ioutil.ReadAll(res.Body); err == nil {
msg = string(data)
}
return nil, fmt.Errorf("remote error: %d %s", res.StatusCode, msg)
}
defer res.Body.Close()
info = &dag.Info{}
err = json.NewDecoder(res.Body).Decode(info)
return
}
// GetBlock fetches a block from a remote source over HTTP
func (rem *HTTPClient) GetBlock(ctx context.Context, id string) (data []byte, err error) {
url := fmt.Sprintf("%s?block=%s", rem.URL, id)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", binaryMIMEType)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
var msg string
if data, err := ioutil.ReadAll(res.Body); err == nil {
msg = string(data)
}
return nil, fmt.Errorf("remote error: %d %s", res.StatusCode, msg)
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
// OpenBlockStream sends a dag.Info to the remote & asks that it returns a
// stream of blocks in the info's manifest
func (rem *HTTPClient) OpenBlockStream(ctx context.Context, info *dag.Info, meta map[string]string) (io.ReadCloser, error) {
u, err := url.Parse(rem.URL)
if err != nil {
return nil, err
}
q := u.Query()
for key, value := range meta {
q.Add(key, value)
}
u.RawQuery = q.Encode()
bodyData, err := info.MarshalCBOR()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPatch, u.String(), bytes.NewBuffer(bodyData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", cborMIMEType)
req.Header.Set("Accept", carMIMEType)
req.Header.Set(httpDsyncProtocolIDHeader, string(DsyncProtocolID))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(res.Body)
return nil, fmt.Errorf("unexpected HTTP response: %d: %q", res.StatusCode, string(body))
}
if res.Header.Get("Content-Type") != carMIMEType {
return nil, fmt.Errorf("unexpected media type: %s", res.Header.Get("Content-Type"))
}
return res.Body, nil
}
// RemoveCID asks a remote to remove a CID
func (rem *HTTPClient) RemoveCID(ctx context.Context, id string, meta map[string]string) (err error) {
u, err := url.Parse(rem.URL)
if err != nil {
return
}
q := u.Query()
q.Set("cid", id)
for key, val := range meta {
q.Set(key, val)
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodDelete, u.String(), nil)
if err != nil {
return err
}
// response body is only used for error reporting
req.Header.Set("Accept", binaryMIMEType)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
var msg string
if data, err := ioutil.ReadAll(res.Body); err == nil {
msg = string(data)
}
if msg == ErrRemoveNotSupported.Error() {
return ErrRemoveNotSupported
}
return fmt.Errorf("remote: %d %s", res.StatusCode, msg)
}
return nil
}
// HTTPRemoteHandler exposes a Dsync remote over HTTP by exposing a HTTP handler
// that interlocks with methods exposed by HTTPClient
func HTTPRemoteHandler(ds *Dsync) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(httpDsyncProtocolIDHeader, string(DsyncProtocolID))
switch r.Method {
case http.MethodPost:
createDsyncSession(ds, w, r)
case http.MethodPut:
if r.Header.Get("Content-Type") == carMIMEType {
if err := ds.ReceiveBlocks(r.Context(), r.FormValue("sid"), r.Body); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
return
}
receiveBlockHTTP(ds, w, r)
case http.MethodGet:
mfstID := r.FormValue("manifest")
blockID := r.FormValue("block")
if mfstID == "" && blockID == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("either manifest or block query params are required"))
} else if mfstID != "" {
meta := map[string]string{}
for key := range r.URL.Query() {
if key != "manifest" {
meta[key] = r.URL.Query().Get(key)
}
}
mfst, err := ds.GetDagInfo(r.Context(), mfstID, meta)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
data, err := json.Marshal(mfst)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", jsonMIMEType)
w.Write(data)
} else {
data, err := ds.GetBlock(r.Context(), blockID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", binaryMIMEType)
w.Write(data)
}
case http.MethodPatch:
meta := map[string]string{}
for key := range r.URL.Query() {
meta[key] = r.URL.Query().Get(key)
}
info, err := decodeDAGInfoBody(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
r, err := ds.OpenBlockStream(r.Context(), info, meta)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", carMIMEType)
w.WriteHeader(http.StatusOK)
defer r.Close()
io.Copy(w, r)
return
case http.MethodDelete:
cid := r.FormValue("cid")
meta := map[string]string{}
for key := range r.URL.Query() {
if key != "cid" {
meta[key] = r.URL.Query().Get(key)
}
}
if err := ds.RemoveCID(r.Context(), cid, meta); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
}
}
}
func protocolIDFromHTTPData(url *url.URL, headers http.Header) protocol.ID {
protocolIDHeaderStr := headers.Get(httpDsyncProtocolIDHeader)
if protocolIDHeaderStr == "" {
// protocol ID header only exists in version 0.2.0 and up, when header isn't
// present assume version 0.1.1, the latest version before header was set
// 0.1.1 is wire-compatible with all lower versions of dsync
return protocol.ID("/dsync/0.1.1")
}
return protocol.ID(protocolIDHeaderStr)
}
func decodeDAGInfoBody(r *http.Request) (*dag.Info, error) {
defer r.Body.Close()
info := &dag.Info{}
switch r.Header.Get("Content-Type") {
case cborMIMEType:
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
info, err = dag.UnmarshalCBORDagInfo(data)
if err != nil {
return nil, err
}
default:
// default to JSON for legacy reads
err := json.NewDecoder(r.Body).Decode(info)
if err != nil {
return nil, err
}
}
if info.Manifest == nil {
return nil, fmt.Errorf("body must be a json dag info object")
}
return info, nil
}
func receiveBlockHTTP(ds *Dsync, w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
res := ds.ReceiveBlock(r.FormValue("sid"), r.FormValue("hash"), data)
if res.Status == StatusErrored {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(res.Err.Error()))
} else if res.Status == StatusRetry {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(res.Err.Error()))
} else {
w.WriteHeader(http.StatusOK)
}
}
func createDsyncSession(ds *Dsync, w http.ResponseWriter, r *http.Request) {
info, err := decodeDAGInfoBody(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
pinOnComplete := r.FormValue("pin") == "true"
meta := map[string]string{}
for key := range r.URL.Query() {
if key != "pin" {
meta[key] = r.URL.Query().Get(key)
}
}
sid, diff, err := ds.NewReceiveSession(info, pinOnComplete, meta)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.Header().Set(sidHeader, sid)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(diff)
}