Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Push completion block to queueSocketWrite #55

Merged
merged 2 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Sources/HTTP/HTTPStreamingParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public class StreamingParser: HTTPResponseWriter {
// FIXME headers are US-ASCII, anything else should be encoded using [RFC5987] some lines above
// TODO use requested encoding if specified
if let data = header.data(using: .utf8) {
self.parserConnector?.queueSocketWrite(data)
self.parserConnector?.queueSocketWrite(data, completion: completion)
if !isContinue {
headersWritten = true
}
Expand Down Expand Up @@ -448,14 +448,13 @@ public class StreamingParser: HTTPResponseWriter {
dataToWrite = data.withUnsafeBytes { Data($0) }
}

self.parserConnector?.queueSocketWrite(dataToWrite)
completion(.ok)
self.parserConnector?.queueSocketWrite(dataToWrite, completion: completion)
}

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

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

/// Let the network know that a response has started to avoid closing a connection during a slow write
func responseBeginning()
Expand Down
3 changes: 2 additions & 1 deletion Sources/HTTP/PoCSocket/PoCSocketConnectionListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,10 @@ public class PoCSocketConnectionListener: ParserConnecting {
/// Called by the parser to give us data to send back out of the socket
///
/// - Parameter bytes: Data object to be queued to be written to the socket
public func queueSocketWrite(_ bytes: Data) {
public func queueSocketWrite(_ bytes: Data, completion:@escaping (Result) -> Void) {
self.socketWriterQueue.async { [weak self] in
self?.write(bytes)
completion(.ok)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/HTTPTests/ServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ class ServerTests: XCTestCase {
XCTAssertEqual("Hello, World!", String(data: responseBody ?? Data(), encoding: .utf8) ?? "Nil")
XCTAssertEqual(Int(testHandler.chunkCalledCount), 1)
XCTAssertLessThan(testHandler.chunkLength, executableLength, "Should have written less than the length of the file")
XCTAssertEqual(Int(testHandler.chunkLength), chunkSize)
XCTAssertLessThanOrEqual(Int(testHandler.chunkLength), chunkSize)
receivedExpectation.fulfill()
}
uploadTask.resume()
Expand Down