Skip to content

Commit defd4c7

Browse files
carlbrownseabaylea
authored andcommitted
Push completion block to queueSocketWrite (swift-server#55)
* Push completion block to queueSocketWrite * Fix test assert I had put this fix in the PR for Swift 4 instead of the PR for this when I split them up. Sorry.
1 parent d81bed0 commit defd4c7

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

Sources/HTTP/HTTPStreamingParser.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public class StreamingParser: HTTPResponseWriter {
377377
// FIXME headers are US-ASCII, anything else should be encoded using [RFC5987] some lines above
378378
// TODO use requested encoding if specified
379379
if let data = header.data(using: .utf8) {
380-
self.parserConnector?.queueSocketWrite(data)
380+
self.parserConnector?.queueSocketWrite(data, completion: completion)
381381
if !isContinue {
382382
headersWritten = true
383383
}
@@ -448,14 +448,13 @@ public class StreamingParser: HTTPResponseWriter {
448448
dataToWrite = data.withUnsafeBytes { Data($0) }
449449
}
450450

451-
self.parserConnector?.queueSocketWrite(dataToWrite)
452-
completion(.ok)
451+
self.parserConnector?.queueSocketWrite(dataToWrite, completion: completion)
453452
}
454453

455454
public func done(completion: @escaping (Result) -> Void) {
456455
if isChunked {
457456
let chunkTerminate = "0\r\n\r\n".data(using: .utf8)!
458-
self.parserConnector?.queueSocketWrite(chunkTerminate)
457+
self.parserConnector?.queueSocketWrite(chunkTerminate, completion: completion)
459458
}
460459

461460
self.parsedHTTPMethod = nil
@@ -496,7 +495,7 @@ public class StreamingParser: HTTPResponseWriter {
496495
/// :nodoc:
497496
public protocol ParserConnecting: class {
498497
/// Send data to the network do be written to the client
499-
func queueSocketWrite(_ from: Data)
498+
func queueSocketWrite(_ from: Data, completion: @escaping (Result) -> Void)
500499

501500
/// Let the network know that a response has started to avoid closing a connection during a slow write
502501
func responseBeginning()

Sources/HTTP/PoCSocket/PoCSocketConnectionListener.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,10 @@ public class PoCSocketConnectionListener: ParserConnecting {
266266
/// Called by the parser to give us data to send back out of the socket
267267
///
268268
/// - Parameter bytes: Data object to be queued to be written to the socket
269-
public func queueSocketWrite(_ bytes: Data) {
269+
public func queueSocketWrite(_ bytes: Data, completion:@escaping (Result) -> Void) {
270270
self.socketWriterQueue.async { [weak self] in
271271
self?.write(bytes)
272+
completion(.ok)
272273
}
273274
}
274275

Tests/HTTPTests/ServerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class ServerTests: XCTestCase {
466466
XCTAssertEqual("Hello, World!", String(data: responseBody ?? Data(), encoding: .utf8) ?? "Nil")
467467
XCTAssertEqual(Int(testHandler.chunkCalledCount), 1)
468468
XCTAssertLessThan(testHandler.chunkLength, executableLength, "Should have written less than the length of the file")
469-
XCTAssertEqual(Int(testHandler.chunkLength), chunkSize)
469+
XCTAssertLessThanOrEqual(Int(testHandler.chunkLength), chunkSize)
470470
receivedExpectation.fulfill()
471471
}
472472
uploadTask.resume()

0 commit comments

Comments
 (0)