2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"errors"
|
2011-01-02 22:36:03 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2011-05-10 19:56:59 +00:00
|
|
|
"io/ioutil"
|
2011-01-02 22:36:03 +00:00
|
|
|
"log"
|
2011-06-17 05:14:50 +00:00
|
|
|
"mime/multipart"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2011-01-02 22:36:03 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"time"
|
2011-05-11 15:19:23 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2014-02-07 18:59:23 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2014-01-05 01:32:08 +00:00
|
|
|
"camlistore.org/pkg/blobserver/protocol"
|
2014-01-28 20:46:52 +00:00
|
|
|
"camlistore.org/pkg/constants"
|
2014-01-29 06:00:52 +00:00
|
|
|
"camlistore.org/pkg/httputil"
|
2011-01-02 22:36:03 +00:00
|
|
|
)
|
|
|
|
|
2012-12-24 20:16:03 +00:00
|
|
|
var debugUploads = os.Getenv("CAMLI_DEBUG_UPLOADS") != ""
|
|
|
|
|
2011-06-17 05:14:50 +00:00
|
|
|
// multipartOverhead is how many extra bytes mime/multipart's
|
|
|
|
// Writer adds around content
|
|
|
|
var multipartOverhead = calculateMultipartOverhead()
|
|
|
|
|
2014-01-16 02:32:02 +00:00
|
|
|
// UploadHandle contains the parameters is a request to upload a blob.
|
2011-01-02 22:36:03 +00:00
|
|
|
type UploadHandle struct {
|
2014-01-16 02:32:02 +00:00
|
|
|
// BlobRef is the required blobref of the blob to upload.
|
|
|
|
BlobRef blob.Ref
|
|
|
|
|
|
|
|
// Contents is the blob data.
|
2011-01-02 22:36:03 +00:00
|
|
|
Contents io.Reader
|
2014-01-16 02:32:02 +00:00
|
|
|
|
|
|
|
// Size optionally specifies the size of Contents.
|
|
|
|
// If <= 0, the Contents are slurped into memory to count the size.
|
2014-01-28 20:46:52 +00:00
|
|
|
Size uint32
|
2014-01-16 02:32:02 +00:00
|
|
|
|
|
|
|
// 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
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PutResult struct {
|
2013-08-04 02:54:30 +00:00
|
|
|
BlobRef blob.Ref
|
2014-01-28 20:46:52 +00:00
|
|
|
Size uint32
|
2011-05-10 13:25:18 +00:00
|
|
|
Skipped bool // already present on blobserver
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (pr *PutResult) SizedBlobRef() blob.SizedRef {
|
|
|
|
return blob.SizedRef{pr.BlobRef, pr.Size}
|
2011-05-10 22:51:10 +00:00
|
|
|
}
|
|
|
|
|
2014-01-05 01:32:08 +00:00
|
|
|
// TODO: ditch this type and use protocol.StatResponse directly?
|
|
|
|
// Or at least make HaveMap keyed by a blob.Ref instead of a string.
|
2011-05-10 19:56:59 +00:00
|
|
|
type statResponse struct {
|
2014-01-05 01:32:08 +00:00
|
|
|
HaveMap map[string]blob.SizedRef
|
|
|
|
canLongPoll bool
|
2011-03-01 03:20:26 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
type ResponseFormatError error
|
2011-05-10 19:56:59 +00:00
|
|
|
|
2011-06-17 05:14:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-05-10 19:56:59 +00:00
|
|
|
func newResFormatError(s string, arg ...interface{}) ResponseFormatError {
|
|
|
|
return ResponseFormatError(fmt.Errorf(s, arg...))
|
|
|
|
}
|
|
|
|
|
2014-01-29 06:00:52 +00:00
|
|
|
func parseStatResponse(res *http.Response) (*statResponse, error) {
|
2014-01-05 01:32:08 +00:00
|
|
|
var s = &statResponse{HaveMap: make(map[string]blob.SizedRef)}
|
|
|
|
var pres protocol.StatResponse
|
2014-01-29 06:00:52 +00:00
|
|
|
if err := httputil.DecodeJSON(res, &pres); err != nil {
|
2014-01-05 01:32:08 +00:00
|
|
|
return nil, ResponseFormatError(err)
|
2011-05-10 19:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-05 01:32:08 +00:00
|
|
|
s.canLongPoll = pres.CanLongPoll
|
|
|
|
for _, statItem := range pres.Stat {
|
|
|
|
br := statItem.Ref
|
|
|
|
if !br.Valid() {
|
|
|
|
continue
|
2011-05-10 19:56:59 +00:00
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
s.HaveMap[br.String()] = blob.SizedRef{br, uint32(statItem.Size)}
|
2011-05-10 19:56:59 +00:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
2011-03-01 03:20:26 +00:00
|
|
|
|
2014-01-16 02:32:02 +00:00
|
|
|
// NewUploadHandleFromString returns an upload handle
|
2011-01-18 18:29:38 +00:00
|
|
|
func NewUploadHandleFromString(data string) *UploadHandle {
|
2013-08-04 02:54:30 +00:00
|
|
|
bref := blob.SHA1FromString(data)
|
2011-05-29 17:38:53 +00:00
|
|
|
r := strings.NewReader(data)
|
2014-01-28 20:46:52 +00:00
|
|
|
return &UploadHandle{BlobRef: bref, Size: uint32(len(data)), Contents: r}
|
2011-01-18 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 06:00:52 +00:00
|
|
|
// 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) {
|
2011-01-02 22:36:03 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2011-05-10 13:25:18 +00:00
|
|
|
log.Printf("After %s request, failed to JSON from response; status code is %d", requestName, resp.StatusCode)
|
2011-01-05 16:22:33 +00:00
|
|
|
io.Copy(os.Stderr, resp.Body)
|
2014-01-29 06:00:52 +00:00
|
|
|
return nil, fmt.Errorf("After %s request, HTTP response code is %d; no JSON to parse.", requestName, resp.StatusCode)
|
2014-01-28 00:07:15 +00:00
|
|
|
}
|
2011-01-02 22:36:03 +00:00
|
|
|
jmap := make(map[string]interface{})
|
2014-01-29 06:00:52 +00:00
|
|
|
if err := httputil.DecodeJSON(resp, &jmap); err != nil {
|
|
|
|
return nil, err
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
return jmap, nil
|
|
|
|
}
|
|
|
|
|
2013-02-03 05:51:08 +00:00
|
|
|
// statReq is a request to stat a blob.
|
|
|
|
type statReq struct {
|
2013-08-04 02:54:30 +00:00
|
|
|
br blob.Ref
|
|
|
|
dest chan<- blob.SizedRef // written to on success
|
|
|
|
errc chan<- error // written to on both failure and success (after any dest)
|
2013-02-03 05:51:08 +00:00
|
|
|
}
|
2011-05-10 13:25:18 +00:00
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
func (c *Client) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) error {
|
2014-02-07 18:59:23 +00:00
|
|
|
if c.sto != nil {
|
|
|
|
return c.sto.StatBlobs(dest, blobs)
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
var needStat []blob.Ref
|
|
|
|
for _, br := range blobs {
|
|
|
|
if !br.Valid() {
|
|
|
|
panic("invalid blob")
|
2011-05-10 13:25:18 +00:00
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
if size, ok := c.haveCache.StatBlobCache(br); ok {
|
|
|
|
dest <- blob.SizedRef{br, size}
|
2013-02-03 05:51:08 +00:00
|
|
|
} else {
|
|
|
|
if needStat == nil {
|
2013-08-04 02:54:30 +00:00
|
|
|
needStat = make([]blob.Ref, 0, len(blobs))
|
2013-02-03 05:51:08 +00:00
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
needStat = append(needStat, br)
|
2013-01-02 04:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-03 05:51:08 +00:00
|
|
|
if len(needStat) == 0 {
|
2013-01-02 04:23:44 +00:00
|
|
|
return nil
|
2011-05-10 13:25:18 +00:00
|
|
|
}
|
2013-02-03 05:51:08 +00:00
|
|
|
|
|
|
|
// 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 {
|
2013-08-23 14:06:40 +00:00
|
|
|
c.pendStat = make(map[blob.Ref][]statReq)
|
2013-02-03 05:51:08 +00:00
|
|
|
}
|
|
|
|
for _, blob := range needStat {
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
errcs = append(errcs, errc)
|
2013-08-23 14:06:40 +00:00
|
|
|
c.pendStat[blob] = append(c.pendStat[blob], statReq{blob, dest, errc})
|
2013-02-03 05:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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() {
|
2014-01-27 22:07:51 +00:00
|
|
|
c.httpGate.Start()
|
|
|
|
defer c.httpGate.Done()
|
2013-02-03 05:51:08 +00:00
|
|
|
|
2013-08-23 14:06:40 +00:00
|
|
|
var batch map[blob.Ref][]statReq
|
2011-05-10 13:25:18 +00:00
|
|
|
|
2013-02-03 05:51:08 +00:00
|
|
|
c.pendStatMu.Lock()
|
|
|
|
{
|
|
|
|
if len(c.pendStat) == 0 {
|
|
|
|
// Lost race. Another batch got these.
|
|
|
|
c.pendStatMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2013-08-23 14:06:40 +00:00
|
|
|
batch = make(map[blob.Ref][]statReq)
|
|
|
|
for br, reqs := range c.pendStat {
|
|
|
|
batch[br] = reqs
|
|
|
|
delete(c.pendStat, br)
|
2013-02-03 05:51:08 +00:00
|
|
|
if len(batch) == maxStatPerReq {
|
|
|
|
go c.doSomeStats() // kick off next batch
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.pendStatMu.Unlock()
|
|
|
|
|
2013-02-03 06:27:54 +00:00
|
|
|
if debugUploads {
|
|
|
|
println("doing stat batch of", len(batch))
|
|
|
|
}
|
2013-02-03 05:51:08 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
blobs := make([]blob.Ref, 0, len(batch))
|
2013-08-23 14:06:40 +00:00
|
|
|
for br := range batch {
|
|
|
|
blobs = append(blobs, br)
|
2013-02-03 05:51:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
ourDest := make(chan blob.SizedRef)
|
2013-02-03 05:51:08 +00:00
|
|
|
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 {
|
2013-08-23 14:06:40 +00:00
|
|
|
for _, req := range batch[sb.Ref] {
|
2013-02-03 05:51:08 +00:00
|
|
|
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.
|
2013-08-04 02:54:30 +00:00
|
|
|
func (c *Client) doStat(dest chan<- blob.SizedRef, blobs []blob.Ref, wait time.Duration, gated bool) error {
|
2013-02-03 05:51:08 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf, "camliversion=1")
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
if wait > 0 {
|
|
|
|
secs := int(wait.Seconds())
|
|
|
|
if secs == 0 {
|
|
|
|
secs = 1
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&buf, "&maxwaitsec=%d", secs)
|
2011-05-10 20:02:48 +00:00
|
|
|
}
|
2013-02-03 05:51:08 +00:00
|
|
|
for i, blob := range blobs {
|
|
|
|
fmt.Fprintf(&buf, "&blob%d=%s", i+1, blob)
|
|
|
|
}
|
2011-05-10 20:02:48 +00:00
|
|
|
|
2012-11-08 04:23:45 +00:00
|
|
|
pfx, err := c.prefix()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-02 04:23:44 +00:00
|
|
|
req := c.newRequest("POST", fmt.Sprintf("%s/camli/stat", pfx), &buf)
|
2011-09-28 18:06:16 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2011-05-10 13:25:18 +00:00
|
|
|
|
2013-02-03 05:51:08 +00:00
|
|
|
var resp *http.Response
|
|
|
|
if gated {
|
|
|
|
resp, err = c.doReqGated(req)
|
|
|
|
} else {
|
|
|
|
resp, err = c.httpClient.Do(req)
|
|
|
|
}
|
2011-05-10 13:25:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stat HTTP error: %v", err)
|
|
|
|
}
|
2011-07-31 03:29:01 +00:00
|
|
|
if resp.Body != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
2011-05-10 13:25:18 +00:00
|
|
|
|
2011-05-10 20:02:48 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("stat response had http status %d", resp.StatusCode)
|
|
|
|
}
|
2011-05-10 13:25:18 +00:00
|
|
|
|
2014-01-29 06:00:52 +00:00
|
|
|
stat, err := parseStatResponse(resp)
|
2011-05-10 20:02:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sb := range stat.HaveMap {
|
2011-05-10 23:13:37 +00:00
|
|
|
dest <- sb
|
2011-05-10 20:02:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
2011-05-10 13:25:18 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 00:01:52 +00:00
|
|
|
// Figure out the size of the contents.
|
|
|
|
// If the size was provided, trust it.
|
2014-01-16 02:32:02 +00:00
|
|
|
func (h *UploadHandle) readerAndSize() (io.Reader, int64, error) {
|
|
|
|
if h.Size > 0 {
|
2014-01-28 20:46:52 +00:00
|
|
|
return h.Contents, int64(h.Size), nil
|
2011-10-11 00:01:52 +00:00
|
|
|
}
|
|
|
|
var b bytes.Buffer
|
|
|
|
n, err := io.Copy(&b, h.Contents)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return &b, n, nil
|
|
|
|
}
|
|
|
|
|
2014-01-16 02:32:02 +00:00
|
|
|
// Upload uploads a blob, as described by the provided UploadHandle parameters.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (c *Client) Upload(h *UploadHandle) (*PutResult, error) {
|
|
|
|
errorf := func(msg string, arg ...interface{}) (*PutResult, error) {
|
2011-05-10 19:56:59 +00:00
|
|
|
err := fmt.Errorf(msg, arg...)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
c.log.Print(err.Error())
|
2011-01-02 22:36:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-16 02:32:02 +00:00
|
|
|
bodyReader, bodySize, err := h.readerAndSize()
|
2011-10-11 00:01:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("client: error slurping upload handle to find its length: %v", err)
|
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
if bodySize > constants.MaxBlobSize {
|
|
|
|
return nil, errors.New("client: body is bigger then max blob size")
|
|
|
|
}
|
2011-10-11 00:01:52 +00:00
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
c.statsMutex.Lock()
|
|
|
|
c.stats.UploadRequests.Blobs++
|
2011-10-11 00:01:52 +00:00
|
|
|
c.stats.UploadRequests.Bytes += bodySize
|
2011-01-02 22:36:03 +00:00
|
|
|
c.statsMutex.Unlock()
|
|
|
|
|
2014-01-28 20:46:52 +00:00
|
|
|
pr := &PutResult{BlobRef: h.BlobRef, Size: uint32(bodySize)}
|
2014-02-07 18:59:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:06:03 +00:00
|
|
|
if !h.Vivify {
|
|
|
|
if _, ok := c.haveCache.StatBlobCache(h.BlobRef); ok {
|
|
|
|
pr.Skipped = true
|
|
|
|
return pr, nil
|
|
|
|
}
|
2012-12-31 21:45:13 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 00:01:52 +00:00
|
|
|
blobrefStr := h.BlobRef.String()
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2012-11-08 04:23:45 +00:00
|
|
|
// Pre-upload. Check whether the blob already exists on the
|
2011-01-02 22:36:03 +00:00
|
|
|
// server and if not, the URL to upload it to.
|
2012-11-08 04:23:45 +00:00
|
|
|
pfx, err := c.prefix()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2014-01-16 02:32:02 +00:00
|
|
|
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")
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2014-01-16 02:32:02 +00:00
|
|
|
resp, err := c.doReqGated(req)
|
|
|
|
if err != nil {
|
|
|
|
return errorf("stat http error: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2014-01-16 02:32:02 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return errorf("stat response had http status %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2014-01-29 06:00:52 +00:00
|
|
|
stat, err := parseStatResponse(resp)
|
2014-01-16 02:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, sbr := range stat.HaveMap {
|
2014-01-28 20:46:52 +00:00
|
|
|
c.haveCache.NoteBlobExists(sbr.Ref, uint32(sbr.Size))
|
2014-01-16 02:32:02 +00:00
|
|
|
}
|
|
|
|
_, serverHasIt := stat.HaveMap[blobrefStr]
|
|
|
|
if debugUploads {
|
|
|
|
log.Printf("HTTP Stat(%s) = %v", blobrefStr, serverHasIt)
|
|
|
|
}
|
|
|
|
if !h.Vivify && serverHasIt {
|
2013-01-30 15:06:03 +00:00
|
|
|
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()
|
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
c.haveCache.NoteBlobExists(h.BlobRef, uint32(bodySize))
|
2013-01-30 15:06:03 +00:00
|
|
|
return pr, nil
|
2011-05-10 21:55:12 +00:00
|
|
|
}
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 20:16:03 +00:00
|
|
|
if debugUploads {
|
|
|
|
log.Printf("Uploading: %s (%d bytes)", blobrefStr, bodySize)
|
|
|
|
}
|
|
|
|
|
2011-06-17 05:14:50 +00:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
multipartWriter := multipart.NewWriter(pipeWriter)
|
2011-01-13 17:59:49 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
copyResult := make(chan error, 1)
|
2011-06-17 05:14:50 +00:00
|
|
|
go func() {
|
2011-09-24 21:13:57 +00:00
|
|
|
defer pipeWriter.Close()
|
2011-10-11 00:01:52 +00:00
|
|
|
part, err := multipartWriter.CreateFormFile(blobrefStr, blobrefStr)
|
2011-06-17 05:14:50 +00:00
|
|
|
if err != nil {
|
|
|
|
copyResult <- err
|
|
|
|
return
|
|
|
|
}
|
2011-10-11 00:01:52 +00:00
|
|
|
_, err = io.Copy(part, bodyReader)
|
2011-06-17 05:14:50 +00:00
|
|
|
if err == nil {
|
|
|
|
err = multipartWriter.Close()
|
|
|
|
}
|
|
|
|
copyResult <- err
|
|
|
|
}()
|
2011-01-13 17:59:49 +00:00
|
|
|
|
2012-04-21 15:29:43 +00:00
|
|
|
// TODO(bradfitz): verbosity levels. make this VLOG(2) or something. it's noisy:
|
2014-01-05 01:32:08 +00:00
|
|
|
// c.log.Printf("Uploading %s", br)
|
2012-04-21 15:29:43 +00:00
|
|
|
|
2014-01-05 01:32:08 +00:00
|
|
|
uploadURL := fmt.Sprintf("%s/camli/upload", pfx)
|
2014-01-16 02:32:02 +00:00
|
|
|
req := c.newRequest("POST", uploadURL)
|
2011-06-17 05:14:50 +00:00
|
|
|
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
|
2012-12-20 17:34:14 +00:00
|
|
|
if h.Vivify {
|
|
|
|
req.Header.Add("X-Camlistore-Vivify", "1")
|
|
|
|
}
|
2011-06-17 05:14:50 +00:00
|
|
|
req.Body = ioutil.NopCloser(pipeReader)
|
2011-10-11 00:01:52 +00:00
|
|
|
req.ContentLength = multipartOverhead + bodySize + int64(len(blobrefStr))*2
|
2014-01-16 02:32:02 +00:00
|
|
|
resp, err := c.doReqGated(req)
|
2011-01-02 22:36:03 +00:00
|
|
|
if err != nil {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("upload http error: %v", err)
|
2011-05-10 21:55:12 +00:00
|
|
|
}
|
2011-09-21 01:30:15 +00:00
|
|
|
defer resp.Body.Close()
|
2011-05-10 21:55:12 +00:00
|
|
|
|
2011-06-17 05:14:50 +00:00
|
|
|
// check error from earlier copy
|
|
|
|
if err := <-copyResult; err != nil {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("failed to copy contents into multipart writer: %v", err)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The only valid HTTP responses are 200 and 303.
|
|
|
|
if resp.StatusCode != 200 && resp.StatusCode != 303 {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("invalid http response %d in upload response", resp.StatusCode)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == 303 {
|
2011-03-01 03:20:26 +00:00
|
|
|
otherLocation := resp.Header.Get("Location")
|
|
|
|
if otherLocation == "" {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("303 without a Location")
|
2011-01-05 16:22:33 +00:00
|
|
|
}
|
2014-01-05 01:32:08 +00:00
|
|
|
baseURL, _ := url.Parse(uploadURL)
|
|
|
|
absURL, err := baseURL.Parse(otherLocation)
|
2011-01-13 15:53:15 +00:00
|
|
|
if err != nil {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("303 Location URL relative resolve error: %v", err)
|
2011-01-13 15:53:15 +00:00
|
|
|
}
|
2014-01-05 01:32:08 +00:00
|
|
|
otherLocation = absURL.String()
|
2011-05-14 23:40:31 +00:00
|
|
|
resp, err = http.Get(otherLocation)
|
2011-01-13 15:53:15 +00:00
|
|
|
if err != nil {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("error following 303 redirect after upload: %v", err)
|
2011-01-13 15:53:15 +00:00
|
|
|
}
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 06:00:52 +00:00
|
|
|
ures, err := c.responseJSONMap("upload", resp)
|
2011-01-02 22:36:03 +00:00
|
|
|
if err != nil {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("json parse from upload error: %v", err)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errorText, ok := ures["errorText"].(string)
|
|
|
|
if ok {
|
2011-01-18 18:29:38 +00:00
|
|
|
c.log.Printf("Blob server reports error: %s", errorText)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
received, ok := ures["received"].([]interface{})
|
|
|
|
if !ok {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("upload json validity error: no 'received'")
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 00:01:52 +00:00
|
|
|
expectedSize := bodySize
|
2011-09-28 18:06:16 +00:00
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
for _, rit := range received {
|
|
|
|
it, ok := rit.(map[string]interface{})
|
|
|
|
if !ok {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("upload json validity error: 'received' is malformed")
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
2011-10-11 00:01:52 +00:00
|
|
|
if it["blobRef"] == blobrefStr {
|
2011-01-02 22:36:03 +00:00
|
|
|
switch size := it["size"].(type) {
|
|
|
|
case nil:
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("upload json validity error: 'received' is missing 'size'")
|
2011-01-02 22:36:03 +00:00
|
|
|
case float64:
|
2011-09-28 18:06:16 +00:00
|
|
|
if int64(size) == expectedSize {
|
2011-01-02 22:36:03 +00:00
|
|
|
// Success!
|
|
|
|
c.statsMutex.Lock()
|
|
|
|
c.stats.Uploads.Blobs++
|
2011-09-28 18:06:16 +00:00
|
|
|
c.stats.Uploads.Bytes += expectedSize
|
2011-01-02 22:36:03 +00:00
|
|
|
c.statsMutex.Unlock()
|
2014-01-28 20:46:52 +00:00
|
|
|
if pr.Size <= 0 {
|
|
|
|
pr.Size = uint32(expectedSize)
|
2011-09-28 18:06:16 +00:00
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
c.haveCache.NoteBlobExists(pr.BlobRef, uint32(expectedSize))
|
2011-01-02 22:36:03 +00:00
|
|
|
return pr, nil
|
|
|
|
} else {
|
2011-09-28 18:06:16 +00:00
|
|
|
return errorf("Server got blob, but reports wrong length (%v; we sent %d)",
|
|
|
|
size, expectedSize)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
default:
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("unsupported type of 'size' in received response")
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return nil, errors.New("Server didn't receive blob.")
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|