2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/api/server/router/container"
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-31 13:39:48 +01:00
|
|
|
"compress/flate"
|
|
|
|
"compress/gzip"
|
2018-04-19 15:30:59 -07:00
|
|
|
"context"
|
2015-07-28 14:35:24 -04:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-09-06 11:46:37 -07:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/versions"
|
2018-01-31 13:39:48 +01:00
|
|
|
gddohttputil "github.com/golang/gddo/httputil"
|
2015-07-28 14:35:24 -04:00
|
|
|
)
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
type pathError struct{}
|
|
|
|
|
|
|
|
func (pathError) Error() string {
|
|
|
|
return "Path cannot be empty"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pathError) InvalidParameter() {}
|
|
|
|
|
2015-08-11 19:45:23 -04:00
|
|
|
// postContainersCopy is deprecated in favor of getContainersArchive.
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersCopy(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-06-03 19:38:03 +02:00
|
|
|
// Deprecated since 1.8, Errors out since 1.12
|
|
|
|
version := httputils.VersionFromContext(ctx)
|
|
|
|
if versions.GreaterThanOrEqualTo(version, "1.24") {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := types.CopyConfig{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Resource == "" {
|
2017-07-19 10:20:13 -04:00
|
|
|
return pathError{}
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
data, err := s.backend.ContainerCopy(vars["name"], cfg.Resource)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer data.Close()
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
2016-12-14 23:16:12 +02:00
|
|
|
_, err = io.Copy(w, data)
|
|
|
|
return err
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// // Encode the stat to JSON, base64 encode, and place in a header.
|
|
|
|
func setContainerPathStatHeader(stat *types.ContainerPathStat, header http.Header) error {
|
|
|
|
statJSON, err := json.Marshal(stat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Set(
|
|
|
|
"X-Docker-Container-Path-Stat",
|
|
|
|
base64.StdEncoding.EncodeToString(statJSON),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) headContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
v, err := httputils.ArchiveFormValues(r, vars)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
stat, err := s.backend.ContainerStatPath(v.Name, v.Path)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return setContainerPathStatHeader(stat, w.Header())
|
|
|
|
}
|
|
|
|
|
2018-01-31 13:39:48 +01:00
|
|
|
func writeCompressedResponse(w http.ResponseWriter, r *http.Request, body io.Reader) error {
|
|
|
|
var cw io.Writer
|
|
|
|
switch gddohttputil.NegotiateContentEncoding(r, []string{"gzip", "deflate"}) {
|
|
|
|
case "gzip":
|
|
|
|
gw := gzip.NewWriter(w)
|
|
|
|
defer gw.Close()
|
|
|
|
cw = gw
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
case "deflate":
|
|
|
|
fw, err := flate.NewWriter(w, flate.DefaultCompression)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fw.Close()
|
|
|
|
cw = fw
|
|
|
|
w.Header().Set("Content-Encoding", "deflate")
|
|
|
|
default:
|
|
|
|
cw = w
|
|
|
|
}
|
|
|
|
_, err := io.Copy(cw, body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) getContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
v, err := httputils.ArchiveFormValues(r, vars)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
tarArchive, stat, err := s.backend.ContainerArchivePath(v.Name, v.Path)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tarArchive.Close()
|
|
|
|
|
|
|
|
if err := setContainerPathStatHeader(stat, w.Header()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
2018-01-31 13:39:48 +01:00
|
|
|
return writeCompressedResponse(w, r, tarArchive)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) putContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
v, err := httputils.ArchiveFormValues(r, vars)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
noOverwriteDirNonDir := httputils.BoolValue(r, "noOverwriteDirNonDir")
|
2016-11-14 05:37:08 -08:00
|
|
|
copyUIDGID := httputils.BoolValue(r, "copyUIDGID")
|
|
|
|
|
|
|
|
return s.backend.ContainerExtractToDir(v.Name, v.Path, copyUIDGID, noOverwriteDirNonDir, r.Body)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|