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  Q_DECL_DEPRECATED_X("Will be removed in new major release")
150  static const char *httpStatusMessage(quint16 status, int *len = nullptr);
151 
152 Q_SIGNALS:
160  void processRequestAsync(Cutelyst::EngineRequest *request);
161 
162 protected:
172  bool initApplication();
173 
186  bool postForkApplication();
187 
191  Headers &defaultHeaders();
192 
193  EnginePrivate *d_ptr;
194 
195 private:
196  Q_DECLARE_PRIVATE(Engine)
197  friend class Application;
198  friend class Response;
199 
204  virtual bool init() = 0;
205 };
206 
207 inline bool Engine::isZeroWorker() const
208 {
209  return !workerId() && !workerCore();
210 }
211 
212 } // namespace Cutelyst
213 
214 #endif // CUTELYST_ENGINE_H
static QString camelCaseHeader(const QString &headerKey)
Definition: engine.h:103
int size() const const
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