You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "serverConf.h"
  2. #include <cstdlib>
  3. namespace mtx {
  4. bool parseDurationMs(const std::string & s, int64_t & ms)
  5. {
  6. if (s.empty())
  7. {
  8. return false;
  9. }
  10. char * tEnd = nullptr;
  11. double tV = strtod(s.c_str(), &tEnd);
  12. if (tEnd == s.c_str() || tV < 0)
  13. {
  14. return false;
  15. }
  16. std::string tUnit(tEnd);
  17. double tMul;
  18. if (tUnit.empty() || tUnit == "s")
  19. {
  20. tMul = 1000.0;
  21. }
  22. else if (tUnit == "ms")
  23. {
  24. tMul = 1.0;
  25. }
  26. else if (tUnit == "m")
  27. {
  28. tMul = 60000.0;
  29. }
  30. else if (tUnit == "h")
  31. {
  32. tMul = 3600000.0;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. ms = (int64_t) (tV * tMul + 0.5);
  39. return true;
  40. }
  41. bool splitHostPort(const std::string & addr, std::string & host, uint16_t & port)
  42. {
  43. size_t tColon = addr.rfind(':');
  44. if (tColon == std::string::npos)
  45. {
  46. return false;
  47. }
  48. host = addr.substr(0, tColon);
  49. int tPort = atoi(addr.c_str() + tColon + 1);
  50. if (tPort <= 0 || tPort > 65535)
  51. {
  52. return false;
  53. }
  54. port = (uint16_t) tPort;
  55. if (host.size() >= 2 && host.front() == '[' && host.back() == ']')
  56. {
  57. host = host.substr(1, host.size() - 2);
  58. }
  59. return true;
  60. }
  61. std::string hideCredentials(const std::string & url)
  62. {
  63. size_t tScheme = url.find("://");
  64. if (tScheme == std::string::npos)
  65. {
  66. return url;
  67. }
  68. size_t tHostStart = tScheme + 3;
  69. size_t tSlash = url.find('/', tHostStart);
  70. size_t tAt = url.rfind('@', tSlash == std::string::npos ? url.size() : tSlash);
  71. if (tAt == std::string::npos || tAt < tHostStart)
  72. {
  73. return url;
  74. }
  75. return url.substr(0, tHostStart) + url.substr(tAt + 1);
  76. }
  77. } // namespace mtx