Disambiguate routing for multi-level repository names

To be able to support multi-level repository names, the API has been adjusted
to disabiguate routes tagged image manifest routes and tag list routes. With
this effort, the regular expressions have been defined in a single place to
reduce repitition and ensure that validation is consistent across the registry.

The router was also refactored to remove the use of subrouters, simplifying the
route definition code. This also reduces the number of regular expression match
checks during the routing process.
This commit is contained in:
Stephen J Day 2014-11-12 16:59:50 -08:00
parent 375f3cc136
commit 145c89bb94
3 changed files with 90 additions and 75 deletions

View file

@ -1,12 +1,11 @@
package registry
import (
"github.com/docker/docker-registry/common"
"github.com/gorilla/mux"
)
const (
routeNameRoot = "root"
routeNameName = "name"
routeNameImageManifest = "image-manifest"
routeNameTags = "tags"
routeNameLayer = "layer"
@ -25,47 +24,36 @@ var allEndpoints = []string{
// v2APIRouter builds a gorilla router with named routes for the various API
// methods. We may export this for use by the client.
func v2APIRouter() *mux.Router {
router := mux.NewRouter()
rootRouter := router.
PathPrefix("/v2").
Name(routeNameRoot).
Subrouter()
// All routes are subordinate to named routes
namedRouter := rootRouter.
PathPrefix("/{name:[A-Za-z0-9-_]+/[A-Za-z0-9-_]+}"). // TODO(stevvooe): Verify this format with core
Name(routeNameName).
Subrouter().
router := mux.NewRouter().
StrictSlash(true)
// GET /v2/<name>/image/<tag> Image Manifest Fetch the image manifest identified by name and tag.
// PUT /v2/<name>/image/<tag> Image Manifest Upload the image manifest identified by name and tag.
// DELETE /v2/<name>/image/<tag> Image Manifest Delete the image identified by name and tag.
namedRouter.
Path("/image/{tag:[A-Za-z0-9-_]+}").
router.
Path("/v2/{name:" + common.RepositoryNameRegexp.String() + "}/image/{tag:" + common.TagNameRegexp.String() + "}").
Name(routeNameImageManifest)
// GET /v2/<name>/tags Tags Fetch the tags under the repository identified by name.
namedRouter.
Path("/tags").
// GET /v2/<name>/tags/list Tags Fetch the tags under the repository identified by name.
router.
Path("/v2/{name:" + common.RepositoryNameRegexp.String() + "}/tags/list").
Name(routeNameTags)
// GET /v2/<name>/layer/<tarsum> Layer Fetch the layer identified by tarsum.
namedRouter.
Path("/layer/{tarsum}").
router.
Path("/v2/{name:" + common.RepositoryNameRegexp.String() + "}/layer/{tarsum:" + common.TarsumRegexp.String() + "}").
Name(routeNameLayer)
// POST /v2/<name>/layer/<tarsum>/upload/ Layer Upload Initiate an upload of the layer identified by tarsum. Requires length and a checksum parameter.
namedRouter.
Path("/layer/{tarsum}/upload/").
router.
Path("/v2/{name:" + common.RepositoryNameRegexp.String() + "}/layer/{tarsum:" + common.TarsumRegexp.String() + "}/upload/").
Name(routeNameLayerUpload)
// GET /v2/<name>/layer/<tarsum>/upload/<uuid> Layer Upload Get the status of the upload identified by tarsum and uuid.
// PUT /v2/<name>/layer/<tarsum>/upload/<uuid> Layer Upload Upload all or a chunk of the upload identified by tarsum and uuid.
// DELETE /v2/<name>/layer/<tarsum>/upload/<uuid> Layer Upload Cancel the upload identified by layer and uuid
namedRouter.
Path("/layer/{tarsum}/upload/{uuid}").
router.
Path("/v2/{name:" + common.RepositoryNameRegexp.String() + "}/layer/{tarsum:" + common.TarsumRegexp.String() + "}/upload/{uuid}").
Name(routeNameLayerUploadResume)
return router