cutelyst  3.9.1
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 <Cutelyst/Headers>
9 #include <Cutelyst/cutelyst_global.h>
10 
11 #include <QHostAddress>
12 #include <QObject>
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  {
105  // The RFC 2616 and 7230 states keys are not case
106  // case sensitive, however several tools fail
107  // if the headers are not on camel case form.
108  QString key = headerKey;
109  bool lastWasLetter = false;
110  for (int i = 0; i < key.size(); ++i) {
111  QChar c = key[i];
112  if (c == u'_') {
113  key[i] = u'-';
114  lastWasLetter = false;
115  } else if (lastWasLetter) {
116  key[i] = c.toLower();
117  } else if (c.isLetter()) {
118  lastWasLetter = true;
119  }
120  }
121  return key;
122  }
123 
127  static inline void camelCaseByteArrayHeader(QByteArray &key)
128  {
129  // The RFC 2616 and 7230 states keys are not case
130  // case sensitive, however several tools fail
131  // if the headers are not on camel case form.
132  bool lastWasLetter = false;
133  for (int i = 0; i < key.size(); ++i) {
134  char c = key[i];
135  if (c == '_') {
136  key[i] = '-';
137  lastWasLetter = false;
138  } else if (lastWasLetter) {
139  key[i] = QChar::toLower(c);
140  } else if (QChar::isLetter(c)) {
141  lastWasLetter = true;
142  }
143  }
144  }
145 
149  static const char *httpStatusMessage(quint16 status, int *len = nullptr);
150 
151 Q_SIGNALS:
159  void processRequestAsync(Cutelyst::EngineRequest *request);
160 
161 protected:
171  bool initApplication();
172 
185  bool postForkApplication();
186 
190  Headers &defaultHeaders();
191 
192  EnginePrivate *d_ptr;
193 
194 private:
195  Q_DECLARE_PRIVATE(Engine)
196  friend class Application;
197  friend class Response;
198 
203  virtual bool init() = 0;
204 };
205 
206 inline bool Engine::isZeroWorker() const
207 {
208  return !workerId() && !workerCore();
209 }
210 
211 } // namespace Cutelyst
212 
213 #endif // CUTELYST_ENGINE_H
static QString camelCaseHeader(const QString &headerKey)
Definition: engine.h:103
int size() const const
virtual int workerId() const =0
The id is the number of the spawned engine process, a single process workerId = 0, two process 0 for the first 1 for the second.
bool isLetter() const const
static void camelCaseByteArrayHeader(QByteArray &key)
Definition: engine.h:127
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
QChar toLower() const const
The Cutelyst Application.
Definition: application.h:42
int size() const const
The Cutelyst Engine
Definition: engine.h:20
int workerCore() const
Each worker process migth have a number of worker cores (threads), a single process with two worker ...
Definition: engine.cpp:89
bool isZeroWorker() const
Definition: engine.h:206