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"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
2015-07-13 20:08:13 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maximumReturnedEntries = 100
|
|
|
|
|
|
|
|
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) {
|
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")
|
|
|
|
maxEntries, err := strconv.Atoi(q.Get("n"))
|
|
|
|
if err != nil || maxEntries < 0 {
|
|
|
|
maxEntries = maximumReturnedEntries
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
repos := make([]string, maxEntries)
|
|
|
|
|
|
|
|
filled, err := ch.App.registry.Repositories(ch.Context, repos, lastEntry)
|
2016-07-14 22:03:18 +00:00
|
|
|
_, pathNotFound := err.(driver.PathNotFoundError)
|
2016-07-14 20:24:16 +00:00
|
|
|
|
2016-07-14 22:03:18 +00:00
|
|
|
if err == io.EOF || pathNotFound {
|
2015-07-17 18:42:47 +00:00
|
|
|
moreEntries = false
|
2016-08-06 00:21:48 +00:00
|
|
|
} else if err != nil {
|
2015-07-13 20:08:13 +00:00
|
|
|
ch.Errors = append(ch.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-07-17 18:42:47 +00:00
|
|
|
lastEntry = repos[len(repos)-1]
|
|
|
|
urlStr, err := createLinkEntry(r.URL.String(), maxEntries, 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
|
|
|
|
}
|