Add new HTTP compression methods

This commit is contained in:
Nikolay Degterinsky 2022-10-04 03:13:46 +00:00
parent 011748e431
commit 99dc396c77
4 changed files with 32 additions and 28 deletions

View File

@ -55,6 +55,32 @@ std::string toContentEncodingName(CompressionMethod method)
__builtin_unreachable();
}
CompressionMethod chooseHTTPCompressionMethod(const std::string & list)
{
/// If client supports brotli - it's preferred.
/// Both gzip and deflate are supported. If the client supports both, gzip is preferred.
/// NOTE parsing of the list of methods is slightly incorrect.
if (std::string::npos != list.find("br"))
return CompressionMethod::Brotli;
else if (std::string::npos != list.find("gzip"))
return CompressionMethod::Gzip;
else if (std::string::npos != list.find("deflate"))
return CompressionMethod::Zlib;
else if (std::string::npos != list.find("xz"))
return CompressionMethod::Xz;
else if (std::string::npos != list.find("zstd"))
return CompressionMethod::Zstd;
else if (std::string::npos != list.find("lz4"))
return CompressionMethod::Lz4;
else if (std::string::npos != list.find("snappy"))
return CompressionMethod::Snappy;
else if (std::string::npos != list.find("bz2"))
return CompressionMethod::Bzip2;
else
return CompressionMethod::None;
}
CompressionMethod chooseCompressionMethod(const std::string & path, const std::string & hint)
{
std::string file_extension;

View File

@ -46,6 +46,10 @@ std::string toContentEncodingName(CompressionMethod method);
*/
CompressionMethod chooseCompressionMethod(const std::string & path, const std::string & hint);
/** Choose a compression method from HTTP header list of supported compression methods.
*/
CompressionMethod chooseHTTPCompressionMethod(const std::string & list)
/// Get a range of the valid compression levels for the compression method.
std::pair<uint64_t, uint64_t> getCompressionLevelRange(const CompressionMethod & method);

View File

@ -542,22 +542,7 @@ void HTTPHandler::processQuery(
CompressionMethod http_response_compression_method = CompressionMethod::None;
if (!http_response_compression_methods.empty())
{
/// If client supports brotli - it's preferred.
/// Both gzip and deflate are supported. If the client supports both, gzip is preferred.
/// NOTE parsing of the list of methods is slightly incorrect.
if (std::string::npos != http_response_compression_methods.find("br"))
http_response_compression_method = CompressionMethod::Brotli;
else if (std::string::npos != http_response_compression_methods.find("gzip"))
http_response_compression_method = CompressionMethod::Gzip;
else if (std::string::npos != http_response_compression_methods.find("deflate"))
http_response_compression_method = CompressionMethod::Zlib;
else if (std::string::npos != http_response_compression_methods.find("xz"))
http_response_compression_method = CompressionMethod::Xz;
else if (std::string::npos != http_response_compression_methods.find("zstd"))
http_response_compression_method = CompressionMethod::Zstd;
}
http_response_compression_method = chooseHTTPCompressionMethod(http_response_compression_methods);
bool client_supports_http_compression = http_response_compression_method != CompressionMethod::None;

View File

@ -41,18 +41,7 @@ responseWriteBuffer(HTTPServerRequest & request, HTTPServerResponse & response,
CompressionMethod http_response_compression_method = CompressionMethod::None;
if (!http_response_compression_methods.empty())
{
/// If client supports brotli - it's preferred.
/// Both gzip and deflate are supported. If the client supports both, gzip is preferred.
/// NOTE parsing of the list of methods is slightly incorrect.
if (std::string::npos != http_response_compression_methods.find("br"))
http_response_compression_method = CompressionMethod::Brotli;
else if (std::string::npos != http_response_compression_methods.find("gzip"))
http_response_compression_method = CompressionMethod::Gzip;
else if (std::string::npos != http_response_compression_methods.find("deflate"))
http_response_compression_method = CompressionMethod::Zlib;
}
http_response_compression_method = chooseHTTPCompressionMethod(http_response_compression_methods);
bool client_supports_http_compression = http_response_compression_method != CompressionMethod::None;