diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-02-04 14:41:18 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-02-05 08:24:09 +0900 |
| commit | e0c720681bbce364910df241658dc987851e579a (patch) | |
| tree | bdbfbd67f26d562e8f1b2b2980a4bde6a10c94cc /internal/pkg/net.go | |
| parent | f982b13a59a59e22d1ff3fdf791c1aa86b9420df (diff) | |
internal/pkg: standardise artifact IR
This should hopefully provide good separation between the artifact curing backend implementation and the (still work in progress) language. Making the IR parseable also guarantees uniqueness of the representation.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/net.go')
| -rw-r--r-- | internal/pkg/net.go | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/internal/pkg/net.go b/internal/pkg/net.go index b8fb8fb3..1f37a92d 100644 --- a/internal/pkg/net.go +++ b/internal/pkg/net.go @@ -19,8 +19,8 @@ type httpArtifact struct { // closing the [io.ReadCloser] returned by Cure. checksum unique.Handle[Checksum] - // doFunc is the Do method of [http.Client] supplied by the caller. - doFunc func(req *http.Request) (*http.Response, error) + // client is the address of the caller-supplied [http.Client]. + client *http.Client } var _ KnownChecksum = new(httpArtifact) @@ -33,10 +33,7 @@ func NewHTTPGet( url string, checksum Checksum, ) FileArtifact { - if c == nil { - c = http.DefaultClient - } - return &httpArtifact{url: url, checksum: unique.Make(checksum), doFunc: c.Do} + return &httpArtifact{url: url, checksum: unique.Make(checksum), client: c} } // Kind returns the hardcoded [Kind] constant. @@ -44,8 +41,17 @@ func (*httpArtifact) Kind() Kind { return KindHTTPGet } // Params writes the backing url string. Client is not represented as it does // not affect [Cache.Cure] outcome. -func (a *httpArtifact) Params(ctx *IContext) { - ctx.GetHash().Write([]byte(a.url)) +func (a *httpArtifact) Params(ctx *IContext) { ctx.WriteString(a.url) } + +func init() { + register(KindHTTPGet, func(r *IRReader) Artifact { + url := r.ReadString() + checksum, ok := r.Finalise() + if !ok { + panic(ErrExpectedChecksum) + } + return NewHTTPGet(nil, url, checksum.Value()) + }) } // Dependencies returns a nil slice. @@ -80,8 +86,13 @@ func (a *httpArtifact) Cure(r *RContext) (rc io.ReadCloser, err error) { } req.Header.Set("User-Agent", "Hakurei/1.1") + c := a.client + if c == nil { + c = http.DefaultClient + } + var resp *http.Response - if resp, err = a.doFunc(req); err != nil { + if resp, err = c.Do(req); err != nil { return } |
