Files

32 lines
1.4 KiB
Swift

import Foundation
/// Wo der Cockpit-Server lauscht. Er druckt beim Start "Cockpit: http://127.0.0.1:PORT (Modus)"
/// (sicht/start.py) - nur 127.0.0.1 wird angenommen.
public struct ServerAdresse: Equatable, Sendable {
public static let host = "127.0.0.1"
public var port: Int
public init(port: Int) { self.port = port }
public init?(startzeile: String) {
let praefix = "Cockpit: http://\(Self.host):"
guard startzeile.hasPrefix(praefix) else { return nil }
let rest = startzeile.dropFirst(praefix.count).prefix { $0.isNumber }
guard let p = Int(rest), (1...65535).contains(p) else { return nil }
port = p
}
public var http: URL { URL(string: "http://\(Self.host):\(port)")! }
public var websocket: URL { URL(string: "ws://\(Self.host):\(port)/ws")! }
public var steuern: URL { http.appending(path: "api/steuern") }
/// Bild-Adresse aus einer Rueckmeldung - nur /foto/<name>.png wie der Server sie ausliefert.
public func foto(_ pfad: String) -> URL? {
let name = pfad.hasPrefix("/foto/") ? String(pfad.dropFirst("/foto/".count)) : ""
let erlaubt = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-_."))
guard name.hasPrefix("foto-"), name.hasSuffix(".png"), !name.contains(".."),
name.unicodeScalars.allSatisfy(erlaubt.contains) else { return nil }
return http.appending(path: "foto/\(name)")
}
}