Skip to content

Commit 00adc53

Browse files
Aaron Liberatoreseabaylea
authored andcommitted
Closes requests with Connection: close header (swift-server#52)
* Closes requests with Connection: close header * Fixes whitespace and merge mistake
1 parent 280caad commit 00adc53

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Sources/HTTP/HTTPStreamingParser.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public class StreamingParser: HTTPResponseWriter {
477477
self.keepAliveUntil = Date(timeIntervalSinceNow: StreamingParser.keepAliveTimeout).timeIntervalSinceReferenceDate
478478
self.parserConnector?.responseComplete()
479479
} else {
480-
self.parserConnector?.closeWriter()
480+
self.parserConnector?.responseCompleteCloseWriter()
481481
}
482482
completion(.ok)
483483
}
@@ -503,6 +503,9 @@ public protocol ParserConnecting: class {
503503

504504
/// Let the network know that a response is complete, so it can be closed after timeout
505505
func responseComplete()
506+
507+
/// Let the network know that a response is complete and we're ready to close the connection
508+
func responseCompleteCloseWriter()
506509

507510
/// Used to let the network know we're ready to close the connection
508511
func closeWriter()

Sources/HTTP/PoCSocket/PoCSocketConnectionListener.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ public class PoCSocketConnectionListener: ParserConnecting {
184184
}
185185
}
186186
}
187+
188+
/// Called by the parser to let us know that a response is complete and we should close the socket
189+
public func responseCompleteCloseWriter() {
190+
self.socketWriterQueue.async { [weak self] in
191+
self?.responseCompleted = true
192+
self?.close()
193+
}
194+
}
187195

188196
/// Starts reading from the socket and feeding that data to the parser
189197
public func process() {

Tests/HTTPTests/ServerTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import XCTest
10+
import Dispatch
1011

1112
@testable import HTTP
1213

@@ -481,6 +482,45 @@ class ServerTests: XCTestCase {
481482
}
482483

483484

485+
func testExplicitCloseConnections() {
486+
let expectation = self.expectation(description: "0 Open Connection")
487+
let server = HTTPServer()
488+
do {
489+
try server.start(port: 0, handler: OkHandler().handle)
490+
491+
let session = URLSession(configuration: URLSessionConfiguration.default)
492+
let url1 = URL(string: "http://localhost:\(server.port)")!
493+
var request = URLRequest(url: url1)
494+
request.httpMethod = "POST"
495+
request.setValue("close", forHTTPHeaderField: "Connection")
496+
497+
let dataTask1 = session.dataTask(with: request) { (responseBody, rawResponse, error) in
498+
XCTAssertNil(error, "\(error!.localizedDescription)")
499+
#if os(Linux)
500+
XCTAssertEqual(server.connectionCount, 0)
501+
expectation.fulfill()
502+
503+
// Darwin's URLSession replaces the `Connection: close` header with `Connection: keep-alive`, so allow it to expire
504+
#else
505+
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
506+
XCTAssertEqual(server.connectionCount, 0)
507+
expectation.fulfill()
508+
}
509+
#endif
510+
}
511+
dataTask1.resume()
512+
513+
self.waitForExpectations(timeout: 30) { (error) in
514+
if let error = error {
515+
XCTFail("\(error)")
516+
}
517+
}
518+
server.stop()
519+
} catch {
520+
XCTFail("Error listening on port \(0): \(error). Use server.failed(callback:) to handle")
521+
}
522+
}
523+
484524
static var allTests = [
485525
("testEcho", testEcho),
486526
("testHello", testHello),
@@ -492,6 +532,7 @@ class ServerTests: XCTestCase {
492532
("testRequestEchoEndToEnd", testRequestEchoEndToEnd),
493533
("testRequestKeepAliveEchoEndToEnd", testRequestKeepAliveEchoEndToEnd),
494534
("testRequestLargeEchoEndToEnd", testRequestLargeEchoEndToEnd),
535+
("testExplicitCloseConnections", testExplicitCloseConnections),
495536
("testRequestLargePostHelloWorld", testRequestLargePostHelloWorld),
496537
]
497538
}

0 commit comments

Comments
 (0)