Skip to content

Commit

Permalink
fix: encode query strings with stricter character set
Browse files Browse the repository at this point in the history
  • Loading branch information
Mapleshade20 committed Dec 30, 2024
1 parent b713e92 commit 5aa92eb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ struct GeniusLyricsRepository: LyricsRepository {
var stringUrl = "\(apiUrl)\(path)"

if !query.isEmpty {
let queryString = query.queryString.addingPercentEncoding(
withAllowedCharacters: .urlHostAllowed
)!
let queryString = query.queryString

stringUrl += "?\(queryString)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ struct LrcLibLyricsRepository: LyricsRepository {
var stringUrl = "\(apiUrl)\(path)"

if !query.isEmpty {
let queryString = query.queryString.addingPercentEncoding(
withAllowedCharacters: .urlHostAllowed
)!
let queryString = query.queryString

stringUrl += "?\(queryString)"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

extension CharacterSet {
static var urlQueryAllowedStrict: CharacterSet {
var allowed = CharacterSet.urlQueryAllowed
allowed.remove(charactersIn: "/?@&=+$")
return allowed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import Foundation
extension Dictionary {
var queryString: String {
return self
.compactMap({ (key, value) -> String in
return "\(key)=\(value)"
.compactMap({ (key, value) -> String? in
guard let keyString = "\(key)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowedStrict),
let valueString = "\(value)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowedStrict) else {
return nil
}
return "\(keyString)=\(valueString)"
})
.joined(separator: "&")
.joined(separator: "&")
}
}

0 comments on commit 5aa92eb

Please sign in to comment.