diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/http/io/compression.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/http/io/compression.h')
-rw-r--r-- | library/cpp/http/io/compression.h | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/library/cpp/http/io/compression.h b/library/cpp/http/io/compression.h new file mode 100644 index 0000000000..f16c4a18eb --- /dev/null +++ b/library/cpp/http/io/compression.h @@ -0,0 +1,72 @@ +#pragma once + +#include "stream.h" + +#include <util/generic/deque.h> +#include <util/generic/hash.h> + +class TCompressionCodecFactory { +public: + using TDecoderConstructor = std::function<THolder<IInputStream>(IInputStream*)>; + using TEncoderConstructor = std::function<THolder<IOutputStream>(IOutputStream*)>; + + TCompressionCodecFactory(); + + static inline TCompressionCodecFactory& Instance() noexcept { + return *SingletonWithPriority<TCompressionCodecFactory, 0>(); + } + + inline const TDecoderConstructor* FindDecoder(TStringBuf name) const { + if (auto codec = Codecs_.FindPtr(name)) { + return &codec->Decoder; + } + + return nullptr; + } + + inline const TEncoderConstructor* FindEncoder(TStringBuf name) const { + if (auto codec = Codecs_.FindPtr(name)) { + return &codec->Encoder; + } + + return nullptr; + } + + inline TArrayRef<const TStringBuf> GetBestCodecs() const { + return BestCodecs_; + } + +private: + void Add(TStringBuf name, TDecoderConstructor d, TEncoderConstructor e); + + struct TCodec { + TDecoderConstructor Decoder; + TEncoderConstructor Encoder; + }; + + TDeque<TString> Strings_; + THashMap<TStringBuf, TCodec> Codecs_; + TVector<TStringBuf> BestCodecs_; +}; + +namespace NHttp { + template <typename F> + TString ChooseBestCompressionScheme(F accepted, TArrayRef<const TStringBuf> available) { + if (available.empty()) { + return "identity"; + } + + if (accepted("*")) { + return TString(available[0]); + } + + for (const auto& coding : available) { + TString s(coding); + if (accepted(s)) { + return s; + } + } + + return "identity"; + } +} |