diff --git a/README.md b/README.md index 608c9b74..90d771e3 100644 --- a/README.md +++ b/README.md @@ -698,6 +698,10 @@ https://lists.boost.org/Archives/boost/2023/01/253944.php. [this](https://github.com/boostorg/redis/issues/181#issuecomment-1913346983) comment. +* ([Issue 182](https://github.com/boostorg/redis/issues/182)). + Sets `"default"` as the default value of `config::username`. This + makes it simpler to use the `requirepass` configuration in Redis. + ### Boost 1.84 (First release in Boost) * Deprecates the `async_receive` overload that takes a response. Users diff --git a/include/boost/redis/config.hpp b/include/boost/redis/config.hpp index b3297501..caadd80d 100644 --- a/include/boost/redis/config.hpp +++ b/include/boost/redis/config.hpp @@ -38,7 +38,7 @@ struct config { * [HELLO](https://redis.io/commands/hello/) command. If left * empty `HELLO` will be sent without authentication parameters. */ - std::string username; + std::string username = "default"; /** @brief Password passed to the * [HELLO](https://redis.io/commands/hello/) command. If left diff --git a/include/boost/redis/detail/runner.hpp b/include/boost/redis/detail/runner.hpp index 5d0c66b5..f257019b 100644 --- a/include/boost/redis/detail/runner.hpp +++ b/include/boost/redis/detail/runner.hpp @@ -30,6 +30,8 @@ namespace boost::redis::detail { +void push_hello(config const& cfg, request& req); + template struct hello_op { Runner* runner_ = nullptr; @@ -42,9 +44,6 @@ struct hello_op { { BOOST_ASIO_CORO_REENTER (coro_) { - runner_->hello_req_.clear(); - if (runner_->hello_resp_.has_value()) - runner_->hello_resp_.value().clear(); runner_->add_hello(); BOOST_ASIO_CORO_YIELD @@ -232,17 +231,10 @@ private: void add_hello() { - if (!cfg_.username.empty() && !cfg_.password.empty() && !cfg_.clientname.empty()) - hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password, "SETNAME", cfg_.clientname); - else if (cfg_.username.empty() && cfg_.password.empty() && cfg_.clientname.empty()) - hello_req_.push("HELLO", "3"); - else if (cfg_.clientname.empty()) - hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password); - else - hello_req_.push("HELLO", "3", "SETNAME", cfg_.clientname); - - if (cfg_.database_index && cfg_.database_index.value() != 0) - hello_req_.push("SELECT", cfg_.database_index.value()); + hello_req_.clear(); + if (hello_resp_.has_value()) + hello_resp_.value().clear(); + push_hello(cfg_, hello_req_); } bool has_error_in_response() const noexcept diff --git a/include/boost/redis/impl/runner.ipp b/include/boost/redis/impl/runner.ipp new file mode 100644 index 00000000..293ad92e --- /dev/null +++ b/include/boost/redis/impl/runner.ipp @@ -0,0 +1,27 @@ +/* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com) + * + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE.txt) + */ + +#include + +namespace boost::redis::detail +{ + +void push_hello(config const& cfg, request& req) +{ + if (!cfg.username.empty() && !cfg.password.empty() && !cfg.clientname.empty()) + req.push("HELLO", "3", "AUTH", cfg.username, cfg.password, "SETNAME", cfg.clientname); + else if (cfg.password.empty() && cfg.clientname.empty()) + req.push("HELLO", "3"); + else if (cfg.clientname.empty()) + req.push("HELLO", "3", "AUTH", cfg.username, cfg.password); + else + req.push("HELLO", "3", "SETNAME", cfg.clientname); + + if (cfg.database_index && cfg.database_index.value() != 0) + req.push("SELECT", cfg.database_index.value()); +} + +} // boost::redis::detail diff --git a/include/boost/redis/src.hpp b/include/boost/redis/src.hpp index 7751971a..f749e3f3 100644 --- a/include/boost/redis/src.hpp +++ b/include/boost/redis/src.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/test/test_low_level_sync_sans_io.cpp b/test/test_low_level_sync_sans_io.cpp index 441f8098..12e4980a 100644 --- a/test/test_low_level_sync_sans_io.cpp +++ b/test/test_low_level_sync_sans_io.cpp @@ -4,6 +4,7 @@ * accompanying file LICENSE.txt) */ +#include #include #include #define BOOST_TEST_MODULE conn-quit @@ -11,6 +12,9 @@ #include #include +using boost::redis::request; +using boost::redis::config; +using boost::redis::detail::push_hello; using boost::redis::adapter::adapt2; using boost::redis::adapter::result; using boost::redis::resp3::detail::deserialize; @@ -31,3 +35,56 @@ BOOST_AUTO_TEST_CASE(low_level_sync_sans_io) exit(EXIT_FAILURE); } } + +BOOST_AUTO_TEST_CASE(config_to_hello) +{ + config cfg; + cfg.clientname = ""; + request req; + + push_hello(cfg, req); + + std::string_view const expected = "*2\r\n$5\r\nHELLO\r\n$1\r\n3\r\n"; + BOOST_CHECK_EQUAL(req.payload(), expected); +} + +BOOST_AUTO_TEST_CASE(config_to_hello_with_select) +{ + config cfg; + cfg.clientname = ""; + cfg.database_index = 10; + request req; + + push_hello(cfg, req); + + std::string_view const expected = + "*2\r\n$5\r\nHELLO\r\n$1\r\n3\r\n" + "*2\r\n$6\r\nSELECT\r\n$2\r\n10\r\n"; + + BOOST_CHECK_EQUAL(req.payload(), expected); +} + +BOOST_AUTO_TEST_CASE(config_to_hello_cmd_clientname) +{ + config cfg; + request req; + + push_hello(cfg, req); + + std::string_view const expected = "*4\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$7\r\nSETNAME\r\n$11\r\nBoost.Redis\r\n"; + BOOST_CHECK_EQUAL(req.payload(), expected); +} + +BOOST_AUTO_TEST_CASE(config_to_hello_cmd_auth) +{ + config cfg; + cfg.clientname = ""; + cfg.username = "foo"; + cfg.password = "bar"; + request req; + + push_hello(cfg, req); + + std::string_view const expected = "*5\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$4\r\nAUTH\r\n$3\r\nfoo\r\n$3\r\nbar\r\n"; + BOOST_CHECK_EQUAL(req.payload(), expected); +}