Skip to content

Commit 317f4fa

Browse files
authored
close connections after request is processed (#34)
* close connections after request is processed
1 parent e0c1691 commit 317f4fa

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Sources/NIOHTTPClient/HTTPHandler.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ internal class TaskHandler<T: HTTPClientResponseDelegate>: ChannelInboundHandler
286286
headers.add(name: "Host", value: request.host)
287287
}
288288

289+
headers.add(name: "Connection", value: "close")
290+
289291
do {
290292
try headers.validate(body: request.body)
291293
} catch {
@@ -321,6 +323,11 @@ internal class TaskHandler<T: HTTPClientResponseDelegate>: ChannelInboundHandler
321323

322324
self.state = .sent
323325
self.delegate.didTransmitRequestBody(task: self.task)
326+
327+
let channel = context.channel
328+
self.promise.futureResult.whenComplete { _ in
329+
channel.close(promise: nil)
330+
}
324331
}
325332

326333
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
@@ -346,6 +353,7 @@ internal class TaskHandler<T: HTTPClientResponseDelegate>: ChannelInboundHandler
346353
case .redirected(let head, let redirectURL):
347354
self.state = .end
348355
self.redirectHandler?.redirect(status: head.status, to: redirectURL, promise: self.promise)
356+
context.close(promise: nil)
349357
default:
350358
self.state = .end
351359
do {

Tests/NIOHTTPClientTests/SwiftNIOHTTPTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class SwiftHTTPTests: XCTestCase {
5555
head.headers.add(name: "X-Test-Header", value: "X-Test-Value")
5656
head.headers.add(name: "Host", value: "localhost")
5757
head.headers.add(name: "Content-Length", value: "4")
58+
head.headers.add(name: "Connection", value: "close")
5859
XCTAssertEqual(HTTPClientRequestPart.head(head), recorder.writes[0])
5960
var buffer = ByteBufferAllocator().buffer(capacity: 4)
6061
buffer.writeString("1234")

0 commit comments

Comments
 (0)