diff options
| author | Nicolas De Loof <nicolas.deloof@gmail.com> | 2019-08-28 13:46:32 +0200 |
|---|---|---|
| committer | Nicolas De Loof <nicolas.deloof@gmail.com> | 2022-05-12 11:36:31 +0200 |
| commit | af5d83a6410f6a6bbe89f1a13d72b1982a3973cc (patch) | |
| tree | b34ec548bd48a729864191765cc064301f96710f /api/server | |
| parent | 7c69b6dc08c7ce128c3015e08076641c2c5c40e5 (diff) | |
| download | docker-af5d83a6410f6a6bbe89f1a13d72b1982a3973cc.tar.gz | |
Make it explicit raw|multiplexed stream implementation being used
fix #35761
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
Diffstat (limited to 'api/server')
| -rw-r--r-- | api/server/router/container/container_routes.go | 26 | ||||
| -rw-r--r-- | api/server/router/container/exec.go | 6 | ||||
| -rw-r--r-- | api/server/router/swarm/helpers.go | 8 |
3 files changed, 29 insertions, 11 deletions
diff --git a/api/server/router/container/container_routes.go b/api/server/router/container/container_routes.go index e0c3c29dc8..b66bdac847 100644 --- a/api/server/router/container/container_routes.go +++ b/api/server/router/container/container_routes.go @@ -153,6 +153,12 @@ func (s *containerRouter) getContainersLogs(ctx context.Context, w http.Response return err } + contentType := types.MediaTypeRawStream + if !tty && versions.GreaterThanOrEqualTo(httputils.VersionFromContext(ctx), "1.42") { + contentType = types.MediaTypeMultiplexedStream + } + w.Header().Set("Content-Type", contentType) + // if has a tty, we're not muxing streams. if it doesn't, we are. simple. // this is the point of no return for writing a response. once we call // WriteLogStream, the response has been started and errors will be @@ -598,7 +604,8 @@ func (s *containerRouter) postContainersAttach(ctx context.Context, w http.Respo return errdefs.InvalidParameter(errors.Errorf("error attaching to container %s, hijack connection missing", containerName)) } - setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) { + contentType := types.MediaTypeRawStream + setupStreams := func(multiplexed bool) (io.ReadCloser, io.Writer, io.Writer, error) { conn, _, err := hijacker.Hijack() if err != nil { return nil, nil, nil, err @@ -608,7 +615,10 @@ func (s *containerRouter) postContainersAttach(ctx context.Context, w http.Respo conn.Write([]byte{}) if upgrade { - fmt.Fprintf(conn, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n") + if multiplexed && versions.GreaterThanOrEqualTo(httputils.VersionFromContext(ctx), "1.42") { + contentType = types.MediaTypeMultiplexedStream + } + fmt.Fprintf(conn, "HTTP/1.1 101 UPGRADED\r\nContent-Type: "+contentType+"\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n") } else { fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n") } @@ -632,16 +642,16 @@ func (s *containerRouter) postContainersAttach(ctx context.Context, w http.Respo } if err = s.backend.ContainerAttach(containerName, attachConfig); err != nil { - logrus.Errorf("Handler for %s %s returned error: %v", r.Method, r.URL.Path, err) + logrus.WithError(err).Errorf("Handler for %s %s returned error", r.Method, r.URL.Path) // Remember to close stream if error happens conn, _, errHijack := hijacker.Hijack() - if errHijack == nil { + if errHijack != nil { + logrus.WithError(err).Errorf("Handler for %s %s: unable to close stream; error when hijacking connection", r.Method, r.URL.Path) + } else { statusCode := httpstatus.FromError(err) statusText := http.StatusText(statusCode) - fmt.Fprintf(conn, "HTTP/1.1 %d %s\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n%s\r\n", statusCode, statusText, err.Error()) + fmt.Fprintf(conn, "HTTP/1.1 %d %s\r\nContent-Type: %s\r\n\r\n%s\r\n", statusCode, statusText, contentType, err.Error()) httputils.CloseStreams(conn) - } else { - logrus.Errorf("Error Hijacking: %v", err) } } return nil @@ -661,7 +671,7 @@ func (s *containerRouter) wsContainersAttach(ctx context.Context, w http.Respons version := httputils.VersionFromContext(ctx) - setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) { + setupStreams := func(multiplexed bool) (io.ReadCloser, io.Writer, io.Writer, error) { wsChan := make(chan *websocket.Conn) h := func(conn *websocket.Conn) { wsChan <- conn diff --git a/api/server/router/container/exec.go b/api/server/router/container/exec.go index 5a612bc977..3187fe6c83 100644 --- a/api/server/router/container/exec.go +++ b/api/server/router/container/exec.go @@ -98,7 +98,11 @@ func (s *containerRouter) postContainerExecStart(ctx context.Context, w http.Res defer httputils.CloseStreams(inStream, outStream) if _, ok := r.Header["Upgrade"]; ok { - fmt.Fprint(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n") + contentType := types.MediaTypeRawStream + if !execStartCheck.Tty && versions.GreaterThanOrEqualTo(httputils.VersionFromContext(ctx), "1.42") { + contentType = types.MediaTypeMultiplexedStream + } + fmt.Fprint(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: "+contentType+"\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n") } else { fmt.Fprint(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n") } diff --git a/api/server/router/swarm/helpers.go b/api/server/router/swarm/helpers.go index 969724a1a2..f7931d5865 100644 --- a/api/server/router/swarm/helpers.go +++ b/api/server/router/swarm/helpers.go @@ -3,7 +3,6 @@ package swarm // import "github.com/docker/docker/api/server/router/swarm" import ( "context" "fmt" - "io" "net/http" "github.com/docker/docker/api/server/httputils" @@ -15,7 +14,7 @@ import ( // swarmLogs takes an http response, request, and selector, and writes the logs // specified by the selector to the response -func (sr *swarmRouter) swarmLogs(ctx context.Context, w io.Writer, r *http.Request, selector *backend.LogSelector) error { +func (sr *swarmRouter) swarmLogs(ctx context.Context, w http.ResponseWriter, r *http.Request, selector *backend.LogSelector) error { // Args are validated before the stream starts because when it starts we're // sending HTTP 200 by writing an empty chunk of data to tell the client that // daemon is going to stream. By sending this initial HTTP 200 we can't report @@ -63,6 +62,11 @@ func (sr *swarmRouter) swarmLogs(ctx context.Context, w io.Writer, r *http.Reque return err } + contentType := basictypes.MediaTypeRawStream + if !tty && versions.GreaterThanOrEqualTo(httputils.VersionFromContext(ctx), "1.42") { + contentType = basictypes.MediaTypeMultiplexedStream + } + w.Header().Set("Content-Type", contentType) httputils.WriteLogStream(ctx, w, msgs, logsConfig, !tty) return nil } |
