Skip to content

Commit 327e890

Browse files
authored
Merge pull request swift-server#41 from djones6/keepalive
Correct function of Keep-Alive header
2 parents fee5702 + a8205d2 commit 327e890

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

Sources/HTTP/HTTPStreamingParser.swift

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class StreamingParser: HTTPResponseWriter {
1919
/// Time to leave socket open waiting for next request to start
2020
public static let keepAliveTimeout: TimeInterval = 5
2121

22-
/// Flag to track if the client wants to send multiple requests on the same TCP connection
22+
/// Flag to track if the client wants to send consecutive requests on the same TCP connection
2323
var clientRequestedKeepAlive = false
2424

2525
/// Tracks when socket should be closed. Needs to have a lock, since it's updated often
@@ -42,12 +42,7 @@ public class StreamingParser: HTTPResponseWriter {
4242
}
4343
}
4444

45-
/// Theoretical limit of how many open requests we can have. Used in Keep-Alive Header
46-
let maxRequests = 100
47-
48-
/// Optional delegate that can tell us how many connections are in-flight so we can set the Keep-Alive header
49-
/// to the correct number of available connections. If not present, the client will not be limited in number of
50-
/// connections that can be made simultaneously
45+
/// Optional delegate that can tell us how many connections are in-flight.
5146
public weak var connectionCounter: CurrentConnectionCounting?
5247

5348
/// Holds the bytes that come from the CHTTPParser until we have enough of them to do something with it
@@ -392,14 +387,10 @@ public class StreamingParser: HTTPResponseWriter {
392387

393388

394389
if clientRequestedKeepAlive {
395-
let availableConnections = maxRequests - (self.connectionCounter?.connectionCount ?? 0)
396-
if availableConnections > 0 {
397-
headers[.connection] = "Keep-Alive"
398-
headers[.keepAlive] = "timeout=\(Int(StreamingParser.keepAliveTimeout)), max=\(availableConnections)"
399-
return
400-
}
390+
headers[.connection] = "Keep-Alive"
391+
} else {
392+
headers[.connection] = "Close"
401393
}
402-
headers[.connection] = "Close"
403394
}
404395

405396
public func writeTrailer(_ trailers: HTTPHeaders, completion: @escaping (Result) -> Void) {

Tests/HTTPTests/ServerTests.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,8 @@ class ServerTests: XCTestCase {
220220
XCTAssertNotNil(response)
221221
let headers = response?.allHeaderFields ?? ["": ""]
222222
let connectionHeader: String = headers["Connection"] as? String ?? ""
223-
let keepAliveHeader = headers["Keep-Alive"]
224223
XCTAssertEqual(connectionHeader, "Keep-Alive", "No Keep-Alive Connection")
225-
XCTAssertNotNil(keepAliveHeader)
226-
XCTAssertNotNil(responseBody, "No Keep-Alive Header")
224+
XCTAssertNotNil(responseBody, "No Response Body")
227225
XCTAssertEqual(server.connectionCount, 1)
228226
XCTAssertEqual(Int(HTTPResponseStatus.ok.code), response?.statusCode ?? 0)
229227
XCTAssertEqual(testString1, String(data: responseBody ?? Data(), encoding: .utf8) ?? "Nil")
@@ -237,9 +235,7 @@ class ServerTests: XCTestCase {
237235
XCTAssertNotNil(response2)
238236
let headers = response2?.allHeaderFields ?? ["": ""]
239237
let connectionHeader: String = headers["Connection"] as? String ?? ""
240-
let keepAliveHeader = headers["Keep-Alive"]
241238
XCTAssertEqual(connectionHeader, "Keep-Alive", "No Keep-Alive Connection")
242-
XCTAssertNotNil(keepAliveHeader, "No Keep-Alive Header")
243239
XCTAssertEqual(server.connectionCount, 1)
244240
XCTAssertNotNil(responseBody2)
245241
XCTAssertEqual(Int(HTTPResponseStatus.ok.code), response2?.statusCode ?? 0)
@@ -254,9 +250,7 @@ class ServerTests: XCTestCase {
254250
XCTAssertNotNil(response)
255251
let headers = response?.allHeaderFields ?? ["": ""]
256252
let connectionHeader: String = headers["Connection"] as? String ?? ""
257-
let keepAliveHeader = headers["Keep-Alive"]
258253
XCTAssertEqual(connectionHeader, "Keep-Alive", "No Keep-Alive Connection")
259-
XCTAssertNotNil(keepAliveHeader, "No Keep-Alive Header")
260254
XCTAssertEqual(server.connectionCount, 1)
261255
XCTAssertNotNil(responseBody)
262256
XCTAssertEqual(Int(HTTPResponseStatus.ok.code), response?.statusCode ?? 0)

0 commit comments

Comments
 (0)