mirror of https://github.com/perkeep/perkeep.git
Merge "client: factorize the transport setup, and use it in camget"
This commit is contained in:
commit
90ecd785ba
|
@ -23,7 +23,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -89,18 +88,11 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
httpStats := &httputil.StatsTransport{
|
tr := cl.TransportForConfig(&client.TransportConfig{
|
||||||
VerboseLog: *flagHTTP,
|
Verbose: *flagHTTP,
|
||||||
}
|
})
|
||||||
if *flagHTTP {
|
httpStats, _ := tr.(*httputil.StatsTransport)
|
||||||
httpStats.Transport = &http.Transport{
|
cl.SetHTTPClient(&http.Client{Transport: tr})
|
||||||
Dial: func(net_, addr string) (net.Conn, error) {
|
|
||||||
log.Printf("Dialing %s", addr)
|
|
||||||
return net.Dial(net_, addr)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cl.SetHTTPClient(&http.Client{Transport: httpStats})
|
|
||||||
|
|
||||||
diskCacheFetcher, err := cacher.NewDiskCache(cl)
|
diskCacheFetcher, err := cacher.NewDiskCache(cl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -85,6 +85,7 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
cl = client.NewOrFail() // automatic from flags
|
cl = client.NewOrFail() // automatic from flags
|
||||||
}
|
}
|
||||||
|
// TODO(mpl): probably needs the transport setup for trusted certs here.
|
||||||
|
|
||||||
diskCacheFetcher, err := cacher.NewDiskCache(cl)
|
diskCacheFetcher, err := cacher.NewDiskCache(cl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -107,31 +107,17 @@ func newUploader() *Uploader {
|
||||||
cc.SetLogger(nil)
|
cc.SetLogger(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
var transport http.RoundTripper
|
|
||||||
proxy := http.ProxyFromEnvironment
|
proxy := http.ProxyFromEnvironment
|
||||||
if flagProxyLocal {
|
if flagProxyLocal {
|
||||||
proxy = proxyFromEnvironment
|
proxy = proxyFromEnvironment
|
||||||
}
|
}
|
||||||
tlsConfig, err := cc.TLSConfig()
|
tr := cc.TransportForConfig(
|
||||||
if err != nil {
|
&client.TransportConfig{
|
||||||
log.Fatalf("Error while configuring TLS for client: %v", err)
|
Proxy: proxy,
|
||||||
}
|
Verbose: *flagHTTP,
|
||||||
transport = &http.Transport{
|
})
|
||||||
Dial: cc.DialFunc(),
|
httpStats, _ := tr.(*httputil.StatsTransport)
|
||||||
TLSClientConfig: tlsConfig,
|
cc.SetHTTPClient(&http.Client{Transport: tr})
|
||||||
Proxy: proxy,
|
|
||||||
}
|
|
||||||
|
|
||||||
httpStats := &httputil.StatsTransport{
|
|
||||||
VerboseLog: *flagHTTP,
|
|
||||||
Transport: transport,
|
|
||||||
}
|
|
||||||
transport = httpStats
|
|
||||||
|
|
||||||
if client.AndroidOutput() {
|
|
||||||
transport = client.AndroidStatsTransport{transport}
|
|
||||||
}
|
|
||||||
cc.SetHTTPClient(&http.Client{Transport: transport})
|
|
||||||
|
|
||||||
pwd, err := os.Getwd()
|
pwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -160,6 +160,7 @@ func (c *syncCmd) storageFromParam(which storageType, val string) (blobserver.St
|
||||||
return disk, nil
|
return disk, nil
|
||||||
}
|
}
|
||||||
cl := client.New(val)
|
cl := client.New(val)
|
||||||
|
// TODO(mpl): probably needs the transport setup for trusted certs here.
|
||||||
cl.SetupAuth()
|
cl.SetupAuth()
|
||||||
cl.SetLogger(c.logger)
|
cl.SetLogger(c.logger)
|
||||||
return noHub{cl}, nil
|
return noHub{cl}, nil
|
||||||
|
|
|
@ -35,6 +35,7 @@ import (
|
||||||
|
|
||||||
"camlistore.org/pkg/auth"
|
"camlistore.org/pkg/auth"
|
||||||
"camlistore.org/pkg/blobref"
|
"camlistore.org/pkg/blobref"
|
||||||
|
"camlistore.org/pkg/httputil"
|
||||||
"camlistore.org/pkg/misc"
|
"camlistore.org/pkg/misc"
|
||||||
"camlistore.org/pkg/schema"
|
"camlistore.org/pkg/schema"
|
||||||
"camlistore.org/pkg/search"
|
"camlistore.org/pkg/search"
|
||||||
|
@ -120,6 +121,49 @@ func NewOrFail() *Client {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TransportConfig contains options for SetupTransport.
|
||||||
|
type TransportConfig struct {
|
||||||
|
// Proxy optionally specifies the Proxy for the transport. Useful with
|
||||||
|
// camput for debugging even localhost requests.
|
||||||
|
Proxy func(*http.Request) (*url.URL, error)
|
||||||
|
Verbose bool // Verbose enables verbose logging of HTTP requests.
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransportForConfig returns a transport for the client, setting the correct
|
||||||
|
// Proxy, Dial, and TLSClientConfig if needed. It does not mutate c.
|
||||||
|
// It is the caller's responsibility to then use that transport to set
|
||||||
|
// the client's httpClient with SetHTTPClient.
|
||||||
|
func (c *Client) TransportForConfig(tc *TransportConfig) http.RoundTripper {
|
||||||
|
if c == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
tlsConfig, err := c.TLSConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error while configuring TLS for client: %v", err)
|
||||||
|
}
|
||||||
|
var transport http.RoundTripper
|
||||||
|
proxy := http.ProxyFromEnvironment
|
||||||
|
if tc != nil && tc.Proxy != nil {
|
||||||
|
proxy = tc.Proxy
|
||||||
|
}
|
||||||
|
transport = &http.Transport{
|
||||||
|
Dial: c.DialFunc(),
|
||||||
|
TLSClientConfig: tlsConfig,
|
||||||
|
Proxy: proxy,
|
||||||
|
}
|
||||||
|
httpStats := &httputil.StatsTransport{
|
||||||
|
Transport: transport,
|
||||||
|
}
|
||||||
|
if tc != nil {
|
||||||
|
httpStats.VerboseLog = tc.Verbose
|
||||||
|
}
|
||||||
|
transport = httpStats
|
||||||
|
if AndroidOutput() {
|
||||||
|
transport = &AndroidStatsTransport{transport}
|
||||||
|
}
|
||||||
|
return transport
|
||||||
|
}
|
||||||
|
|
||||||
var shareURLRx = regexp.MustCompile(`^(.+)/(` + blobref.Pattern + ")$")
|
var shareURLRx = regexp.MustCompile(`^(.+)/(` + blobref.Pattern + ")$")
|
||||||
|
|
||||||
func NewFromShareRoot(shareBlobURL string) (c *Client, target *blobref.BlobRef, err error) {
|
func NewFromShareRoot(shareBlobURL string) (c *Client, target *blobref.BlobRef, err error) {
|
||||||
|
@ -139,7 +183,10 @@ func NewFromShareRoot(shareBlobURL string) (c *Client, target *blobref.BlobRef,
|
||||||
c.via = make(map[string]string)
|
c.via = make(map[string]string)
|
||||||
root = m[2]
|
root = m[2]
|
||||||
|
|
||||||
res, err := http.Get(shareBlobURL)
|
c.SetHTTPClient(&http.Client{Transport: c.TransportForConfig(nil)})
|
||||||
|
|
||||||
|
req := c.newRequest("GET", shareBlobURL, nil)
|
||||||
|
res, err := c.doReqGated(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("Error fetching %s: %v", shareBlobURL, err)
|
return nil, nil, fmt.Errorf("Error fetching %s: %v", shareBlobURL, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue