QHttpEngine  1.0.0
Simple and secure HTTP server for Qt applications
qobjecthandler.h
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 #ifndef QHTTPENGINE_QOBJECTHANDLER_H
24 #define QHTTPENGINE_QOBJECTHANDLER_H
25 
26 #include <qhttpengine/handler.h>
27 
28 #include "qhttpengine_export.h"
29 
30 namespace QHttpEngine
31 {
32 
33 class Socket;
34 
35 class QHTTPENGINE_EXPORT QObjectHandlerPrivate;
36 
76 class QHTTPENGINE_EXPORT QObjectHandler : public Handler
77 {
78  Q_OBJECT
79 
80 public:
81 
85  explicit QObjectHandler(QObject *parent = 0);
86 
95  void registerMethod(const QString &name, QObject *receiver, const char *method, bool readAll = true);
96 
97 #ifdef DOXYGEN
98 
103  void registerMethod(const QString &name, QObject *receiver, PointerToMemberFunction method, bool readAll = true);
104 
110  void registerMethod(const QString &name, Functor functor, bool readAll = true);
111 
117  void registerMethod(const QString &name, QObject *receiver, Functor functor, bool readAll = true);
118 #else
119  template <typename Func1>
120  inline void registerMethod(const QString &name,
121  typename QtPrivate::FunctionPointer<Func1>::Object *receiver,
122  Func1 slot,
123  bool readAll = true) {
124 
125  typedef QtPrivate::FunctionPointer<Func1> SlotType;
126 
127  // Ensure the slot doesn't have too many arguments
128  Q_STATIC_ASSERT_X(int(SlotType::ArgumentCount) == 1,
129  "The slot must have exactly one argument.");
130 
131  // Ensure the argument is of the correct type
132  Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<Socket*, typename QtPrivate::List_Select<typename SlotType::Arguments, 0>::Value>::value),
133  "The slot parameters do not match");
134 
135  // Invoke the implementation
136  registerMethodImpl(name, receiver, new QtPrivate::QSlotObject<Func1, typename SlotType::Arguments, void>(slot), readAll);
137  }
138 
139  template <typename Func1>
140  inline typename QtPrivate::QEnableIf<!QtPrivate::AreArgumentsCompatible<Func1, QObject*>::value, void>::Type
141  registerMethod(const QString &name, Func1 slot, bool readAll = true) {
142  registerMethod(name, Q_NULLPTR, slot, readAll);
143  }
144 
145  template <typename Func1>
146  inline typename QtPrivate::QEnableIf<!QtPrivate::FunctionPointer<Func1>::IsPointerToMemberFunction &&
147 #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
148  !std::is_same<const char*, Func1>::value,
149 #else
150  !QtPrivate::is_same<const char*, Func1>::value,
151 #endif
152  void>::Type
153  registerMethod(const QString &name, QObject *context, Func1 slot, bool readAll = true) {
154 
155  // There is an easier way to do this but then the header wouldn't
156  // compile on non-C++11 compilers
157  return registerMethod_functor(name, context, slot, &Func1::operator(), readAll);
158  }
159 #endif
160 
161 protected:
162 
166  virtual void process(Socket *socket, const QString &path);
167 
168 private:
169 
170  template <typename Func1, typename Func1Operator>
171  inline void registerMethod_functor(const QString &name, QObject *context, Func1 slot, Func1Operator, bool readAll) {
172 
173  typedef QtPrivate::FunctionPointer<Func1Operator> SlotType;
174 
175  // Ensure the slot doesn't have too many arguments
176  Q_STATIC_ASSERT_X(int(SlotType::ArgumentCount) == 1,
177  "The slot must have exactly one argument.");
178 
179  // Ensure the argument is of the correct type
180  Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<Socket*, typename QtPrivate::List_Select<typename SlotType::Arguments, 0>::Value>::value),
181  "The slot parameters do not match");
182 
183  registerMethodImpl(name, context,
184  new QtPrivate::QFunctorSlotObject<Func1, 1, typename SlotType::Arguments, void>(slot),
185  readAll);
186  }
187 
188  void registerMethodImpl(const QString &name, QObject *receiver, QtPrivate::QSlotObjectBase *slotObj, bool readAll);
189 
190  QObjectHandlerPrivate *const d;
191  friend class QObjectHandlerPrivate;
192 };
193 
194 }
195 
196 #endif // QHTTPENGINE_QOBJECTHANDLER_H
Handler for invoking slots
Definition: qobjecthandler.h:76
Base class for HTTP handlers.
Definition: handler.h:77
Definition: basicauthmiddleware.h:30
Implementation of the HTTP protocol.
Definition: socket.h:96