diff options
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 } |
