scheme and host mandatory in baseurl

Signed-off-by: Andrew Hsu <andrewhsu@acm.org> (github: andrewhsu)
This commit is contained in:
Andrew Hsu 2016-04-25 10:28:32 -07:00 committed by Andrew Hsu
parent 059bc5f5ef
commit 80248c3d3a
2 changed files with 10 additions and 13 deletions

View file

@ -3,7 +3,6 @@ package middleware
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"strings"
"github.com/docker/distribution/context" "github.com/docker/distribution/context"
storagedriver "github.com/docker/distribution/registry/storage/driver" storagedriver "github.com/docker/distribution/registry/storage/driver"
@ -27,12 +26,15 @@ func newRedirectStorageMiddleware(sd storagedriver.StorageDriver, options map[st
if !ok { if !ok {
return nil, fmt.Errorf("baseurl must be a string") return nil, fmt.Errorf("baseurl must be a string")
} }
if !strings.Contains(b, "://") {
b = "https://" + b
}
u, err := url.Parse(b) u, err := url.Parse(b)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid baseurl: %v", err) return nil, fmt.Errorf("unable to parse redirect baseurl: %s", b)
}
if u.Scheme == "" {
return nil, fmt.Errorf("no scheme specified for redirect baseurl")
}
if u.Host == "" {
return nil, fmt.Errorf("no host specified for redirect baseurl")
} }
return &redirectStorageMiddleware{StorageDriver: sd, scheme: u.Scheme, host: u.Host}, nil return &redirectStorageMiddleware{StorageDriver: sd, scheme: u.Scheme, host: u.Host}, nil

View file

@ -17,16 +17,11 @@ func (s *MiddlewareSuite) TestNoConfig(c *check.C) {
c.Assert(err, check.ErrorMatches, "no baseurl provided") c.Assert(err, check.ErrorMatches, "no baseurl provided")
} }
func (s *MiddlewareSuite) TestDefaultScheme(c *check.C) { func (s *MiddlewareSuite) TestMissingScheme(c *check.C) {
options := make(map[string]interface{}) options := make(map[string]interface{})
options["baseurl"] = "example.com" options["baseurl"] = "example.com"
middleware, err := newRedirectStorageMiddleware(nil, options) _, err := newRedirectStorageMiddleware(nil, options)
c.Assert(err, check.Equals, nil) c.Assert(err, check.ErrorMatches, "no scheme specified for redirect baseurl")
m, ok := middleware.(*redirectStorageMiddleware)
c.Assert(ok, check.Equals, true)
c.Assert(m.scheme, check.Equals, "https")
c.Assert(m.host, check.Equals, "example.com")
} }
func (s *MiddlewareSuite) TestHTTPS(c *check.C) { func (s *MiddlewareSuite) TestHTTPS(c *check.C) {