Skip to content

Commit

Permalink
Bugfix: Encoding
Browse files Browse the repository at this point in the history
- Fix double encoding issue
- Maintain order of query parameters
  • Loading branch information
ardian4 committed Mar 13, 2024
1 parent 8697df5 commit 6ee9d34
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
32 changes: 22 additions & 10 deletions Sources/TSMobileAnalytics/Utilities/APIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ final class APIService {
appVersion: String,
sdkVersion: String
) -> URL {
let categoryString = categoryString.replacingOccurrences(
of: String.slash, with: String.slashReplacement)

let parameters: [String : String] = [
let parameters: KeyValuePairs<String, String> = [
.siteId : cpid,
.appClientId : userID,
.cp : categoryString,
Expand All @@ -144,7 +142,7 @@ final class APIService {
}

func syncURL(sdkVersion: String, appName: String, json: String) -> URL {
let parameters: [String: String] = [
let parameters: KeyValuePairs<String,String> = [
.sdkVersion : sdkVersion,
.appName.lowercased() : appName,
.frameworkInfo : json
Expand All @@ -166,6 +164,22 @@ extension String {

return encoded
}

func queryEncoded() -> String {
guard let encoded = self.addingPercentEncoding(withAllowedCharacters: .urlQueryExtendedAllowed)
else {
TSMobileAnalytics.logger.log(
message: "Failed to add percent encoding to URL",
verbosity: .error)
return .empty
}

return encoded
}
}

extension CharacterSet{
static let urlQueryExtendedAllowed = CharacterSet.urlQueryAllowed.subtracting(CharacterSet(charactersIn: String.slash))
}

// MARK: - Private
Expand Down Expand Up @@ -200,17 +214,17 @@ private extension APIService {
contentID.count <= .contentIDMaxLength
}

func url(_ baseUrl: URL, withParameters parameters: [String : String]) -> URL {
func url(_ baseUrl: URL, withParameters parameters: KeyValuePairs<String,String>) -> URL {
guard var urlComponents = URLComponents(url: baseUrl, resolvingAgainstBaseURL: false)
else { return baseUrl }

var queryItems = urlComponents.queryItems ?? [URLQueryItem]()

for (key, value) in parameters {
queryItems.append(URLQueryItem(name: key, value: value))
queryItems.append(URLQueryItem(name: key.queryEncoded(), value: value.queryEncoded()))
}

urlComponents.queryItems = queryItems
urlComponents.percentEncodedQueryItems = queryItems

return urlComponents.url ?? baseUrl
}
Expand All @@ -222,8 +236,6 @@ private extension String {
static let sdkVersion = "sdkversion"
static let frameworkInfo = "SifoAppFrameworkInfo"

static let slashReplacement = "%2F"

static let siteId = "siteId"
static let appClientId = "appClientId"
static let cp = "cp"
Expand Down
5 changes: 5 additions & 0 deletions release-notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ using Kantar Sifo’s services. The SDK contains three folders:

RELEASE NOTES:

6.0.2 2024-03-13
- Fix double encoding issue
- Maintain order of query parameters
- No API changes

6.0.1 2024-02-27
- Add Privacy Manifest

Expand Down

0 comments on commit 6ee9d34

Please sign in to comment.