From a1cfd267c8ff193d8b6a00a9c83a56faa346f89c Mon Sep 17 00:00:00 2001 From: MATSUMOTO TAKEAKI Date: Fri, 17 Jul 2020 13:58:06 +0900 Subject: [PATCH 1/3] Make redirect middleware can use path Signed-off-by: MATSUMOTO TAKEAKI --- registry/storage/driver/middleware/redirect/middleware.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/registry/storage/driver/middleware/redirect/middleware.go b/registry/storage/driver/middleware/redirect/middleware.go index 8f63674c6..dca6584a3 100644 --- a/registry/storage/driver/middleware/redirect/middleware.go +++ b/registry/storage/driver/middleware/redirect/middleware.go @@ -13,6 +13,7 @@ type redirectStorageMiddleware struct { storagedriver.StorageDriver scheme string host string + path string } var _ storagedriver.StorageDriver = &redirectStorageMiddleware{} @@ -37,10 +38,13 @@ func newRedirectStorageMiddleware(sd storagedriver.StorageDriver, options map[st 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, path: u.Path}, nil } func (r *redirectStorageMiddleware) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) { + if r.path != "" { + path = r.path + path + } u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path} return u.String(), nil } From a3eb956464cdfac3133987efe2ad529623e109f8 Mon Sep 17 00:00:00 2001 From: MATSUMOTO TAKEAKI Date: Tue, 11 Aug 2020 12:03:24 +0900 Subject: [PATCH 2/3] use path.Join() for building path Signed-off-by: MATSUMOTO TAKEAKI --- .../driver/middleware/redirect/middleware.go | 13 +++--- .../middleware/redirect/middleware_test.go | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/registry/storage/driver/middleware/redirect/middleware.go b/registry/storage/driver/middleware/redirect/middleware.go index dca6584a3..4a4584619 100644 --- a/registry/storage/driver/middleware/redirect/middleware.go +++ b/registry/storage/driver/middleware/redirect/middleware.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/url" + pathutil "path" storagedriver "github.com/docker/distribution/registry/storage/driver" storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware" @@ -11,9 +12,9 @@ import ( type redirectStorageMiddleware struct { storagedriver.StorageDriver - scheme string - host string - path string + scheme string + host string + basePath string } 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 &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) { - if r.path != "" { - path = r.path + path + if r.basePath != "" { + path = pathutil.Join(r.basePath, path) } u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path} return u.String(), nil diff --git a/registry/storage/driver/middleware/redirect/middleware_test.go b/registry/storage/driver/middleware/redirect/middleware_test.go index 36f08c288..a12ad3194 100644 --- a/registry/storage/driver/middleware/redirect/middleware_test.go +++ b/registry/storage/driver/middleware/redirect/middleware_test.go @@ -57,3 +57,46 @@ func (s *MiddlewareSuite) TestHTTP(c *check.C) { c.Assert(err, check.Equals, nil) 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") +} From 316e1c6b82bd2f84e3fe534254c5db7c58e42813 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Fri, 14 Jul 2023 10:32:42 +0100 Subject: [PATCH 3/3] Get rid of unnecessary import alias Signed-off-by: Milos Gajdos --- registry/storage/driver/middleware/redirect/middleware.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/registry/storage/driver/middleware/redirect/middleware.go b/registry/storage/driver/middleware/redirect/middleware.go index 4a4584619..39364beef 100644 --- a/registry/storage/driver/middleware/redirect/middleware.go +++ b/registry/storage/driver/middleware/redirect/middleware.go @@ -4,7 +4,7 @@ import ( "context" "fmt" "net/url" - pathutil "path" + "path" storagedriver "github.com/docker/distribution/registry/storage/driver" storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware" @@ -42,11 +42,11 @@ func newRedirectStorageMiddleware(sd storagedriver.StorageDriver, options map[st 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, urlPath string, options map[string]interface{}) (string, error) { if r.basePath != "" { - path = pathutil.Join(r.basePath, path) + urlPath = path.Join(r.basePath, urlPath) } - u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path} + u := &url.URL{Scheme: r.scheme, Host: r.host, Path: urlPath} return u.String(), nil }