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 (
|
2012-12-23 06:48:21 +00:00
|
|
|
"bytes"
|
2012-11-08 04:23:45 +00:00
|
|
|
"encoding/json"
|
2012-12-23 02:42:35 +00:00
|
|
|
"errors"
|
2012-11-08 04:23:45 +00:00
|
|
|
"fmt"
|
2012-12-23 06:48:21 +00:00
|
|
|
"io"
|
2012-04-22 23:19:04 +00:00
|
|
|
"io/ioutil"
|
2011-01-18 18:29:38 +00:00
|
|
|
"log"
|
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"
|
2012-11-08 04:23:45 +00:00
|
|
|
"net/url"
|
2011-01-18 18:29:38 +00:00
|
|
|
"os"
|
2013-01-02 20:55:12 +00:00
|
|
|
"regexp"
|
2012-11-08 04:23:45 +00:00
|
|
|
"strings"
|
2011-01-02 22:36:03 +00:00
|
|
|
"sync"
|
2011-12-02 10:35:28 +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/auth"
|
2012-12-23 06:48:21 +00:00
|
|
|
"camlistore.org/pkg/blobref"
|
2013-01-02 20:55:12 +00:00
|
|
|
"camlistore.org/pkg/schema"
|
2011-01-02 22:36:03 +00:00
|
|
|
)
|
|
|
|
|
2012-12-23 06:48:21 +00:00
|
|
|
// A Client provides access to a Camlistore server.
|
2011-09-17 22:14:37 +00:00
|
|
|
type Client struct {
|
2012-11-08 04:23:45 +00:00
|
|
|
// server is the input from user, pre-discovery.
|
|
|
|
// For example "http://foo.com" or "foo.com:1234".
|
|
|
|
// It is the responsibility of initPrefix to parse
|
|
|
|
// server and set prefix, including doing discovery
|
|
|
|
// to figure out what the proper server-declared
|
|
|
|
// prefix is.
|
|
|
|
server string
|
|
|
|
|
|
|
|
prefixOnce sync.Once
|
|
|
|
prefixErr error
|
2012-12-23 06:48:21 +00:00
|
|
|
prefixv string // URL prefix before "/camli/"
|
|
|
|
|
|
|
|
discoOnce sync.Once
|
|
|
|
discoErr error
|
2013-01-15 13:53:25 +00:00
|
|
|
searchRoot string // Handler prefix, or "" if none
|
|
|
|
downloadHelper string // or "" if none
|
|
|
|
storageGen string // storage generation, or "" if not reported
|
|
|
|
syncHandlers []*SyncInfo // "from" and "to" url prefix for each syncHandler
|
2012-11-08 04:23:45 +00:00
|
|
|
|
2011-12-02 10:35:28 +00:00
|
|
|
authMode auth.AuthMode
|
2011-09-17 22:14:37 +00:00
|
|
|
|
|
|
|
httpClient *http.Client
|
2012-12-31 21:45:13 +00:00
|
|
|
haveCache HaveCache
|
2011-09-17 22:14:37 +00:00
|
|
|
|
|
|
|
statsMutex sync.Mutex
|
|
|
|
stats Stats
|
|
|
|
|
2013-01-02 20:55:12 +00:00
|
|
|
// via maps the access path from a share root to a desired target.
|
|
|
|
// It is non-nil when in "sharing" mode, where the Client is fetching
|
|
|
|
// a share.
|
|
|
|
via map[string]string // target => via (target is referenced from via)
|
|
|
|
|
2012-12-28 17:24:26 +00:00
|
|
|
log *log.Logger // not nil
|
|
|
|
reqGate chan bool
|
2011-09-17 22:14:37 +00:00
|
|
|
}
|
|
|
|
|
2012-12-28 17:24:26 +00:00
|
|
|
const maxParallelHTTP = 5
|
|
|
|
|
2012-12-23 06:48:21 +00:00
|
|
|
// New returns a new Camlistore Client.
|
|
|
|
// The provided server is either "host:port" (assumed http, not https) or a
|
|
|
|
// URL prefix, with or without a path.
|
|
|
|
// Errors are not returned until subsequent operations.
|
2011-11-16 10:41:38 +00:00
|
|
|
func New(server string) *Client {
|
2011-03-02 02:02:01 +00:00
|
|
|
return &Client{
|
2011-05-10 19:56:40 +00:00
|
|
|
server: server,
|
2011-05-10 13:25:18 +00:00
|
|
|
httpClient: http.DefaultClient,
|
2012-12-28 17:24:26 +00:00
|
|
|
reqGate: make(chan bool, maxParallelHTTP),
|
2012-12-31 21:45:13 +00:00
|
|
|
haveCache: noHaveCache{},
|
2011-03-02 02:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 21:45:13 +00:00
|
|
|
func NewOrFail() *Client {
|
|
|
|
c := New(blobServerOrDie())
|
|
|
|
c.log = log.New(os.Stderr, "", log.Ldate|log.Ltime)
|
|
|
|
err := c.SetupAuth()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2013-01-02 20:55:12 +00:00
|
|
|
var shareURLRx = regexp.MustCompile(`^(.+)/camli/(` + blobref.Pattern + ")")
|
|
|
|
|
|
|
|
func NewFromShareRoot(shareBlobURL string) (c *Client, target *blobref.BlobRef, err error) {
|
|
|
|
var root string
|
|
|
|
if m := shareURLRx.FindStringSubmatch(shareBlobURL); m == nil {
|
|
|
|
return nil, nil, fmt.Errorf("Unkown URL base; doesn't contain /camli/")
|
|
|
|
} else {
|
|
|
|
c = New(m[1])
|
|
|
|
c.discoOnce.Do(func() { /* nothing */
|
|
|
|
})
|
|
|
|
c.prefixOnce.Do(func() { /* nothing */
|
|
|
|
})
|
|
|
|
c.prefixv = m[1]
|
|
|
|
c.authMode = auth.None{}
|
|
|
|
c.via = make(map[string]string)
|
|
|
|
root = m[2]
|
|
|
|
}
|
|
|
|
res, err := http.Get(shareBlobURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Error fetching %s: %v", shareBlobURL, err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
ss, err := schema.ParseSuperset(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Error parsing JSON from %s: %v", shareBlobURL, err)
|
|
|
|
}
|
|
|
|
if ss.AuthType != "haveref" {
|
|
|
|
return nil, nil, fmt.Errorf("Unknown share authType of %q", ss.AuthType)
|
|
|
|
}
|
|
|
|
if ss.Target == nil {
|
|
|
|
return nil, nil, fmt.Errorf("No target.")
|
|
|
|
}
|
|
|
|
c.via[ss.Target.String()] = root
|
|
|
|
// TODO(bradfitz): send via in requests, populate via as we fetch more things
|
|
|
|
return c, ss.Target, nil
|
|
|
|
}
|
|
|
|
|
2012-11-08 03:06:10 +00:00
|
|
|
// SetHTTPClient sets the Camlistore client's HTTP client.
|
|
|
|
// If nil, the default HTTP client is used.
|
2012-11-08 03:03:46 +00:00
|
|
|
func (c *Client) SetHTTPClient(client *http.Client) {
|
2012-11-08 03:06:10 +00:00
|
|
|
if client == nil {
|
|
|
|
client = http.DefaultClient
|
|
|
|
}
|
2011-05-10 13:25:18 +00:00
|
|
|
c.httpClient = client
|
|
|
|
}
|
|
|
|
|
2012-12-31 21:45:13 +00:00
|
|
|
// A HaveCache caches whether a remote blobserver has a blob.
|
|
|
|
type HaveCache interface {
|
2013-01-02 04:23:44 +00:00
|
|
|
StatBlobCache(br *blobref.BlobRef) (size int64, ok bool)
|
|
|
|
NoteBlobExists(br *blobref.BlobRef, size int64)
|
2012-12-31 21:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type noHaveCache struct{}
|
|
|
|
|
2013-01-02 04:23:44 +00:00
|
|
|
func (noHaveCache) StatBlobCache(*blobref.BlobRef) (int64, bool) { return 0, false }
|
|
|
|
func (noHaveCache) NoteBlobExists(*blobref.BlobRef, int64) {}
|
2012-12-31 21:45:13 +00:00
|
|
|
|
|
|
|
func (c *Client) SetHaveCache(cache HaveCache) {
|
|
|
|
if cache == nil {
|
|
|
|
cache = noHaveCache{}
|
2011-11-16 10:41:38 +00:00
|
|
|
}
|
2012-12-31 21:45:13 +00:00
|
|
|
c.haveCache = cache
|
2011-01-18 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetLogger(logger *log.Logger) {
|
|
|
|
if logger == nil {
|
2012-04-22 23:19:04 +00:00
|
|
|
c.log = log.New(ioutil.Discard, "", 0)
|
2011-01-18 18:29:38 +00:00
|
|
|
} else {
|
|
|
|
c.log = logger
|
|
|
|
}
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Stats() Stats {
|
|
|
|
c.statsMutex.Lock()
|
|
|
|
defer c.statsMutex.Unlock()
|
2011-05-10 13:25:18 +00:00
|
|
|
return c.stats // copy
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
2011-01-15 01:22:45 +00:00
|
|
|
|
2012-12-23 02:42:35 +00:00
|
|
|
// ErrNoSearchRoot is returned by SearchRoot if the server doesn't support search.
|
|
|
|
var ErrNoSearchRoot = errors.New("client: server doesn't support search")
|
|
|
|
|
2012-12-31 19:36:58 +00:00
|
|
|
// ErrNoStorageGeneration is returned by StorageGeneration if the
|
|
|
|
// server doesn't report a storage generation value.
|
|
|
|
var ErrNoStorageGeneration = errors.New("client: server doesn't report a storage generation")
|
|
|
|
|
2013-01-15 13:53:25 +00:00
|
|
|
// ErrNoSync is returned by SyncHandlers if the server does not advertise syncs.
|
|
|
|
var ErrNoSync = errors.New("client: server has no sync handlers")
|
|
|
|
|
|
|
|
// BlobRoot returns the server's blobroot URL prefix.
|
|
|
|
// If the client was constructed with an explicit path,
|
|
|
|
// that path is used. Otherwise the server's
|
|
|
|
// default advertised blobRoot is used.
|
|
|
|
func (c *Client) BlobRoot() (string, error) {
|
|
|
|
prefix, err := c.prefix()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return prefix + "/", nil
|
|
|
|
}
|
|
|
|
|
2012-12-31 19:36:58 +00:00
|
|
|
// SearchRoot returns the server's search handler.
|
|
|
|
// If the server isn't running an index and search handler, the error
|
|
|
|
// will be ErrNoSearchRoot.
|
2012-12-23 02:42:35 +00:00
|
|
|
func (c *Client) SearchRoot() (string, error) {
|
2012-12-23 06:48:21 +00:00
|
|
|
c.condDiscovery()
|
|
|
|
if c.discoErr != nil {
|
|
|
|
return "", c.discoErr
|
|
|
|
}
|
|
|
|
if c.searchRoot == "" {
|
|
|
|
return "", ErrNoSearchRoot
|
|
|
|
}
|
|
|
|
return c.searchRoot, nil
|
|
|
|
}
|
|
|
|
|
2012-12-31 19:36:58 +00:00
|
|
|
// StorageGeneration returns the server's unique ID for its storage
|
|
|
|
// generation, reset whenever storage is reset, moved, or partially
|
|
|
|
// lost.
|
|
|
|
//
|
|
|
|
// This is a value that can be used in client cache keys to add
|
|
|
|
// certainty that they're talking to the same instance as previously.
|
|
|
|
//
|
|
|
|
// If the server doesn't return such a value, the error will be
|
|
|
|
// ErrNoStorageGeneration.
|
|
|
|
func (c *Client) StorageGeneration() (string, error) {
|
|
|
|
c.condDiscovery()
|
|
|
|
if c.discoErr != nil {
|
|
|
|
return "", c.discoErr
|
|
|
|
}
|
|
|
|
if c.storageGen == "" {
|
|
|
|
return "", ErrNoStorageGeneration
|
|
|
|
}
|
|
|
|
return c.storageGen, nil
|
|
|
|
}
|
|
|
|
|
2013-01-15 13:53:25 +00:00
|
|
|
// SyncInfo holds the data that were acquired with a discovery
|
|
|
|
// and that are relevant to a syncHandler.
|
|
|
|
type SyncInfo struct {
|
|
|
|
From string
|
|
|
|
To string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncHandlers returns the server's sync handlers "from" and
|
|
|
|
// "to" prefix URLs.
|
|
|
|
// If the server isn't running any sync handler, the error
|
|
|
|
// will be ErrNoSync.
|
|
|
|
func (c *Client) SyncHandlers() ([]*SyncInfo, error) {
|
|
|
|
c.condDiscovery()
|
|
|
|
if c.discoErr != nil {
|
|
|
|
return nil, c.discoErr
|
|
|
|
}
|
|
|
|
if c.syncHandlers == nil {
|
|
|
|
return nil, ErrNoSync
|
|
|
|
}
|
|
|
|
return c.syncHandlers, nil
|
|
|
|
}
|
|
|
|
|
2012-12-23 06:48:21 +00:00
|
|
|
// SearchExistingFileSchema does a search query looking for an
|
|
|
|
// existing file with entire contents of wholeRef, then does a HEAD
|
|
|
|
// request to verify the file still exists on the server. If so,
|
|
|
|
// it returns that file schema's blobref.
|
|
|
|
//
|
|
|
|
// May return (nil, nil) on ENOENT. A non-nil error is only returned
|
|
|
|
// if there were problems searching.
|
|
|
|
func (c *Client) SearchExistingFileSchema(wholeRef *blobref.BlobRef) (*blobref.BlobRef, error) {
|
|
|
|
sr, err := c.SearchRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
url := sr + "camli/search/files?wholedigest=" + wholeRef.String()
|
|
|
|
req := c.newRequest("GET", url)
|
2012-12-28 17:24:26 +00:00
|
|
|
res, err := c.doReq(req)
|
2012-12-23 06:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-12-28 17:24:26 +00:00
|
|
|
defer res.Body.Close()
|
2012-12-23 06:48:21 +00:00
|
|
|
var buf bytes.Buffer
|
2012-12-26 21:17:36 +00:00
|
|
|
body := io.TeeReader(io.LimitReader(res.Body, 1<<20), &buf)
|
2012-12-26 21:36:45 +00:00
|
|
|
type justWriter struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
2012-12-23 06:48:21 +00:00
|
|
|
if res.StatusCode != 200 {
|
2012-12-26 21:36:45 +00:00
|
|
|
io.Copy(justWriter{ioutil.Discard}, body) // golang.org/issue/4589
|
2012-12-23 06:48:21 +00:00
|
|
|
return nil, fmt.Errorf("client: got status code %d from URL %s; body %s", res.StatusCode, url, buf.String())
|
|
|
|
}
|
|
|
|
var ress struct {
|
|
|
|
Files []*blobref.BlobRef `json:"files"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(body).Decode(&ress); err != nil {
|
2012-12-26 21:36:45 +00:00
|
|
|
io.Copy(justWriter{ioutil.Discard}, body) // golang.org/issue/4589
|
2012-12-23 06:48:21 +00:00
|
|
|
return nil, fmt.Errorf("client: error parsing JSON from URL %s: %v; body=%s", url, err, buf.String())
|
|
|
|
}
|
|
|
|
if len(ress.Files) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
for _, f := range ress.Files {
|
|
|
|
if c.FileHasContents(f, wholeRef) {
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileHasContents returns true iff f refers to a "file" or "bytes" schema blob,
|
|
|
|
// the server is configured with a "download helper", and the server responds
|
|
|
|
// that all chunks of 'f' are available and match the digest of wholeRef.
|
|
|
|
func (c *Client) FileHasContents(f, wholeRef *blobref.BlobRef) bool {
|
|
|
|
c.condDiscovery()
|
|
|
|
if c.discoErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.downloadHelper == "" {
|
|
|
|
return false
|
|
|
|
}
|
2012-12-26 21:17:36 +00:00
|
|
|
req := c.newRequest("HEAD", c.downloadHelper+f.String()+"/?verifycontents="+wholeRef.String())
|
2012-12-28 17:24:26 +00:00
|
|
|
res, err := c.doReq(req)
|
2012-12-23 06:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("download helper HEAD error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
return res.Header.Get("X-Camli-Contents") == wholeRef.String()
|
2012-12-23 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 04:23:45 +00:00
|
|
|
func (c *Client) prefix() (string, error) {
|
|
|
|
c.prefixOnce.Do(func() { c.initPrefix() })
|
|
|
|
if c.prefixErr != nil {
|
|
|
|
return "", c.prefixErr
|
|
|
|
}
|
2012-12-23 06:48:21 +00:00
|
|
|
if c.discoErr != nil {
|
|
|
|
return "", c.discoErr
|
|
|
|
}
|
2012-11-08 04:23:45 +00:00
|
|
|
return c.prefixv, nil
|
|
|
|
}
|
|
|
|
|
2012-12-23 06:48:21 +00:00
|
|
|
func (c *Client) discoRoot() string {
|
2012-11-08 04:23:45 +00:00
|
|
|
s := c.server
|
|
|
|
if !strings.HasPrefix(s, "http") {
|
|
|
|
s = "http://" + s
|
|
|
|
}
|
2012-12-23 06:48:21 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2013-01-15 13:53:25 +00:00
|
|
|
// initPrefix uses the user provided server URL to define the URL
|
|
|
|
// prefix to the blobserver root. If the server URL has a path
|
|
|
|
// component then it is directly used, otherwise the blobRoot
|
|
|
|
// from the discovery is used as the path.
|
2012-12-23 06:48:21 +00:00
|
|
|
func (c *Client) initPrefix() {
|
|
|
|
root := c.discoRoot()
|
|
|
|
u, err := url.Parse(root)
|
2012-11-08 04:23:45 +00:00
|
|
|
if err != nil {
|
|
|
|
c.prefixErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(u.Path) > 1 {
|
2012-12-23 06:48:21 +00:00
|
|
|
c.prefixv = strings.TrimRight(root, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.condDiscovery()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) condDiscovery() {
|
|
|
|
c.discoOnce.Do(func() { c.doDiscovery() })
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) doDiscovery() {
|
|
|
|
root, err := url.Parse(c.discoRoot())
|
|
|
|
if err != nil {
|
|
|
|
c.discoErr = err
|
2012-11-08 04:23:45 +00:00
|
|
|
return
|
|
|
|
}
|
2012-12-23 06:48:21 +00:00
|
|
|
|
2012-11-08 04:23:45 +00:00
|
|
|
// If the path is just "" or "/", do discovery against
|
|
|
|
// the URL to see which path we should actually use.
|
2012-12-23 06:48:21 +00:00
|
|
|
req, _ := http.NewRequest("GET", c.discoRoot(), nil)
|
2012-11-08 04:23:45 +00:00
|
|
|
req.Header.Set("Accept", "text/x-camli-configuration")
|
2012-12-23 06:48:21 +00:00
|
|
|
c.authMode.AddAuthHeader(req)
|
2012-12-28 17:24:26 +00:00
|
|
|
res, err := c.doReq(req)
|
2012-11-08 04:23:45 +00:00
|
|
|
if err != nil {
|
2012-12-23 06:48:21 +00:00
|
|
|
c.discoErr = err
|
2012-11-08 04:23:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
2012-12-23 06:48:21 +00:00
|
|
|
c.discoErr = fmt.Errorf("Got status %q from blobserver URL %q during configuration discovery", res.Status, c.discoRoot())
|
2012-11-08 04:23:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO(bradfitz): little weird in retrospect that we request
|
|
|
|
// text/x-camli-configuration and expect to get back
|
|
|
|
// text/javascript. Make them consistent.
|
|
|
|
if ct := res.Header.Get("Content-Type"); ct != "text/javascript" {
|
2012-12-23 06:48:21 +00:00
|
|
|
c.discoErr = fmt.Errorf("Blobserver returned unexpected type %q from discovery", ct)
|
2012-11-08 04:23:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&m); err != nil {
|
2012-12-23 06:48:21 +00:00
|
|
|
c.discoErr = err
|
2012-11-08 04:23:45 +00:00
|
|
|
return
|
|
|
|
}
|
2012-12-23 06:48:21 +00:00
|
|
|
searchRoot, ok := m["searchRoot"].(string)
|
|
|
|
if ok {
|
|
|
|
u, err := root.Parse(searchRoot)
|
|
|
|
if err != nil {
|
|
|
|
c.discoErr = fmt.Errorf("client: invalid searchRoot %q; failed to resolve", searchRoot)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.searchRoot = u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadHelper, ok := m["downloadHelper"].(string)
|
|
|
|
if ok {
|
|
|
|
u, err := root.Parse(downloadHelper)
|
|
|
|
if err != nil {
|
|
|
|
c.discoErr = fmt.Errorf("client: invalid downloadHelper %q; failed to resolve", downloadHelper)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.downloadHelper = u.String()
|
|
|
|
}
|
|
|
|
|
2012-12-31 19:36:58 +00:00
|
|
|
c.storageGen, _ = m["storageGeneration"].(string)
|
|
|
|
|
2012-11-08 04:23:45 +00:00
|
|
|
blobRoot, ok := m["blobRoot"].(string)
|
|
|
|
if !ok {
|
2012-12-23 06:48:21 +00:00
|
|
|
c.discoErr = fmt.Errorf("No blobRoot in config discovery response")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u, err := root.Parse(blobRoot)
|
|
|
|
if err != nil {
|
|
|
|
c.discoErr = fmt.Errorf("client: error resolving blobRoot: %v", err)
|
2012-11-08 04:23:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.prefixv = strings.TrimRight(u.String(), "/")
|
2013-01-15 13:53:25 +00:00
|
|
|
|
|
|
|
syncHandlers, ok := m["syncHandlers"].([]interface{})
|
|
|
|
if ok {
|
|
|
|
for _, v := range syncHandlers {
|
|
|
|
vmap := v.(map[string]interface{})
|
|
|
|
from := vmap["from"].(string)
|
|
|
|
ufrom, err := root.Parse(from)
|
|
|
|
if err != nil {
|
|
|
|
c.discoErr = fmt.Errorf("client: invalid %q \"from\" sync; failed to resolve", from)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
to := vmap["to"].(string)
|
|
|
|
uto, err := root.Parse(to)
|
|
|
|
if err != nil {
|
|
|
|
c.discoErr = fmt.Errorf("client: invalid %q \"to\" sync; failed to resolve", to)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.syncHandlers = append(c.syncHandlers,
|
|
|
|
&SyncInfo{From: ufrom.String(), To: uto.String()})
|
|
|
|
}
|
|
|
|
}
|
2012-11-08 04:23:45 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 04:23:44 +00:00
|
|
|
func (c *Client) newRequest(method, url string, body ...io.Reader) *http.Request {
|
|
|
|
var bodyR io.Reader
|
|
|
|
if len(body) > 0 {
|
|
|
|
bodyR = body[0]
|
|
|
|
}
|
|
|
|
if len(body) > 1 {
|
|
|
|
panic("too many body arguments")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, url, bodyR)
|
2011-05-15 00:14:54 +00:00
|
|
|
if err != 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
|
|
|
panic(err.Error())
|
2011-05-15 00:14:54 +00:00
|
|
|
}
|
2013-01-02 04:23:44 +00:00
|
|
|
// not done by http.NewRequest in Go 1.0:
|
|
|
|
if br, ok := bodyR.(*bytes.Reader); ok {
|
|
|
|
req.ContentLength = int64(br.Len())
|
|
|
|
}
|
2011-12-02 10:35:28 +00:00
|
|
|
c.authMode.AddAuthHeader(req)
|
2011-05-15 00:14:54 +00:00
|
|
|
return req
|
|
|
|
}
|
2012-12-28 17:24:26 +00:00
|
|
|
|
|
|
|
func (c *Client) doReq(req *http.Request) (*http.Response, error) {
|
|
|
|
c.reqGate <- true
|
|
|
|
defer func() {
|
|
|
|
<-c.reqGate
|
|
|
|
}()
|
|
|
|
return c.httpClient.Do(req)
|
|
|
|
}
|