2015-07-13 20:08:13 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-07-17 18:42:47 +00:00
|
|
|
"io"
|
2015-07-13 20:08:13 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2023-04-24 16:52:27 +00:00
|
|
|
v2 "github.com/docker/distribution/registry/api/v2"
|
2016-07-14 20:24:16 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
2023-04-24 16:52:27 +00:00
|
|
|
|
2015-07-13 20:08:13 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
)
|
|
|
|
|
2023-04-24 16:52:27 +00:00
|
|
|
const defaultReturnedEntries = 100
|
2015-07-13 20:08:13 +00:00
|
|
|
|
|
|
|
func catalogDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|
|
|
catalogHandler := &catalogHandler{
|
|
|
|
Context: ctx,
|
|
|
|
}
|
|
|
|
|
|
|
|
return handlers.MethodHandler{
|
|
|
|
"GET": http.HandlerFunc(catalogHandler.GetCatalog),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type catalogHandler struct {
|
|
|
|
*Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type catalogAPIResponse struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *catalogHandler) GetCatalog(w http.ResponseWriter, r *http.Request) {
|
2015-07-17 18:42:47 +00:00
|
|
|
var moreEntries = true
|
|
|
|
|
2015-07-13 20:08:13 +00:00
|
|
|
q := r.URL.Query()
|
|
|
|
lastEntry := q.Get("last")
|
2023-04-24 16:52:27 +00:00
|
|
|
|
|
|
|
entries := defaultReturnedEntries
|
|
|
|
maximumConfiguredEntries := ch.App.Config.Catalog.MaxEntries
|
|
|
|
|
|
|
|
// parse n, if n unparseable, or negative assign it to defaultReturnedEntries
|
|
|
|
if n := q.Get("n"); n != "" {
|
|
|
|
parsedMax, err := strconv.Atoi(n)
|
|
|
|
if err == nil {
|
|
|
|
if parsedMax > maximumConfiguredEntries {
|
|
|
|
ch.Errors = append(ch.Errors, v2.ErrorCodePaginationNumberInvalid.WithDetail(map[string]int{"n": parsedMax}))
|
|
|
|
return
|
|
|
|
} else if parsedMax >= 0 {
|
|
|
|
entries = parsedMax
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 16:52:27 +00:00
|
|
|
// then enforce entries to be between 0 & maximumConfiguredEntries
|
|
|
|
// max(0, min(entries, maximumConfiguredEntries))
|
|
|
|
if entries < 0 || entries > maximumConfiguredEntries {
|
|
|
|
entries = maximumConfiguredEntries
|
|
|
|
}
|
2015-07-17 18:42:47 +00:00
|
|
|
|
2023-04-24 16:52:27 +00:00
|
|
|
repos := make([]string, entries)
|
|
|
|
filled := 0
|
2016-07-14 20:24:16 +00:00
|
|
|
|
2023-04-24 16:52:27 +00:00
|
|
|
// entries is guaranteed to be >= 0 and < maximumConfiguredEntries
|
|
|
|
if entries == 0 {
|
2015-07-17 18:42:47 +00:00
|
|
|
moreEntries = false
|
2023-04-24 16:52:27 +00:00
|
|
|
} else {
|
|
|
|
returnedRepositories, err := ch.App.registry.Repositories(ch.Context, repos, lastEntry)
|
|
|
|
if err != nil {
|
|
|
|
_, pathNotFound := err.(driver.PathNotFoundError)
|
|
|
|
if err != io.EOF && !pathNotFound {
|
|
|
|
ch.Errors = append(ch.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// err is either io.EOF or not PathNotFoundError
|
|
|
|
moreEntries = false
|
|
|
|
}
|
|
|
|
filled = returnedRepositories
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
|
|
|
// Add a link header if there are more entries to retrieve
|
|
|
|
if moreEntries {
|
2023-04-24 16:52:27 +00:00
|
|
|
lastEntry = repos[filled-1]
|
|
|
|
urlStr, err := createLinkEntry(r.URL.String(), entries, lastEntry)
|
2015-07-13 20:08:13 +00:00
|
|
|
if err != nil {
|
|
|
|
ch.Errors = append(ch.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Link", urlStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
if err := enc.Encode(catalogAPIResponse{
|
2015-07-17 18:42:47 +00:00
|
|
|
Repositories: repos[0:filled],
|
2015-07-13 20:08:13 +00:00
|
|
|
}); err != nil {
|
|
|
|
ch.Errors = append(ch.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the original URL from the request to create a new URL for
|
|
|
|
// the link header
|
2015-07-17 18:42:47 +00:00
|
|
|
func createLinkEntry(origURL string, maxEntries int, lastEntry string) (string, error) {
|
2015-07-13 20:08:13 +00:00
|
|
|
calledURL, err := url.Parse(origURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
v := url.Values{}
|
|
|
|
v.Add("n", strconv.Itoa(maxEntries))
|
|
|
|
v.Add("last", lastEntry)
|
|
|
|
|
|
|
|
calledURL.RawQuery = v.Encode()
|
|
|
|
|
2015-07-13 20:08:13 +00:00
|
|
|
calledURL.Fragment = ""
|
|
|
|
urlStr := fmt.Sprintf("<%s>; rel=\"next\"", calledURL.String())
|
|
|
|
|
|
|
|
return urlStr, nil
|
|
|
|
}
|