forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
522 lines (449 loc) · 13.5 KB
/
upload.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
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/blobserver/protocol"
"camlistore.org/pkg/constants"
"camlistore.org/pkg/httputil"
)
var debugUploads = os.Getenv("CAMLI_DEBUG_UPLOADS") != ""
// multipartOverhead is how many extra bytes mime/multipart's
// Writer adds around content
var multipartOverhead = calculateMultipartOverhead()
// UploadHandle contains the parameters is a request to upload a blob.
type UploadHandle struct {
// BlobRef is the required blobref of the blob to upload.
BlobRef blob.Ref
// Contents is the blob data.
Contents io.Reader
// Size optionally specifies the size of Contents.
// If <= 0, the Contents are slurped into memory to count the size.
Size uint32
// Vivify optionally instructs the server to create a
// permanode for this blob. If used, the blob should be a
// "file" schema blob. This is typically used by
// lesser-trusted clients (such a mobile phones) which don't
// have rights to do signing directly.
Vivify bool
// SkipStat indicates whether the stat check (checking whether
// the server already has the blob) will be skipped and the
// blob should be uploaded immediately. This is useful for
// small blobs that the server is unlikely to already have
// (e.g. new claims).
SkipStat bool
}
type PutResult struct {
BlobRef blob.Ref
Size uint32
Skipped bool // already present on blobserver
}
func (pr *PutResult) SizedBlobRef() blob.SizedRef {
return blob.SizedRef{pr.BlobRef, pr.Size}
}
// TODO: ditch this type and use protocol.StatResponse directly?
// Or at least make HaveMap keyed by a blob.Ref instead of a string.
type statResponse struct {
HaveMap map[string]blob.SizedRef
canLongPoll bool
}
type ResponseFormatError error
func calculateMultipartOverhead() int64 {
var b bytes.Buffer
w := multipart.NewWriter(&b)
part, _ := w.CreateFormFile("0", "0")
dummyContents := []byte("0")
part.Write(dummyContents)
w.Close()
return int64(b.Len()) - 3 // remove what was added
}
func newResFormatError(s string, arg ...interface{}) ResponseFormatError {
return ResponseFormatError(fmt.Errorf(s, arg...))
}
func parseStatResponse(res *http.Response) (*statResponse, error) {
var s = &statResponse{HaveMap: make(map[string]blob.SizedRef)}
var pres protocol.StatResponse
if err := httputil.DecodeJSON(res, &pres); err != nil {
return nil, ResponseFormatError(err)
}
s.canLongPoll = pres.CanLongPoll
for _, statItem := range pres.Stat {
br := statItem.Ref
if !br.Valid() {
continue
}
s.HaveMap[br.String()] = blob.SizedRef{br, uint32(statItem.Size)}
}
return s, nil
}
// NewUploadHandleFromString returns an upload handle
func NewUploadHandleFromString(data string) *UploadHandle {
bref := blob.SHA1FromString(data)
r := strings.NewReader(data)
return &UploadHandle{BlobRef: bref, Size: uint32(len(data)), Contents: r}
}
// TODO(bradfitz): delete most of this. use new camlistore.org/pkg/blobserver/protocol types instead
// of a map[string]interface{}.
func (c *Client) responseJSONMap(requestName string, resp *http.Response) (map[string]interface{}, error) {
if resp.StatusCode != 200 {
log.Printf("After %s request, failed to JSON from response; status code is %d", requestName, resp.StatusCode)
io.Copy(os.Stderr, resp.Body)
return nil, fmt.Errorf("After %s request, HTTP response code is %d; no JSON to parse.", requestName, resp.StatusCode)
}
jmap := make(map[string]interface{})
if err := httputil.DecodeJSON(resp, &jmap); err != nil {
return nil, err
}
return jmap, nil
}
// statReq is a request to stat a blob.
type statReq struct {
br blob.Ref
dest chan<- blob.SizedRef // written to on success
errc chan<- error // written to on both failure and success (after any dest)
}
func (c *Client) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) error {
if c.sto != nil {
return c.sto.StatBlobs(dest, blobs)
}
var needStat []blob.Ref
for _, br := range blobs {
if !br.Valid() {
panic("invalid blob")
}
if size, ok := c.haveCache.StatBlobCache(br); ok {
dest <- blob.SizedRef{br, size}
} else {
if needStat == nil {
needStat = make([]blob.Ref, 0, len(blobs))
}
needStat = append(needStat, br)
}
}
if len(needStat) == 0 {
return nil
}
// Here begins all the batching logic. In a SPDY world, this
// will all be somewhat useless, so consider detecting SPDY on
// the underlying connection and just always calling doStat
// instead. The one thing this code below is also cut up
// >1000 stats into smaller batches. But with SPDY we could
// even just do lots of little 1-at-a-time stats.
var errcs []chan error // one per blob to stat
c.pendStatMu.Lock()
{
if c.pendStat == nil {
c.pendStat = make(map[blob.Ref][]statReq)
}
for _, blob := range needStat {
errc := make(chan error, 1)
errcs = append(errcs, errc)
c.pendStat[blob] = append(c.pendStat[blob], statReq{blob, dest, errc})
}
}
c.pendStatMu.Unlock()
// Kick off at least one worker. It may do nothing and lose
// the race, but somebody will handle our requests in
// pendStat.
go c.doSomeStats()
for _, errc := range errcs {
if err := <-errc; err != nil {
return err
}
}
return nil
}
const maxStatPerReq = 1000 // TODO: detect this from client discovery? add it on server side too.
func (c *Client) doSomeStats() {
c.httpGate.Start()
defer c.httpGate.Done()
var batch map[blob.Ref][]statReq
c.pendStatMu.Lock()
{
if len(c.pendStat) == 0 {
// Lost race. Another batch got these.
c.pendStatMu.Unlock()
return
}
batch = make(map[blob.Ref][]statReq)
for br, reqs := range c.pendStat {
batch[br] = reqs
delete(c.pendStat, br)
if len(batch) == maxStatPerReq {
go c.doSomeStats() // kick off next batch
break
}
}
}
c.pendStatMu.Unlock()
if debugUploads {
println("doing stat batch of", len(batch))
}
blobs := make([]blob.Ref, 0, len(batch))
for br := range batch {
blobs = append(blobs, br)
}
ourDest := make(chan blob.SizedRef)
errc := make(chan error, 1)
go func() {
// false for not gated, since we already grabbed the
// token at the beginning of this function.
errc <- c.doStat(ourDest, blobs, 0, false)
close(ourDest)
}()
for sb := range ourDest {
for _, req := range batch[sb.Ref] {
req.dest <- sb
}
}
// Copy the doStat's error to all waiters for all blobrefs in this batch.
err := <-errc
for _, reqs := range batch {
for _, req := range reqs {
req.errc <- err
}
}
}
// doStat does an HTTP request for the stat. the number of blobs is used verbatim. No extra splitting
// or batching is done at this layer.
func (c *Client) doStat(dest chan<- blob.SizedRef, blobs []blob.Ref, wait time.Duration, gated bool) error {
var buf bytes.Buffer
fmt.Fprintf(&buf, "camliversion=1")
if wait > 0 {
secs := int(wait.Seconds())
if secs == 0 {
secs = 1
}
fmt.Fprintf(&buf, "&maxwaitsec=%d", secs)
}
for i, blob := range blobs {
fmt.Fprintf(&buf, "&blob%d=%s", i+1, blob)
}
pfx, err := c.prefix()
if err != nil {
return err
}
req := c.newRequest("POST", fmt.Sprintf("%s/camli/stat", pfx), &buf)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var resp *http.Response
if gated {
resp, err = c.doReqGated(req)
} else {
resp, err = c.httpClient.Do(req)
}
if err != nil {
return fmt.Errorf("stat HTTP error: %v", err)
}
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode != 200 {
return fmt.Errorf("stat response had http status %d", resp.StatusCode)
}
stat, err := parseStatResponse(resp)
if err != nil {
return err
}
for _, sb := range stat.HaveMap {
dest <- sb
}
return nil
}
// Figure out the size of the contents.
// If the size was provided, trust it.
func (h *UploadHandle) readerAndSize() (io.Reader, int64, error) {
if h.Size > 0 {
return h.Contents, int64(h.Size), nil
}
var b bytes.Buffer
n, err := io.Copy(&b, h.Contents)
if err != nil {
return nil, 0, err
}
return &b, n, nil
}
// Upload uploads a blob, as described by the provided UploadHandle parameters.
func (c *Client) Upload(h *UploadHandle) (*PutResult, error) {
errorf := func(msg string, arg ...interface{}) (*PutResult, error) {
err := fmt.Errorf(msg, arg...)
c.log.Print(err.Error())
return nil, err
}
bodyReader, bodySize, err := h.readerAndSize()
if err != nil {
return nil, fmt.Errorf("client: error slurping upload handle to find its length: %v", err)
}
if bodySize > constants.MaxBlobSize {
return nil, errors.New("client: body is bigger then max blob size")
}
c.statsMutex.Lock()
c.stats.UploadRequests.Blobs++
c.stats.UploadRequests.Bytes += bodySize
c.statsMutex.Unlock()
pr := &PutResult{BlobRef: h.BlobRef, Size: uint32(bodySize)}
if c.sto != nil {
// TODO: stat first so we can show skipped?
_, err := blobserver.Receive(c.sto, h.BlobRef, bodyReader)
if err != nil {
return nil, err
}
return pr, nil
}
if !h.Vivify {
if _, ok := c.haveCache.StatBlobCache(h.BlobRef); ok {
pr.Skipped = true
return pr, nil
}
}
blobrefStr := h.BlobRef.String()
// Pre-upload. Check whether the blob already exists on the
// server and if not, the URL to upload it to.
pfx, err := c.prefix()
if err != nil {
return nil, err
}
if !h.SkipStat {
url_ := fmt.Sprintf("%s/camli/stat", pfx)
req := c.newRequest("POST", url_, strings.NewReader("camliversion=1&blob1="+blobrefStr))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.doReqGated(req)
if err != nil {
return errorf("stat http error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errorf("stat response had http status %d", resp.StatusCode)
}
stat, err := parseStatResponse(resp)
if err != nil {
return nil, err
}
for _, sbr := range stat.HaveMap {
c.haveCache.NoteBlobExists(sbr.Ref, uint32(sbr.Size))
}
_, serverHasIt := stat.HaveMap[blobrefStr]
if debugUploads {
log.Printf("HTTP Stat(%s) = %v", blobrefStr, serverHasIt)
}
if !h.Vivify && serverHasIt {
pr.Skipped = true
if closer, ok := h.Contents.(io.Closer); ok {
// TODO(bradfitz): I did this
// Close-if-possible thing early on, before I
// knew better. Fix the callers instead, and
// fix the docs.
closer.Close()
}
c.haveCache.NoteBlobExists(h.BlobRef, uint32(bodySize))
return pr, nil
}
}
if debugUploads {
log.Printf("Uploading: %s (%d bytes)", blobrefStr, bodySize)
}
pipeReader, pipeWriter := io.Pipe()
multipartWriter := multipart.NewWriter(pipeWriter)
copyResult := make(chan error, 1)
go func() {
defer pipeWriter.Close()
part, err := multipartWriter.CreateFormFile(blobrefStr, blobrefStr)
if err != nil {
copyResult <- err
return
}
_, err = io.Copy(part, bodyReader)
if err == nil {
err = multipartWriter.Close()
}
copyResult <- err
}()
// TODO(bradfitz): verbosity levels. make this VLOG(2) or something. it's noisy:
// c.log.Printf("Uploading %s", br)
uploadURL := fmt.Sprintf("%s/camli/upload", pfx)
req := c.newRequest("POST", uploadURL)
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
if h.Vivify {
req.Header.Add("X-Camlistore-Vivify", "1")
}
req.Body = ioutil.NopCloser(pipeReader)
req.ContentLength = multipartOverhead + bodySize + int64(len(blobrefStr))*2
resp, err := c.doReqGated(req)
if err != nil {
return errorf("upload http error: %v", err)
}
defer resp.Body.Close()
// check error from earlier copy
if err := <-copyResult; err != nil {
return errorf("failed to copy contents into multipart writer: %v", err)
}
// The only valid HTTP responses are 200 and 303.
if resp.StatusCode != 200 && resp.StatusCode != 303 {
return errorf("invalid http response %d in upload response", resp.StatusCode)
}
if resp.StatusCode == 303 {
otherLocation := resp.Header.Get("Location")
if otherLocation == "" {
return errorf("303 without a Location")
}
baseURL, _ := url.Parse(uploadURL)
absURL, err := baseURL.Parse(otherLocation)
if err != nil {
return errorf("303 Location URL relative resolve error: %v", err)
}
otherLocation = absURL.String()
resp, err = http.Get(otherLocation)
if err != nil {
return errorf("error following 303 redirect after upload: %v", err)
}
}
var ures protocol.UploadResponse
if err := httputil.DecodeJSON(resp, &ures); err != nil {
return errorf("error in upload response: %v", err)
}
if ures.ErrorText != "" {
c.log.Printf("Blob server reports error: %s", ures.ErrorText)
}
expectedSize := uint32(bodySize)
for _, sb := range ures.Received {
if sb.Ref != h.BlobRef {
continue
}
if sb.Size != expectedSize {
return errorf("Server got blob %v, but reports wrong length (%v; we sent %d)",
sb.Ref, sb.Size, expectedSize)
}
c.statsMutex.Lock()
c.stats.Uploads.Blobs++
c.stats.Uploads.Bytes += bodySize
c.statsMutex.Unlock()
if pr.Size <= 0 {
pr.Size = sb.Size
}
c.haveCache.NoteBlobExists(pr.BlobRef, pr.Size)
return pr, nil
}
return nil, errors.New("Server didn't receive blob.")
}