Skip to content

Commit

Permalink
Use correct path separators for URL and target files that are formatt…
Browse files Browse the repository at this point in the history
…ed like name/target

Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Feb 6, 2024
1 parent 0b437b3 commit c65dab4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions metadata/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, timeout t
}
defer res.Body.Close()
// Handle HTTP status codes.
if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusForbidden || res.StatusCode != http.StatusOK {
if res.StatusCode != http.StatusOK {
return nil, &metadata.ErrDownloadHTTP{StatusCode: res.StatusCode, URL: urlPath}
}
var length int64
Expand All @@ -74,7 +74,7 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, timeout t
// Although the size has been checked above, use a LimitReader in case
// the reported size is inaccurate, or size is -1 which indicates an
// unknown length. We read maxLength + 1 in order to check if the read data
// surpased our set limit.
// surpassed our set limit.
data, err := io.ReadAll(io.LimitReader(res.Body, maxLength+1))
if err != nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions metadata/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath
targetFilePath = fmt.Sprintf("%s.%s", hashes, dirName)
} else {
// <dir-prefix>/<hash>.<target-name>
targetFilePath = filepath.Join(dirName, fmt.Sprintf("%s.%s", hashes, baseName))
targetFilePath = fmt.Sprintf("%s/%s.%s", dirName, hashes, baseName)
}
}
fullURL := fmt.Sprintf("%s%s", targetBaseURL, targetFilePath)
Expand Down Expand Up @@ -676,8 +676,9 @@ func (update *Updater) generateTargetFilePath(tf *metadata.TargetFiles) (string,
if update.cfg.LocalTargetsDir == "" && !update.cfg.DisableLocalCache {
return "", &metadata.ErrValue{Msg: "LocalTargetsDir must be set if filepath is not given"}
}
// Use URL encoded target path as filename
return url.JoinPath(update.cfg.LocalTargetsDir, url.QueryEscape(tf.Path))
// Use URL encoded target path as filename (e.g. "foo/bar" -> "foo%2Fbar")
// Normalize the path separator to the OS specific one to avoid failures on Windows
return url.JoinPath(update.cfg.LocalTargetsDir, url.QueryEscape(filepath.FromSlash(tf.Path)))
}

// loadLocalMetadata reads a local <roleName>.json file and returns its bytes
Expand Down

0 comments on commit c65dab4

Please sign in to comment.