github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/api/handlers/compat/resize.go (about)

     1  package compat
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/hanks177/podman/v4/libpod"
     9  	"github.com/hanks177/podman/v4/libpod/define"
    10  	"github.com/hanks177/podman/v4/pkg/api/handlers/utils"
    11  	api "github.com/hanks177/podman/v4/pkg/api/types"
    12  	"github.com/gorilla/mux"
    13  	"github.com/gorilla/schema"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  func ResizeTTY(w http.ResponseWriter, r *http.Request) {
    18  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    19  	decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
    20  
    21  	// /containers/{id}/resize
    22  	query := struct {
    23  		Height           uint16 `schema:"h"`
    24  		Width            uint16 `schema:"w"`
    25  		IgnoreNotRunning bool   `schema:"running"`
    26  	}{
    27  		// override any golang type defaults
    28  	}
    29  
    30  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    31  		utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    32  		return
    33  	}
    34  
    35  	sz := define.TerminalSize{
    36  		Width:  query.Width,
    37  		Height: query.Height,
    38  	}
    39  
    40  	var status int
    41  	switch {
    42  	case strings.Contains(r.URL.Path, "/containers/"):
    43  		name := utils.GetName(r)
    44  		ctnr, err := runtime.LookupContainer(name)
    45  		if err != nil {
    46  			utils.ContainerNotFound(w, name, err)
    47  			return
    48  		}
    49  		if err := ctnr.AttachResize(sz); err != nil {
    50  			if errors.Cause(err) != define.ErrCtrStateInvalid {
    51  				utils.InternalServerError(w, errors.Wrapf(err, "cannot resize container"))
    52  			} else {
    53  				utils.Error(w, http.StatusConflict, err)
    54  			}
    55  			return
    56  		}
    57  		// This is not a 204, even though we write nothing, for compatibility
    58  		// reasons.
    59  		status = http.StatusOK
    60  	case strings.Contains(r.URL.Path, "/exec/"):
    61  		name := mux.Vars(r)["id"]
    62  		ctnr, err := runtime.GetExecSessionContainer(name)
    63  		if err != nil {
    64  			utils.SessionNotFound(w, name, err)
    65  			return
    66  		}
    67  		if state, err := ctnr.State(); err != nil {
    68  			utils.InternalServerError(w, errors.Wrapf(err, "cannot obtain session container state"))
    69  			return
    70  		} else if state != define.ContainerStateRunning && !query.IgnoreNotRunning {
    71  			utils.Error(w, http.StatusConflict, fmt.Errorf("container %q in wrong state %q", name, state.String()))
    72  			return
    73  		}
    74  		if err := ctnr.ExecResize(name, sz); err != nil {
    75  			if errors.Cause(err) != define.ErrExecSessionStateInvalid || !query.IgnoreNotRunning {
    76  				utils.InternalServerError(w, errors.Wrapf(err, "cannot resize session"))
    77  				return
    78  			}
    79  		}
    80  		// This is not a 204, even though we write nothing, for compatibility
    81  		// reasons.
    82  		status = http.StatusCreated
    83  	}
    84  	w.WriteHeader(status)
    85  }