|
@@ -1,6 +1,7 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "bufio"
|
|
"bytes"
|
|
"bytes"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"errors"
|
|
"errors"
|
|
@@ -354,6 +355,36 @@ func sockRequest(method, endpoint string, data interface{}) (int, []byte, error)
|
|
}
|
|
}
|
|
|
|
|
|
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
|
|
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
|
|
|
|
+ req, client, err := newRequestClient(method, endpoint, data, ct)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resp, err := client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ client.Close()
|
|
|
|
+ return nil, nil, err
|
|
|
|
+ }
|
|
|
|
+ body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ return client.Close()
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ return resp, body, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func sockRequestHijack(method, endpoint string, data io.Reader, ct string) (net.Conn, *bufio.Reader, error) {
|
|
|
|
+ req, client, err := newRequestClient(method, endpoint, data, ct)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ client.Do(req)
|
|
|
|
+ conn, br := client.Hijack()
|
|
|
|
+ return conn, br, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func newRequestClient(method, endpoint string, data io.Reader, ct string) (*http.Request, *httputil.ClientConn, error) {
|
|
c, err := sockConn(time.Duration(10 * time.Second))
|
|
c, err := sockConn(time.Duration(10 * time.Second))
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
|
|
return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
|
|
@@ -370,18 +401,7 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.R
|
|
if ct != "" {
|
|
if ct != "" {
|
|
req.Header.Set("Content-Type", ct)
|
|
req.Header.Set("Content-Type", ct)
|
|
}
|
|
}
|
|
-
|
|
|
|
- resp, err := client.Do(req)
|
|
|
|
- if err != nil {
|
|
|
|
- client.Close()
|
|
|
|
- return nil, nil, fmt.Errorf("could not perform request: %v", err)
|
|
|
|
- }
|
|
|
|
- body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
|
|
|
- defer resp.Body.Close()
|
|
|
|
- return client.Close()
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- return resp, body, nil
|
|
|
|
|
|
+ return req, client, nil
|
|
}
|
|
}
|
|
|
|
|
|
func readBody(b io.ReadCloser) ([]byte, error) {
|
|
func readBody(b io.ReadCloser) ([]byte, error) {
|