summaryrefslogtreecommitdiff
path: root/client/transport.go
diff options
context:
space:
mode:
authorStephen J Day <stephen.day@docker.com>2016-09-08 20:44:25 -0700
committerStephen J Day <stephen.day@docker.com>2016-09-19 11:19:55 -0700
commit9a072adff3fcd90c4f36214b355ef27b423f0144 (patch)
tree6446b5142faf77cfdd5af70093ed450b2f78bdf6 /client/transport.go
parent9d7be9df8f79b26a4b9778851cf19a6579f8bc61 (diff)
downloaddocker-9a072adff3fcd90c4f36214b355ef27b423f0144.tar.gz
client: remove transport package
This package doesn't really seem to do anything of real interest. Removing it and replacing with a few helper functions. Most of this was maintaining a fork of ctxhttp to support a mock that was unnecessary. We could probably do with a further refactor of the client interface. There is a lot of confusion of between transport, http layer and application layer that makes for some awkward code. This change improves the situation to the point where no breaking changes are introduced. Signed-off-by: Stephen J Day <stephen.day@docker.com>
Diffstat (limited to 'client/transport.go')
-rw-r--r--client/transport.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/client/transport.go b/client/transport.go
new file mode 100644
index 0000000000..43a667272d
--- /dev/null
+++ b/client/transport.go
@@ -0,0 +1,51 @@
+package client
+
+import (
+ "crypto/tls"
+ "errors"
+ "net/http"
+)
+
+var errTLSConfigUnavailable = errors.New("TLSConfig unavailable")
+
+// transportFunc allows us to inject a mock transport for testing. We define it
+// here so we can detect the tlsconfig and return nil for only this type.
+type transportFunc func(*http.Request) (*http.Response, error)
+
+func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) {
+ return tf(req)
+}
+
+// resolveTLSConfig attempts to resolve the tls configuration from the
+// RoundTripper.
+func resolveTLSConfig(transport http.RoundTripper) (*tls.Config, error) {
+ switch tr := transport.(type) {
+ case *http.Transport:
+ return tr.TLSClientConfig, nil
+ case transportFunc:
+ return nil, nil // detect this type for testing.
+ default:
+ return nil, errTLSConfigUnavailable
+ }
+}
+
+// resolveScheme detects a tls config on the transport and returns the
+// appropriate http scheme.
+//
+// TODO(stevvooe): This isn't really the right way to write clients in Go.
+// `NewClient` should probably only take an `*http.Client` and work from there.
+// Unfortunately, the model of having a host-ish/url-thingy as the connection
+// string has us confusing protocol and transport layers. We continue doing
+// this to avoid breaking existing clients but this should be addressed.
+func resolveScheme(transport http.RoundTripper) (string, error) {
+ c, err := resolveTLSConfig(transport)
+ if err != nil {
+ return "", err
+ }
+
+ if c != nil {
+ return "https", nil
+ }
+
+ return "http", nil
+}