diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/cgiparam | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/cgiparam')
-rw-r--r-- | library/cpp/cgiparam/cgiparam.cpp | 172 | ||||
-rw-r--r-- | library/cpp/cgiparam/cgiparam.h | 38 | ||||
-rw-r--r-- | library/cpp/cgiparam/cgiparam_ut.cpp | 102 | ||||
-rw-r--r-- | library/cpp/cgiparam/fuzz/main.cpp | 20 | ||||
-rw-r--r-- | library/cpp/cgiparam/fuzz/ya.make | 14 |
5 files changed, 173 insertions, 173 deletions
diff --git a/library/cpp/cgiparam/cgiparam.cpp b/library/cpp/cgiparam/cgiparam.cpp index f3277b8e4b..89efbe4c1d 100644 --- a/library/cpp/cgiparam/cgiparam.cpp +++ b/library/cpp/cgiparam/cgiparam.cpp @@ -1,8 +1,8 @@ -#include "cgiparam.h" - +#include "cgiparam.h" + #include <library/cpp/string_utils/scan/scan.h> #include <library/cpp/string_utils/quote/quote.h> - + #include <util/generic/singleton.h> TCgiParameters::TCgiParameters(std::initializer_list<std::pair<TString, TString>> il) { @@ -12,21 +12,21 @@ TCgiParameters::TCgiParameters(std::initializer_list<std::pair<TString, TString> } const TString& TCgiParameters::Get(const TStringBuf name, size_t numOfValue) const noexcept { - const auto it = Find(name, numOfValue); - + const auto it = Find(name, numOfValue); + return end() == it ? Default<TString>() : it->second; -} - +} + bool TCgiParameters::Erase(const TStringBuf name, size_t pos) { - const auto pair = equal_range(name); - - for (auto it = pair.first; it != pair.second; ++it, --pos) { + const auto pair = equal_range(name); + + for (auto it = pair.first; it != pair.second; ++it, --pos) { if (0 == pos) { erase(it); return true; } - } - + } + return false; } @@ -48,31 +48,31 @@ bool TCgiParameters::Erase(const TStringBuf name, const TStringBuf val) { size_t TCgiParameters::EraseAll(const TStringBuf name) { size_t num = 0; - - const auto pair = equal_range(name); - - for (auto it = pair.first; it != pair.second; erase(it++), ++num) + + const auto pair = equal_range(name); + + for (auto it = pair.first; it != pair.second; erase(it++), ++num) ; - + return num; } void TCgiParameters::JoinUnescaped(const TStringBuf key, char sep, TStringBuf val) { - const auto pair = equal_range(key); - auto it = pair.first; - + const auto pair = equal_range(key); + auto it = pair.first; + if (it == pair.second) { // not found - if (val.IsInited()) { + if (val.IsInited()) { emplace_hint(it, TString(key), TString(val)); - } - } else { + } + } else { TString& dst = it->second; - - for (++it; it != pair.second; erase(it++)) { + + for (++it; it != pair.second; erase(it++)) { dst += sep; dst.AppendNoAlias(it->second.data(), it->second.size()); } - + if (val.IsInited()) { dst += sep; dst += val; @@ -85,44 +85,44 @@ static inline TString DoUnescape(const TStringBuf s) { res.reserve(CgiUnescapeBufLen(s.size())); res.ReserveAndResize(CgiUnescape(res.begin(), s).size()); - - return res; -} + + return res; +} void TCgiParameters::InsertEscaped(const TStringBuf name, const TStringBuf value) { InsertUnescaped(DoUnescape(name), DoUnescape(value)); } -template <bool addAll, class F> +template <bool addAll, class F> static inline void DoScan(const TStringBuf s, F& f) { - ScanKeyValue<addAll, '&', '='>(s, f); -} - -struct TAddEscaped { - TCgiParameters* C; - + ScanKeyValue<addAll, '&', '='>(s, f); +} + +struct TAddEscaped { + TCgiParameters* C; + inline void operator()(const TStringBuf key, const TStringBuf val) { C->InsertEscaped(key, val); - } -}; - + } +}; + void TCgiParameters::Scan(const TStringBuf query, bool form) { - Flush(); + Flush(); form ? ScanAdd(query) : ScanAddAll(query); } void TCgiParameters::ScanAdd(const TStringBuf query) { TAddEscaped f = {this}; - - DoScan<false>(query, f); + + DoScan<false>(query, f); } void TCgiParameters::ScanAddUnescaped(const TStringBuf query) { auto f = [this](const TStringBuf key, const TStringBuf val) { this->InsertUnescaped(key, val); - }; + }; - DoScan<false>(query, f); + DoScan<false>(query, f); } void TCgiParameters::ScanAddAllUnescaped(const TStringBuf query) { @@ -135,48 +135,48 @@ void TCgiParameters::ScanAddAllUnescaped(const TStringBuf query) { void TCgiParameters::ScanAddAll(const TStringBuf query) { TAddEscaped f = {this}; - - DoScan<true>(query, f); + + DoScan<true>(query, f); } TString TCgiParameters::Print() const { TString res; - - res.reserve(PrintSize()); - const char* end = Print(res.begin()); + + res.reserve(PrintSize()); + const char* end = Print(res.begin()); res.ReserveAndResize(end - res.data()); - - return res; + + return res; } -char* TCgiParameters::Print(char* res) const { - if (empty()) { - return res; - } - - for (auto i = begin();;) { +char* TCgiParameters::Print(char* res) const { + if (empty()) { + return res; + } + + for (auto i = begin();;) { res = CGIEscape(res, i->first); - *res++ = '='; + *res++ = '='; res = CGIEscape(res, i->second); - - if (++i == end()) { + + if (++i == end()) { break; - } - - *res++ = '&'; - } - + } + + *res++ = '&'; + } + return res; } size_t TCgiParameters::PrintSize() const noexcept { - size_t res = size(); // for '&' - + size_t res = size(); // for '&' + for (const auto& i : *this) { res += CgiEscapeBufLen(i.first.size() + i.second.size()); // extra zero will be used for '=' - } - - return res; + } + + return res; } TString TCgiParameters::QuotedPrint(const char* safe) const { @@ -205,27 +205,27 @@ TString TCgiParameters::QuotedPrint(const char* safe) const { } TCgiParameters::const_iterator TCgiParameters::Find(const TStringBuf name, size_t pos) const noexcept { - const auto pair = equal_range(name); - - for (auto it = pair.first; it != pair.second; ++it, --pos) { - if (0 == pos) { + const auto pair = equal_range(name); + + for (auto it = pair.first; it != pair.second; ++it, --pos) { + if (0 == pos) { return it; - } - } - + } + } + return end(); } bool TCgiParameters::Has(const TStringBuf name, const TStringBuf value) const noexcept { - const auto pair = equal_range(name); - - for (auto it = pair.first; it != pair.second; ++it) { - if (value == it->second) { - return true; - } - } - - return false; + const auto pair = equal_range(name); + + for (auto it = pair.first; it != pair.second; ++it) { + if (value == it->second) { + return true; + } + } + + return false; } TQuickCgiParam::TQuickCgiParam(const TStringBuf cgiParamStr) { diff --git a/library/cpp/cgiparam/cgiparam.h b/library/cpp/cgiparam/cgiparam.h index 87d1ab0ad4..f4ee174213 100644 --- a/library/cpp/cgiparam/cgiparam.h +++ b/library/cpp/cgiparam/cgiparam.h @@ -6,16 +6,16 @@ #include <util/generic/map.h> #include <util/generic/strbuf.h> #include <util/generic/string.h> - + #include <initializer_list> - -struct TStringLess { - template <class T1, class T2> + +struct TStringLess { + template <class T1, class T2> inline bool operator()(const T1& t1, const T2& t2) const noexcept { - return TStringBuf(t1) < TStringBuf(t2); - } -}; - + return TStringBuf(t1) < TStringBuf(t2); + } +}; + class TCgiParameters: public TMultiMap<TString, TString> { public: TCgiParameters() = default; @@ -45,7 +45,7 @@ public: void ScanAddUnescaped(const TStringBuf cgiParStr); void ScanAddAllUnescaped(const TStringBuf cgiParStr); void ScanAddAll(const TStringBuf cgiParStr); - + /// Returns the string representation of all the stored parameters /** * @note The returned string has format <name1>=<value1>&<name2>=<value2>&... @@ -84,9 +84,9 @@ public: */ Y_PURE_FUNCTION const TString& Get(const TStringBuf name, size_t numOfValue = 0) const noexcept; - + void InsertEscaped(const TStringBuf name, const TStringBuf value); - + #if !defined(__GLIBCXX__) template <typename TName, typename TValue> inline void InsertUnescaped(TName&& name, TValue&& value) { @@ -102,7 +102,7 @@ public: emplace(TString(name), TString(value)); } #endif - + // replace all values for a given key with new values template <typename TIter> void ReplaceUnescaped(const TStringBuf key, TIter valuesBegin, const TIter valuesEnd); @@ -112,7 +112,7 @@ public: } void ReplaceUnescaped(const TStringBuf key, const TStringBuf value) { - ReplaceUnescaped(key, {value}); + ReplaceUnescaped(key, {value}); } // join multiple values into a single one using a separator @@ -121,13 +121,13 @@ public: bool Erase(const TStringBuf name, size_t numOfValue = 0); bool Erase(const TStringBuf name, const TStringBuf val); - + inline const char* FormField(const TStringBuf name, size_t numOfValue = 0) const { const_iterator it = Find(name, numOfValue); - + if (it == end()) { return nullptr; - } + } return it->second.data(); } @@ -139,18 +139,18 @@ void TCgiParameters::ReplaceUnescaped(const TStringBuf key, TIter valuesBegin, c auto current = oldRange.first; // reuse as many existing nodes as possible (probably none) - for (; valuesBegin != valuesEnd && current != oldRange.second; ++valuesBegin, ++current) { + for (; valuesBegin != valuesEnd && current != oldRange.second; ++valuesBegin, ++current) { current->second = *valuesBegin; } // if there were more nodes than we need to insert then erase remaining ones - for (; current != oldRange.second; erase(current++)) { + for (; current != oldRange.second; erase(current++)) { } // if there were less nodes than we need to insert then emplace the rest of the range if (valuesBegin != valuesEnd) { const TString keyStr = TString(key); - for (; valuesBegin != valuesEnd; ++valuesBegin) { + for (; valuesBegin != valuesEnd; ++valuesBegin) { emplace_hint(oldRange.second, keyStr, TString(*valuesBegin)); } } diff --git a/library/cpp/cgiparam/cgiparam_ut.cpp b/library/cpp/cgiparam/cgiparam_ut.cpp index a562342084..f7e6ddf6be 100644 --- a/library/cpp/cgiparam/cgiparam_ut.cpp +++ b/library/cpp/cgiparam/cgiparam_ut.cpp @@ -1,22 +1,22 @@ -#include "cgiparam.h" - +#include "cgiparam.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TCgiParametersTest) { Y_UNIT_TEST(TestScan1) { - TCgiParameters C; - C.Scan("aaa=b%62b&ccc=ddd&ag0="); - UNIT_ASSERT_EQUAL(C.Get("aaa") == "bbb", true); - UNIT_ASSERT_EQUAL(C.NumOfValues("ag0") == 1, true); - UNIT_ASSERT(C.Has("ccc", "ddd")); - UNIT_ASSERT(C.Has("ag0", "")); - UNIT_ASSERT(!C.Has("a", "bbb")); - UNIT_ASSERT(!C.Has("aaa", "bb")); + TCgiParameters C; + C.Scan("aaa=b%62b&ccc=ddd&ag0="); + UNIT_ASSERT_EQUAL(C.Get("aaa") == "bbb", true); + UNIT_ASSERT_EQUAL(C.NumOfValues("ag0") == 1, true); + UNIT_ASSERT(C.Has("ccc", "ddd")); + UNIT_ASSERT(C.Has("ag0", "")); + UNIT_ASSERT(!C.Has("a", "bbb")); + UNIT_ASSERT(!C.Has("aaa", "bb")); UNIT_ASSERT(C.Has("ccc")); UNIT_ASSERT(!C.Has("zzzzzz")); - } - + } + Y_UNIT_TEST(TestQuick) { TQuickCgiParam C("aaa=b%62b&ccc=ddd&ag0="); UNIT_ASSERT_EQUAL(C.Get("aaa") == "bbb", true); @@ -40,47 +40,47 @@ Y_UNIT_TEST_SUITE(TCgiParametersTest) { Y_UNIT_TEST(TestScan2) { const TString parsee("=000&aaa=bbb&ag0=&ccc=ddd"); - TCgiParameters c; - c.Scan(parsee); - - UNIT_ASSERT_VALUES_EQUAL(c.Print(), parsee); - } - + TCgiParameters c; + c.Scan(parsee); + + UNIT_ASSERT_VALUES_EQUAL(c.Print(), parsee); + } + Y_UNIT_TEST(TestScan3) { const TString parsee("aaa=bbb&ag0=&ccc=ddd"); - TCgiParameters c; - c.Scan(parsee); - + TCgiParameters c; + c.Scan(parsee); + c.InsertUnescaped("d", "xxx"); - - UNIT_ASSERT_VALUES_EQUAL(c.Print(), parsee + "&d=xxx"); - } - + + UNIT_ASSERT_VALUES_EQUAL(c.Print(), parsee + "&d=xxx"); + } + Y_UNIT_TEST(TestScanAddAll1) { - TCgiParameters c; - c.ScanAddAll("qw"); - + TCgiParameters c; + c.ScanAddAll("qw"); + UNIT_ASSERT_VALUES_EQUAL(c.size(), 1u); - UNIT_ASSERT(c.Get("qw").empty()); - } - + UNIT_ASSERT(c.Get("qw").empty()); + } + Y_UNIT_TEST(TestScanAddAll2) { - TCgiParameters c; - c.ScanAddAll("qw&"); - + TCgiParameters c; + c.ScanAddAll("qw&"); + UNIT_ASSERT_VALUES_EQUAL(c.size(), 1u); - UNIT_ASSERT(c.Get("qw").empty()); - } - + UNIT_ASSERT(c.Get("qw").empty()); + } + Y_UNIT_TEST(TestScanAddAll3) { - TCgiParameters c; - c.ScanAddAll("qw=1&x"); - + TCgiParameters c; + c.ScanAddAll("qw=1&x"); + UNIT_ASSERT_VALUES_EQUAL(c.size(), 2u); - UNIT_ASSERT_VALUES_EQUAL(c.Get("qw"), "1"); - UNIT_ASSERT(c.Get("x").empty()); - } - + UNIT_ASSERT_VALUES_EQUAL(c.Get("qw"), "1"); + UNIT_ASSERT(c.Get("x").empty()); + } + Y_UNIT_TEST(TestScanAddAll4) { TCgiParameters c; c.ScanAddAll("ccc=1&aaa=1&ccc=3&bbb&ccc=2"); @@ -191,8 +191,8 @@ Y_UNIT_TEST_SUITE(TCgiParametersTest) { } Y_UNIT_TEST(TestEmpty) { - UNIT_ASSERT(TCgiParameters().Print().empty()); - } + UNIT_ASSERT(TCgiParameters().Print().empty()); + } Y_UNIT_TEST(TestJoinUnescaped) { TCgiParameters c; @@ -218,23 +218,23 @@ Y_UNIT_TEST_SUITE(TCgiParametersTest) { Y_UNIT_TEST(TestPrintAsQuote) { TCgiParameters c = { - std::make_pair("aaa", "value/with/slashes"), + std::make_pair("aaa", "value/with/slashes"), std::make_pair("b/b/b", "value_without_slashes"), - std::make_pair("ccc", "value")}; + std::make_pair("ccc", "value")}; UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=value/with/slashes&b/b/b=value_without_slashes&ccc=value"); UNIT_ASSERT_VALUES_EQUAL(c.QuotedPrint(""), "aaa=value%2Fwith%2Fslashes&b%2Fb%2Fb=value_without_slashes&ccc=value"); } Y_UNIT_TEST(TestPrintAsQuoteEmpty) { - TCgiParameters c = {}; + TCgiParameters c = {}; UNIT_ASSERT_VALUES_EQUAL(c.QuotedPrint(""), ""); } Y_UNIT_TEST(TestPrintAsQuoteEmptyKeyOrValue) { TCgiParameters c = { std::make_pair("", "value/of/empty"), - std::make_pair("key/for/empty", "")}; + std::make_pair("key/for/empty", "")}; UNIT_ASSERT_VALUES_EQUAL(c.Print(), "=value/of/empty&key/for/empty="); UNIT_ASSERT_VALUES_EQUAL(c.QuotedPrint(""), "=value%2Fof%2Fempty&key%2Ffor%2Fempty="); diff --git a/library/cpp/cgiparam/fuzz/main.cpp b/library/cpp/cgiparam/fuzz/main.cpp index 69d82b5f32..f1ff8c4634 100644 --- a/library/cpp/cgiparam/fuzz/main.cpp +++ b/library/cpp/cgiparam/fuzz/main.cpp @@ -1,11 +1,11 @@ #include <library/cpp/cgiparam/cgiparam.h> - -extern "C" int LLVMFuzzerTestOneInput(const ui8* data, size_t size) { - try { - TCgiParameters(TStringBuf((const char*)data, size)); - } catch (...) { - // ¯\_(ツ)_/¯ - } - - return 0; // Non-zero return values are reserved for future use. -} + +extern "C" int LLVMFuzzerTestOneInput(const ui8* data, size_t size) { + try { + TCgiParameters(TStringBuf((const char*)data, size)); + } catch (...) { + // ¯\_(ツ)_/¯ + } + + return 0; // Non-zero return values are reserved for future use. +} diff --git a/library/cpp/cgiparam/fuzz/ya.make b/library/cpp/cgiparam/fuzz/ya.make index 8fb9d50d3b..594f9c227d 100644 --- a/library/cpp/cgiparam/fuzz/ya.make +++ b/library/cpp/cgiparam/fuzz/ya.make @@ -1,16 +1,16 @@ FUZZ() - + OWNER( pg g:util ) - -SRCS( - main.cpp -) - + +SRCS( + main.cpp +) + PEERDIR( library/cpp/cgiparam ) -END() +END() |