Skip to content

Commit

Permalink
Add % encoding in ClientRequest.init, remove % encoding in HTTPServer…
Browse files Browse the repository at this point in the history
…Request.init
  • Loading branch information
Pushkar N Kulkarni committed Feb 21, 2019
1 parent a54fffe commit 00583e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public class ClientRequest {
if thePath.first != "/" {
thePath = "/" + thePath
}
path = thePath
path = thePath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) ?? thePath
self.path = path
case .username(let userName):
self.userName = userName
Expand Down
13 changes: 11 additions & 2 deletions Sources/KituraNet/HTTP/HTTPServerRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,16 @@ public class HTTPServerRequest: ServerRequest {

private var enableSSL: Bool = false

private var _urlString: String
private var rawURLString: String

private var urlStringPercentEncodingRemoved: String?

private var _urlString: String {
if urlStringPercentEncodingRemoved == nil {
urlStringPercentEncodingRemoved = rawURLString.removingPercentEncoding ?? rawURLString
}
return urlStringPercentEncodingRemoved!
}

private static func host(socketAddress: SocketAddress?) -> String {
guard let socketAddress = socketAddress else {
Expand All @@ -148,7 +157,7 @@ public class HTTPServerRequest: ServerRequest {
self.method = requestHead.method.string()
self.httpVersionMajor = requestHead.version.major
self.httpVersionMinor = requestHead.version.minor
self._urlString = requestHead.uri
self.rawURLString = requestHead.uri
self.enableSSL = enableSSL
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/KituraNetTests/ClientRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ class ClientRequestTests: KituraNetTest {
let options = ClientRequest.parse("https://username:password@66o.tech:8080/path?key=value")
let testRequest = ClientRequest(options: options, callback: testCallback)
XCTAssertEqual(testRequest.url, "https://username:password@66o.tech:8080/path?key=value")

let options1: [ClientRequest.Options] = [ .schema("https"),
.hostname("66o.tech"),
.path("/view/matching?key=\"viewTest\"")
]
let testRequest1 = ClientRequest(options: options1, callback: testCallback)
XCTAssertEqual(testRequest1.url, "https://66o.tech/view/matching?key=%22viewTest%22")

let options2: [ClientRequest.Options] = [ .schema("https"),
.hostname("66o.tech"),
.path("/view/match?key?=value?")
]
let testRequest2 = ClientRequest(options: options2, callback: testCallback)
XCTAssertEqual(testRequest2.url, "https://66o.tech/view/match?key?=value?")
}

func testClientRequestBasicAuthentcation() {
Expand Down

0 comments on commit 00583e0

Please sign in to comment.