1 /*
2  * Copyright (c) 2017 Nathan Osman
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #include <QGenericArgument>
24 #include <QMetaMethod>
25 
26 #include <qhttpengine/qobjecthandler.h>
27 #include <qhttpengine/socket.h>
28 
29 #include "qobjecthandler_p.h"
30 
31 using namespace QHttpEngine;
32 
QObjectHandlerPrivate(QObjectHandler * handler)33 QObjectHandlerPrivate::QObjectHandlerPrivate(QObjectHandler *handler)
34     : QObject(handler),
35       q(handler)
36 {
37 }
38 
QObjectHandler(QObject * parent)39 QObjectHandler::QObjectHandler(QObject *parent)
40     : Handler(parent),
41       d(new QObjectHandlerPrivate(this))
42 {
43 }
44 
invokeSlot(Socket * socket,Method m)45 void QObjectHandlerPrivate::invokeSlot(Socket *socket, Method m)
46 {
47     // Invoke the slot
48     if (m.oldSlot) {
49 
50         // Obtain the slot index
51         int index = m.receiver->metaObject()->indexOfSlot(m.slot.method + 1);
52         if (index == -1) {
53             socket->writeError(Socket::InternalServerError);
54             return;
55         }
56 
57         QMetaMethod method = m.receiver->metaObject()->method(index);
58 
59         // Ensure the parameter is correct
60         QList<QByteArray> params = method.parameterTypes();
61         if (params.count() != 1 || params.at(0) != "QHttpEngine::Socket*") {
62             socket->writeError(Socket::InternalServerError);
63             return;
64         }
65 
66         // Invoke the method
67         if (!m.receiver->metaObject()->method(index).invoke(
68                     m.receiver, Q_ARG(Socket*, socket))) {
69             socket->writeError(Socket::InternalServerError);
70             return;
71         }
72     } else {
73         void *args[] = {
74             Q_NULLPTR,
75             &socket
76         };
77         m.slot.slotObj->call(m.receiver, args);
78     }
79 }
80 
process(Socket * socket,const QString & path)81 void QObjectHandler::process(Socket *socket, const QString &path)
82 {
83     // Ensure the method has been registered
84     if (!d->map.contains(path)) {
85         socket->writeError(Socket::NotFound);
86         return;
87     }
88 
89     QObjectHandlerPrivate::Method m = d->map.value(path);
90 
91     // If the slot requires all data to be received, check to see if this is
92     // already the case, otherwise, wait until the rest of it arrives
93     if (!m.readAll || socket->bytesAvailable() >= socket->contentLength()) {
94         d->invokeSlot(socket, m);
95     } else {
96         connect(socket, &Socket::readChannelFinished, [this, socket, m]() {
97             d->invokeSlot(socket, m);
98         });
99     }
100 }
101 
registerMethod(const QString & name,QObject * receiver,const char * method,bool readAll)102 void QObjectHandler::registerMethod(const QString &name, QObject *receiver, const char *method, bool readAll)
103 {
104     d->map.insert(name, QObjectHandlerPrivate::Method(receiver, method, readAll));
105 }
106 
registerMethodImpl(const QString & name,QObject * receiver,QtPrivate::QSlotObjectBase * slotObj,bool readAll)107 void QObjectHandler::registerMethodImpl(const QString &name, QObject *receiver, QtPrivate::QSlotObjectBase *slotObj, bool readAll)
108 {
109     d->map.insert(name, QObjectHandlerPrivate::Method(receiver, slotObj, readAll));
110 }
111