Browse Source

Merge pull request #18650 from calavera/remove_httputils_dep_from_api

Remove httputils dependency from API client lib.
Arnaud Porterie 9 năm trước cách đây
mục cha
commit
2ec34dca05
2 tập tin đã thay đổi với 30 bổ sung5 xóa
  1. 13 5
      api/client/lib/image_build.go
  2. 17 0
      api/client/lib/image_build_test.go

+ 13 - 5
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
+}

+ 17 - 0
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)
+		}
+	}
+}