Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,11 @@ func upstreamMetadataStatus(resp *http.Response) error {
body, _ := io.ReadAll(io.LimitReader(resp.Body, upstreamNotFoundBodyLimit))
return &upstreamNotFoundError{body: body}
}
// A server error or a 429 is logged by its status alone, without the
// upstream's error page; as in registries' fetcher, no StatusError.
if resp.StatusCode >= http.StatusInternalServerError || resp.StatusCode == http.StatusTooManyRequests {
return fmt.Errorf("upstream returned %d", resp.StatusCode)
}
// The status, body and type are kept, so a policy denial can be
// forwarded as upstream sent it.
body, _ := io.ReadAll(io.LimitReader(resp.Body, upstreamStatusBodyLimit))
Expand Down
23 changes: 23 additions & 0 deletions internal/handler/metadata_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package handler
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/git-pkgs/cooldown"
"github.com/git-pkgs/registries/fetch"
)

// A firewall that refuses a metadata request reaches the client with its own
Expand Down Expand Up @@ -281,3 +284,23 @@ func TestMetadataPolicyBlockServesTheCachedCopy(t *testing.T) {
t.Error("the second request wasn't sent upstream")
}
}

// A server error or a 429 is logged by its status alone, not with the
// upstream's error page.
func TestUpstreamMetadataStatusKeepsServerErrorsShort(t *testing.T) {
for _, code := range []int{http.StatusTooManyRequests, http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable} {
resp := &http.Response{StatusCode: code, Header: http.Header{}, Body: io.NopCloser(strings.NewReader("<html>an error page</html>"))}
err := upstreamMetadataStatus(resp)
var statusErr *fetch.StatusError
if want := fmt.Sprintf("upstream returned %d", code); err == nil || err.Error() != want || errors.As(err, &statusErr) {
t.Errorf("%d: error %v, want %q", code, err, want)
}
}
for _, code := range []int{http.StatusForbidden, http.StatusConflict, http.StatusGone} {
resp := &http.Response{StatusCode: code, Header: http.Header{}, Body: io.NopCloser(strings.NewReader("denied"))}
var statusErr *fetch.StatusError
if err := upstreamMetadataStatus(resp); !errors.As(err, &statusErr) || statusErr.Code != code || statusErr.Body != "denied" {
t.Errorf("%d: error %v, want a StatusError with the body", code, err)
}
}
}
Loading