diff options
author | dinmukhammed <dinmukhammed@yandex-team.ru> | 2022-02-21 20:49:41 +0300 |
---|---|---|
committer | dinmukhammed <dinmukhammed@yandex-team.ru> | 2022-02-21 20:49:41 +0300 |
commit | 4bc75f9614e93bb89dd7c077468df9a50c409d12 (patch) | |
tree | bf75ef4992af58f521cf984d8d1e315886421b85 | |
parent | 4857e1fb5ab36ab0eafcbd6207a0bbe593d26ff2 (diff) | |
download | ydb-4bc75f9614e93bb89dd7c077468df9a50c409d12.tar.gz |
YQ-895 Added UrlBuilder
UrlBuilder
ref:cf1b35f4fecea418c414ea09d677c07abc4fab1b
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | ydb/library/yql/utils/CMakeLists.txt | 6 | ||||
-rw-r--r-- | ydb/library/yql/utils/url_builder.cpp | 23 | ||||
-rw-r--r-- | ydb/library/yql/utils/url_builder.h | 24 | ||||
-rw-r--r-- | ydb/library/yql/utils/url_builder_ut.cpp | 23 | ||||
-rw-r--r-- | ydb/library/yql/utils/ut/ya.make | 1 | ||||
-rw-r--r-- | ydb/library/yql/utils/ya.make | 6 |
7 files changed, 81 insertions, 6 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 26aaa45130..abfba04995 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,7 +173,6 @@ add_subdirectory(contrib/libs/base64/neon32) add_subdirectory(contrib/libs/base64/neon64) add_subdirectory(contrib/libs/base64/plain32) add_subdirectory(contrib/libs/base64/plain64) -add_subdirectory(library/cpp/threading/future) add_subdirectory(library/cpp/messagebus) add_subdirectory(contrib/libs/sparsehash) add_subdirectory(library/cpp/codecs) @@ -217,7 +216,9 @@ add_subdirectory(library/cpp/build_info) add_subdirectory(library/cpp/messagebus/monitoring) add_subdirectory(library/cpp/monlib/encode/legacy_protobuf/protos) add_subdirectory(library/cpp/messagebus/scheduler) +add_subdirectory(library/cpp/threading/future) add_subdirectory(library/cpp/string_utils/indent_text) +add_subdirectory(library/cpp/string_utils/quote) add_subdirectory(ydb/library/yql/core/issue) add_subdirectory(ydb/library/yql/public/issue) add_subdirectory(ydb/public/api/protos) @@ -271,7 +272,6 @@ add_subdirectory(library/cpp/case_insensitive_string) add_subdirectory(library/cpp/digest/murmur) add_subdirectory(library/cpp/cgiparam) add_subdirectory(library/cpp/iterator) -add_subdirectory(library/cpp/string_utils/quote) add_subdirectory(library/cpp/string_utils/scan) add_subdirectory(library/cpp/digest/lower_case) add_subdirectory(library/cpp/mime/types) diff --git a/ydb/library/yql/utils/CMakeLists.txt b/ydb/library/yql/utils/CMakeLists.txt index 8fc66b28c5..6d74641e30 100644 --- a/ydb/library/yql/utils/CMakeLists.txt +++ b/ydb/library/yql/utils/CMakeLists.txt @@ -3,8 +3,9 @@ target_link_libraries(library-yql-utils PUBLIC contrib-libs-cxxsupp yutil cpp-digest-md5 - cpp-threading-future library-cpp-messagebus + cpp-string_utils-quote + cpp-threading-future ) target_sources(library-yql-utils PRIVATE ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/bind_in_range.cpp @@ -18,7 +19,8 @@ target_sources(library-yql-utils PRIVATE ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/rand_guid.cpp ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/retry.cpp ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/swap_bytes.cpp + ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/url_builder.cpp + ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/utf8.cpp ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/yql_panic.cpp ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/yql_paths.cpp - ${CMAKE_SOURCE_DIR}/ydb/library/yql/utils/utf8.cpp ) diff --git a/ydb/library/yql/utils/url_builder.cpp b/ydb/library/yql/utils/url_builder.cpp new file mode 100644 index 0000000000..9ed5e6f0c3 --- /dev/null +++ b/ydb/library/yql/utils/url_builder.cpp @@ -0,0 +1,23 @@ +#include "url_builder.h" +#include <library/cpp/string_utils/quote/quote.h> + +namespace NYql { + +TUrlBuilder::TUrlBuilder( + const TString& uri) + : MainUri(uri) + {} + +void TUrlBuilder::AddUrlParam(const TString& name, const TString& value) { + Params.emplace_back(TParam {name, value}); +} + +TString TUrlBuilder::Build() const { + TString res = MainUri; + for (size_t i = 0; i < Params.size(); ++i) { + res += TStringBuilder() << (i > 0 ? "&" : "") << Params[i].Name << "=" << CGIEscapeRet(Params[i].Value); + } + return res; +} + +} // NYql diff --git a/ydb/library/yql/utils/url_builder.h b/ydb/library/yql/utils/url_builder.h new file mode 100644 index 0000000000..87dbd99a52 --- /dev/null +++ b/ydb/library/yql/utils/url_builder.h @@ -0,0 +1,24 @@ +#pragma once + +#include <vector> +#include <util/string/builder.h> + +namespace NYql { + +class TUrlBuilder { + struct TParam { + TString Name; + TString Value; + }; +public: + explicit TUrlBuilder(const TString& uri); + + void AddUrlParam(const TString& name, const TString& value); // Assuming name is already escaped, do not use strings from user input + + TString Build() const; +private: + std::vector<TParam> Params; + TString MainUri; +}; + +} // NYql diff --git a/ydb/library/yql/utils/url_builder_ut.cpp b/ydb/library/yql/utils/url_builder_ut.cpp new file mode 100644 index 0000000000..5cb3316b3d --- /dev/null +++ b/ydb/library/yql/utils/url_builder_ut.cpp @@ -0,0 +1,23 @@ +#include "url_builder.h" + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NYql; + +Y_UNIT_TEST_SUITE(TUrlBuilder) { + Y_UNIT_TEST(Basic) { + TUrlBuilder builder("https://localhost/abc?"); + builder.AddUrlParam("param1", "val1"); + builder.AddUrlParam("param2", "val2"); + + UNIT_ASSERT_VALUES_EQUAL(builder.Build(), "https://localhost/abc?param1=val1¶m2=val2"); + } + + Y_UNIT_TEST(Test1) { + TUrlBuilder builder("https://localhost/abc?"); + builder.AddUrlParam("param1", "=!@#$%^&*(){}[]\" "); + builder.AddUrlParam("param2", "val2"); + + UNIT_ASSERT_VALUES_EQUAL(builder.Build(), "https://localhost/abc?param1=%3D!@%23$%25^%26*%28%29%7B%7D%5B%5D%22+¶m2=val2"); + } +} diff --git a/ydb/library/yql/utils/ut/ya.make b/ydb/library/yql/utils/ut/ya.make index 0d26eb2c5f..28b1f609f8 100644 --- a/ydb/library/yql/utils/ut/ya.make +++ b/ydb/library/yql/utils/ut/ya.make @@ -9,6 +9,7 @@ SRCS( parse_double_ut.cpp range_walker_ut.cpp retry_ut.cpp + url_builder_ut.cpp utf8_ut.cpp ) diff --git a/ydb/library/yql/utils/ya.make b/ydb/library/yql/utils/ya.make index afba70be53..b6f38f1f7d 100644 --- a/ydb/library/yql/utils/ya.make +++ b/ydb/library/yql/utils/ya.make @@ -30,17 +30,19 @@ SRCS( retry.h swap_bytes.cpp swap_bytes.h + url_builder.cpp + utf8.cpp yql_panic.cpp yql_panic.h yql_paths.cpp yql_paths.h - utf8.cpp ) PEERDIR( library/cpp/digest/md5 - library/cpp/threading/future library/cpp/messagebus + library/cpp/string_utils/quote + library/cpp/threading/future ) END() |