2020-07-12 23:09:37 -04:00
|
|
|
//
|
|
|
|
// GeminiProtocol.swift
|
|
|
|
// Gemini
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 7/12/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Network
|
|
|
|
|
|
|
|
class GeminiProtocol: NWProtocolFramerImplementation {
|
|
|
|
static let definition = NWProtocolFramer.Definition(implementation: GeminiProtocol.self)
|
|
|
|
|
|
|
|
static let label = "Gemini"
|
|
|
|
|
2020-07-23 09:18:59 -04:00
|
|
|
private var tempStatusCode: GeminiResponseHeader.StatusCode?
|
|
|
|
private var tempMeta: String?
|
2021-06-17 22:58:46 -04:00
|
|
|
private var lastAttemptedMetaLength: Int?
|
|
|
|
private var lastFoundCR = false
|
2020-07-23 09:18:59 -04:00
|
|
|
|
2020-07-12 23:09:37 -04:00
|
|
|
required init(framer: NWProtocolFramer.Instance) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(framer: NWProtocolFramer.Instance) -> NWProtocolFramer.StartResult {
|
|
|
|
return .ready
|
|
|
|
}
|
|
|
|
|
|
|
|
func wakeup(framer: NWProtocolFramer.Instance) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func stop(framer: NWProtocolFramer.Instance) -> Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanup(framer: NWProtocolFramer.Instance) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleInput(framer: NWProtocolFramer.Instance) -> Int {
|
2020-07-23 09:18:59 -04:00
|
|
|
if tempStatusCode == nil {
|
|
|
|
_ = framer.parseInput(minimumIncompleteLength: 3, maximumLength: 3) { (buffer, isComplete) -> Int in
|
2020-07-12 23:09:37 -04:00
|
|
|
guard let buffer = buffer,
|
|
|
|
buffer.count == 3 else { return 0 }
|
2020-07-23 09:18:59 -04:00
|
|
|
self.tempStatusCode = GeminiResponseHeader.StatusCode(buffer)
|
2020-07-12 23:09:37 -04:00
|
|
|
return 3
|
|
|
|
}
|
2020-07-23 09:18:59 -04:00
|
|
|
}
|
|
|
|
guard let statusCode = tempStatusCode else {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
if tempMeta == nil {
|
2021-06-17 22:58:46 -04:00
|
|
|
let min: Int
|
|
|
|
// if we previously tried to get the meta but failed (because the <CR><LF> was not found,
|
|
|
|
// the minimum amount we need before trying to parse is at least 1 or 2 (depending on whether we found the <CR>) bytes more
|
|
|
|
if let lastAttemptedMetaLength = lastAttemptedMetaLength {
|
|
|
|
min = lastAttemptedMetaLength + (lastFoundCR ? 1 : 2)
|
|
|
|
} else {
|
|
|
|
// Minimum length is 2 bytes, spec does not say meta string is required
|
|
|
|
min = 2
|
|
|
|
}
|
|
|
|
_ = framer.parseInput(minimumIncompleteLength: min, maximumLength: 1024 + 2) { (buffer, isComplete) -> Int in
|
2020-07-12 23:09:37 -04:00
|
|
|
guard let buffer = buffer,
|
|
|
|
buffer.count >= 2 else { return 0 }
|
2021-06-17 22:58:46 -04:00
|
|
|
print("got count: \(buffer.count)")
|
|
|
|
self.lastAttemptedMetaLength = buffer.count
|
2020-07-12 23:09:37 -04:00
|
|
|
|
|
|
|
let lastPossibleCRIndex = buffer.index(before: buffer.index(before: buffer.endIndex))
|
|
|
|
var index = buffer.startIndex
|
|
|
|
var found = false
|
|
|
|
while index <= lastPossibleCRIndex {
|
|
|
|
// <CR><LF>
|
|
|
|
if buffer[index] == 13 && buffer[buffer.index(after: index)] == 10 {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
index = buffer.index(after: index)
|
|
|
|
}
|
|
|
|
|
2020-07-23 09:18:59 -04:00
|
|
|
if !found {
|
2021-06-17 22:58:46 -04:00
|
|
|
if buffer[index] == 13 {
|
|
|
|
// if we found <CR>, but not <LF>, save that info so that next time we only wait for 1 more byte instead of 2
|
|
|
|
self.lastFoundCR = true
|
|
|
|
}
|
2020-07-23 09:18:59 -04:00
|
|
|
if buffer.count < 1026 {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
fatalError("Didn't find <CR><LF> in buffer. Meta string was longer than 1024 bytes")
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 23:09:37 -04:00
|
|
|
|
2020-07-23 09:18:59 -04:00
|
|
|
self.tempMeta = String(bytes: buffer[..<index], encoding: .utf8)
|
|
|
|
return buffer.startIndex.distance(to: index) + 2
|
2020-07-12 23:09:37 -04:00
|
|
|
}
|
2020-07-23 09:18:59 -04:00
|
|
|
}
|
|
|
|
guard let meta = tempMeta else {
|
2021-06-17 22:58:46 -04:00
|
|
|
if let attempted = self.lastAttemptedMetaLength {
|
|
|
|
return attempted + (lastFoundCR ? 1 : 2)
|
2020-07-23 09:18:59 -04:00
|
|
|
} else {
|
2020-07-12 23:09:37 -04:00
|
|
|
return 2
|
|
|
|
}
|
2020-07-23 09:18:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let header = GeminiResponseHeader(status: statusCode, meta: meta)
|
|
|
|
|
|
|
|
let message = NWProtocolFramer.Message(geminiResponseHeader: header)
|
2021-06-15 22:24:16 -04:00
|
|
|
// Deliver all the input (the response body) to the client without copying.
|
2020-12-20 22:57:29 -05:00
|
|
|
_ = framer.deliverInputNoCopy(length: statusCode.isSuccess ? .max : 0, message: message, isComplete: true)
|
2021-06-15 22:24:16 -04:00
|
|
|
// Just in case, set the framer to pass-through input so it never invokes this method again.
|
|
|
|
// todo: this should work according to an apple engineer, but the request seems to hang forever on a real device
|
|
|
|
// sometimes works fine when stepping through w/ debugger => race condition?
|
|
|
|
// framer.passThroughInput()
|
2020-12-20 22:57:29 -05:00
|
|
|
return 0
|
2020-07-12 23:09:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleOutput(framer: NWProtocolFramer.Instance, message: NWProtocolFramer.Message, messageLength: Int, isComplete: Bool) {
|
|
|
|
guard let request = message.geminiRequest else { fatalError("GeminiProtocol can't send message that doesn't have an associated GeminiRequest") }
|
|
|
|
framer.writeOutput(data: request.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-14 22:13:09 -04:00
|
|
|
fileprivate extension GeminiResponseHeader.StatusCode {
|
2020-07-12 23:09:37 -04:00
|
|
|
init?(_ buffer: UnsafeMutableRawBufferPointer) {
|
|
|
|
guard let str = String(bytes: buffer[...buffer.index(after: buffer.startIndex)], encoding: .utf8),
|
|
|
|
let value = Int(str, radix: 10) else { return nil }
|
|
|
|
self.init(rawValue: value)
|
|
|
|
}
|
|
|
|
}
|