8000 client: remove getDockerOS utility in favor of "Ostype" header · moby/moby@fcf3ff1 · GitHub
[go: up one dir, main page]

Skip to content

Commit fcf3ff1

Browse files
committed
client: remove getDockerOS utility in favor of "Ostype" header
This utility was added in 83b5729 to replace httputils.ParseServerHeader, which was added to print a warning on Windows in 126529c. 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, 7199522 introduced an `OSType` ("Ostype") header that's included on all responses, and a later commit e9dac5e changed 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) in 76a5ca1, 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>
1 parent d3dca6e commit fcf3ff1

File tree

4 files changed

+4
-29
lines changed

4 files changed

+4
-29
lines changed

client/container_stats.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea
2828

2929
return container.StatsResponseReader{
3030
Body: resp.Body,
31-
OSType: getDockerOS(resp.Header.Get("Server")),
31+
OSType: resp.Header.Get("Ostype"),
3232
}, nil
3333
}
3434

@@ -51,6 +51,6 @@ func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string
5151

5252
return container.StatsResponseReader{
5353
Body: resp.Body,
54-
OSType: getDockerOS(resp.Header.Get("Server")),
54+
OSType: resp.Header.Get("Ostype"),
5555
}, nil
5656
}

client/image_build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (cli *Client) ImageBuild(ctx context.Context, buildContext io.Reader, optio
4040

4141
return build.ImageBuildResponse{
4242
Body: resp.Body,
43-
OSType: getDockerOS(resp.Header.Get("Server")),
43+
OSType: resp.Header.Get("Ostype"),
4444
}, nil
4545
}
4646

client/image_build_test.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func TestImageBuild(t *testing.T) {
191191
}
192192

193193
headers := http.Header{}
194-
headers.Add("Server", "Docker/v1.23 (MyOS)")
194+
headers.Add("Ostype", "MyOS")
195195
return &http.Response{
196196
StatusCode: http.StatusOK,
197197
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
@@ -208,15 +208,3 @@ func TestImageBuild(t *testing.T) {
208208
assert.Check(t, is.Equal(string(response), "body"))
209209
}
210210
}
211-
212-
func TestGetDockerOS(t *testing.T) {
213-
cases := map[string]string{
214-
"Docker/v1.22 (linux)": "linux",
215-
"Docker/v1.22 (windows)": "windows",
216-
"Foo/v1.22 (bar)": "",
217-
}
218-
for header, os := range cases {
219-
g := getDockerOS(header)
220-
assert.Check(t, is.Equal(g, os))
221-
}
222-
}

client/utils.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import (
88

99
cerrdefs "github.com/containerd/errdefs"
1010
"github.com/docker/docker/api/types/filters"
11-
"github.com/docker/docker/internal/lazyregexp"
1211
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1312
)
1413

15-
var headerRegexp = lazyregexp.New(`\ADocker/.+\s\((.+)\)\z`)
16-
1714
type emptyIDError string
1815

1916
func (e emptyIDError) InvalidParameter() {}
@@ -31,16 +28,6 @@ func trimID(objType, id string) (string, error) {
3128
return id, nil
3229
}
3330

34-
// getDockerOS returns the operating system based on the server header from the daemon.
35-
func getDockerOS(serverHeader string) string {
36-
var osType string
37-
matches := headerRegexp.FindStringSubmatch(serverHeader)
38-
if len(matches) > 0 {
39-
osType = matches[1]
40-
}
41-
return osType
42-
}
43-
4431
// getFiltersQuery returns a url query with "filters" query term, based on the
4532
// filters provided.
4633
func getFiltersQuery(f filters.Args) (url.Values, error) {

0 commit comments

Comments
 (0)
0