Cutelyst  2.13.0
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

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)

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);
// 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.

Cutelyst 1.11.0

Definition at line 138 of file staticcompressed.h.

Constructor & Destructor Documentation

◆ StaticCompressed()

StaticCompressed::StaticCompressed ( Application parent)

Constructs a new StaticCompressed object with the given parent.

Definition at line 49 of file staticcompressed.cpp.

◆ ~StaticCompressed()

StaticCompressed::~StaticCompressed ( )

Deconstructs the StaticCompressed object.

Definition at line 56 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 70 of file staticcompressed.cpp.

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

◆ setup()

bool StaticCompressed::setup ( Application app)

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(), and Cutelyst::Application::engine().

StaticCompressed(Application *parent)
Definition: staticcompressed.cpp:49