/install/xbt/linux/misc/bt_tracker_url.cpp

http://torrentpier2.googlecode.com/ · C++ · 82 lines · 76 code · 6 blank · 0 comment · 15 complexity · a4608d9ae1472c6c69374c5ba5335314 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "bt_tracker_url.h"
  3. #include <boost/algorithm/string.hpp>
  4. Cbt_tracker_url::Cbt_tracker_url()
  5. {
  6. }
  7. Cbt_tracker_url::Cbt_tracker_url(const std::string& v)
  8. {
  9. write(v);
  10. }
  11. void Cbt_tracker_url::clear()
  12. {
  13. m_protocol = tp_unknown;
  14. m_host.erase();
  15. m_port = 0;
  16. m_path.erase();
  17. }
  18. bool Cbt_tracker_url::valid() const
  19. {
  20. switch (m_protocol)
  21. {
  22. case tp_http:
  23. if (m_path.empty() || m_path[0] != '/')
  24. return false;
  25. case tp_udp:
  26. return !m_host.empty()
  27. && m_port >= 0 && m_port < 0x10000;
  28. }
  29. return false;
  30. }
  31. void Cbt_tracker_url::write(const std::string& v)
  32. {
  33. clear();
  34. size_t a;
  35. int protocol;
  36. int port;
  37. if (boost::istarts_with(v, "http://"))
  38. {
  39. a = 7;
  40. protocol = tp_http;
  41. port = 80;
  42. }
  43. else if (boost::istarts_with(v, "udp://"))
  44. {
  45. a = 6;
  46. protocol = tp_udp;
  47. port = 2710;
  48. }
  49. else
  50. return;
  51. size_t b = v.find_first_of("/:", a);
  52. std::string host;
  53. if (b == std::string::npos)
  54. host = v.substr(a);
  55. else
  56. {
  57. host = v.substr(a, b - a);
  58. if (v[b] == '/')
  59. m_path = v.substr(b);
  60. else
  61. {
  62. b++;
  63. a = v.find('/', b);
  64. if (a == std::string::npos)
  65. port = atoi(v.substr(b).c_str());
  66. else
  67. {
  68. port = atoi(v.substr(b, a - b).c_str());
  69. m_path = v.substr(a);
  70. }
  71. }
  72. }
  73. m_protocol = protocol;
  74. m_host = host;
  75. m_port = port;
  76. }