Browse Source

Merge pull request #30763 from ripcurld0/fix_30714

Fix the create API when fromSrc has a bad URL
Vincent Demeester 8 years ago
parent
commit
35e5975b90
2 changed files with 29 additions and 6 deletions
  1. 6 6
      daemon/import.go
  2. 23 0
      integration-cli/docker_api_images_test.go

+ 6 - 6
daemon/import.go

@@ -6,6 +6,7 @@ import (
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
 	"runtime"
 	"runtime"
+	"strings"
 	"time"
 	"time"
 
 
 	"github.com/docker/distribution/reference"
 	"github.com/docker/distribution/reference"
@@ -59,20 +60,19 @@ func (daemon *Daemon) ImportImage(src string, repository, tag string, msg string
 		rc = inConfig
 		rc = inConfig
 	} else {
 	} else {
 		inConfig.Close()
 		inConfig.Close()
+		if len(strings.Split(src, "://")) == 1 {
+			src = "http://" + src
+		}
 		u, err := url.Parse(src)
 		u, err := url.Parse(src)
 		if err != nil {
 		if err != nil {
 			return err
 			return err
 		}
 		}
-		if u.Scheme == "" {
-			u.Scheme = "http"
-			u.Host = src
-			u.Path = ""
-		}
-		outStream.Write(sf.FormatStatus("", "Downloading from %s", u))
+
 		resp, err = httputils.Download(u.String())
 		resp, err = httputils.Download(u.String())
 		if err != nil {
 		if err != nil {
 			return err
 			return err
 		}
 		}
+		outStream.Write(sf.FormatStatus("", "Downloading from %s", u))
 		progressOutput := sf.NewProgressOutput(outStream, true)
 		progressOutput := sf.NewProgressOutput(outStream, true)
 		rc = progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Importing")
 		rc = progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Importing")
 	}
 	}

+ 23 - 0
integration-cli/docker_api_images_test.go

@@ -117,6 +117,29 @@ func (s *DockerSuite) TestAPIImagesHistory(c *check.C) {
 	c.Assert(historydata[0].Tags[0], checker.Equals, "test-api-images-history:latest")
 	c.Assert(historydata[0].Tags[0], checker.Equals, "test-api-images-history:latest")
 }
 }
 
 
+func (s *DockerSuite) TestAPIImagesImportBadSrc(c *check.C) {
+	testRequires(c, Network)
+
+	tt := []struct {
+		statusExp int
+		fromSrc   string
+	}{
+		{http.StatusNotFound, "http://example.com/nofile.tar"},
+		{http.StatusNotFound, "example.com/nofile.tar"},
+		{http.StatusNotFound, "example.com%2Fdata%2Ffile.tar"},
+		{http.StatusInternalServerError, "%2Fdata%2Ffile.tar"},
+	}
+
+	for _, te := range tt {
+		res, b, err := request.SockRequestRaw("POST", strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), nil, "application/json", daemonHost())
+		c.Assert(err, check.IsNil)
+		b.Close()
+		c.Assert(res.StatusCode, checker.Equals, te.statusExp)
+		c.Assert(res.Header.Get("Content-Type"), checker.Equals, "application/json")
+	}
+
+}
+
 // #14846
 // #14846
 func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *check.C) {
 func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *check.C) {
 	testRequires(c, Network)
 	testRequires(c, Network)