2014-11-18 00:29:42 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-03-03 16:57:52 +00:00
|
|
|
"net/http"
|
2014-11-18 00:29:42 +00:00
|
|
|
"time"
|
2014-11-19 22:39:32 +00:00
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
"github.com/docker/distribution"
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-03-13 02:31:41 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
2014-11-18 00:29:42 +00:00
|
|
|
)
|
|
|
|
|
2015-03-06 17:48:25 +00:00
|
|
|
// layerReader implements Layer and provides facilities for reading and
|
2014-11-18 00:29:42 +00:00
|
|
|
// seeking.
|
|
|
|
type layerReader struct {
|
2014-11-21 01:49:35 +00:00
|
|
|
fileReader
|
2014-11-18 00:29:42 +00:00
|
|
|
|
2014-12-05 04:55:59 +00:00
|
|
|
digest digest.Digest
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 23:41:33 +00:00
|
|
|
// newLayerReader returns a new layerReader with the digest, path and length,
|
|
|
|
// eliding round trips to the storage backend.
|
|
|
|
func newLayerReader(driver driver.StorageDriver, dgst digest.Digest, path string, length int64) (*layerReader, error) {
|
|
|
|
fr := &fileReader{
|
|
|
|
driver: driver,
|
|
|
|
path: path,
|
|
|
|
size: length,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &layerReader{
|
|
|
|
fileReader: *fr,
|
|
|
|
digest: dgst,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
var _ distribution.Layer = &layerReader{}
|
2014-11-18 00:29:42 +00:00
|
|
|
|
2015-03-06 17:48:25 +00:00
|
|
|
func (lr *layerReader) Digest() digest.Digest {
|
|
|
|
return lr.digest
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 17:48:25 +00:00
|
|
|
func (lr *layerReader) Length() int64 {
|
|
|
|
return lr.size
|
2015-03-05 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 17:48:25 +00:00
|
|
|
func (lr *layerReader) CreatedAt() time.Time {
|
|
|
|
return lr.modtime
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
2015-02-12 00:49:49 +00:00
|
|
|
|
|
|
|
// Close the layer. Should be called when the resource is no longer needed.
|
2015-03-06 17:48:25 +00:00
|
|
|
func (lr *layerReader) Close() error {
|
|
|
|
return lr.closeWithErr(distribution.ErrLayerClosed)
|
2015-02-12 00:49:49 +00:00
|
|
|
}
|
2015-03-03 16:57:52 +00:00
|
|
|
|
2015-03-13 02:31:41 +00:00
|
|
|
func (lr *layerReader) Handler(r *http.Request) (h http.Handler, err error) {
|
|
|
|
var handlerFunc http.HandlerFunc
|
2015-03-03 16:57:52 +00:00
|
|
|
|
2015-03-13 02:31:41 +00:00
|
|
|
redirectURL, err := lr.fileReader.driver.URLFor(lr.path, map[string]interface{}{"method": r.Method})
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
handlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Redirect to storage URL.
|
|
|
|
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
case driver.ErrUnsupportedMethod:
|
|
|
|
handlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Fallback to serving the content directly.
|
|
|
|
http.ServeContent(w, r, lr.digest.String(), lr.CreatedAt(), lr)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Some unexpected error.
|
|
|
|
return nil, err
|
2015-03-03 16:57:52 +00:00
|
|
|
}
|
2015-03-13 02:31:41 +00:00
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Docker-Content-Digest", lr.digest.String())
|
|
|
|
handlerFunc.ServeHTTP(w, r)
|
|
|
|
}), nil
|
2015-03-03 16:57:52 +00:00
|
|
|
}
|