cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Cutelyst::StaticCompressed Class Reference

Deliver static files compressed on the fly or precompressed. More...

#include <Cutelyst/Plugins/StaticCompressed/StaticCompressed>

Inheritance diagram for Cutelyst::StaticCompressed:

Public Member Functions

 StaticCompressed (Application *parent)
virtual ~StaticCompressed () override
void setDirs (const QStringList &dirs)
void setIncludePaths (const QStringList &paths)
virtual bool setup (Application *app) override
Public Member Functions inherited from Cutelyst::Plugin
 Plugin (Application *parent)
Public Member Functions inherited from QObject
 QObject (QObject *parent)
bool blockSignals (bool block)
const QObjectListchildren () const const
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
void deleteLater ()
void destroyed (QObject *obj)
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
bool disconnect (const QObject *receiver, const char *method) const const
void dumpObjectInfo ()
void dumpObjectInfo () const const
void dumpObjectTree ()
void dumpObjectTree () const const
QList< QByteArraydynamicPropertyNames () const const
virtual bool event (QEvent *e)
virtual bool eventFilter (QObject *watched, QEvent *event)
findChild (const QString &name, Qt::FindChildOptions options) const const
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
bool inherits (const char *className) const const
void installEventFilter (QObject *filterObj)
bool isWidgetType () const const
bool isWindowType () const const
void killTimer (int id)
virtual const QMetaObjectmetaObject () const const
void moveToThread (QThread *targetThread)
QString objectName () const const
void objectNameChanged (const QString &objectName, QPrivateSignal)
QObjectparent () const const
QVariant property (const char *name) const const
 Q_CLASSINFO (Name, Value)
 Q_DISABLE_COPY (Class)
 Q_DISABLE_COPY_MOVE (Class)
 Q_DISABLE_MOVE (Class)
 Q_EMIT Q_EMIT
 Q_ENUM (...)
 Q_ENUM_NS (...)
 Q_ENUMS (...)
 Q_FLAG (...)
 Q_FLAG_NS (...)
 Q_FLAGS (...)
 Q_GADGET Q_GADGET
 Q_INTERFACES (...)
 Q_INVOKABLE Q_INVOKABLE
 Q_NAMESPACE Q_NAMESPACE
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 Q_OBJECT Q_OBJECT
 Q_PROPERTY (...)
 Q_REVISION Q_REVISION
 Q_SET_OBJECT_NAME (Object)
 Q_SIGNAL Q_SIGNAL
 Q_SIGNALS Q_SIGNALS
 Q_SLOT Q_SLOT
 Q_SLOTS Q_SLOTS
qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
qobject_cast (const QObject *object)
qobject_cast (QObject *object)
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
void removeEventFilter (QObject *obj)
void setObjectName (const QString &name)
void setParent (QObject *parent)
bool setProperty (const char *name, const QVariant &value)
bool signalsBlocked () const const
int startTimer (int interval, Qt::TimerType timerType)
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
QThreadthread () const const

Additional Inherited Members

Static Public Member Functions inherited from QObject
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect (const QMetaObject::Connection &connection)
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
QString tr (const char *sourceText, const char *disambiguation, int n)
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
Public Attributes inherited from QObject
typedef QObjectList
Protected Member Functions inherited from QObject
virtual void childEvent (QChildEvent *event)
virtual void connectNotify (const QMetaMethod &signal)
virtual void customEvent (QEvent *event)
virtual void disconnectNotify (const QMetaMethod &signal)
bool isSignalConnected (const QMetaMethod &signal) const const
int receivers (const char *signal) const const
QObjectsender () const const
int senderSignalIndex () const const
virtual void timerEvent (QTimerEvent *event)
Properties inherited from QObject
 objectName

Detailed Description

The StaticCompressed plugin for Cutelyst can be used to deliver specific static files like CSS and JavaScript files compressed. It has built in support for gzip and DEFLATE compression format and can be extended by external libraries to support the Brotli compression algorithm and to use Zopfli for gzip compression. Beside compressing the raw data on the fly and store the result in a cache directory, it supports pre-compressed files distinguished by file extension in the static source directories. The plugin uses the Accept-Encoding HTTP request header to determine the compression methods supported by the user agent. If you do not need this, use the StaticSimple plugin to deliver your static files.

Compression formats

Support for gzip and DEFLATE compression format is built in by using the qCompress() function. To enable suport for Brotli, build with -DPLUGIN_STATICCOMPRESSED_BROTLI:BOOL=ON and have the libbrotlienc development and header files available. To use Zopfli for the gzip compression, build with -DPLUGIN_STATICCOMPRESSED_ZOPFLI:BOOL=ON and have the libzopfli development and header files available. Also set the configuration key use_zopfli to true. Be aware that Zopfli gives better compression rate than default gzip but is also much slower. So Zopfli is disabled by default even if it is enabled at compilation time.

On the fly compression

Static files of the configured mime_types or with the configured suffixes can be compressed on the fly into a format that is accepted by the requesting user agent. The compressed data is saved into files in the cache_diretory specified in the configuration. The cache file name will be the MD5 hash sum of the original local file path together with the file extension indicating the compression format (.br for Brotli, .gz for gzip/Zopfli and .deflate for DEFLATE). If the modification time of the original file is newer than the modification time of the cached compressed file, the file will be compressed again. It is safe to clean the content of the cache directory - the files will than be recompressed on the next request. On the fly compression can be disabled by setting on_the_fly_compression to false in the configuration file.

Pre-compressed files

Beside the cached on the fly compression it is also possible to deliver pre-comrpessed static files that are saved in the same place as the original files are. The StaticCompressed plugin will try to find a compressed file at the same path as the original file appended by an extension indicating the compression method. So if you have for example boostrap.min.css and bootstrap.min.css.gz in your static files directory, the plugin will deliver the compressed variant if the requesting user agent supports the gzip encoding. The delivery of pre-compressed files can be disabled by setting check_pre_compressed to false in the configuration file.

Used file extensions/suffixes

  • .br - Brotli compressed files
  • .gz - gzip/Zopfli compressed files
  • .deflate - DEFLATE compressed files

Runtime configuration

The plugin offers some configuration options that can be set in the Cutelyst application configuration file in the Cutelyst_StaticCompressed_Plugin section.

  • cache_directory - string value, sets the directory where on the fly compressed data is saved (default: QStandardPaths::CacheLocation + /compressed-static)
  • mime_types - string value, comma separated list of MIME types that should be compressed (default: text/css,application/javascript)
  • suffixes - string value, comma separted list of file suffixes/extensions that should be compressed (default: js.map,css.map,min.js.map,min.css.map)
  • check_pre_compressed - boolean value, enables or disables the check for pre compressed files (default: true)
  • on_the_fly_compression - boolean value, enables or disables the compression on the fly (default: true)
  • zlib_compression_level - integer value, compression level for built in zlib based compression between 0 and 9, with 9 corresponding to the greatest compression (default: 9)
  • brotli_quality_level - integer value, quality level for optional Brotli compression between 0 and 11, with 11 corresponding to the greates compression (default: 11)
  • use_zopfli - boolean value, enables the optional use of Zopfli for the gzip compression if available
  • zopfli_iterations - integer value, number of iterations used for Zopfli compression, more gives more compression but is slower (default: 15)

Usage example

#include <Cutelyst/Plugins/StaticCompressed/StaticCompressed>
bool MyCutelystApp::init()
{
// other initialization stuff
// ...
auto staticCompressed = new StaticCompressed(this);
staticCompressed->setIncludePaths({QStringLiteral("/path/to/my/static/files")});
// maybe more initialization stuff
//...
}
StaticCompressed(Application *parent)

Build configuration

  • -DPLUGIN_STATICCOMPRESSED:BOOL=ON - enables the build of the plugin (default: off)
  • -DPLUGIN_STATICCOMPRESSED_ZOPFLI:BOOL=ON - enables the Zopfli support for gzip, libzopfli development and header files have to be present (default: off)
  • -DPLUGIN_STATICCOMPRESSED_BROTLI:BOOL=ON - enables the Brotli support, libbrotlienc development and header files have to be present (default: off)

Since Cutelyst 2.0.0 you can check if CUTELYST_STATICCOMPRESSED_WITH_ZOPFLI and/or CUTELYST_STATICCOMPRESSED_WITH_BROTLI are defined if you need to know that the plugin supports that compressions.

Since
Cutelyst 1.11.0

Definition at line 132 of file staticcompressed.h.

Constructor & Destructor Documentation

◆ StaticCompressed()

StaticCompressed::StaticCompressed ( Application * parent)
explicit

Constructs a new StaticCompressed object with the given parent.

Definition at line 36 of file staticcompressed.cpp.

References Cutelyst::Plugin::Plugin(), StaticCompressed(), and QObject::parent().

Referenced by StaticCompressed(), setDirs(), setIncludePaths(), and setup().

◆ ~StaticCompressed()

StaticCompressed::~StaticCompressed ( )
overridevirtual

Deconstructs the StaticCompressed object.

Definition at line 44 of file staticcompressed.cpp.

Member Function Documentation

◆ setDirs()

void StaticCompressed::setDirs ( const QStringList & dirs)

Sets a list of top-level directories beneath your 'root' directory that should always be served in static mode.

Definition at line 57 of file staticcompressed.cpp.

References StaticCompressed().

◆ setIncludePaths()

void StaticCompressed::setIncludePaths ( const QStringList & paths)

Sets a list of directories in which to search for your static files. The directories will be searched in order and will return the first file found. Note that your root directory is not automatically added to the search path when you specify an include_path.

Definition at line 48 of file staticcompressed.cpp.

References StaticCompressed().

◆ setup()

bool StaticCompressed::setup ( Application * app)
overridevirtual

Configures the plugin by reading the Cutelyst_StaticCompressed_Plugin section from the Cutelyst application configuration file and connects to the Application::beforePrepareAction() signal. Returns false if the cache directory can not be created if it not exists.

Reimplemented from Cutelyst::Plugin.

Definition at line 63 of file staticcompressed.cpp.

References StaticCompressed(), Cutelyst::Application::beforePrepareAction(), QStandardPaths::CacheLocation, Cutelyst::Engine::config(), QObject::connect(), Cutelyst::Application::engine(), QStringList::join(), Qt::SkipEmptyParts, QString::split(), and QStandardPaths::writableLocation().