forked from security/hakurei
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>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user