2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2015-01-06 18:37:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
"github.com/docker/distribution"
|
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"
|
|
|
|
"github.com/docker/distribution/manifest"
|
2015-02-11 02:18:45 +00:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// imageManifestDispatcher takes the request context and builds the
|
|
|
|
// appropriate handler for handling image manifest requests.
|
|
|
|
func imageManifestDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|
|
|
imageManifestHandler := &imageManifestHandler{
|
|
|
|
Context: ctx,
|
2015-02-07 00:19:19 +00:00
|
|
|
Tag: getTag(ctx),
|
2015-01-06 18:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return handlers.MethodHandler{
|
|
|
|
"GET": http.HandlerFunc(imageManifestHandler.GetImageManifest),
|
|
|
|
"PUT": http.HandlerFunc(imageManifestHandler.PutImageManifest),
|
|
|
|
"DELETE": http.HandlerFunc(imageManifestHandler.DeleteImageManifest),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// imageManifestHandler handles http operations on image manifests.
|
|
|
|
type imageManifestHandler struct {
|
|
|
|
*Context
|
|
|
|
|
|
|
|
Tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageManifest fetches the image manifest from the storage backend, if it exists.
|
|
|
|
func (imh *imageManifestHandler) GetImageManifest(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu.GetLogger(imh).Debug("GetImageManifest")
|
2015-01-17 02:32:27 +00:00
|
|
|
manifests := imh.Repository.Manifests()
|
|
|
|
manifest, err := manifests.Get(imh.Tag)
|
2015-01-06 18:37:27 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors.Push(v2.ErrorCodeManifestUnknown, err)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprint(len(manifest.Raw)))
|
|
|
|
w.Write(manifest.Raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutImageManifest validates and stores and image in the registry.
|
|
|
|
func (imh *imageManifestHandler) PutImageManifest(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu.GetLogger(imh).Debug("PutImageManifest")
|
2015-01-17 02:32:27 +00:00
|
|
|
manifests := imh.Repository.Manifests()
|
2015-01-06 18:37:27 +00:00
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
|
|
|
|
var manifest manifest.SignedManifest
|
|
|
|
if err := dec.Decode(&manifest); err != nil {
|
|
|
|
imh.Errors.Push(v2.ErrorCodeManifestInvalid, err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-17 02:32:27 +00:00
|
|
|
if err := manifests.Put(imh.Tag, &manifest); err != nil {
|
2015-01-06 18:37:27 +00:00
|
|
|
// TODO(stevvooe): These error handling switches really need to be
|
|
|
|
// handled by an app global mapper.
|
|
|
|
switch err := err.(type) {
|
|
|
|
case storage.ErrManifestVerification:
|
|
|
|
for _, verificationError := range err {
|
|
|
|
switch verificationError := verificationError.(type) {
|
2015-02-12 00:49:49 +00:00
|
|
|
case distribution.ErrUnknownLayer:
|
2015-01-06 18:37:27 +00:00
|
|
|
imh.Errors.Push(v2.ErrorCodeBlobUnknown, verificationError.FSLayer)
|
|
|
|
case storage.ErrManifestUnverified:
|
|
|
|
imh.Errors.Push(v2.ErrorCodeManifestUnverified)
|
|
|
|
default:
|
|
|
|
if verificationError == digest.ErrDigestInvalidFormat {
|
|
|
|
// TODO(stevvooe): We need to really need to move all
|
|
|
|
// errors to types. Its much more straightforward.
|
|
|
|
imh.Errors.Push(v2.ErrorCodeDigestInvalid)
|
|
|
|
} else {
|
|
|
|
imh.Errors.PushErr(verificationError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
imh.Errors.PushErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2015-02-04 02:27:40 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2015-01-06 18:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteImageManifest removes the image with the given tag from the registry.
|
|
|
|
func (imh *imageManifestHandler) DeleteImageManifest(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu.GetLogger(imh).Debug("DeleteImageManifest")
|
2015-01-17 02:32:27 +00:00
|
|
|
manifests := imh.Repository.Manifests()
|
|
|
|
if err := manifests.Delete(imh.Tag); err != nil {
|
2015-01-06 18:37:27 +00:00
|
|
|
switch err := err.(type) {
|
|
|
|
case storage.ErrUnknownManifest:
|
|
|
|
imh.Errors.Push(v2.ErrorCodeManifestUnknown, err)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
default:
|
|
|
|
imh.Errors.Push(v2.ErrorCodeUnknown, err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Length", "0")
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
}
|