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

Commit

Permalink
Push completion block to queueSocketWrite (#55)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
carlbrown authored and seabaylea committed Sep 26, 2017
1 parent d81bed0 commit defd4c7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
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

0 comments on commit defd4c7

Please sign in to comment.