Fix protocol client not delivering entire response body

This commit is contained in:
Shadowfacts 2020-07-14 22:11:08 -04:00
parent 7da0368758
commit 4c2daa3b54
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 18 additions and 29 deletions

View File

@ -58,10 +58,15 @@ extension AppDelegate: GeminiConnectionDelegate {
print("!! Meta: '\(header.meta)'")
}
if let data = data {
print(String(data: data, encoding: .utf8)!)
print("Received: \(data)")
print(String(data: data, encoding: .utf8)!.debugDescription)
}
}
func connection(_ connection: GeminiConnection, handleError error: GeminiConnection.Error) {
print("!! connection error: \(error)")
}
func connectionCompleted(_ connection: GeminiConnection) {
print("!! completed")
}

View File

@ -11,11 +11,14 @@ import Network
public protocol GeminiConnectionDelegate: class {
func connectionReady(_ connection: GeminiConnection)
func connection(_ connection: GeminiConnection, receivedData data: Data?, header: GeminiResponseHeader)
func connection(_ connection: GeminiConnection, handleError error: GeminiConnection.Error)
func connectionCompleted(_ connection: GeminiConnection)
}
public class GeminiConnection {
public typealias Error = NWError
public weak var delegate: GeminiConnectionDelegate?
private var connection: NWConnection?
@ -63,40 +66,22 @@ public class GeminiConnection {
private func receiveNextMessage() {
guard let connection = connection else { return }
// todo: should this really be .max
connection.receive(minimumIncompleteLength: 1, maximumLength: 65536) { (data, context, isComplete, error) in
// todo: handle error
if let message = context?.protocolMetadata(definition: GeminiProtocol.definition) as? NWProtocolFramer.Message,
connection.receiveMessage { (data, context, isComplete, error) in
if let error = error {
self.delegate?.connection(self, handleError: error)
} else if let message = context?.protocolMetadata(definition: GeminiProtocol.definition) as? NWProtocolFramer.Message,
let header = message.geminiResponseHeader,
let delegate = self.delegate {
delegate.connection(self, receivedData: data, header: header)
self.receiveNextMessage()
}
if isComplete {
self.delegate?.connectionCompleted(self)
self.report.collect(queue: .main) { (report) in
print(report.debugDescription)
}
// connection.cancel()
connection.cancel()
} else {
self.receiveNextMessage()
}
}
// connection.receiveMessage { (data, context, isComplete, error) in
// if let message = context?.protocolMetadata(definition: GeminiProtocol.definition) as? NWProtocolFramer.Message,
// let header = message.geminiResponseHeader,
// let delegate = self.delegate {
//// let response = GeminiResponse(status: header.status, meta: header.meta, body: data)
// delegate.connection(self, receivedData: data, header: header)
// }
// // todo: error handling
// if let error = error {
// print(error)
// } else if isComplete {
// self.delegate?.connectionCompleted(self)
// connection.cancel()
// } else {
// self.receiveNextMessage()
// }
// }
}
}

View File

@ -73,8 +73,7 @@ class GeminiProtocol: NWProtocolFramerImplementation {
let header = GeminiResponseHeader(status: statusCode, meta: meta)
let message = NWProtocolFramer.Message(geminiResponseHeader: header)
if !framer.deliverInputNoCopy(length: 2 + 1 + meta.utf8.count + 2, message: message, isComplete: true) {
// todo: why return zero here?
if !framer.deliverInputNoCopy(length: .max, message: message, isComplete: true) {
return 0
}
}