Debian Codegraph @

deps/libmaia/maiaXmlRpcServerConnection.cpp

raw symbols SCIP
1 /*
2 * libMaia - maiaXmlRpcServerConnection.cpp
3 * Copyright (c) 2007 Sebastian Wiedenroth <[email protected]>
4 * and Karl Glatz
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "maiaXmlRpcServerConnection.h"
29 #include "maiaXmlRpcServer.h"
30
31 MaiaXmlRpcServerConnection::MaiaXmlRpcServerConnection(QTcpSocket *connection, QObject* parent) : QObject(parent) {
32 header = NULL;
33 clientConnection = connection;
34 connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readFromSocket()));
35 connect(clientConnection, SIGNAL(disconnected()), this, SLOT(deleteLater()));
36 }
37
38 MaiaXmlRpcServerConnection::~MaiaXmlRpcServerConnection() {
39 clientConnection->deleteLater();
40 delete header;
41 }
42
43 void MaiaXmlRpcServerConnection::readFromSocket() {
44 QString lastLine;
45
46 while(clientConnection->canReadLine() && !header) {
47 lastLine = clientConnection->readLine();
48 headerString += lastLine;
49 if(lastLine == "\r\n") { /* http header end */
50 header = new QHttpRequestHeader(headerString);
51 if(!header->isValid()) {
52 /* return http error */
53 qDebug() << "Invalid Header";
54 return;
55 } else if(header->method() != "POST") {
56 /* return http error */
57 qDebug() << "No Post!";
58 return;
59 } else if(!header->contentLength()) {
60 /* return fault */
61 qDebug() << "No Content Length";
62 return;
63 }
64 }
65 }
66
67 if(header) {
68 if(header->contentLength() <= clientConnection->bytesAvailable()) {
69 /* all data complete */
70 parseCall(clientConnection->readAll());
71 }
72 }
73 }
74
75 void MaiaXmlRpcServerConnection::sendResponse(QString content) {
76 QHttpResponseHeader header(200, "Ok");
77 QByteArray block;
78 header.setValue("Server", "MaiaXmlRpc/0.1");
79 header.setValue("Content-Type", "text/xml");
80 header.setValue("Connection","close");
81 block.append(header.toString().toUtf8());
82 block.append(content.toUtf8());
83 clientConnection->write(block);
84 clientConnection->disconnectFromHost();
85 }
86
87 void MaiaXmlRpcServerConnection::parseCall(QString call) {
88 QDomDocument doc;
89 QList<QVariant> args;
90 QVariant ret;
91 QString response;
92 QObject *responseObject;
93 const char *responseSlot;
94
95 if(!doc.setContent(call)) { /* recieved invalid xml */
96 MaiaFault fault(-32700, "parse error: not well formed");
97 sendResponse(fault.toString());
98 return;
99 }
100
101 QDomElement methodNameElement = doc.documentElement().firstChildElement("methodName");
102 QDomElement params = doc.documentElement().firstChildElement("params");
103 if(methodNameElement.isNull()) { /* invalid call */
104 MaiaFault fault(-32600, "server error: invalid xml-rpc. not conforming to spec");
105 sendResponse(fault.toString());
106 return;
107 }
108
109 QString methodName = methodNameElement.text();
110
111 emit getMethod(methodName, &responseObject, &responseSlot);
112 if(!responseObject) { /* unknown method */
113 MaiaFault fault(-32601, "server error: requested method not found");
114 sendResponse(fault.toString());
115 return;
116 }
117
118 QDomNode paramNode = params.firstChild();
119 while(!paramNode.isNull()) {
120 args << MaiaObject::fromXml( paramNode.firstChild().toElement());
121 paramNode = paramNode.nextSibling();
122 }
123
124
125 if(!invokeMethodWithVariants(responseObject, responseSlot, args, &ret)) { /* error invoking... */
126 MaiaFault fault(-32602, "server error: invalid method parameters");
127 sendResponse(fault.toString());
128 return;
129 }
130
131
132 if(ret.canConvert<MaiaFault>()) {
133 response = ret.value<MaiaFault>().toString();
134 } else {
135 response = MaiaObject::prepareResponse(ret);
136 }
137
138 sendResponse(response);
139 }
140
141
142 /* taken from http://delta.affinix.com/2006/08/14/invokemethodwithvariants/
143 thanks to Justin Karneges once again :) */
144 bool MaiaXmlRpcServerConnection::invokeMethodWithVariants(QObject *obj,
145 const QByteArray &method, const QVariantList &args,
146 QVariant *ret, Qt::ConnectionType type) {
147
148 // QMetaObject::invokeMethod() has a 10 argument maximum
149 if(args.count() > 10)
150 return false;
151
152 QList<QByteArray> argTypes;
153 for(int n = 0; n < args.count(); ++n)
154 argTypes += args[n].typeName();
155
156 // get return type
157 int metatype = 0;
158 QByteArray retTypeName = getReturnType(obj->metaObject(), method, argTypes);
159 if(!retTypeName.isEmpty() && retTypeName != "QVariant") {
160 metatype = QMetaType::type(retTypeName.data());
161 if(metatype == 0) // lookup failed
162 return false;
163 }
164
165 QGenericArgument arg[10];
166 for(int n = 0; n < args.count(); ++n)
167 arg[n] = QGenericArgument(args[n].typeName(), args[n].constData());
168
169 QGenericReturnArgument retarg;
170 QVariant retval;
171 if(metatype != 0) {
172 retval = QVariant(metatype, (const void *)0);
173 retarg = QGenericReturnArgument(retval.typeName(), retval.data());
174 } else { /* QVariant */
175 retarg = QGenericReturnArgument("QVariant", &retval);
176 }
177
178 if(retTypeName.isEmpty()) { /* void */
179 if(!QMetaObject::invokeMethod(obj, method.data(), type,
180 arg[0], arg[1], arg[2], arg[3], arg[4],
181 arg[5], arg[6], arg[7], arg[8], arg[9]))
182 return false;
183 } else {
184 if(!QMetaObject::invokeMethod(obj, method.data(), type, retarg,
185 arg[0], arg[1], arg[2], arg[3], arg[4],
186 arg[5], arg[6], arg[7], arg[8], arg[9]))
187 return false;
188 }
189
190 if(retval.isValid() && ret)
191 *ret = retval;
192 return true;
193 }
194
195 QByteArray MaiaXmlRpcServerConnection::getReturnType(const QMetaObject *obj,
196 const QByteArray &method, const QList<QByteArray> argTypes) {
197 for(int n = 0; n < obj->methodCount(); ++n) {
198 QMetaMethod m = obj->method(n);
199 #if QT_VERSION >= 0x050000
200 QByteArray sig = m.methodSignature();
201 #else
202 QByteArray sig = m.signature();
203 #endif
204 int offset = sig.indexOf('(');
205 if(offset == -1)
206 continue;
207 QByteArray name = sig.mid(0, offset);
208 if(name != method)
209 continue;
210 if(m.parameterTypes() != argTypes)
211 continue;
212
213 return m.typeName();
214 }
215 return QByteArray();
216 }
217
218 /*
219 simple Qt4 class emulater
220 */
221
222 #if QT_VERSION >= 0x050000
223 QHttpRequestHeader::QHttpRequestHeader(QString headerString)
224 {
225 this->mHeaderString = headerString;
226
227 QStringList hdrs = headerString.split("\r\n");
228 QStringList hdrkv;
229 for (int i = 0; i < hdrs.size(); i++) {
230 if (hdrs.at(i).trimmed().isEmpty()) break;
231 if (i == 0) {
232 hdrkv = hdrs.at(i).split(" ");
233 this->mMethod = hdrkv.at(0);
234 } else {
235 hdrkv = hdrs.at(i).split(":");
236 this->mHeaders[hdrkv.at(0)] = hdrkv.at(1).trimmed();
237 }
238 }
239 }
240
241 bool QHttpRequestHeader::isValid()
242 {
243 if (this->mHeaderString.isEmpty()) return false;
244 if (this->mMethod != "GET" && this->mMethod != "POST") return false;
245 if (this->mHeaders.size() < 2) return false;
246 return true;
247 }
248
249 QString QHttpRequestHeader::method()
250 {
251 return this->mMethod;
252 }
253
254 uint QHttpRequestHeader::contentLength() const
255 {
256 uint clen = 0;
257
258 clen = this->mHeaders.value("Content-Length").toUInt();
259
260 return clen;
261 }
262
263 QHttpResponseHeader::QHttpResponseHeader(int code, QString text)
264 {
265 this->mCode = code;
266 this->mText = text;
267 }
268
269 void QHttpResponseHeader::setValue(const QString &key, const QString &value)
270 {
271 this->mHeaders[key] = value;
272 }
273
274 QString QHttpResponseHeader::toString() const
275 {
276 QMapIterator<QString, QString> it(this->mHeaders);
277 QString hdrstr;
278
279 hdrstr += QString("HTTP/1.1 %1 %2\r\n").arg(this->mCode).arg(this->mText);
280 while (it.hasNext()) {
281 it.next();
282 hdrstr += it.key() + ": " + it.value() + "\r\n";
283 }
284 hdrstr += "\r\n";
285
286 return hdrstr;
287 }
288
289 #endif