f089932de0
Several storage drivers and storage middlewares need to introspect the client HTTP request in order to construct content-redirect URLs. The request is indirectly passed into the driver interface method URLFor() through the context argument, which is bad practice. The request should be passed in as an explicit argument as the method is only called from request handlers. Replace the URLFor() method with a RedirectURL() method which takes an HTTP request as a parameter instead of a context. Drop the options argument from URLFor() as in practice it only ever encoded the request method, which can now be fetched directly from the request. No URLFor() callers ever passed in an "expiry" option, either. Signed-off-by: Cory Snider <csnider@mirantis.com>
75 lines
2 KiB
Go
75 lines
2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/distribution/distribution/v3"
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
|
"github.com/opencontainers/go-digest"
|
|
)
|
|
|
|
// TODO(stevvooe): This should configurable in the future.
|
|
const blobCacheControlMaxAge = 365 * 24 * time.Hour
|
|
|
|
// blobServer simply serves blobs from a driver instance using a path function
|
|
// to identify paths and a descriptor service to fill in metadata.
|
|
type blobServer struct {
|
|
driver driver.StorageDriver
|
|
statter distribution.BlobStatter
|
|
pathFn func(dgst digest.Digest) (string, error)
|
|
redirect bool // allows disabling RedirectURL redirects
|
|
}
|
|
|
|
func (bs *blobServer) ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error {
|
|
desc, err := bs.statter.Stat(ctx, dgst)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
path, err := bs.pathFn(desc.Digest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if bs.redirect {
|
|
redirectURL, err := bs.driver.RedirectURL(r, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if redirectURL != "" {
|
|
// Redirect to storage URL.
|
|
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
|
|
return nil
|
|
}
|
|
// Fallback to serving the content directly.
|
|
}
|
|
|
|
br, err := newFileReader(ctx, bs.driver, path, desc.Size)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer br.Close()
|
|
|
|
w.Header().Set("ETag", fmt.Sprintf(`"%s"`, desc.Digest)) // If-None-Match handled by ServeContent
|
|
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%.f", blobCacheControlMaxAge.Seconds()))
|
|
|
|
if w.Header().Get("Docker-Content-Digest") == "" {
|
|
w.Header().Set("Docker-Content-Digest", desc.Digest.String())
|
|
}
|
|
|
|
if w.Header().Get("Content-Type") == "" {
|
|
// Set the content type if not already set.
|
|
w.Header().Set("Content-Type", desc.MediaType)
|
|
}
|
|
|
|
if w.Header().Get("Content-Length") == "" {
|
|
// Set the content length if not already set.
|
|
w.Header().Set("Content-Length", fmt.Sprint(desc.Size))
|
|
}
|
|
|
|
http.ServeContent(w, r, desc.Digest.String(), time.Time{}, br)
|
|
return nil
|
|
}
|