internal/pkg: compute http identifier from url
All checks were successful
Test / Create distribution (push) Successful in 44s
Test / Sandbox (push) Successful in 2m30s
Test / ShareFS (push) Successful in 3m40s
Test / Hpkg (push) Successful in 4m24s
Test / Sandbox (race detector) (push) Successful in 4m46s
Test / Hakurei (race detector) (push) Successful in 5m51s
Test / Hakurei (push) Successful in 2m28s
Test / Flake checks (push) Successful in 1m41s

The previous implementation exposes arbitrary user input to the cache as an identifier, which is highly error-prone and can cause the cache to enter an inconsistent state if the user is not careful. This change replaces the implementation to compute identifier late, using url string as params.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-05 00:43:21 +09:00
parent 4897b0259e
commit 4da26681b5
7 changed files with 95 additions and 119 deletions

View File

@@ -11,10 +11,14 @@ import (
"hakurei.app/container/check"
)
// An httpArtifact is an [Artifact] backed by an [http] request.
// An httpArtifact is an [Artifact] backed by a [http] url string. The method is
// hardcoded as [http.MethodGet]. Request body is not allowed because it cannot
// be deterministically represented by Params.
type httpArtifact struct {
// Caller-supplied request.
req *http.Request
// Caller-supplied context.
ctx context.Context
// Caller-supplied url string.
url string
// Caller-supplied checksum of the response body. This is validated during
// curing and the first call to Data.
@@ -30,15 +34,6 @@ type httpArtifact struct {
mu sync.Mutex
}
// NewHTTP returns a new [File] backed by the supplied client and request. If
// c is nil, [http.DefaultClient] is used instead.
func NewHTTP(c *http.Client, req *http.Request, checksum Checksum) File {
if c == nil {
c = http.DefaultClient
}
return &httpArtifact{req: req, checksum: checksum, doFunc: c.Do}
}
// NewHTTPGet returns a new [File] backed by the supplied client. A GET request
// is set up for url. If c is nil, [http.DefaultClient] is used instead.
func NewHTTPGet(
@@ -46,24 +41,22 @@ func NewHTTPGet(
c *http.Client,
url string,
checksum Checksum,
) (File, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
) File {
if ctx == nil {
ctx = context.Background()
}
return NewHTTP(c, req, checksum), nil
if c == nil {
c = http.DefaultClient
}
return &httpArtifact{ctx: ctx, url: url, checksum: checksum, doFunc: c.Do}
}
// Kind returns the hardcoded [Kind] constant.
func (a *httpArtifact) Kind() Kind { return KindHTTP }
func (a *httpArtifact) Kind() Kind { return KindHTTPGet }
// ID returns the caller-supplied hash of the response body.
func (a *httpArtifact) ID() ID { return a.checksum }
// Params is unreachable.
func (a *httpArtifact) Params() []byte {
panic("not implemented")
}
// Params returns the backing url string. Context is not represented as it does
// not affect [Cache.Cure] outcome.
func (a *httpArtifact) Params() []byte { return []byte(a.url) }
// Dependencies returns a nil slice.
func (a *httpArtifact) Dependencies() []Artifact { return nil }
@@ -82,8 +75,14 @@ func (e ResponseStatusError) Error() string {
// do sends the caller-supplied request on the caller-supplied [http.Client]
// and reads its response body to EOF and returns the resulting bytes.
func (a *httpArtifact) do() (data []byte, err error) {
var req *http.Request
req, err = http.NewRequestWithContext(a.ctx, http.MethodGet, a.url, nil)
if err != nil {
return
}
var resp *http.Response
if resp, err = a.doFunc(a.req); err != nil {
if resp, err = a.doFunc(req); err != nil {
return
}