weldr: improve ostree ref and URL handling

Replacing repeated calls to u.Parse() with path.Join() on the URL's
path. This method handles certain edge cases differently:
- location not ending in / (http://example.org/repo):
    - with the old method, the subsequent parsing of "refs/heads/" would
      overwrite the path segment of the original URL, resulting in
      http://example.org/refs/heads
    - with the new method, "refs/heads" is appended to the location and
      a / is added between the two parts if necessary.
- ref begins with / (location: http://example.org/repo/, ref: /ref):
    - with the old method, the final parsing of ref would overwrite the
      path segment of the URL, resulting in http://example.org/ref
    - with the new method, the ref is appended and a / is added between
      parts where necessary (same as above).
- ref is a full URL
(location: http://example.org/repo/, ref: http://example.com):
    - with the old method, u.Parse(ref) would completely overwrite the
      existing URL in u.
    - with the new method, the ref is added as a sanitised URL path
      resulting in http://example.org/refs/heads/http:/example.com.

The last one will probably result in an error in either case, but it's
probably less incorrect to coerce the ref argument into a path.

The response status code of the GET request is checked as well to
provide an appropriate error message if it is not 200 (OK).

If the data in the response is not a valid hex string, the error message
from the DecodeString() method isn't returned directly and it is
replaced by a more useful message. The original error message is
discarded.
This commit is contained in:
Achilleas Koutsou 2021-03-24 20:20:42 +01:00 committed by Tom Gundersen
parent 1c9ad19a42
commit 91e5b6bf9b

View file

@ -1806,18 +1806,14 @@ func ostreeResolveRef(location, ref string) (string, error) {
if err != nil {
return "", err
}
u, err = u.Parse("refs/heads/")
if err != nil {
return "", err
}
u, err = u.Parse(ref)
if err != nil {
return "", err
}
u.Path = path.Join(u.Path, "refs/heads/", ref)
resp, err := http.Get(u.String())
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("ostree repository %q returned status: %s", u.String(), resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
@ -1826,7 +1822,7 @@ func ostreeResolveRef(location, ref string) (string, error) {
// Check that this is at least a hex string.
_, err = hex.DecodeString(parent)
if err != nil {
return "", err
return "", fmt.Errorf("ostree repository %q returned invalid reference", u.String())
}
return parent, nil
}