Cutelyst  1.11.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Friends | List of all members
Cutelyst::Engine Class Referenceabstract

The Cutelyst Engine. More...

Inheritance diagram for Cutelyst::Engine:
Inheritance graph
[legend]

Public Member Functions

 Engine (Application *app, int workerCore, const QVariantMap &opts)
 
Applicationapp () const
 application More...
 
QVariantMap config (const QString &entity) const
 user configuration for the application More...
 
bool isZeroWorker () const
 
QVariantMap opts () const
 
ContextprocessRequest (EngineRequest *request)
 
void setConfig (const QVariantMap &config)
 
virtual quint64 time ()
 
int workerCore () const
 Each worker process migth have a number of worker cores (threads), a single process with two worker threads will return 0 and 1 for each of the thread respectively. More...
 
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. More...
 

Static Public Member Functions

static QVariantMap loadIniConfig (const QString &filename)
 
static QVariantMap loadJsonConfig (const QString &filename)
 

Protected Member Functions

HeadersdefaultHeaders ()
 
virtual qint64 doWrite (Context *c, const char *data, qint64 len, void *engineData)=0
 
Q_DECL_DEPRECATED void finalize (Context *c)
 
virtual void finalizeBody (Context *c)
 
virtual void finalizeCookies (Context *c)
 
virtual void finalizeError (Context *c)
 
virtual Q_DECL_DEPRECATED bool finalizeHeaders (Context *c)
 
virtual bool finalizeHeadersWrite (Context *c, quint16 status, const Headers &headers, void *engineData)=0
 
bool initApplication ()
 initApplication More...
 
bool postForkApplication ()
 postForkApplication More...
 
Q_DECL_DEPRECATED void processRequest (const EngineRequest &req)
 
Q_DECL_DEPRECATED void processRequest (const QString &method, const QString &path, const QByteArray &query, const QString &protocol, bool https, const QString &serverAddress, const QHostAddress &remoteAddress, quint16 remotePort, const QString &remoteUser, const Headers &headers, quint64 startOfRequest, QIODevice *body, void *requestPtr)
 
ContextprocessRequest2 (const EngineRequest &req)
 
virtual Q_DECL_DEPRECATED bool webSocketClose (Context *c, quint16 code, const QString &reason)
 
Q_DECL_DEPRECATED bool webSocketHandshake (Context *c, const QString &key, const QString &origin, const QString &protocol)
 
virtual Q_DECL_DEPRECATED bool webSocketHandshakeDo (Context *c, const QString &key, const QString &origin, const QString &protocol, void *engineData)
 
virtual Q_DECL_DEPRECATED bool webSocketSendBinaryMessage (Context *c, const QByteArray &message)
 
virtual Q_DECL_DEPRECATED bool webSocketSendPing (Context *c, const QByteArray &payload)
 
virtual Q_DECL_DEPRECATED bool webSocketSendTextMessage (Context *c, const QString &message)
 
qint64 write (Context *c, const char *data, qint64 len, void *engineData)
 

Static Protected Member Functions

static void camelCaseByteArrayHeader (QByteArray &key)
 
static QString camelCaseHeader (const QString &headerKey)
 
static const char * httpStatusMessage (quint16 status, int *len=nullptr)
 

Friends

class Application
 
class Response
 

Detailed Description

This class is responsible receiving the request and sending the response. It must be reimplemented by real HTTP engines due some pure virtual methods.

The subclass must create an Engine per thread (worker core), if the Application passed to the constructor has a worker core greater than 0 it will issue a new Application instance, failing to do so a fatal error is generated (usually indicating that the Application does not have a Q_INVOKABLE constructor).

Definition at line 33 of file engine.h.

Constructor & Destructor Documentation

Engine::Engine ( Cutelyst::Application app,
int  workerCore,
const QVariantMap &  opts 
)
explicit

Constructs an Engine object, where app is the application that might be used to create new instances if workerCore is greater than 1, opts is the options loaded by the engine subclass.

Parameters
appThe application loaded
workerCoreThe thread number
optsThe configuation options

Definition at line 57 of file engine.cpp.

References app(), opts(), and workerCore().

Member Function Documentation

Application * Engine::app ( ) const

Returns the application associated with this engine.

Returns
the Application object we are dealing with

Definition at line 136 of file engine.cpp.

Referenced by Engine().

static void Cutelyst::Engine::camelCaseByteArrayHeader ( QByteArray &  key)
inlinestaticprotected

Convert Header key to camel case

Definition at line 227 of file engine.h.

static QString Cutelyst::Engine::camelCaseHeader ( const QString &  headerKey)
inlinestaticprotected

Returns the header key in camel case form

Definition at line 204 of file engine.h.

QVariantMap Engine::config ( const QString &  entity) const
Parameters
entitythe entity you are interested in
Returns
the configuration settings

Definition at line 383 of file engine.cpp.

Referenced by Cutelyst::MemcachedSessionStore::MemcachedSessionStore(), setConfig(), Cutelyst::Session::setup(), Cutelyst::StaticCompressed::setup(), Cutelyst::Application::setup(), and Cutelyst::Memcached::setup().

Headers & Engine::defaultHeaders ( )
protected

This is the HTTP default response headers that each request gets

Definition at line 348 of file engine.cpp.

virtual qint64 Cutelyst::Engine::doWrite ( Context c,
const char *  data,
qint64  len,
void *  engineData 
)
protectedpure virtual

Reimplement this to do the RAW writing to the client

void Engine::finalize ( Context c)
protected

Called by Application to deal with finalizing cookies, headers and body

Definition at line 433 of file engine.cpp.

References Cutelyst::Context::response(), and Cutelyst::Response::webSocketHandshake().

void Engine::finalizeBody ( Context c)
protectedvirtual

Engines must reimplement this to write the response body back to the caller

Definition at line 106 of file engine.cpp.

References Cutelyst::EngineRequest::finalizeBody(), and Cutelyst::Context::response().

void Engine::finalizeCookies ( Context c)
protectedvirtual

Reimplement if you need a custom way to Set-Cookie, the default implementation writes them to c->res()->headers()

Definition at line 90 of file engine.cpp.

References Cutelyst::Response::cookies(), Cutelyst::Response::headers(), Cutelyst::Headers::pushHeader(), and Cutelyst::Context::response().

void Engine::finalizeError ( Context c)
protectedvirtual

Engines should overwrite this if they want to to make custom error messages. Default implementation render an html with errors.

Definition at line 112 of file engine.cpp.

References Cutelyst::Context::errors(), Cutelyst::Context::response(), Cutelyst::Response::setBody(), Cutelyst::Response::setContentType(), and Cutelyst::Response::setStatus().

bool Engine::finalizeHeaders ( Context c)
protectedvirtual

Finalize the headers, and call doWriteHeader(), reimplemententions must call this first

Definition at line 100 of file engine.cpp.

References Cutelyst::EngineRequest::finalizeHeaders(), and Cutelyst::Context::response().

virtual bool Cutelyst::Engine::finalizeHeadersWrite ( Context c,
quint16  status,
const Headers headers,
void *  engineData 
)
protectedpure virtual

Reimplement this to write the headers back to the client

const char * Engine::httpStatusMessage ( quint16  status,
int *  len = nullptr 
)
staticprotected

Returns the HTTP status message for the given status.

Definition at line 210 of file engine.cpp.

bool Engine::initApplication ( )
protected

This method inits the application and calls init on the engine. It must be called on the engine's thread

Returns
true if succeded

Definition at line 168 of file engine.cpp.

bool Cutelyst::Engine::isZeroWorker ( ) const
inline

Returns true if this is the Zero worker, ie if workerId() == 0 and workerCore() == 0

Note
the value returned from this function is only valid when postFork() is issued.

Definition at line 302 of file engine.h.

QVariantMap Engine::loadIniConfig ( const QString &  filename)
static

Returns a QVariantMap with the INI parsed from filename.

Definition at line 395 of file engine.cpp.

QVariantMap Engine::loadJsonConfig ( const QString &  filename)
static

Returns a QVariantMap with the JSON parsed from filename.

Definition at line 419 of file engine.cpp.

QVariantMap Engine::opts ( ) const

Engine options

Definition at line 377 of file engine.cpp.

Referenced by Engine().

bool Engine::postForkApplication ( )
protected

Subclasses must be call after the engine forks by the worker thread, if no forking is involved it must be called once the worker thread has started.

For convenience QThread::currentThread() has it's object name set with the worker core number.

Returns
true if the engine should use this process

Definition at line 185 of file engine.cpp.

void Engine::processRequest ( const EngineRequest req)
protected

Deprecated

Definition at line 372 of file engine.cpp.

References processRequest2().

void Engine::processRequest ( const QString &  method,
const QString &  path,
const QByteArray &  query,
const QString &  protocol,
bool  https,
const QString &  serverAddress,
const QHostAddress &  remoteAddress,
quint16  remotePort,
const QString &  remoteUser,
const Headers headers,
quint64  startOfRequest,
QIODevice *  body,
void *  requestPtr 
)
protected

Deprecated

Definition at line 483 of file engine.cpp.

Context * Engine::processRequest2 ( const EngineRequest req)
protected

Process the EngineRequest req, the caller must delete the context when the request is finished.

This method allows for engines to keep the Context alive while processing websocket data.

Definition at line 354 of file engine.cpp.

Referenced by processRequest().

void Engine::setConfig ( const QVariantMap &  config)

Sets the configuration to be used by Application

Definition at line 389 of file engine.cpp.

References config().

quint64 Engine::time ( )
virtual
Returns
current micro seconds time to be used for stats, the default implementation returns QDateTime::currentMSecsSinceEpoch() * 1000, to become micro seconds, so if the engine supports a more precise value it can reimplement this method.

Definition at line 199 of file engine.cpp.

Referenced by Cutelyst::Application::handleRequest2().

int Engine::workerCore ( ) const

Returns the worker core set when constructing the engine

Returns
the worker core (thread)

Definition at line 162 of file engine.cpp.

Referenced by Engine(), and Cutelyst::Application::setup().

int Engine::workerId ( ) const
pure virtual

Reimplement this to get the workerId of the engine subclass, this is the same as processes id.

Note
the value returned from this function is only valid when postFork() is issued.
Returns
the worker id (process)
qint64 Engine::write ( Context c,
const char *  data,
qint64  len,
void *  engineData 
)
protected

Called by Response to manually write data

Definition at line 204 of file engine.cpp.

References Cutelyst::Context::response().