Cutelyst  1.5.0
engine.h
1 /*
2  * Copyright (C) 2013-2016 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef CUTELYST_ENGINE_H
21 #define CUTELYST_ENGINE_H
22 
23 #include <QObject>
24 #include <QHostAddress>
25 
26 #include <Cutelyst/cutelyst_global.h>
27 #include <Cutelyst/Headers>
28 
29 namespace Cutelyst {
30 
31 struct EngineRequest {
32  QString method;
33  QString path;
34  QByteArray query;
35  QString protocol;
36  QString serverAddress;
37  QHostAddress remoteAddress;
38  QString remoteUser;
39  Headers headers;
40  quint64 startOfRequest;
41  QIODevice *body;
42  void *requestPtr;
43  quint16 remotePort;
44  bool isSecure;
45 };
46 
47 class Application;
48 class Context;
49 class EnginePrivate;
50 class CUTELYST_LIBRARY Engine : public QObject
51 {
52  Q_OBJECT
53 public:
54  explicit Engine(Application *app, int workerCore, const QVariantMap &opts);
55  virtual ~Engine();
56 
57  Application *app() const;
58 
59  virtual int workerId() const = 0;
60 
61  int workerCore() const;
62 
70  inline bool isZeroWorker() const;
71 
75  QVariantMap opts() const;
76 
82  QVariantMap config(const QString &entity) const;
83 
84  virtual quint64 time();
85 
86 protected:
98  bool initApplication();
99 
112  bool postForkApplication();
113 
117  qint64 write(Context *c, const char *data, qint64 len, void *engineData);
118 
119  virtual qint64 doWrite(Context *c, const char *data, qint64 len, void *engineData) = 0;
120 
126  virtual void finalizeCookies(Context *c);
127 
133  virtual bool finalizeHeaders(Context *c);
134 
135  virtual bool finalizeHeadersWrite(Context *c, quint16 status, const Headers &headers, void *engineData) = 0;
136 
141  virtual void finalizeBody(Context *c);
142 
149  virtual void finalizeError(Context *c);
150 
155  void finalize(Context *c);
156 
160  static inline QString camelCaseHeader(const QString &headerKey) {
161  // The RFC 2616 and 7230 states keys are not case
162  // case sensitive, however several tools fail
163  // if the headers are not on camel case form.
164  QString key = headerKey;
165  bool lastWasDash = true;
166  for (int i = 0 ; i < key.size() ; ++i) {
167  QCharRef c = key[i];
168  if (c == QLatin1Char('_')) {
169  c = QLatin1Char('-');
170  lastWasDash = true;
171  } else if (lastWasDash) {
172  lastWasDash = false;
173  c = c.toUpper();
174  }
175  }
176  return key;
177  }
178 
179  static inline void camelCaseByteArrayHeader(QByteArray &key) {
180  // The RFC 2616 and 7230 states keys are not case
181  // case sensitive, however several tools fail
182  // if the headers are not on camel case form.
183  bool lastWasDash = true;
184  for (int i = 0 ; i < key.size() ; ++i) {
185  QByteRef c = key[i];
186  if (c == '_') {
187  c = '-';
188  lastWasDash = true;
189  } else if (lastWasDash) {
190  lastWasDash = false;
191  c = QChar::toUpper(c);
192  }
193  }
194  }
195 
196  static const char *httpStatusMessage(quint16 status, int *len = nullptr);
197 
201  Headers &defaultHeaders();
202 
203  void processRequest(const EngineRequest &req);
204 
205  Q_DECL_DEPRECATED
206  void processRequest(const QString &method,
207  const QString &path,
208  const QByteArray &query,
209  const QString &protocol,
210  bool https,
211  const QString &serverAddress,
212  const QHostAddress &remoteAddress,
213  quint16 remotePort,
214  const QString &remoteUser,
215  const Headers &headers,
216  quint64 startOfRequest,
217  QIODevice *body,
218  void *requestPtr);
219 
220  EnginePrivate *d_ptr;
221 
222 private:
223  Q_DECLARE_PRIVATE(Engine)
224  friend class Application;
225  friend class Response;
226 
231  virtual bool init() = 0;
232 };
233 
234 inline bool Engine::isZeroWorker() const {
235  return !workerId() && !workerCore();
236 }
237 
238 }
239 
240 #endif // CUTELYST_ENGINE_H
bool isZeroWorker() const
Definition: engine.h:234
static QString camelCaseHeader(const QString &headerKey)
Definition: engine.h:160
The Cutelyst Context.
Definition: context.h:52
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
The Cutelyst Application.
Definition: application.h:55