2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2015-01-06 18:37:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2015-02-11 01:32:22 +00:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu "github.com/docker/distribution/context"
|
2015-01-06 18:37:27 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-02-11 01:41:09 +00:00
|
|
|
"github.com/docker/distribution/registry/storage"
|
2015-01-06 18:37:27 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// layerDispatcher uses the request context to build a layerHandler.
|
|
|
|
func layerDispatcher(ctx *Context, r *http.Request) http.Handler {
|
2015-02-07 00:19:19 +00:00
|
|
|
dgst, err := getDigest(ctx)
|
2015-01-06 18:37:27 +00:00
|
|
|
if err != nil {
|
2015-02-07 00:19:19 +00:00
|
|
|
|
|
|
|
if err == errDigestNotAvailable {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:37:27 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
layerHandler := &layerHandler{
|
|
|
|
Context: ctx,
|
|
|
|
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) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu.GetLogger(lh).Debug("GetImageLayer")
|
2015-01-17 02:32:27 +00:00
|
|
|
layers := lh.Repository.Layers()
|
|
|
|
layer, err := layers.Fetch(lh.Digest)
|
2015-01-06 18:37:27 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2015-01-09 01:29:22 +00:00
|
|
|
if lh.layerHandler != nil {
|
|
|
|
handler, _ := lh.layerHandler.Resolve(layer)
|
|
|
|
if handler != nil {
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:37:27 +00:00
|
|
|
http.ServeContent(w, r, layer.Digest().String(), layer.CreatedAt(), layer)
|
|
|
|
}
|