forked from TrueCloudLab/distribution
0a6a6f5b81
Make HTTP status codes match the ErrorCode by looking it up in the Descriptors Signed-off-by: Doug Davis <dug@us.ibm.com>
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/docker/distribution"
|
|
"github.com/docker/distribution/context"
|
|
"github.com/docker/distribution/digest"
|
|
"github.com/docker/distribution/registry/api/errcode"
|
|
"github.com/docker/distribution/registry/api/v2"
|
|
"github.com/gorilla/handlers"
|
|
)
|
|
|
|
// blobDispatcher uses the request context to build a blobHandler.
|
|
func blobDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|
dgst, err := getDigest(ctx)
|
|
if err != nil {
|
|
|
|
if err == errDigestNotAvailable {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
|
})
|
|
}
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
|
})
|
|
}
|
|
|
|
blobHandler := &blobHandler{
|
|
Context: ctx,
|
|
Digest: dgst,
|
|
}
|
|
|
|
return handlers.MethodHandler{
|
|
"GET": http.HandlerFunc(blobHandler.GetBlob),
|
|
"HEAD": http.HandlerFunc(blobHandler.GetBlob),
|
|
}
|
|
}
|
|
|
|
// blobHandler serves http blob requests.
|
|
type blobHandler struct {
|
|
*Context
|
|
|
|
Digest digest.Digest
|
|
}
|
|
|
|
// GetBlob fetches the binary data from backend storage returns it in the
|
|
// response.
|
|
func (bh *blobHandler) GetBlob(w http.ResponseWriter, r *http.Request) {
|
|
context.GetLogger(bh).Debug("GetBlob")
|
|
blobs := bh.Repository.Blobs(bh)
|
|
desc, err := blobs.Stat(bh, bh.Digest)
|
|
if err != nil {
|
|
if err == distribution.ErrBlobUnknown {
|
|
bh.Errors.Push(v2.ErrorCodeBlobUnknown, bh.Digest)
|
|
} else {
|
|
bh.Errors.Push(errcode.ErrorCodeUnknown, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
if err := blobs.ServeBlob(bh, w, r, desc.Digest); err != nil {
|
|
context.GetLogger(bh).Debugf("unexpected error getting blob HTTP handler: %v", err)
|
|
bh.Errors.Push(errcode.ErrorCodeUnknown, err)
|
|
return
|
|
}
|
|
}
|