mirror of
https://github.com/moby/moby.git
synced 2026-07-13 11:00:42 +00:00
This utility was added in83b5729f64to replace httputils.ParseServerHeader, which was added to print a warning on Windows in126529c6d0. At the time, the only available option to detect the daemon's OS was to parse the `Server` header, which contained the version of Docker as well as the OS. However,7199522ea2introduced an `OSType` ("Ostype") header that's included on all responses, and a later commite9dac5ef5echanged that to also be included when producing an error for unsupported API versions. Note that the casing in the midddleware was changed from `OSType` to `Ostype` (normalized form) in76a5ca1d4d, but headers are case-insensitive, and `header.Get()` should handle either case in the response. In short; every API response contains an "Ostype" header, which already contains the OS ("windows" or "linux") that doesn't require any parsing, so let's put that header to use. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/docker/docker/api/types/filters"
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
)
|
|
|
|
type emptyIDError string
|
|
|
|
func (e emptyIDError) InvalidParameter() {}
|
|
|
|
func (e emptyIDError) Error() string {
|
|
return "invalid " + string(e) + " name or ID: value is empty"
|
|
}
|
|
|
|
// trimID trims the given object-ID / name, returning an error if it's empty.
|
|
func trimID(objType, id string) (string, error) {
|
|
id = strings.TrimSpace(id)
|
|
if id == "" {
|
|
return "", emptyIDError(objType)
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// getFiltersQuery returns a url query with "filters" query term, based on the
|
|
// filters provided.
|
|
func getFiltersQuery(f filters.Args) (url.Values, error) {
|
|
query := url.Values{}
|
|
if f.Len() > 0 {
|
|
filterJSON, err := filters.ToJSON(f)
|
|
if err != nil {
|
|
return query, err
|
|
}
|
|
query.Set("filters", filterJSON)
|
|
}
|
|
return query, nil
|
|
}
|
|
|
|
// encodePlatforms marshals the given platform(s) to JSON format, to
|
|
// be used for query-parameters for filtering / selecting platforms.
|
|
func encodePlatforms(platform ...ocispec.Platform) ([]string, error) {
|
|
if len(platform) == 0 {
|
|
return []string{}, nil
|
|
}
|
|
if len(platform) == 1 {
|
|
p, err := encodePlatform(&platform[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []string{p}, nil
|
|
}
|
|
|
|
seen := make(map[string]struct{}, len(platform))
|
|
out := make([]string, 0, len(platform))
|
|
for i := range platform {
|
|
p, err := encodePlatform(&platform[i])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, ok := seen[p]; !ok {
|
|
out = append(out, p)
|
|
seen[p] = struct{}{}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// encodePlatform marshals the given platform to JSON format, to
|
|
// be used for query-parameters for filtering / selecting platforms. It
|
|
// is used as a helper for encodePlatforms,
|
|
func encodePlatform(platform *ocispec.Platform) (string, error) {
|
|
p, err := json.Marshal(platform)
|
|
if err != nil {
|
|
return "", fmt.Errorf("%w: invalid platform: %v", cerrdefs.ErrInvalidArgument, err)
|
|
}
|
|
return string(p), nil
|
|
}
|