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 (#171)
  • Loading branch information
Pushkar N Kulkarni authored and djones6 committed Mar 14, 2019
1 parent f6fcdc9 commit ba679fd
Show file tree
Hide file tree
Showing 3 changed files with 28 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
15 changes: 13 additions & 2 deletions Sources/KituraNet/HTTP/HTTPServerRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@ 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 {
guard let urlStringPercentEncodingRemoved = self.urlStringPercentEncodingRemoved else {
let _urlStringPercentEncodingRemoved = rawURLString.removingPercentEncoding ?? rawURLString
self.urlStringPercentEncodingRemoved = _urlStringPercentEncodingRemoved
return _urlStringPercentEncodingRemoved
}
return urlStringPercentEncodingRemoved
}

private static func host(socketAddress: SocketAddress?) -> String {
guard let socketAddress = socketAddress else {
Expand All @@ -148,7 +159,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 ba679fd

Please sign in to comment.