d08f0edcf1
Routes and errors are now all referenced from a single v2 package. This packages exports are acceptable for use in the server side as well as integration into docker core.
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package registry
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/docker/docker-registry/api/v2"
|
|
"github.com/docker/docker-registry/digest"
|
|
"github.com/docker/docker-registry/storage"
|
|
"github.com/gorilla/handlers"
|
|
)
|
|
|
|
// layerDispatcher uses the request context to build a layerHandler.
|
|
func layerDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|
dgst, err := digest.ParseDigest(ctx.vars["digest"])
|
|
|
|
if err != nil {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
|
})
|
|
}
|
|
|
|
layerHandler := &layerHandler{
|
|
Context: ctx,
|
|
Digest: dgst,
|
|
}
|
|
|
|
layerHandler.log = layerHandler.log.WithField("digest", dgst)
|
|
|
|
return handlers.MethodHandler{
|
|
"GET": http.HandlerFunc(layerHandler.GetLayer),
|
|
"HEAD": http.HandlerFunc(layerHandler.GetLayer),
|
|
}
|
|
}
|
|
|
|
// layerHandler serves http layer requests.
|
|
type layerHandler struct {
|
|
*Context
|
|
|
|
Digest digest.Digest
|
|
}
|
|
|
|
// GetLayer fetches the binary data from backend storage returns it in the
|
|
// response.
|
|
func (lh *layerHandler) GetLayer(w http.ResponseWriter, r *http.Request) {
|
|
layers := lh.services.Layers()
|
|
|
|
layer, err := layers.Fetch(lh.Name, lh.Digest)
|
|
|
|
if err != nil {
|
|
switch err := err.(type) {
|
|
case storage.ErrUnknownLayer:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
lh.Errors.Push(v2.ErrorCodeBlobUnknown, err.FSLayer)
|
|
default:
|
|
lh.Errors.Push(v2.ErrorCodeUnknown, err)
|
|
}
|
|
return
|
|
}
|
|
defer layer.Close()
|
|
|
|
http.ServeContent(w, r, layer.Digest().String(), layer.CreatedAt(), layer)
|
|
}
|