[lxc-devel] [lxd/master] http client access in new client
stgraber on Github
lxc-bot at linuxcontainers.org
Tue Jul 25 03:54:03 UTC 2017
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170725/ff5205c2/attachment.bin>
-------------- next part --------------
From addcd5858a6f801baed2b823f1fd7504ae259801 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 24 Jul 2017 23:41:52 -0400
Subject: [PATCH 1/2] client: Allow specifying base http client
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Closes #3580
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
client/connection.go | 11 +++++++----
client/util.go | 18 ++++++++++--------
2 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/client/connection.go b/client/connection.go
index 02e0e6aa6..ae13332d3 100644
--- a/client/connection.go
+++ b/client/connection.go
@@ -29,6 +29,9 @@ type ConnectionArgs struct {
// Custom proxy
Proxy func(*http.Request) (*url.URL, error)
+
+ // Custom HTTP Client (used as base for the connection)
+ HTTPClient *http.Client
}
// ConnectLXD lets you connect to a remote LXD daemon over HTTPs.
@@ -55,7 +58,7 @@ func ConnectLXD(url string, args *ConnectionArgs) (ContainerServer, error) {
}
// Setup the HTTP client
- httpClient, err := tlsHTTPClient(args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy)
+ httpClient, err := tlsHTTPClient(args.HTTPClient, args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy)
if err != nil {
return nil, err
}
@@ -100,7 +103,7 @@ func ConnectLXDUnix(path string, args *ConnectionArgs) (ContainerServer, error)
}
// Setup the HTTP client
- httpClient, err := unixHTTPClient(path)
+ httpClient, err := unixHTTPClient(args.HTTPClient, path)
if err != nil {
return nil, err
}
@@ -138,7 +141,7 @@ func ConnectPublicLXD(url string, args *ConnectionArgs) (ImageServer, error) {
}
// Setup the HTTP client
- httpClient, err := tlsHTTPClient(args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy)
+ httpClient, err := tlsHTTPClient(args.HTTPClient, args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy)
if err != nil {
return nil, err
}
@@ -172,7 +175,7 @@ func ConnectSimpleStreams(url string, args *ConnectionArgs) (ImageServer, error)
}
// Setup the HTTP client
- httpClient, err := tlsHTTPClient(args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy)
+ httpClient, err := tlsHTTPClient(args.HTTPClient, args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy)
if err != nil {
return nil, err
}
diff --git a/client/util.go b/client/util.go
index de5073628..3649a0a83 100644
--- a/client/util.go
+++ b/client/util.go
@@ -13,7 +13,7 @@ import (
"github.com/lxc/lxd/shared/ioprogress"
)
-func tlsHTTPClient(tlsClientCert string, tlsClientKey string, tlsCA string, tlsServerCert string, proxy func(req *http.Request) (*url.URL, error)) (*http.Client, error) {
+func tlsHTTPClient(client *http.Client, tlsClientCert string, tlsClientKey string, tlsCA string, tlsServerCert string, proxy func(req *http.Request) (*url.URL, error)) (*http.Client, error) {
// Get the TLS configuration
tlsConfig, err := shared.GetTLSConfigMem(tlsClientCert, tlsClientKey, tlsCA, tlsServerCert)
if err != nil {
@@ -34,9 +34,10 @@ func tlsHTTPClient(tlsClientCert string, tlsClientKey string, tlsCA string, tlsS
}
// Define the http client
- client := http.Client{
- Transport: transport,
+ if client == nil {
+ client = &http.Client{}
}
+ client.Transport = transport
// Setup redirect policy
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
@@ -46,10 +47,10 @@ func tlsHTTPClient(tlsClientCert string, tlsClientKey string, tlsCA string, tlsS
return nil
}
- return &client, nil
+ return client, nil
}
-func unixHTTPClient(path string) (*http.Client, error) {
+func unixHTTPClient(client *http.Client, path string) (*http.Client, error) {
// Setup a Unix socket dialer
unixDial := func(network, addr string) (net.Conn, error) {
raddr, err := net.ResolveUnixAddr("unix", path)
@@ -67,9 +68,10 @@ func unixHTTPClient(path string) (*http.Client, error) {
}
// Define the http client
- client := http.Client{
- Transport: transport,
+ if client == nil {
+ client = &http.Client{}
}
+ client.Transport = transport
// Setup redirect policy
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
@@ -79,7 +81,7 @@ func unixHTTPClient(path string) (*http.Client, error) {
return nil
}
- return &client, nil
+ return client, nil
}
func downloadFileSha256(httpClient *http.Client, useragent string, progress func(progress ProgressData), canceler *cancel.Canceler, filename string, url string, hash string, target io.WriteSeeker) (int64, error) {
From 61187fb89b8f730b1d73da968a742a7584d5b809 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 24 Jul 2017 23:52:46 -0400
Subject: [PATCH 2/2] client: Make it possible to retrieve HTTP client
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Closes #3580
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
client/interfaces.go | 2 ++
client/lxd.go | 9 +++++++++
client/simplestreams.go | 10 ++++++++++
3 files changed, 21 insertions(+)
diff --git a/client/interfaces.go b/client/interfaces.go
index ed72b5c28..1260c3e9a 100644
--- a/client/interfaces.go
+++ b/client/interfaces.go
@@ -2,6 +2,7 @@ package lxd
import (
"io"
+ "net/http"
"github.com/gorilla/websocket"
@@ -12,6 +13,7 @@ import (
// The Server type represents a generic read-only server.
type Server interface {
GetConnectionInfo() (info *ConnectionInfo, err error)
+ GetHTTPClient() (client *http.Client, err error)
}
// The ImageServer type represents a read-only image server.
diff --git a/client/lxd.go b/client/lxd.go
index a6e071308..3473e317c 100644
--- a/client/lxd.go
+++ b/client/lxd.go
@@ -53,6 +53,15 @@ func (r *ProtocolLXD) GetConnectionInfo() (*ConnectionInfo, error) {
return &info, nil
}
+// GetHTTPClient returns the http client used for the connection. This can be used to set custom http options.
+func (r *ProtocolLXD) GetHTTPClient() (*http.Client, error) {
+ if r.http == nil {
+ return nil, fmt.Errorf("HTTP client isn't set, bad connection")
+ }
+
+ return r.http, nil
+}
+
// RawQuery allows directly querying the LXD API
//
// This should only be used by internal LXD tools.
diff --git a/client/simplestreams.go b/client/simplestreams.go
index 79fef42ae..68fa55b8b 100644
--- a/client/simplestreams.go
+++ b/client/simplestreams.go
@@ -1,6 +1,7 @@
package lxd
import (
+ "fmt"
"net/http"
"github.com/lxc/lxd/shared/simplestreams"
@@ -25,3 +26,12 @@ func (r *ProtocolSimpleStreams) GetConnectionInfo() (*ConnectionInfo, error) {
return &info, nil
}
+
+// GetHTTPClient returns the http client used for the connection. This can be used to set custom http options.
+func (r *ProtocolSimpleStreams) GetHTTPClient() (*http.Client, error) {
+ if r.http == nil {
+ return nil, fmt.Errorf("HTTP client isn't set, bad connection")
+ }
+
+ return r.http, nil
+}
More information about the lxc-devel
mailing list