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"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/registry/api/errcode"
|
2023-03-31 11:16:29 +00:00
|
|
|
v2 "github.com/distribution/distribution/v3/registry/api/v2"
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
2015-07-13 20:08:13 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
)
|
|
|
|
|
2023-03-31 11:16:29 +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{
|
2022-11-02 22:31:23 +00:00
|
|
|
http.MethodGet: http.HandlerFunc(catalogHandler.GetCatalog),
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type catalogHandler struct {
|
|
|
|
*Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type catalogAPIResponse struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *catalogHandler) GetCatalog(w http.ResponseWriter, r *http.Request) {
|
2022-11-02 21:05:45 +00:00
|
|
|
moreEntries := true
|
2015-07-17 18:42:47 +00:00
|
|
|
|
2015-07-13 20:08:13 +00:00
|
|
|
q := r.URL.Query()
|
|
|
|
lastEntry := q.Get("last")
|
2023-03-31 11:16:29 +00:00
|
|
|
|
|
|
|
entries := defaultReturnedEntries
|
|
|
|
maximumConfiguredEntries := ch.App.Config.Catalog.MaxEntries
|
|
|
|
|
|
|
|
// parse n, if n is negative abort with an error
|
|
|
|
if n := q.Get("n"); n != "" {
|
|
|
|
parsedMax, err := strconv.Atoi(n)
|
|
|
|
if err != nil || parsedMax < 0 {
|
|
|
|
ch.Errors = append(ch.Errors, v2.ErrorCodePaginationNumberInvalid.WithDetail(map[string]string{"n": n}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if a client requests more than it's allowed to receive
|
|
|
|
if parsedMax > maximumConfiguredEntries {
|
|
|
|
ch.Errors = append(ch.Errors, v2.ErrorCodePaginationNumberInvalid.WithDetail(map[string]int{"n": parsedMax}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entries = parsedMax
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 11:16:29 +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-03-31 11:16:29 +00:00
|
|
|
repos := make([]string, entries)
|
|
|
|
filled := 0
|
2016-07-14 20:24:16 +00:00
|
|
|
|
2023-03-31 11:16:29 +00:00
|
|
|
// entries is guaranteed to be >= 0 and < maximumConfiguredEntries
|
|
|
|
if entries == 0 {
|
2015-07-17 18:42:47 +00:00
|
|
|
moreEntries = false
|
2023-03-31 11:16:29 +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
|
|
|
}
|
|
|
|
|
2019-01-14 08:53:03 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2015-07-13 20:08:13 +00:00
|
|
|
|
|
|
|
// Add a link header if there are more entries to retrieve
|
|
|
|
if moreEntries {
|
2023-03-31 11:16:29 +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
|
|
|
|
}
|