From 83b5729f6452de6b40719b9485947eea0bd0eedd Mon Sep 17 00:00:00 2001 From: David Calavera Date: Mon, 14 Dec 2015 16:45:29 -0500 Subject: [PATCH] Remove httputils dependency from API client lib. Signed-off-by: David Calavera --- api/client/lib/image_build.go | 18 +++++++++++++----- api/client/lib/image_build_test.go | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 api/client/lib/image_build_test.go diff --git a/api/client/lib/image_build.go b/api/client/lib/image_build.go index 46ad161472..c451e6f723 100644 --- a/api/client/lib/image_build.go +++ b/api/client/lib/image_build.go @@ -5,14 +5,16 @@ import ( "encoding/json" "net/http" "net/url" + "regexp" "strconv" "github.com/docker/docker/api/types" - "github.com/docker/docker/pkg/httputils" "github.com/docker/docker/pkg/units" "github.com/docker/docker/runconfig" ) +var headerRegexp = regexp.MustCompile(`\ADocker/.+\s\((.+)\)\z`) + // ImageBuild sends request to the daemon to build images. // The Body in the response implement an io.ReadCloser and it's up to the caller to // close it. @@ -35,10 +37,7 @@ func (cli *Client) ImageBuild(options types.ImageBuildOptions) (types.ImageBuild return types.ImageBuildResponse{}, err } - var osType string - if h, err := httputils.ParseServerHeader(serverResp.header.Get("Server")); err == nil { - osType = h.OS - } + osType := getDockerOS(serverResp.header.Get("Server")) return types.ImageBuildResponse{ Body: serverResp.body, @@ -111,3 +110,12 @@ func imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, erro return query, nil } + +func getDockerOS(serverHeader string) string { + var osType string + matches := headerRegexp.FindStringSubmatch(serverHeader) + if len(matches) > 0 { + osType = matches[1] + } + return osType +} diff --git a/api/client/lib/image_build_test.go b/api/client/lib/image_build_test.go new file mode 100644 index 0000000000..a1737f14c9 --- /dev/null +++ b/api/client/lib/image_build_test.go @@ -0,0 +1,17 @@ +package lib + +import "testing" + +func TestGetDockerOS(t *testing.T) { + cases := map[string]string{ + "Docker/v1.22 (linux)": "linux", + "Docker/v1.22 (windows)": "windows", + "Foo/v1.22 (bar)": "", + } + for header, os := range cases { + g := getDockerOS(header) + if g != os { + t.Fatalf("Expected %s, got %s", os, g) + } + } +}