| /** @file |
| |
| Transforms content using gzip, deflate, brotli or zstd |
| |
| @section license License |
| |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| #pragma once |
| |
| #include <set> |
| #include <string> |
| #include <vector> |
| |
| #include "ts/ts.h" |
| #include "tscpp/api/noncopyable.h" |
| #include "swoc/TextView.h" |
| |
| namespace Compress |
| { |
| using StringContainer = std::vector<std::string>; |
| |
| enum CompressionAlgorithm { |
| ALGORITHM_DEFAULT = 0, |
| ALGORITHM_DEFLATE = 1, |
| ALGORITHM_GZIP = 2, |
| ALGORITHM_BROTLI = 4, |
| ALGORITHM_ZSTD = 8 |
| }; |
| |
| enum class RangeRequestCtrl : int { |
| NONE = 0, ///< Do nothing |
| NO_COMPRESSION = 1, ///< Do NOT compress Partial Content (default) |
| REMOVE_RANGE = 2, ///< Remove Range Header |
| REMOVE_ACCEPT_ENCODING = 3, ///< Remove Accept-Encoding Header |
| }; |
| |
| class HostConfiguration : private atscppapi::noncopyable |
| { |
| public: |
| explicit HostConfiguration(swoc::TextView host) |
| : host_(host), |
| enabled_(true), |
| cache_(true), |
| remove_accept_encoding_(false), |
| flush_(false), |
| compression_algorithms_(ALGORITHM_GZIP), |
| minimum_content_length_(1024), |
| zlib_compression_level_(6), |
| brotli_compression_level_(6), |
| brotli_lgw_size_(16), |
| zstd_compression_level_(12), |
| content_type_ignore_parameters_(false) |
| { |
| } |
| |
| [[nodiscard]] bool |
| content_type_ignore_parameters() const |
| { |
| return content_type_ignore_parameters_; |
| } |
| void |
| set_content_type_ignore_parameters(bool x) |
| { |
| content_type_ignore_parameters_ = x; |
| } |
| |
| [[nodiscard]] bool |
| enabled() |
| { |
| return enabled_; |
| } |
| void |
| set_enabled(bool x) |
| { |
| enabled_ = x; |
| } |
| [[nodiscard]] RangeRequestCtrl |
| range_request_ctl() |
| { |
| return range_request_ctl_; |
| } |
| [[nodiscard]] bool |
| cache() |
| { |
| return cache_; |
| } |
| void |
| set_cache(bool x) |
| { |
| cache_ = x; |
| } |
| [[nodiscard]] bool |
| flush() |
| { |
| return flush_; |
| } |
| void |
| set_flush(bool x) |
| { |
| flush_ = x; |
| } |
| [[nodiscard]] bool |
| remove_accept_encoding() |
| { |
| return remove_accept_encoding_; |
| } |
| void |
| set_remove_accept_encoding(bool x) |
| { |
| remove_accept_encoding_ = x; |
| } |
| [[nodiscard]] std::string |
| host() |
| { |
| return host_; |
| } |
| |
| [[nodiscard]] bool |
| has_allows() const |
| { |
| return !allows_.empty(); |
| } |
| [[nodiscard]] unsigned int |
| minimum_content_length() const |
| { |
| return minimum_content_length_; |
| } |
| void |
| set_minimum_content_length(unsigned int x) |
| { |
| minimum_content_length_ = x; |
| } |
| |
| [[nodiscard]] unsigned int |
| zlib_compression_level() const |
| { |
| return zlib_compression_level_; |
| } |
| |
| void |
| set_gzip_compression_level(int level) |
| { |
| zlib_compression_level_ = level; |
| } |
| |
| [[nodiscard]] unsigned int |
| brotli_compression_level() const |
| { |
| return brotli_compression_level_; |
| } |
| void |
| set_brotli_compression_level(int level) |
| { |
| brotli_compression_level_ = level; |
| } |
| |
| [[nodiscard]] unsigned int |
| brotli_lgw_size() const |
| { |
| return brotli_lgw_size_; |
| } |
| void |
| set_brotli_lgw_size(unsigned int lgw) |
| { |
| brotli_lgw_size_ = lgw; |
| } |
| |
| [[nodiscard]] int |
| zstd_compression_level() const |
| { |
| return zstd_compression_level_; |
| } |
| void |
| set_zstd_compression_level(int level) |
| { |
| zstd_compression_level_ = level; |
| } |
| |
| void update_defaults(); |
| void add_allow(swoc::TextView allow); |
| void add_compressible_content_type(swoc::TextView content_type); |
| void add_compressible_status_codes(swoc::TextView &status_codes); |
| [[nodiscard]] bool is_url_allowed(const char *url, int url_len); |
| [[nodiscard]] bool is_content_type_compressible(const char *content_type, int content_type_length); |
| [[nodiscard]] bool is_status_code_compressible(const TSHttpStatus status_code) const; |
| void add_compression_algorithms(swoc::TextView &algorithms); |
| [[nodiscard]] int compression_algorithms(); |
| void set_range_request(swoc::TextView token); |
| |
| private: |
| std::string host_; |
| bool enabled_; |
| bool cache_; |
| bool remove_accept_encoding_; |
| bool flush_; |
| int compression_algorithms_; |
| unsigned int minimum_content_length_; |
| unsigned int zlib_compression_level_; |
| unsigned int brotli_compression_level_; |
| unsigned int brotli_lgw_size_; |
| int zstd_compression_level_; |
| bool content_type_ignore_parameters_; |
| |
| RangeRequestCtrl range_request_ctl_ = RangeRequestCtrl::NO_COMPRESSION; |
| StringContainer compressible_content_types_; |
| StringContainer allows_; |
| // maintain backwards compatibility/usability out of the box |
| std::set<TSHttpStatus> compressible_status_codes_ = {TS_HTTP_STATUS_OK, TS_HTTP_STATUS_PARTIAL_CONTENT, |
| TS_HTTP_STATUS_NOT_MODIFIED}; |
| }; |
| |
| using HostContainer = std::vector<HostConfiguration *>; |
| |
| class Configuration : private atscppapi::noncopyable |
| { |
| friend class HostConfiguration; |
| |
| public: |
| [[nodiscard]] static Configuration *Parse(const char *path); |
| [[nodiscard]] HostConfiguration *find(const char *host, int host_length); |
| |
| private: |
| explicit Configuration() {} |
| void add_host_configuration(HostConfiguration *hc); |
| |
| HostContainer host_configurations_; |
| |
| }; // class Configuration |
| |
| } // namespace Compress |