Cutelyst  2.13.0
engine.h
1 /*
2  * Copyright (C) 2013-2017 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 Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CUTELYST_ENGINE_H
19 #define CUTELYST_ENGINE_H
20 
21 #include <QObject>
22 #include <QHostAddress>
23 
24 #include <Cutelyst/cutelyst_global.h>
25 #include <Cutelyst/Headers>
26 
27 namespace Cutelyst {
28 
29 class Application;
30 class Context;
31 class EngineRequest;
32 class EnginePrivate;
33 class CUTELYST_LIBRARY Engine : public QObject
34 {
35  Q_OBJECT
36 public:
42  explicit Engine(Application *app, int workerCore, const QVariantMap &opts);
43  virtual ~Engine();
44 
48  Application *app() const;
49 
54  virtual int workerId() const = 0;
55 
59  int workerCore() const;
60 
68  inline bool isZeroWorker() const;
69 
73  QVariantMap opts() const;
74 
80  QVariantMap config(const QString &entity) const;
81 
85  void setConfig(const QVariantMap &config);
86 
90  static QVariantMap loadIniConfig(const QString &filename);
91 
95  static QVariantMap loadJsonConfig(const QString &filename);
96 
102  virtual quint64 time();
103 
111  void processRequest(EngineRequest *request);
112 
116  static inline QString camelCaseHeader(const QString &headerKey) {
117  // The RFC 2616 and 7230 states keys are not case
118  // case sensitive, however several tools fail
119  // if the headers are not on camel case form.
120  QString key = headerKey;
121  bool lastWasLetter = false;
122  for (int i = 0 ; i < key.size() ; ++i) {
123  QCharRef c = key[i];
124  if (c == QLatin1Char('_')) {
125  c = QLatin1Char('-');
126  lastWasLetter = false;
127  } else if (lastWasLetter) {
128  c = c.toLower();
129  } else if (c.isLetter()) {
130  lastWasLetter = true;
131  }
132  }
133  return key;
134  }
135 
139  static inline void camelCaseByteArrayHeader(QByteArray &key) {
140  // The RFC 2616 and 7230 states keys are not case
141  // case sensitive, however several tools fail
142  // if the headers are not on camel case form.
143  bool lastWasLetter = false;
144  for (int i = 0 ; i < key.size() ; ++i) {
145  QByteRef c = key[i];
146  if (c == '_') {
147  c = '-';
148  lastWasLetter = false;
149  } else if (lastWasLetter) {
150  c = QChar::toLower(c);
151  } else if (QChar::isLetter(c)) {
152  lastWasLetter = true;
153  }
154  }
155  }
156 
160  static const char *httpStatusMessage(quint16 status, int *len = nullptr);
161 
162 Q_SIGNALS:
170  void processRequestAsync(Cutelyst::EngineRequest *request);
171 
172 protected:
182  bool initApplication();
183 
196  bool postForkApplication();
197 
201  Headers &defaultHeaders();
202 
203  EnginePrivate *d_ptr;
204 
205 private:
206  Q_DECLARE_PRIVATE(Engine)
207  friend class Application;
208  friend class Response;
209 
214  virtual bool init() = 0;
215 };
216 
217 inline bool Engine::isZeroWorker() const {
218  return !workerId() && !workerCore();
219 }
220 
221 }
222 
223 #endif // CUTELYST_ENGINE_H
Cutelyst::Engine::camelCaseByteArrayHeader
static void camelCaseByteArrayHeader(QByteArray &key)
Definition: engine.h:139
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Engine::workerCore
int workerCore() const
Each worker process migth have a number of worker cores (threads), a single process with two worker t...
Definition: engine.cpp:119
Cutelyst::Engine::isZeroWorker
bool isZeroWorker() const
Definition: engine.h:217
Cutelyst::EngineRequest
Definition: enginerequest.h:31
Cutelyst::Headers
Definition: headers.h:29
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Engine
The Cutelyst Engine.
Definition: engine.h:33
Cutelyst::Engine::camelCaseHeader
static QString camelCaseHeader(const QString &headerKey)
Definition: engine.h:116
Cutelyst::Engine::workerId
virtual int workerId() const =0
Cutelyst::Response
Definition: response.h:34