Skip to content

Commit 022bb50

Browse files
Andrew-Lees11artemredkin
authored andcommitted
Add support for percent encoded requests (#52)
1 parent d7f2a39 commit 022bb50

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

‎Sources/NIOHTTPClient/HTTPHandler.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ extension HTTPClientResponseDelegate{
228228

229229
internalextensionURL{
230230
varuri:String{
231-
return path.isEmpty ?"/": path +(query.map{"?"+ $0 }??"")
231+
leturlEncodedPath= path.addingPercentEncoding(withAllowedCharacters:.urlPathAllowed)?? path
232+
return path.isEmpty ?"/": urlEncodedPath +(query.map{"?"+ $0 }??"")
232233
}
233234

234235
func hasTheSameOrigin(as other:URL)->Bool{

‎Tests/NIOHTTPClientTests/HTTPClientTestUtils.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ internal final class HttpBinHandler: ChannelInboundHandler{
246246
headers.add(name:"Location", value:"http://127.0.0.1:\(port)/echohostheader")
247247
self.resps.append(HTTPResponseBuilder(status:.found, headers: headers))
248248
return
249+
// Since this String is taken from URL.path, the percent encoding has been removed
250+
case"/percent encoded":
251+
if req.method !=.GET {
252+
self.resps.append(HTTPResponseBuilder(status:.methodNotAllowed))
253+
return
254+
}
255+
self.resps.append(HTTPResponseBuilder(status:.ok))
256+
return
249257
case"/echohostheader":
250258
varbuilder=HTTPResponseBuilder(status:.ok)
251259
lethostValue= req.headers["Host"].first ??""

‎Tests/NIOHTTPClientTests/HTTPClientTests+XCTest.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extension HTTPClientTests{
3232
("testPostHttps", testPostHttps),
3333
("testHttpRedirect", testHttpRedirect),
3434
("testHttpHostRedirect", testHttpHostRedirect),
35+
("testPercentEncoded", testPercentEncoded),
3536
("testMultipleContentLengthHeaders", testMultipleContentLengthHeaders),
3637
("testStreaming", testStreaming),
3738
("testRemoteClose", testRemoteClose),

‎Tests/NIOHTTPClientTests/HTTPClientTests.swift‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ class HTTPClientTests: XCTestCase{
135135
lethostName=try decoder.decode([String:String].self, from: responseData)["data"]
136136
XCTAssert(hostName =="127.0.0.1")
137137
}
138+
139+
func testPercentEncoded()throws{
140+
lethttpBin=HttpBin()
141+
lethttpClient=HTTPClient(eventLoopGroupProvider:.createNew)
142+
defer{
143+
try! httpClient.syncShutdown()
144+
httpBin.shutdown()
145+
}
146+
147+
letresponse=try httpClient.get(url:"http://localhost:\(httpBin.port)/percent%20encoded").wait()
148+
XCTAssertEqual(.ok, response.status)
149+
}
138150

139151
func testMultipleContentLengthHeaders()throws{
140152
lethttpClient=HTTPClient(eventLoopGroupProvider:.createNew)

0 commit comments

Comments
(0)