Files
hakurei/internal/pkg/net.go
Ophestra 4897b0259e
All checks were successful
Test / Hakurei (race detector) (push) Successful in 5m52s
Test / Create distribution (push) Successful in 43s
Test / Sandbox (push) Successful in 2m35s
Test / Hakurei (push) Successful in 3m36s
Test / ShareFS (push) Successful in 3m41s
Test / Hpkg (push) Successful in 4m19s
Test / Sandbox (race detector) (push) Successful in 4m52s
Test / Flake checks (push) Successful in 1m53s
internal/pkg: improve artifact interface
This moves all cache I/O code to Cache. Artifact now only contains methods for constructing their actual contents.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2026-01-05 00:01:23 +09:00

132 lines
3.3 KiB
Go

package pkg
import (
"context"
"crypto/sha512"
"io"
"net/http"
"sync"
"syscall"
"hakurei.app/container/check"
)
// An httpArtifact is an [Artifact] backed by an [http] request.
type httpArtifact struct {
// Caller-supplied request.
req *http.Request
// Caller-supplied checksum of the response body. This is validated during
// curing and the first call to Data.
checksum Checksum
// doFunc is the Do method of [http.Client] supplied by the caller.
doFunc func(req *http.Request) (*http.Response, error)
// Response body read to EOF.
data []byte
// Synchronises access to data.
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(
ctx context.Context,
c *http.Client,
url string,
checksum Checksum,
) (File, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return NewHTTP(c, req, checksum), nil
}
// Kind returns the hardcoded [Kind] constant.
func (a *httpArtifact) Kind() Kind { return KindHTTP }
// 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")
}
// Dependencies returns a nil slice.
func (a *httpArtifact) Dependencies() []Artifact { return nil }
// Checksum returns the address to the caller-supplied checksum.
func (a *httpArtifact) Checksum() *Checksum { return &a.checksum }
// ResponseStatusError is returned for a response returned by an [http.Client]
// with a status code other than [http.StatusOK].
type ResponseStatusError int
func (e ResponseStatusError) Error() string {
return "the requested URL returned non-OK status: " + http.StatusText(int(e))
}
// 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 resp *http.Response
if resp, err = a.doFunc(a.req); err != nil {
return
}
if resp.StatusCode != http.StatusOK {
_ = resp.Body.Close()
return nil, ResponseStatusError(resp.StatusCode)
}
if data, err = io.ReadAll(resp.Body); err != nil {
_ = resp.Body.Close()
return
}
err = resp.Body.Close()
return
}
// Cure returns syscall.ENOTSUP. Callers should use Data instead.
func (a *httpArtifact) Cure(*check.Absolute, CacheDataFunc) error {
return syscall.ENOTSUP
}
// Data completes the http request and returns the resulting response body read
// to EOF. Data does not interact with the filesystem.
func (a *httpArtifact) Data() (data []byte, err error) {
a.mu.Lock()
defer a.mu.Unlock()
if a.data != nil {
// validated by cache or a previous call to Data
return a.data, nil
}
if data, err = a.do(); err != nil {
return
}
h := sha512.New384()
h.Write(data)
if got := (Checksum)(h.Sum(nil)); got != a.checksum {
return nil, &ChecksumMismatchError{got, a.checksum}
}
a.data = data
return
}