use path.Join() for building path

Signed-off-by: MATSUMOTO TAKEAKI <takeaki.matsumoto@linecorp.com>
This commit is contained in:
MATSUMOTO TAKEAKI 2020-08-11 12:03:24 +09:00 committed by Milos Gajdos
parent a1cfd267c8
commit a3eb956464
No known key found for this signature in database
GPG Key ID: 01300E5E6D417439
2 changed files with 50 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/url" "net/url"
pathutil "path"
storagedriver "github.com/docker/distribution/registry/storage/driver" storagedriver "github.com/docker/distribution/registry/storage/driver"
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware" storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware"
@ -11,9 +12,9 @@ import (
type redirectStorageMiddleware struct { type redirectStorageMiddleware struct {
storagedriver.StorageDriver storagedriver.StorageDriver
scheme string scheme string
host string host string
path string basePath string
} }
var _ storagedriver.StorageDriver = &redirectStorageMiddleware{} var _ storagedriver.StorageDriver = &redirectStorageMiddleware{}
@ -38,12 +39,12 @@ func newRedirectStorageMiddleware(sd storagedriver.StorageDriver, options map[st
return nil, fmt.Errorf("no host specified for redirect baseurl") return nil, fmt.Errorf("no host specified for redirect baseurl")
} }
return &redirectStorageMiddleware{StorageDriver: sd, scheme: u.Scheme, host: u.Host, path: u.Path}, nil return &redirectStorageMiddleware{StorageDriver: sd, scheme: u.Scheme, host: u.Host, basePath: u.Path}, nil
} }
func (r *redirectStorageMiddleware) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) { func (r *redirectStorageMiddleware) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
if r.path != "" { if r.basePath != "" {
path = r.path + path path = pathutil.Join(r.basePath, path)
} }
u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path} u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path}
return u.String(), nil return u.String(), nil

View File

@ -57,3 +57,46 @@ func (s *MiddlewareSuite) TestHTTP(c *check.C) {
c.Assert(err, check.Equals, nil) c.Assert(err, check.Equals, nil)
c.Assert(url, check.Equals, "http://example.com/morty/data") c.Assert(url, check.Equals, "http://example.com/morty/data")
} }
func (s *MiddlewareSuite) TestPath(c *check.C) {
// basePath: end with no slash
options := make(map[string]interface{})
options["baseurl"] = "https://example.com/path"
middleware, err := newRedirectStorageMiddleware(nil, options)
c.Assert(err, check.Equals, nil)
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")
c.Assert(m.basePath, check.Equals, "/path")
// call URLFor() with no leading slash
url, err := middleware.URLFor(context.TODO(), "morty/data", nil)
c.Assert(err, check.Equals, nil)
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
// call URLFor() with leading slash
url, err = middleware.URLFor(context.TODO(), "/morty/data", nil)
c.Assert(err, check.Equals, nil)
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
// basePath: end with slash
options["baseurl"] = "https://example.com/path/"
middleware, err = newRedirectStorageMiddleware(nil, options)
c.Assert(err, check.Equals, nil)
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")
c.Assert(m.basePath, check.Equals, "/path/")
// call URLFor() with no leading slash
url, err = middleware.URLFor(context.TODO(), "morty/data", nil)
c.Assert(err, check.Equals, nil)
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
// call URLFor() with leading slash
url, err = middleware.URLFor(context.TODO(), "/morty/data", nil)
c.Assert(err, check.Equals, nil)
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
}