diff --git a/code/cli/munki/munkiCLItesting/urlsTests.swift b/code/cli/munki/munkiCLItesting/urlsTests.swift index 69c928b7..9ba1375e 100644 --- a/code/cli/munki/munkiCLItesting/urlsTests.swift +++ b/code/cli/munki/munkiCLItesting/urlsTests.swift @@ -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") + } } diff --git a/code/cli/munki/shared/network/urls.swift b/code/cli/munki/shared/network/urls.swift index 8e762915..e54f455a 100644 --- a/code/cli/munki/shared/network/urls.swift +++ b/code/cli/munki/shared/network/urls.swift @@ -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