|
6 | 6 | // Copyright © 2020 ZeeZide GmbH. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
9 | | -public extension RouteKeeper { |
10 | | - |
11 | | - @inlinable |
12 | | - @discardableResult |
13 | | - func systemListMethods() -> Self { |
14 | | - post { req, res, next in |
15 | | - guard let call = XmlRpc.parseCall(req.body.text ?? ""), |
16 | | - call.methodName == "system.listMethods" else { |
17 | | - return next() |
| 9 | +import protocol XmlRpc.XmlRpcValueRepresentable |
| 10 | + |
| 11 | +public extension xmlrpc { |
| 12 | + |
| 13 | + /** |
| 14 | + * Provide method reflection information, as described in: |
| 15 | + * |
| 16 | + * http://xmlrpc-c.sourceforge.net/introspection.html |
| 17 | + * |
| 18 | + * This middleware needs to be hooked up to the END, after all XML-RPC |
| 19 | + * functions have been registered in a route. |
| 20 | + * |
| 21 | + * Example: |
| 22 | + * |
| 23 | + * app.route("/RPC2") |
| 24 | + * .use(bodyParser.xmlRpcCall()) |
| 25 | + * .rpc("ping") { _ in "pong" } |
| 26 | + * .rpc("add") { ( a: Int, b: Int ) in a + b } |
| 27 | + * .use(xmlrpc.introspection()) |
| 28 | + * |
| 29 | + * Specifically hosts implementations for those XML-RPC methods: |
| 30 | + * - system.listMethods |
| 31 | + * - system.methodExists |
| 32 | + * - system.methodSignature |
| 33 | + * - system.methodHelp |
| 34 | + * - getCapabilities |
| 35 | + * |
| 36 | + * Note: Careful w/ security implications! |
| 37 | + */ |
| 38 | + static func introspection() -> Middleware { |
| 39 | + return { req, res, next in |
| 40 | + guard let call = req.xmlRpcCall else { return next() } |
| 41 | + |
| 42 | + var missingNameResponse : XmlRpc.Response { |
| 43 | + req.log.error("XML-RPC introspection call was missing a name!") |
| 44 | + return XmlRpc.Response.fault( |
| 45 | + .init(code: 400, reason: "Missing method name parameter!")) |
| 46 | + } |
| 47 | + func unknownMethodResponse(_ name: String) -> XmlRpc.Response { |
| 48 | + req.log.warn( |
| 49 | + "XML-RPC introspection was asking for an unknown name \(name)!") |
| 50 | + return XmlRpc.Response.fault( |
| 51 | + .init(code: 404, reason: "Unknown method '\(name)'.")) |
| 52 | + } |
| 53 | + |
| 54 | + switch call.methodName { |
| 55 | + |
| 56 | + case "system.listMethods": |
| 57 | + xmlRpcIntrospectionMethods.forEach { req.addKnownXmlRpcMethod($0) } |
| 58 | + return res.send(XmlRpc.Response(req.knownXmlRpcMethodNames).xmlString) |
| 59 | + |
| 60 | + case "system.methodSignature": |
| 61 | + guard let methodName = call.parameters.first?.stringValue else { |
| 62 | + return res.send(missingNameResponse.xmlString) |
| 63 | + } |
| 64 | + |
| 65 | + switch methodName { |
| 66 | + case "system.listMethods", "getCapabilities": |
| 67 | + req.addSignature([], for: methodName) |
| 68 | + case "system.methodSignature", "system.methodHelp", |
| 69 | + "system.methodExist": |
| 70 | + req.addSignature([ .string ], for: methodName) |
| 71 | + default: break |
| 72 | + } |
| 73 | + |
| 74 | + if let signatures = req.signatures[methodName] { |
| 75 | + return res.send(XmlRpc.Response(signatures).xmlString) |
| 76 | + } |
| 77 | + else if req.doesMethodExist(methodName) { |
| 78 | + // Forbidden by standard, if we say we do introspection, we should. |
| 79 | + // :-) |
| 80 | + return res.sendStatus(500) |
| 81 | + } |
| 82 | + else { |
| 83 | + return res.send(unknownMethodResponse(methodName).xmlString) |
| 84 | + } |
| 85 | + |
| 86 | + case "system.methodHelp": |
| 87 | + guard let methodName = call.parameters.first?.stringValue else { |
| 88 | + return res.send(missingNameResponse.xmlString) |
| 89 | + } |
| 90 | + |
| 91 | + if let help = req.helps[methodName] { |
| 92 | + return res.send(XmlRpc.Response(help).xmlString) |
| 93 | + } |
| 94 | + else if let signatures = req.signatures[methodName], |
| 95 | + !signatures.isEmpty |
| 96 | + { |
| 97 | + let help = generateHelpForMethod(methodName, signatures: signatures) |
| 98 | + return res.send(XmlRpc.Response(help).xmlString) |
| 99 | + } |
| 100 | + else if req.doesMethodExist(methodName) { |
| 101 | + let help = "The method '\(methodName)' exists, " |
| 102 | + + "but no documentation is available." |
| 103 | + return res.send(XmlRpc.Response(help).xmlString) |
| 104 | + } |
| 105 | + else { |
| 106 | + return res.send(unknownMethodResponse(methodName).xmlString) |
| 107 | + } |
| 108 | + |
| 109 | + case "system.methodExist": |
| 110 | + guard let methodName = call.parameters.first?.stringValue else { |
| 111 | + return res.send(missingNameResponse.xmlString) |
| 112 | + } |
| 113 | + return res.send(XmlRpc.Response( |
| 114 | + req.doesMethodExist(methodName)).xmlString) |
| 115 | + |
| 116 | + case "getCapabilities": |
| 117 | + // http://xmlrpc-c.sourceforge.net/doc/libxmlrpc_server.html#system.getCapabilities |
| 118 | + // TBD: we could collect more capabilities |
| 119 | + return res.send(XmlRpc.Response([ |
| 120 | + "introspection": [ |
| 121 | + "specURL" : |
| 122 | + "http://xmlrpc-c.sourceforge.net/xmlrpc-c/introspection.html", |
| 123 | + "specVersion" : 1 |
| 124 | + ] |
| 125 | + ]).xmlString) |
| 126 | + |
| 127 | + default: |
| 128 | + req.log.warn("unprocessed XML-RPC request after introspection:", |
| 129 | + call.methodName) |
| 130 | + next() |
18 | 131 | } |
19 | | - res.send(XmlRpc.Response(req.knownXmlRpcMethodNames).xmlString) |
20 | 132 | } |
21 | 133 | } |
22 | 134 | } |
23 | 135 |
|
| 136 | +@usableFromInline |
| 137 | +let xmlRpcIntrospectionMethods : Set<String> = [ |
| 138 | + "system.listMethods", "system.methodSignature", "system.methodHelp", |
| 139 | + "system.methodExist", "getCapabilities" |
| 140 | +] |
| 141 | + |
| 142 | +fileprivate extension IncomingMessage { |
| 143 | + |
| 144 | + func doesMethodExist(_ methodName: String) -> Bool { |
| 145 | + let exists = xmlRpcIntrospectionMethods .contains(methodName) |
| 146 | + || self.knownXmlRpcMethodNames.contains(methodName) |
| 147 | + return exists |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +@usableFromInline |
| 152 | +let xmlRpcMethodsRequestKey = "macro.xmlrpc.method-names" |
| 153 | +@usableFromInline |
| 154 | +let xmlRpcHelpsRequestKey = "macro.xmlrpc.method-helps" |
| 155 | +@usableFromInline |
| 156 | +let xmlRpcMethodSignaturesRequestKey = "macro.xmlrpc.method-signatures" |
| 157 | + |
24 | 158 | extension IncomingMessage { |
25 | 159 |
|
26 | | - @usableFromInline |
27 | | - var knownXmlRpcMethodNames : [ String ] { |
28 | | - return (extra["rpc.methods"] as? [ String ]) ?? [] |
| 160 | + fileprivate var knownXmlRpcMethodNames : Set<String> { |
| 161 | + return (extra[xmlRpcMethodsRequestKey] as? Set<String>) ?? [] |
29 | 162 | } |
30 | 163 |
|
31 | 164 | @usableFromInline |
32 | 165 | func addKnownXmlRpcMethod(_ methodName: String) { |
33 | | - if var methods = extra.removeValue(forKey: "rpc.methods") as? [ String ] { |
34 | | - methods.append(methodName) |
35 | | - extra["rpc.methods"] = methods |
| 166 | + if var methods = extra.removeValue(forKey: xmlRpcMethodsRequestKey) |
| 167 | + as? Set<String> |
| 168 | + { |
| 169 | + methods.insert(methodName) |
| 170 | + extra[xmlRpcMethodsRequestKey] = methods |
36 | 171 | } |
37 | 172 | else { |
38 | | - extra["rpc.methods"] = [ methodName ] |
| 173 | + extra[xmlRpcMethodsRequestKey] = Set([ methodName ]) |
39 | 174 | } |
40 | 175 | } |
| 176 | + |
| 177 | + @usableFromInline |
| 178 | + func addHelp(_ help: String, for method: String) { |
| 179 | + var helps = extra.removeValue(forKey: xmlRpcHelpsRequestKey) |
| 180 | + as? [ String : String ] |
| 181 | + ?? [:] |
| 182 | + helps[method] = help |
| 183 | + extra[xmlRpcHelpsRequestKey] = helps |
| 184 | + } |
| 185 | + |
| 186 | + @usableFromInline |
| 187 | + func addSignature(_ signature: [ XmlRpc.Value.ValueType ], |
| 188 | + for method: String) |
| 189 | + { |
| 190 | + var values = extra.removeValue(forKey: xmlRpcMethodSignaturesRequestKey) |
| 191 | + as? [ String : [ [ XmlRpc.Value.ValueType ] ] ] |
| 192 | + ?? [:] |
| 193 | + values[method, default: []].append(signature) |
| 194 | + extra[xmlRpcMethodSignaturesRequestKey] = values |
| 195 | + } |
| 196 | + |
| 197 | + fileprivate var helps : [ String : String ] { |
| 198 | + return (extra[xmlRpcHelpsRequestKey] as? [ String : String ]) ?? [:] |
| 199 | + } |
| 200 | + fileprivate var signatures : [ String : [ [ XmlRpc.Value.ValueType ] ] ] { |
| 201 | + return (extra[xmlRpcMethodSignaturesRequestKey] |
| 202 | + as? [ String : [ [ XmlRpc.Value.ValueType ] ] ]) |
| 203 | + ?? [:] |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +fileprivate |
| 208 | +func generateHelpForMethod(_ methodName: String, |
| 209 | + signatures: [ [ XmlRpc.Value.ValueType ] ]) |
| 210 | + -> String |
| 211 | +{ |
| 212 | + if signatures.isEmpty { |
| 213 | + return |
| 214 | + "The method '\(methodName)' exists, but no documentation is available." |
| 215 | + } |
| 216 | + |
| 217 | + var ms = |
| 218 | + "The method '\(methodName)' can be called with the following signatures:" |
| 219 | + ms += "\n\n" |
| 220 | + for signature in signatures { |
| 221 | + ms += " \(methodName)(" |
| 222 | + ms += signature.map { $0.xmlRpcValue.stringValue }.joined(separator: ", ") |
| 223 | + ms += ")" |
| 224 | + } |
| 225 | + return ms |
41 | 226 | } |
0 commit comments