|
| 1 | +// |
| 2 | +// BodyParser.swift |
| 3 | +// MacroXmlRpc |
| 4 | +// |
| 5 | +// Created by Helge Hess. |
| 6 | +// Copyright © 2020 ZeeZide GmbH. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import MacroExpress |
| 10 | +import enum XmlRpc.XmlRpc |
| 11 | + |
| 12 | +public extension bodyParser { |
| 13 | + |
| 14 | + enum XmlRpcBodyParserBody { |
| 15 | + case invalid |
| 16 | + case call (XmlRpc.Call) |
| 17 | + case response(XmlRpc.Response) |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * If available, parse an XML-RPC method call into the `request.xmlRpcBody` |
| 22 | + * extra slot. |
| 23 | + * |
| 24 | + * Usage: |
| 25 | + * |
| 26 | + * app.route("/RPC2") |
| 27 | + * .use(bodyParser.xmlRpcCall()) |
| 28 | + * .post("/RPC2") { req, res, next in |
| 29 | + * guard let call = req.xmlRpcCall else { return next() } |
| 30 | + * console.log("received call:", call) |
| 31 | + * } |
| 32 | + * |
| 33 | + * Note: Do not unnecessary call this middleware, i.e. maybe not at the top |
| 34 | + * level, but rather as part of an actual XML-RPC route. |
| 35 | + * |
| 36 | + * This plays well w/ other body parsers. If no other parser was active, |
| 37 | + * it will fill `request.body` as `.text`. |
| 38 | + */ |
| 39 | + @inlinable |
| 40 | + static func xmlRpcCall() -> Middleware { |
| 41 | + return { req, res, next in |
| 42 | + if req.extra[xmlRpcRequestKey] != nil { return next() } // parsed already |
| 43 | + |
| 44 | + func registerCallInLogger() { |
| 45 | + guard let call = req.xmlRpcCall else { return } |
| 46 | + // If we parsed an XML-RPC call, add its method name to the logging |
| 47 | + // meta data. It is important contextual information. |
| 48 | + req.log[metadataKey: "xmlrpc"] = .string(call.methodName) |
| 49 | + } |
| 50 | + |
| 51 | + // This deals w/ other bodyParsers being active. If we already have |
| 52 | + // content (e.g. from bodyParser.text or .raw) we reuse that. |
| 53 | + switch req.body { |
| 54 | + |
| 55 | + case .notParsed: |
| 56 | + guard typeIs(req, [ "text/xml" ]) != nil else { return next() } |
| 57 | + |
| 58 | + // Collect request content using the `concat` stream. |
| 59 | + return concatError(request: req, next: next) { bytes in |
| 60 | + do { |
| 61 | + let string = try bytes.toString() |
| 62 | + req.body = .text(string) |
| 63 | + req.xmlRpcBody = XmlRpc.parseCall(string).flatMap { .call($0) } |
| 64 | + ?? .invalid |
| 65 | + registerCallInLogger() |
| 66 | + return nil |
| 67 | + } |
| 68 | + catch { |
| 69 | + req.body = .error(BodyParserError.couldNotDecodeString(error)) |
| 70 | + req.xmlRpcBody = .invalid |
| 71 | + return error |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + case .noBody, .error: |
| 76 | + req.xmlRpcBody = .invalid |
| 77 | + return next() |
| 78 | + |
| 79 | + case .urlEncoded, .json: // TODO: we could try to map those! |
| 80 | + req.xmlRpcBody = .invalid |
| 81 | + return next() |
| 82 | + |
| 83 | + case .raw(let bytes): |
| 84 | + // TBD: check for text/xml? |
| 85 | + do { |
| 86 | + let string = try bytes.toString() |
| 87 | + req.body = .text(string) |
| 88 | + req.xmlRpcBody = XmlRpc.parseCall(string).flatMap { .call($0) } |
| 89 | + ?? .invalid |
| 90 | + registerCallInLogger() |
| 91 | + } |
| 92 | + catch { |
| 93 | + // In this case, this doesn't have to be an error. Could be some |
| 94 | + // other raw data. |
| 95 | + req.xmlRpcBody = .invalid |
| 96 | + } |
| 97 | + return next() |
| 98 | + |
| 99 | + case .text(let string): |
| 100 | + req.xmlRpcBody = XmlRpc.parseCall(string).flatMap { .call($0) } |
| 101 | + ?? .invalid |
| 102 | + registerCallInLogger() |
| 103 | + return next() |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +@usableFromInline |
| 111 | +let xmlRpcRequestKey = "macro.xmlrpc.body-parser" |
| 112 | + |
| 113 | +public extension IncomingMessage { |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns the XML-RPC body parsed by e.g. `bodyParser.xmlRpcCall`. It is |
| 117 | + * only filled when the middleware executed, otherwise it returns `.invalid`. |
| 118 | + */ |
| 119 | + @inlinable |
| 120 | + var xmlRpcBody: bodyParser.XmlRpcBodyParserBody { |
| 121 | + set { extra[xmlRpcRequestKey] = newValue } |
| 122 | + get { |
| 123 | + return (extra[xmlRpcRequestKey] as? bodyParser.XmlRpcBodyParserBody) |
| 124 | + ?? .invalid |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns the XML-RPC body parsed by e.g. `bodyParser.xmlRpcCall`. It is |
| 130 | + * only filled when the middleware executed and the content was a proper body, |
| 131 | + * otherwise it returns `nil`. |
| 132 | + */ |
| 133 | + @inlinable |
| 134 | + var xmlRpcCall: XmlRpc.Call? { |
| 135 | + guard case .call(let call) = xmlRpcBody else { return nil } |
| 136 | + return call |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +// MARK: - Helper |
| 142 | + |
| 143 | +@usableFromInline |
| 144 | +func concatError(request : IncomingMessage, |
| 145 | + next : @escaping Next, |
| 146 | + handler : @escaping ( Buffer ) -> Swift.Error?) |
| 147 | +{ |
| 148 | + var didCallNext = false |
| 149 | + |
| 150 | + request | concat { bytes in |
| 151 | + guard !didCallNext else { return } |
| 152 | + if let error = handler(bytes) { |
| 153 | + next(error) |
| 154 | + } |
| 155 | + else { |
| 156 | + next() |
| 157 | + } |
| 158 | + } |
| 159 | + .onceError { error in |
| 160 | + guard !didCallNext else { return } |
| 161 | + didCallNext = true |
| 162 | + next(error) |
| 163 | + } |
| 164 | +} |
0 commit comments