mirror of
https://github.com/munki/munki.git
synced 2026-06-06 20:08:09 +00:00
When constructing a URL for a Munki resource, percent-encode any + characters in the path.
This commit is contained in:
@@ -53,9 +53,15 @@ struct munkiRepoURLTests {
|
||||
#expect(munkiRepoURL("manifests", resource: "site_default", munkiRepoURL: munkiBaseURL) == "https://munki.example.com/cgi?manifests/site_default")
|
||||
}
|
||||
|
||||
/// Tests building URLs to Munki repo resources when base URL iwhen resource name contains a special character
|
||||
/// Tests building URLs to Munki repo resources when resource name contains a special character
|
||||
@Test func urlWithResourceWithSpecialCharacterContructsExpected() async throws {
|
||||
let munkiBaseURL = "https://munki.example.com/repo"
|
||||
#expect(munkiRepoURL("manifests", resource: "site default", munkiRepoURL: munkiBaseURL) == "https://munki.example.com/repo/manifests/site%20default")
|
||||
}
|
||||
|
||||
/// Tests building URLs to Munki repo resources when resource name contains a +
|
||||
@Test func urlWithResourceWithPlusCharacterContructsExpected() async throws {
|
||||
let munkiBaseURL = "https://munki.example.com/repo"
|
||||
#expect(munkiRepoURL("manifests", resource: "site+default", munkiRepoURL: munkiBaseURL) == "https://munki.example.com/repo/manifests/site%2Bdefault")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,11 @@ func munkiRepoURL(_ type: String = "", resource: String = "", munkiRepoURL: Stri
|
||||
"manifests": "ManifestURL",
|
||||
"pkgs": "PackageURL",
|
||||
]
|
||||
guard let encodedType = (type as NSString).addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
|
||||
// we're going to remove '+' from the set of characters allowed in the urlPath
|
||||
// this forces '+' to be precent encoded, which makes some web servers happier
|
||||
let allowedCharacters = CharacterSet.urlPathAllowed.subtracting(CharacterSet(charactersIn: "+"))
|
||||
|
||||
guard let encodedType = (type as NSString).addingPercentEncoding(withAllowedCharacters: allowedCharacters) else {
|
||||
// encoding failed
|
||||
return nil
|
||||
}
|
||||
@@ -84,7 +88,7 @@ func munkiRepoURL(_ type: String = "", resource: String = "", munkiRepoURL: Stri
|
||||
if resource.isEmpty {
|
||||
return typeURL
|
||||
}
|
||||
if let encodedResource = (resource as NSString).addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
|
||||
if let encodedResource = (resource as NSString).addingPercentEncoding(withAllowedCharacters: allowedCharacters) {
|
||||
return typeURL + encodedResource
|
||||
}
|
||||
// encoding failed
|
||||
|
||||
Reference in New Issue
Block a user