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
|
|
|
"encoding/json"
|
|
|
|
"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
|
|
|
|
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
|
|
|
"camlistore.org/pkg/blobref"
|
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()
|
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
type UploadHandle struct {
|
|
|
|
BlobRef *blobref.BlobRef
|
2011-05-10 21:55:12 +00:00
|
|
|
Size int64 // or -1 if size isn't known
|
2011-01-02 22:36:03 +00:00
|
|
|
Contents io.Reader
|
2012-12-20 17:34:14 +00:00
|
|
|
Vivify bool
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PutResult struct {
|
2011-05-10 13:25:18 +00:00
|
|
|
BlobRef *blobref.BlobRef
|
|
|
|
Size int64
|
|
|
|
Skipped bool // already present on blobserver
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 22:51:10 +00:00
|
|
|
func (pr *PutResult) SizedBlobRef() blobref.SizedBlobRef {
|
|
|
|
return blobref.SizedBlobRef{pr.BlobRef, pr.Size}
|
|
|
|
}
|
|
|
|
|
2011-05-10 19:56:59 +00:00
|
|
|
type statResponse struct {
|
|
|
|
HaveMap map[string]blobref.SizedBlobRef
|
|
|
|
maxUploadSize int64
|
|
|
|
uploadUrl string
|
|
|
|
uploadUrlExpirationSeconds int
|
|
|
|
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...))
|
|
|
|
}
|
|
|
|
|
2012-12-24 19:04:39 +00:00
|
|
|
func parseStatResponse(r io.Reader) (sr *statResponse, err error) {
|
2011-05-10 19:56:59 +00:00
|
|
|
var (
|
|
|
|
ok bool
|
|
|
|
s = &statResponse{HaveMap: make(map[string]blobref.SizedBlobRef)}
|
|
|
|
jmap = make(map[string]interface{})
|
|
|
|
)
|
2012-12-24 19:04:39 +00:00
|
|
|
if err := json.NewDecoder(io.LimitReader(r, 5<<20)).Decode(&jmap); err != nil {
|
2011-05-10 19:56:59 +00:00
|
|
|
return nil, ResponseFormatError(err)
|
|
|
|
}
|
2011-05-10 21:55:12 +00:00
|
|
|
defer func() {
|
|
|
|
if sr == nil {
|
|
|
|
log.Printf("parseStatResponse got map: %#v", jmap)
|
|
|
|
}
|
|
|
|
}()
|
2011-05-10 19:56:59 +00:00
|
|
|
|
|
|
|
s.uploadUrl, ok = jmap["uploadUrl"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, newResFormatError("no 'uploadUrl' in stat response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := jmap["maxUploadSize"].(float64); ok {
|
|
|
|
s.maxUploadSize = int64(n)
|
|
|
|
} else {
|
|
|
|
return nil, newResFormatError("no 'maxUploadSize' in stat response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := jmap["uploadUrlExpirationSeconds"].(float64); ok {
|
|
|
|
s.uploadUrlExpirationSeconds = int(n)
|
|
|
|
} else {
|
|
|
|
return nil, newResFormatError("no 'uploadUrlExpirationSeconds' in stat response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := jmap["canLongPoll"].(bool); ok {
|
|
|
|
s.canLongPoll = v
|
|
|
|
}
|
|
|
|
|
|
|
|
alreadyHave, ok := jmap["stat"].([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, newResFormatError("no 'stat' key in stat response")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, li := range alreadyHave {
|
|
|
|
m, ok := li.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, newResFormatError("'stat' list value of unexpected type %T", li)
|
|
|
|
}
|
|
|
|
blobRefStr, ok := m["blobRef"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, newResFormatError("'stat' list item has non-string 'blobRef' key")
|
|
|
|
}
|
|
|
|
size, ok := m["size"].(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, newResFormatError("'stat' list item has non-number 'size' key")
|
|
|
|
}
|
|
|
|
br := blobref.Parse(blobRefStr)
|
|
|
|
if br == nil {
|
|
|
|
return nil, newResFormatError("'stat' list item has invalid 'blobRef' key")
|
|
|
|
}
|
|
|
|
s.HaveMap[br.String()] = blobref.SizedBlobRef{br, int64(size)}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
2011-03-01 03:20:26 +00:00
|
|
|
|
2011-01-18 18:29:38 +00:00
|
|
|
func NewUploadHandleFromString(data string) *UploadHandle {
|
2011-11-27 19:06:12 +00:00
|
|
|
bref := blobref.SHA1FromString(data)
|
2011-05-29 17:38:53 +00:00
|
|
|
r := strings.NewReader(data)
|
|
|
|
return &UploadHandle{BlobRef: bref, Size: int64(len(data)), Contents: r}
|
2011-01-18 18:29:38 +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
|
|
|
func (c *Client) jsonFromResponse(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)
|
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(fmt.Sprintf("After %s request, HTTP response code is %d; no JSON to parse.", requestName, resp.StatusCode))
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
// TODO: LimitReader here for paranoia
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
jmap := make(map[string]interface{})
|
|
|
|
if jerr := json.Unmarshal(buf.Bytes(), &jmap); jerr != nil {
|
|
|
|
return nil, jerr
|
|
|
|
}
|
|
|
|
return jmap, nil
|
|
|
|
}
|
|
|
|
|
2013-02-03 05:51:08 +00:00
|
|
|
// statReq is a request to stat a blob.
|
|
|
|
type statReq struct {
|
|
|
|
br *blobref.BlobRef
|
|
|
|
dest chan<- blobref.SizedBlobRef // written to on success
|
|
|
|
errc chan<- error // written to on both failure and success (after any dest)
|
|
|
|
}
|
2011-05-10 13:25:18 +00:00
|
|
|
|
2013-02-03 05:51:08 +00:00
|
|
|
func (c *Client) StatBlobs(dest chan<- blobref.SizedBlobRef, blobs []*blobref.BlobRef, wait time.Duration) error {
|
|
|
|
var needStat []*blobref.BlobRef
|
2013-01-02 04:23:44 +00:00
|
|
|
for _, blob := range blobs {
|
2011-05-10 13:25:18 +00:00
|
|
|
if blob == nil {
|
|
|
|
panic("nil blob")
|
|
|
|
}
|
2013-01-02 04:23:44 +00:00
|
|
|
if size, ok := c.haveCache.StatBlobCache(blob); ok {
|
|
|
|
dest <- blobref.SizedBlobRef{blob, size}
|
2013-02-03 05:51:08 +00:00
|
|
|
} else {
|
|
|
|
if needStat == nil {
|
|
|
|
needStat = make([]*blobref.BlobRef, 0, len(blobs))
|
|
|
|
}
|
|
|
|
needStat = append(needStat, blob)
|
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
|
|
|
if wait > 0 {
|
|
|
|
// No batching on wait requests.
|
|
|
|
return c.doStat(dest, needStat, wait, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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[string][]statReq)
|
|
|
|
}
|
|
|
|
for _, blob := range needStat {
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
errcs = append(errcs, errc)
|
|
|
|
brStr := blob.String()
|
|
|
|
c.pendStat[brStr] = append(c.pendStat[brStr], 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.requestHTTPToken()
|
|
|
|
defer c.releaseHTTPToken()
|
|
|
|
|
|
|
|
var batch map[string][]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
|
|
|
|
}
|
|
|
|
batch = make(map[string][]statReq)
|
|
|
|
for blobStr, reqs := range c.pendStat {
|
|
|
|
batch[blobStr] = reqs
|
|
|
|
delete(c.pendStat, blobStr)
|
|
|
|
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
|
|
|
|
|
|
|
blobs := make([]*blobref.BlobRef, 0, len(batch))
|
|
|
|
for _, reqs := range batch {
|
|
|
|
// Just the first waiter's blobref is fine. All
|
|
|
|
// reqs[n] have the same br. This is just to avoid
|
|
|
|
// blobref.Parse from the 'batch' map's string key.
|
|
|
|
blobs = append(blobs, reqs[0].br)
|
|
|
|
}
|
|
|
|
|
|
|
|
ourDest := make(chan blobref.SizedBlobRef)
|
|
|
|
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.BlobRef.String()] {
|
|
|
|
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<- blobref.SizedBlobRef, blobs []*blobref.BlobRef, wait time.Duration, gated bool) error {
|
|
|
|
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
|
|
|
|
2011-05-10 20:02:48 +00:00
|
|
|
stat, err := parseStatResponse(resp.Body)
|
|
|
|
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.
|
|
|
|
// If the size was not provided (-1), slurp.
|
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 readerAndSize(h *UploadHandle) (io.Reader, int64, error) {
|
2011-10-11 00:01:52 +00:00
|
|
|
if h.Size != -1 {
|
|
|
|
return h.Contents, 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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-10-11 00:01:52 +00:00
|
|
|
bodyReader, bodySize, err := readerAndSize(h)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("client: error slurping upload handle to find its length: %v", err)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2012-12-31 21:45:13 +00:00
|
|
|
pr := &PutResult{BlobRef: h.BlobRef, Size: bodySize}
|
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
|
|
|
|
}
|
|
|
|
url_ := fmt.Sprintf("%s/camli/stat", pfx)
|
2013-01-02 20:55:12 +00:00
|
|
|
req := c.newRequest("POST", url_, strings.NewReader("camliversion=1&blob1="+blobrefStr))
|
2011-03-01 03:20:26 +00:00
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2013-02-03 05:51:08 +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("stat http error: %v", err)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
2011-09-16 04:52:33 +00:00
|
|
|
defer resp.Body.Close()
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2011-05-10 19:56:59 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2011-07-09 21:11:20 +00:00
|
|
|
return errorf("stat response had http status %d", resp.StatusCode)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 19:56:59 +00:00
|
|
|
stat, err := parseStatResponse(resp.Body)
|
2012-12-24 19:04:39 +00:00
|
|
|
resp.Body.Close()
|
2011-05-10 19:56:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
2013-01-02 04:23:44 +00:00
|
|
|
for _, sbr := range stat.HaveMap {
|
|
|
|
c.haveCache.NoteBlobExists(sbr.BlobRef, sbr.Size)
|
|
|
|
}
|
2013-01-30 15:06:03 +00:00
|
|
|
if !h.Vivify {
|
|
|
|
if _, ok := stat.HaveMap[blobrefStr]; ok {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
// c.log.Printf("Uploading %s to URL: %s", blobrefStr, stat.uploadUrl)
|
|
|
|
|
2011-05-10 19:56:59 +00:00
|
|
|
req = c.newRequest("POST", stat.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
|
2013-02-03 05:51:08 +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
|
|
|
}
|
2011-08-25 15:14:47 +00:00
|
|
|
baseUrl, _ := url.Parse(stat.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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-05-10 13:25:18 +00:00
|
|
|
ures, err := c.jsonFromResponse("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()
|
2011-09-28 18:06:16 +00:00
|
|
|
if pr.Size == -1 {
|
|
|
|
pr.Size = expectedSize
|
|
|
|
}
|
2013-01-02 04:23:44 +00:00
|
|
|
c.haveCache.NoteBlobExists(pr.BlobRef, 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
|
|
|
}
|