summaryrefslogtreecommitdiff
path: root/pkg/api/handlers/compat/resize.go
blob: 3ead733bc6c224eda02b0eda44ad574c2f396608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package compat

import (
	"net/http"
	"strings"

	"github.com/containers/libpod/libpod"
	"github.com/containers/libpod/pkg/api/handlers/utils"
	"github.com/gorilla/schema"
	"github.com/pkg/errors"
	"k8s.io/client-go/tools/remotecommand"
)

func ResizeTTY(w http.ResponseWriter, r *http.Request) {
	runtime := r.Context().Value("runtime").(*libpod.Runtime)
	decoder := r.Context().Value("decoder").(*schema.Decoder)

	// /containers/{id}/resize
	query := struct {
		height uint16 `schema:"h"`
		width  uint16 `schema:"w"`
	}{
		// override any golang type defaults
	}

	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
		utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
			errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String()))
		return
	}

	sz := remotecommand.TerminalSize{
		Width:  query.width,
		Height: query.height,
	}

	var status int
	name := utils.GetName(r)
	switch {
	case strings.Contains(r.URL.Path, "/containers/"):
		ctnr, err := runtime.LookupContainer(name)
		if err != nil {
			utils.ContainerNotFound(w, name, err)
			return
		}
		if err := ctnr.AttachResize(sz); err != nil {
			utils.InternalServerError(w, errors.Wrapf(err, "cannot resize container"))
			return
		}
		// This is not a 204, even though we write nothing, for compatibility
		// reasons.
		status = http.StatusOK
	case strings.Contains(r.URL.Path, "/exec/"):
		ctnr, err := runtime.GetExecSessionContainer(name)
		if err != nil {
			utils.SessionNotFound(w, name, err)
			return
		}
		if err := ctnr.ExecResize(name, sz); err != nil {
			utils.InternalServerError(w, errors.Wrapf(err, "cannot resize session"))
			return
		}
		// This is not a 204, even though we write nothing, for compatibility
		// reasons.
		status = http.StatusCreated
	}
	utils.WriteResponse(w, status, "")
}