Skip to content
Open
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
23 changes: 13 additions & 10 deletions pkg/apk/apk/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@ func (i *indexCache) get(ctx context.Context, repoName, repoURL string, keys map
return nil, fmt.Errorf("unable to add auth to request: %w", err)
}

resp, err := client.Do(head)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}

fetchAndParse := func(etag string) (NamedIndex, error) {
b, err := fetchRepositoryIndex(ctx, u, etag, opts)
if err != nil {
Expand All @@ -153,6 +143,19 @@ func (i *indexCache) get(ctx context.Context, repoName, repoURL string, keys map
return NewNamedRepositoryWithIndex(repoName, repoRef.WithIndex(idx)), nil
}

resp, err := client.Do(head)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusMethodNotAllowed || resp.StatusCode == http.StatusNotImplemented {
return fetchAndParse("")
}
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}

etag, ok := etagFromResponse(resp)
if !ok {
// If there's no etag, we can't cache it, so just return the result.
Expand Down
26 changes: 26 additions & 0 deletions pkg/apk/apk/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/base32"
"fmt"
"io"
"io/fs"
"maps"
"net/http"
Expand Down Expand Up @@ -91,6 +92,21 @@ guyM+Ks3c29KlRf3iX35Gt0CAwEAAQ==
testArch = "aarch64"
)

type headMethodNotAllowedTransport struct {
wrapped http.RoundTripper
}

func (t *headMethodNotAllowedTransport) RoundTrip(request *http.Request) (*http.Response, error) {
if request.Method == http.MethodHead {
return &http.Response{
StatusCode: http.StatusMethodNotAllowed,
Body: io.NopCloser(strings.NewReader("method not allowed")),
}, nil
}

return t.wrapped.RoundTrip(request)
}

func TestGetRepositoryIndexes(t *testing.T) {
prepLayout := func(t *testing.T, tr http.RoundTripper, cache string, repos []string) *APK {
src := apkfs.NewMemFS()
Expand Down Expand Up @@ -184,6 +200,16 @@ func TestGetRepositoryIndexes(t *testing.T) {
require.NoError(t, err, "unable to read previous index file")
require.Equal(t, index1, index2, "index files do not match")
})
t.Run("head method not allowed falls back to get", func(t *testing.T) {
tmpDir := t.TempDir()
a := prepLayout(t, &headMethodNotAllowedTransport{
wrapped: &testLocalTransport{root: testPrimaryPkgDir, basenameOnly: true},
}, tmpDir, nil)

indexes, err := a.GetRepositoryIndexes(context.Background(), false)
require.NoErrorf(t, err, "unable to get indexes")
require.Greater(t, len(indexes), 0, "no indexes found")
})
t.Run("repo url with http basic auth", func(t *testing.T) {
tmpDir := t.TempDir()
a := prepLayout(t, &testLocalTransport{
Expand Down