Cutelyst  2.14.2
Public Member Functions | List of all members
Cutelyst::StaticCompressed Class Reference

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

#include <Cutelyst/Plugins/StaticCompressed/StaticCompressed>

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

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 QObjectList & children () const
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const
 
bool disconnect (const QObject *receiver, const char *method) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QByteArraydynamicPropertyNames () const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name, Qt::FindChildOptions options) const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const
 
bool inherits (const char *className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const
 
QVariant property (const char *name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType)
 
QThreadthread () 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 *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
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, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
- 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
 
int receivers (const char *signal) const
 
QObjectsender () const
 
int senderSignalIndex () 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

Runtime configuration

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

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
//...
}

Build configuration

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 138 of file staticcompressed.h.

Constructor & Destructor Documentation

StaticCompressed::StaticCompressed ( Application parent)
explicit

Constructs a new StaticCompressed object with the given parent.

Definition at line 49 of file staticcompressed.cpp.

StaticCompressed::~StaticCompressed ( )
overridevirtual

Deconstructs the StaticCompressed object.

Definition at line 56 of file staticcompressed.cpp.

Member Function Documentation

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 70 of file staticcompressed.cpp.

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 61 of file staticcompressed.cpp.

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 76 of file staticcompressed.cpp.

References Cutelyst::Application::beforePrepareAction(), Cutelyst::Engine::config(), QObject::connect(), Cutelyst::Application::engine(), QString::split(), QList::value(), and QStandardPaths::writableLocation().