setup_request_utils.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_SETUP_REQUEST_UTILS_HPP
  7. #define BOOST_REDIS_SETUP_REQUEST_UTILS_HPP
  8. #include <boost/redis/config.hpp>
  9. #include <boost/redis/request.hpp>
  10. #include <boost/redis/response.hpp>
  11. namespace boost::redis::detail {
  12. // Modifies config::setup to make a request suitable to be sent
  13. // to the server using async_exec
  14. inline void compose_setup_request(config& cfg)
  15. {
  16. if (!cfg.use_setup) {
  17. // We're not using the setup request as-is, but should compose one based on
  18. // the values passed by the user
  19. auto& req = cfg.setup;
  20. req.clear();
  21. // Which parts of the command should we send?
  22. // Don't send AUTH if the user is the default and the password is empty.
  23. // Other users may have empty passwords.
  24. // Note that this is just an optimization.
  25. bool send_auth = !(
  26. cfg.username.empty() || (cfg.username == "default" && cfg.password.empty()));
  27. bool send_setname = !cfg.clientname.empty();
  28. // Gather everything we can in a HELLO command
  29. if (send_auth && send_setname)
  30. req.push("HELLO", "3", "AUTH", cfg.username, cfg.password, "SETNAME", cfg.clientname);
  31. else if (send_auth)
  32. req.push("HELLO", "3", "AUTH", cfg.username, cfg.password);
  33. else if (send_setname)
  34. req.push("HELLO", "3", "SETNAME", cfg.clientname);
  35. else
  36. req.push("HELLO", "3");
  37. // SELECT is independent of HELLO
  38. if (cfg.database_index && cfg.database_index.value() != 0)
  39. req.push("SELECT", cfg.database_index.value());
  40. }
  41. // In any case, the setup request should have the priority
  42. // flag set so it's executed before any other request.
  43. // The setup request should never be retried.
  44. request_access::set_priority(cfg.setup, true);
  45. cfg.setup.get_config().cancel_if_unresponded = true;
  46. cfg.setup.get_config().cancel_on_connection_lost = true;
  47. }
  48. } // namespace boost::redis::detail
  49. #endif // BOOST_REDIS_RUNNER_HPP