2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/api/v2"
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu "github.com/docker/distribution/context"
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/storage"
|
2014-11-11 02:57:38 +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)
|
2014-11-21 03:57:01 +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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-12-12 06:24:25 +00:00
|
|
|
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-11 02:57:38 +00:00
|
|
|
layerHandler := &layerHandler{
|
|
|
|
Context: ctx,
|
2014-11-21 03:57:01 +00:00
|
|
|
Digest: dgst,
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return handlers.MethodHandler{
|
2014-11-19 03:38:14 +00:00
|
|
|
"GET": http.HandlerFunc(layerHandler.GetLayer),
|
|
|
|
"HEAD": http.HandlerFunc(layerHandler.GetLayer),
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// layerHandler serves http layer requests.
|
|
|
|
type layerHandler struct {
|
|
|
|
*Context
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
Digest digest.Digest
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2014-11-21 03:57:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
switch err := err.(type) {
|
|
|
|
case storage.ErrUnknownLayer:
|
2014-11-21 03:57:01 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2014-12-12 06:24:25 +00:00
|
|
|
lh.Errors.Push(v2.ErrorCodeBlobUnknown, err.FSLayer)
|
2014-11-21 03:57:01 +00:00
|
|
|
default:
|
2014-12-12 06:24:25 +00:00
|
|
|
lh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
}
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
return
|
2014-11-21 03:57:01 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
http.ServeContent(w, r, layer.Digest().String(), layer.CreatedAt(), layer)
|
|
|
|
}
|