Skip to content

Commit 3cbe0b9

Browse files
Merge pull request #62 from NamelessOne91/url_escaping
Escape pkg names in URLs
2 parents c6a9e56 + 5d50bb3 commit 3cbe0b9

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

get/syncer.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,14 @@ func (r *Syncer) storeRepo(checksumMap map[string]XMLChecksum) (err error) {
179179
downloadCount := len(packagesToDownload)
180180
log.Printf("Downloading %v packages...\n", downloadCount)
181181
for i, pack := range packagesToDownload {
182-
description := fmt.Sprintf("(%v/%v) %v", i+1, downloadCount, path.Base(pack.Location.Href))
183-
err = r.downloadStoreApply(pack.Location.Href, pack.Checksum.Checksum, description, hashMap[pack.Checksum.Type], util.Nop)
182+
// we need to escape package names because some CDN, proxies (...) are not perfectly RFC 3986 compliant
183+
// in such cases characters like '+' (which are common in c++ pkgs) will assume a different meaning
184+
name := path.Base(pack.Location.Href)
185+
escapedName := url.QueryEscape(name)
186+
relativeURL := strings.TrimSuffix(pack.Location.Href, name) + escapedName
187+
188+
description := fmt.Sprintf("(%v/%v) %v", i+1, downloadCount, name)
189+
err = r.downloadStoreApply(relativeURL, pack.Checksum.Checksum, description, hashMap[pack.Checksum.Type], util.Nop)
184190
if err != nil {
185191
return err
186192
}
@@ -207,13 +213,20 @@ func (r *Syncer) storeRepo(checksumMap map[string]XMLChecksum) (err error) {
207213
func (r *Syncer) downloadStoreApply(relativePath string, checksum string, description string, hash crypto.Hash, f util.ReaderConsumer) error {
208214
log.Printf("Downloading %v...", description)
209215

210-
url := r.URL
211-
url.Path = path.Join(r.URL.Path, relativePath)
212-
body, err := ReadURL(url.String())
216+
repoURL := r.URL
217+
repoURL.Path = path.Join(repoURL.Path, relativePath)
218+
finalURL := fmt.Sprintf("%s://%s%s", repoURL.Scheme, repoURL.Host, repoURL.Path)
219+
220+
body, err := ReadURL(finalURL)
221+
if err != nil {
222+
return err
223+
}
224+
// unescape to preserve original pkg name
225+
storagePath, err := url.QueryUnescape(relativePath)
213226
if err != nil {
214227
return err
215228
}
216-
return util.Compose(r.storage.StoringMapper(relativePath, checksum, hash), f)(body)
229+
return util.Compose(r.storage.StoringMapper(storagePath, checksum, hash), f)(body)
217230
}
218231

219232
// processMetadata stores the repo metadata and returns a list of package file

0 commit comments

Comments
 (0)