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
|
|
|
)
|
|
|
|
|
2011-05-10 21:55:12 +00:00
|
|
|
var _ = log.Printf
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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) StatBlobs(dest chan<- blobref.SizedBlobRef, blobs []*blobref.BlobRef, wait time.Duration) error {
|
2011-05-10 13:25:18 +00:00
|
|
|
if len(blobs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: if len(blobs) > 1000 or something, cut this up into
|
|
|
|
// multiple http requests, and also if the server returns a
|
|
|
|
// 400 error, per the blob-stat-protocol.txt document.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf, "camliversion=1")
|
|
|
|
for n, blob := range blobs {
|
|
|
|
if blob == nil {
|
|
|
|
panic("nil blob")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&buf, "&blob%d=%s", n+1, blob)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-08 04:23:45 +00:00
|
|
|
pfx, err := c.prefix()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req := c.newRequest("POST", fmt.Sprintf("%s/camli/stat", pfx))
|
2011-09-28 18:06:16 +00:00
|
|
|
bodyStr := buf.String()
|
|
|
|
req.Body = ioutil.NopCloser(strings.NewReader(bodyStr))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.ContentLength = int64(len(bodyStr))
|
2011-05-10 13:25:18 +00:00
|
|
|
|
2012-12-28 17:24:26 +00:00
|
|
|
resp, err := c.doReq(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()
|
|
|
|
|
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)
|
2011-10-11 00:01:52 +00:00
|
|
|
requestBody := "camliversion=1&blob1=" + blobrefStr
|
2011-08-25 15:14:47 +00:00
|
|
|
req := c.newRequest("POST", url_)
|
2011-03-01 03:20:26 +00:00
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
2011-05-10 19:56:59 +00:00
|
|
|
req.Body = ioutil.NopCloser(strings.NewReader(requestBody))
|
2011-01-05 16:22:33 +00:00
|
|
|
req.ContentLength = int64(len(requestBody))
|
|
|
|
req.TransferEncoding = nil
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2012-12-28 17:24:26 +00:00
|
|
|
resp, err := c.doReq(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
|
|
|
}
|
|
|
|
|
2011-10-11 00:01:52 +00:00
|
|
|
pr := &PutResult{BlobRef: h.BlobRef, Size: bodySize}
|
|
|
|
if _, ok := stat.HaveMap[blobrefStr]; ok {
|
2011-05-10 19:56:59 +00:00
|
|
|
pr.Skipped = true
|
2011-10-11 00:01:52 +00:00
|
|
|
if closer, ok := h.Contents.(io.Closer); ok {
|
2011-05-10 21:55:12 +00:00
|
|
|
closer.Close()
|
|
|
|
}
|
2011-05-10 19:56:59 +00:00
|
|
|
return pr, nil
|
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
|
2011-01-13 17:59:49 +00:00
|
|
|
req.TransferEncoding = nil
|
2012-12-28 17:24:26 +00:00
|
|
|
resp, err = c.doReq(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
|
|
|
|
}
|
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
|
|
|
}
|