cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
engine.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2013-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef CUTELYST_ENGINE_H
6 #define CUTELYST_ENGINE_H
7 
8 #include <QObject>
9 #include <QHostAddress>
10 
11 #include <Cutelyst/cutelyst_global.h>
12 #include <Cutelyst/Headers>
13 
14 namespace Cutelyst {
15 
16 class Application;
17 class Context;
18 class EngineRequest;
19 class EnginePrivate;
20 class CUTELYST_LIBRARY Engine : public QObject
21 {
22  Q_OBJECT
23 public:
29  explicit Engine(Application *app, int workerCore, const QVariantMap &opts);
30  virtual ~Engine();
31 
35  Application *app() const;
36 
41  virtual int workerId() const = 0;
42 
46  int workerCore() const;
47 
55  inline bool isZeroWorker() const;
56 
60  QVariantMap opts() const;
61 
67  QVariantMap config(const QString &entity) const;
68 
72  void setConfig(const QVariantMap &config);
73 
77  static QVariantMap loadIniConfig(const QString &filename);
78 
82  static QVariantMap loadJsonConfig(const QString &filename);
83 
89  virtual quint64 time();
90 
98  void processRequest(EngineRequest *request);
99 
103  static inline QString camelCaseHeader(const QString &headerKey) {
104  // The RFC 2616 and 7230 states keys are not case
105  // case sensitive, however several tools fail
106  // if the headers are not on camel case form.
107  QString key = headerKey;
108  bool lastWasLetter = false;
109  for (int i = 0 ; i < key.size() ; ++i) {
110  QChar c = key[i];
111  if (c == u'_') {
112  key[i] = u'-';
113  lastWasLetter = false;
114  } else if (lastWasLetter) {
115  key[i] = c.toLower();
116  } else if (c.isLetter()) {
117  lastWasLetter = true;
118  }
119  }
120  return key;
121  }
122 
126  static inline void camelCaseByteArrayHeader(QByteArray &key) {
127  // The RFC 2616 and 7230 states keys are not case
128  // case sensitive, however several tools fail
129  // if the headers are not on camel case form.
130  bool lastWasLetter = false;
131  for (int i = 0 ; i < key.size() ; ++i) {
132  char c = key[i];
133  if (c == '_') {
134  key[i] = '-';
135  lastWasLetter = false;
136  } else if (lastWasLetter) {
137  key[i] = QChar::toLower(c);
138  } else if (QChar::isLetter(c)) {
139  lastWasLetter = true;
140  }
141  }
142  }
143 
147  static const char *httpStatusMessage(quint16 status, int *len = nullptr);
148 
149 Q_SIGNALS:
158 
159 protected:
169  bool initApplication();
170 
183  bool postForkApplication();
184 
188  Headers &defaultHeaders();
189 
190  EnginePrivate *d_ptr;
191 
192 private:
193  Q_DECLARE_PRIVATE(Engine)
194  friend class Application;
195  friend class Response;
196 
201  virtual bool init() = 0;
202 };
203 
204 inline bool Engine::isZeroWorker() const {
205  return !workerId() && !workerCore();
206 }
207 
208 }
209 
210 #endif // CUTELYST_ENGINE_H
The Cutelyst Application.
Definition: application.h:43
The Cutelyst Engine.
Definition: engine.h:21
static QString camelCaseHeader(const QString &headerKey)
Definition: engine.h:103
static void camelCaseByteArrayHeader(QByteArray &key)
Definition: engine.h:126
int workerCore() const
Each worker process migth have a number of worker cores (threads), a single process with two worker t...
Definition: engine.cpp:91
virtual int workerId() const =0
The id is the number of the spawned engine process, a single process workerId = 0,...
bool isZeroWorker() const
Definition: engine.h:204
void processRequestAsync(Cutelyst::EngineRequest *request)
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8