Skip to content

Commit

Permalink
Don't percent encode if already encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkar N Kulkarni committed May 27, 2019
1 parent 5c1b42d commit 2b1ec9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 24 additions & 11 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class ClientRequest {
*/
public private(set) var url: String = ""

private var percentEncodedURL: String = ""

/**
The HTTP method (i.e. GET, POST, PUT, DELETE) for the request.

Expand Down Expand Up @@ -226,14 +228,30 @@ public class ClientRequest {
case useHTTP2
}

private func percentEncode(_ url: String) -> String {
var _url = url
let isPercentEncoded = _url.contains("%") && URL(string: _url) != nil
if !isPercentEncoded {
_url = _url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? _url
}
return _url
}

/// Initializes a `ClientRequest` instance
///
/// - Parameter url: url for the request
/// - Parameter callback: The closure of type `Callback` to be used for the callback.
init(url: String, callback: @escaping Callback) {
self.callback = callback
self.url = url
let url = URL(string: url)!
self.percentEncodedURL = percentEncode(url)

if let url = URL(string: self.percentEncodedURL) {
initialize(url)
}
}

private func initialize(_ url: URL) {
if let host = url.host {
self.hostName = host
}
Expand All @@ -257,7 +275,6 @@ public class ClientRequest {
if let password = url.password {
self.password = password
}
self.callback = callback
}

/**
Expand Down Expand Up @@ -329,11 +346,7 @@ public class ClientRequest {
if thePath.first != "/" {
thePath = "/" + thePath
}
if thePath.contains("%") == false {
path = thePath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? thePath
} else {
path = thePath
}
path = thePath
self.path = path
case .username(let userName):
self.userName = userName
Expand All @@ -352,8 +365,8 @@ public class ClientRequest {
}

//the url string
url = "\(theSchema)\(authenticationClause)\(hostName)\(port)\(path)"

self.url = "\(theSchema)\(authenticationClause)\(hostName)\(port)\(path)"
self.percentEncodedURL = percentEncode(self.url)
}

/**
Expand Down Expand Up @@ -524,7 +537,7 @@ public class ClientRequest {
var isHTTPS = false

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
if (URL(string: url)?.scheme)! == "https" {
if (URL(string: percentEncodedURL)?.scheme)! == "https" {
isHTTPS = true
self.sslConfig = TLSConfiguration.forClient(certificateVerification: .none)
}
Expand All @@ -535,7 +548,7 @@ public class ClientRequest {
initializeClientBootstrap(eventLoopGroup: group)
}

let hostName = URL(string: url)?.host ?? "" //TODO: what could be the failure path here
let hostName = URL(string: percentEncodedURL)?.host ?? "" //TODO: what could be the failure path here
if self.headers["Host"] == nil {
self.headers["Host"] = hostName
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/KituraNetTests/ClientRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ClientRequestTests: KituraNetTest {
.path("/view/matching?key=\"viewTest\"")
]
let testRequest1 = ClientRequest(options: options1, callback: testCallback)
XCTAssertEqual(testRequest1.url, "https://66o.tech/view/matching?key=%22viewTest%22")
XCTAssertEqual(testRequest1.url, "https://66o.tech/view/matching?key=\"viewTest\"")

let options2: [ClientRequest.Options] = [ .schema("https"),
.hostname("66o.tech"),
Expand Down

0 comments on commit 2b1ec9a

Please sign in to comment.