|
@@ -12,6 +12,7 @@ import (
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types"
|
|
|
|
+ "github.com/docker/docker/api/types/versions"
|
|
"github.com/docker/go-connections/sockets"
|
|
"github.com/docker/go-connections/sockets"
|
|
"github.com/pkg/errors"
|
|
"github.com/pkg/errors"
|
|
)
|
|
)
|
|
@@ -30,12 +31,12 @@ func (cli *Client) postHijacked(ctx context.Context, path string, query url.Valu
|
|
}
|
|
}
|
|
req = cli.addHeaders(req, headers)
|
|
req = cli.addHeaders(req, headers)
|
|
|
|
|
|
- conn, err := cli.setupHijackConn(ctx, req, "tcp")
|
|
|
|
|
|
+ conn, mediaType, err := cli.setupHijackConn(ctx, req, "tcp")
|
|
if err != nil {
|
|
if err != nil {
|
|
return types.HijackedResponse{}, err
|
|
return types.HijackedResponse{}, err
|
|
}
|
|
}
|
|
|
|
|
|
- return types.HijackedResponse{Conn: conn, Reader: bufio.NewReader(conn)}, err
|
|
|
|
|
|
+ return types.NewHijackedResponse(conn, mediaType), err
|
|
}
|
|
}
|
|
|
|
|
|
// DialHijack returns a hijacked connection with negotiated protocol proto.
|
|
// DialHijack returns a hijacked connection with negotiated protocol proto.
|
|
@@ -46,7 +47,8 @@ func (cli *Client) DialHijack(ctx context.Context, url, proto string, meta map[s
|
|
}
|
|
}
|
|
req = cli.addHeaders(req, meta)
|
|
req = cli.addHeaders(req, meta)
|
|
|
|
|
|
- return cli.setupHijackConn(ctx, req, proto)
|
|
|
|
|
|
+ conn, _, err := cli.setupHijackConn(ctx, req, proto)
|
|
|
|
+ return conn, err
|
|
}
|
|
}
|
|
|
|
|
|
// fallbackDial is used when WithDialer() was not called.
|
|
// fallbackDial is used when WithDialer() was not called.
|
|
@@ -61,7 +63,7 @@ func fallbackDial(proto, addr string, tlsConfig *tls.Config) (net.Conn, error) {
|
|
return net.Dial(proto, addr)
|
|
return net.Dial(proto, addr)
|
|
}
|
|
}
|
|
|
|
|
|
-func (cli *Client) setupHijackConn(ctx context.Context, req *http.Request, proto string) (net.Conn, error) {
|
|
|
|
|
|
+func (cli *Client) setupHijackConn(ctx context.Context, req *http.Request, proto string) (net.Conn, string, error) {
|
|
req.Host = cli.addr
|
|
req.Host = cli.addr
|
|
req.Header.Set("Connection", "Upgrade")
|
|
req.Header.Set("Connection", "Upgrade")
|
|
req.Header.Set("Upgrade", proto)
|
|
req.Header.Set("Upgrade", proto)
|
|
@@ -69,7 +71,7 @@ func (cli *Client) setupHijackConn(ctx context.Context, req *http.Request, proto
|
|
dialer := cli.Dialer()
|
|
dialer := cli.Dialer()
|
|
conn, err := dialer(ctx)
|
|
conn, err := dialer(ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, errors.Wrap(err, "cannot connect to the Docker daemon. Is 'docker daemon' running on this host?")
|
|
|
|
|
|
+ return nil, "", errors.Wrap(err, "cannot connect to the Docker daemon. Is 'docker daemon' running on this host?")
|
|
}
|
|
}
|
|
|
|
|
|
// When we set up a TCP connection for hijack, there could be long periods
|
|
// When we set up a TCP connection for hijack, there could be long periods
|
|
@@ -91,18 +93,18 @@ func (cli *Client) setupHijackConn(ctx context.Context, req *http.Request, proto
|
|
//nolint:staticcheck // ignore SA1019 for connecting to old (pre go1.8) daemons
|
|
//nolint:staticcheck // ignore SA1019 for connecting to old (pre go1.8) daemons
|
|
if err != httputil.ErrPersistEOF {
|
|
if err != httputil.ErrPersistEOF {
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, "", err
|
|
}
|
|
}
|
|
if resp.StatusCode != http.StatusSwitchingProtocols {
|
|
if resp.StatusCode != http.StatusSwitchingProtocols {
|
|
resp.Body.Close()
|
|
resp.Body.Close()
|
|
- return nil, fmt.Errorf("unable to upgrade to %s, received %d", proto, resp.StatusCode)
|
|
|
|
|
|
+ return nil, "", fmt.Errorf("unable to upgrade to %s, received %d", proto, resp.StatusCode)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
c, br := clientconn.Hijack()
|
|
c, br := clientconn.Hijack()
|
|
if br.Buffered() > 0 {
|
|
if br.Buffered() > 0 {
|
|
// If there is buffered content, wrap the connection. We return an
|
|
// If there is buffered content, wrap the connection. We return an
|
|
- // object that implements CloseWrite iff the underlying connection
|
|
|
|
|
|
+ // object that implements CloseWrite if the underlying connection
|
|
// implements it.
|
|
// implements it.
|
|
if _, ok := c.(types.CloseWriter); ok {
|
|
if _, ok := c.(types.CloseWriter); ok {
|
|
c = &hijackedConnCloseWriter{&hijackedConn{c, br}}
|
|
c = &hijackedConnCloseWriter{&hijackedConn{c, br}}
|
|
@@ -113,7 +115,13 @@ func (cli *Client) setupHijackConn(ctx context.Context, req *http.Request, proto
|
|
br.Reset(nil)
|
|
br.Reset(nil)
|
|
}
|
|
}
|
|
|
|
|
|
- return c, nil
|
|
|
|
|
|
+ var mediaType string
|
|
|
|
+ if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.42") {
|
|
|
|
+ // Prior to 1.42, Content-Type is always set to raw-stream and not relevant
|
|
|
|
+ mediaType = resp.Header.Get("Content-Type")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return c, mediaType, nil
|
|
}
|
|
}
|
|
|
|
|
|
// hijackedConn wraps a net.Conn and is returned by setupHijackConn in the case
|
|
// hijackedConn wraps a net.Conn and is returned by setupHijackConn in the case
|