ooni-probe-cli/internal/engine/httpx/fetch.go
Simone Basso 4eeadd06a5
refactor: move more commands to internal/cmd (#207)
* refactor: move more commands to internal/cmd

Part of https://github.com/ooni/probe/issues/1335.

We would like all commands to be at the same level of engine
rather than inside engine (now that we can do it).

* fix: update .gitignore

* refactor: also move jafar outside engine

* We should be good now?
2021-02-03 12:23:15 +01:00

32 lines
864 B
Go

package httpx
import (
"context"
"crypto/sha256"
"fmt"
)
// FetchResource fetches the specified resource and returns it.
func (c Client) FetchResource(ctx context.Context, URLPath string) ([]byte, error) {
request, err := c.NewRequest(ctx, "GET", URLPath, nil, nil)
if err != nil {
return nil, err
}
return c.Do(request)
}
// FetchResourceAndVerify fetches and verifies a specific resource.
func (c Client) FetchResourceAndVerify(ctx context.Context, URL, SHA256Sum string) ([]byte, error) {
c.Logger.Debugf("httpx: expected SHA256: %s", SHA256Sum)
data, err := c.FetchResource(ctx, URL)
if err != nil {
return nil, err
}
s := fmt.Sprintf("%x", sha256.Sum256(data))
c.Logger.Debugf("httpx: real SHA256: %s", s)
if SHA256Sum != s {
return nil, fmt.Errorf("httpx: SHA256 mismatch: got %s and expected %s", s, SHA256Sum)
}
return data, nil
}