Merge pull request #3206 from takmatsu/suppurt-path-in-middleware
Make redirect middleware can use path
This commit is contained in:
commit
d5c1b39b8b
2 changed files with 53 additions and 5 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
||||
storagemiddleware "github.com/distribution/distribution/v3/registry/storage/driver/middleware"
|
||||
|
@ -11,8 +12,9 @@ import (
|
|||
|
||||
type redirectStorageMiddleware struct {
|
||||
storagedriver.StorageDriver
|
||||
scheme string
|
||||
host string
|
||||
scheme string
|
||||
host string
|
||||
basePath string
|
||||
}
|
||||
|
||||
var _ storagedriver.StorageDriver = &redirectStorageMiddleware{}
|
||||
|
@ -37,11 +39,14 @@ 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, basePath: u.Path}, nil
|
||||
}
|
||||
|
||||
func (r *redirectStorageMiddleware) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
|
||||
u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path}
|
||||
func (r *redirectStorageMiddleware) URLFor(ctx context.Context, urlPath string, options map[string]interface{}) (string, error) {
|
||||
if r.basePath != "" {
|
||||
urlPath = path.Join(r.basePath, urlPath)
|
||||
}
|
||||
u := &url.URL{Scheme: r.scheme, Host: r.host, Path: urlPath}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue