mirror of
https://github.com/containers/podman.git
synced 2025-08-06 19:44:14 +08:00

The new golangci-lint version 1.60.1 has problems with typecheck when linting remote files. We have certain pakcages that should never be inlcuded in remote but the typecheck tries to compile all of them but this never works and it seems to ignore the exclude files we gave it. To fix this the proper way is to mark all packages we only use locally with !remote tags. This is a bit ugly but more correct. I also moved the DecodeChanges() code around as it is called from the client so the handles package which should only be remote doesn't really fit anyway. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
//go:build !remote
|
|
|
|
package apiutil
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/blang/semver/v4"
|
|
"github.com/containers/podman/v5/version"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
var (
|
|
// ErrVersionNotGiven returned when version not given by client
|
|
ErrVersionNotGiven = errors.New("version not given in URL path")
|
|
// ErrVersionNotSupported returned when given version is too old
|
|
ErrVersionNotSupported = errors.New("given version is not supported")
|
|
)
|
|
|
|
// IsLibpodRequest returns true if the request related to a libpod endpoint
|
|
// (e.g., /v2/libpod/...).
|
|
func IsLibpodRequest(r *http.Request) bool {
|
|
split := strings.Split(r.URL.String(), "/")
|
|
return len(split) >= 3 && split[2] == "libpod"
|
|
}
|
|
|
|
// SupportedVersion validates that the version provided by client is included in the given condition
|
|
// https://github.com/blang/semver#ranges provides the details for writing conditions
|
|
// If a version is not given in URL path, ErrVersionNotGiven is returned
|
|
func SupportedVersion(r *http.Request, condition string) (semver.Version, error) {
|
|
version := semver.Version{}
|
|
val, ok := mux.Vars(r)["version"]
|
|
if !ok {
|
|
return version, ErrVersionNotGiven
|
|
}
|
|
safeVal, err := url.PathUnescape(val)
|
|
if err != nil {
|
|
return version, fmt.Errorf("unable to unescape given API version: %q: %w", val, err)
|
|
}
|
|
version, err = semver.ParseTolerant(safeVal)
|
|
if err != nil {
|
|
return version, fmt.Errorf("unable to parse given API version: %q from %q: %w", safeVal, val, err)
|
|
}
|
|
|
|
inRange, err := semver.ParseRange(condition)
|
|
if err != nil {
|
|
return version, err
|
|
}
|
|
|
|
if inRange(version) {
|
|
return version, nil
|
|
}
|
|
return version, ErrVersionNotSupported
|
|
}
|
|
|
|
// SupportedVersionWithDefaults validates that the version provided by client valid is supported by server
|
|
// minimal API version <= client path version <= maximum API version focused on the endpoint tree from URL
|
|
func SupportedVersionWithDefaults(r *http.Request) (semver.Version, error) {
|
|
tree := version.Compat
|
|
if IsLibpodRequest(r) {
|
|
tree = version.Libpod
|
|
}
|
|
|
|
return SupportedVersion(r,
|
|
fmt.Sprintf(">=%s <=%s", version.APIVersion[tree][version.MinimalAPI].String(),
|
|
version.APIVersion[tree][version.CurrentAPI].String()))
|
|
}
|