/xbmc/URL.h

http://github.com/xbmc/xbmc · C Header · 202 lines · 142 code · 34 blank · 26 comment · 3 complexity · d3df16328e2675a26ade0bdcefec6405 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #pragma once
  9. #include "utils/UrlOptions.h"
  10. #include <stdlib.h>
  11. #include <string>
  12. #ifdef TARGET_WINDOWS
  13. #undef SetPort // WIN32INCLUDES this is defined as SetPortA in WinSpool.h which is being included _somewhere_
  14. #endif
  15. class CURL
  16. {
  17. public:
  18. explicit CURL(const std::string& strURL)
  19. {
  20. Parse(strURL);
  21. }
  22. CURL() = default;
  23. virtual ~CURL(void);
  24. // explicit equals operator for std::string comparison
  25. bool operator==(const std::string &url) const { return Get() == url; }
  26. void Reset();
  27. void Parse(const std::string& strURL);
  28. void SetFileName(const std::string& strFileName);
  29. void SetHostName(const std::string& strHostName)
  30. {
  31. m_strHostName = strHostName;
  32. }
  33. void SetUserName(const std::string& strUserName)
  34. {
  35. m_strUserName = strUserName;
  36. }
  37. void SetDomain(const std::string& strDomain)
  38. {
  39. m_strDomain = strDomain;
  40. }
  41. void SetPassword(const std::string& strPassword)
  42. {
  43. m_strPassword = strPassword;
  44. }
  45. void SetProtocol(const std::string& strProtocol);
  46. void SetOptions(const std::string& strOptions);
  47. void SetProtocolOptions(const std::string& strOptions);
  48. void SetPort(int port)
  49. {
  50. m_iPort = port;
  51. }
  52. bool HasPort() const
  53. {
  54. return (m_iPort != 0);
  55. }
  56. int GetPort() const
  57. {
  58. return m_iPort;
  59. }
  60. const std::string& GetHostName() const
  61. {
  62. return m_strHostName;
  63. }
  64. const std::string& GetDomain() const
  65. {
  66. return m_strDomain;
  67. }
  68. const std::string& GetUserName() const
  69. {
  70. return m_strUserName;
  71. }
  72. const std::string& GetPassWord() const
  73. {
  74. return m_strPassword;
  75. }
  76. const std::string& GetFileName() const
  77. {
  78. return m_strFileName;
  79. }
  80. const std::string& GetProtocol() const
  81. {
  82. return m_strProtocol;
  83. }
  84. const std::string GetTranslatedProtocol() const;
  85. const std::string& GetFileType() const
  86. {
  87. return m_strFileType;
  88. }
  89. const std::string& GetShareName() const
  90. {
  91. return m_strShareName;
  92. }
  93. const std::string& GetOptions() const
  94. {
  95. return m_strOptions;
  96. }
  97. const std::string& GetProtocolOptions() const
  98. {
  99. return m_strProtocolOptions;
  100. }
  101. const std::string GetFileNameWithoutPath() const; /* return the filename excluding path */
  102. char GetDirectorySeparator() const;
  103. std::string Get() const;
  104. std::string GetWithoutOptions() const;
  105. std::string GetWithoutUserDetails(bool redact = false) const;
  106. std::string GetWithoutFilename() const;
  107. std::string GetRedacted() const;
  108. static std::string GetRedacted(const std::string& path);
  109. bool IsLocal() const;
  110. bool IsLocalHost() const;
  111. static bool IsFileOnly(const std::string &url); ///< return true if there are no directories in the url.
  112. static bool IsFullPath(const std::string &url); ///< return true if the url includes the full path
  113. static std::string Decode(const std::string& strURLData);
  114. static std::string Encode(const std::string& strURLData);
  115. /*! \brief Check whether a URL is a given URL scheme.
  116. Comparison is case-insensitive as per RFC1738
  117. \param type a lower-case scheme name, e.g. "smb".
  118. \return true if the url is of the given scheme, false otherwise.
  119. */
  120. bool IsProtocol(const char *type) const
  121. {
  122. return IsProtocolEqual(m_strProtocol, type);
  123. }
  124. /*! \brief Check whether a URL protocol is a given URL scheme.
  125. Both parameters MUST be lower-case. Typically this would be called using
  126. the result of TranslateProtocol() which enforces this for protocol.
  127. \param protocol a lower-case scheme name, e.g. "ftp"
  128. \param type a lower-case scheme name, e.g. "smb".
  129. \return true if the url is of the given scheme, false otherwise.
  130. */
  131. static bool IsProtocolEqual(const std::string& protocol, const char *type);
  132. /*! \brief Check whether a URL is a given filetype.
  133. Comparison is effectively case-insensitive as both the parameter
  134. and m_strFileType are lower-case.
  135. \param type a lower-case filetype, e.g. "mp3".
  136. \return true if the url is of the given filetype, false otherwise.
  137. */
  138. bool IsFileType(const char *type) const
  139. {
  140. return m_strFileType == type;
  141. }
  142. void GetOptions(std::map<std::string, std::string> &options) const;
  143. bool HasOption(const std::string &key) const;
  144. bool GetOption(const std::string &key, std::string &value) const;
  145. std::string GetOption(const std::string &key) const;
  146. void SetOption(const std::string &key, const std::string &value);
  147. void RemoveOption(const std::string &key);
  148. void GetProtocolOptions(std::map<std::string, std::string> &options) const;
  149. bool HasProtocolOption(const std::string &key) const;
  150. bool GetProtocolOption(const std::string &key, std::string &value) const;
  151. std::string GetProtocolOption(const std::string &key) const;
  152. void SetProtocolOption(const std::string &key, const std::string &value);
  153. void RemoveProtocolOption(const std::string &key);
  154. protected:
  155. int m_iPort = 0;
  156. std::string m_strHostName;
  157. std::string m_strShareName;
  158. std::string m_strDomain;
  159. std::string m_strUserName;
  160. std::string m_strPassword;
  161. std::string m_strFileName;
  162. std::string m_strProtocol;
  163. std::string m_strFileType;
  164. std::string m_strOptions;
  165. std::string m_strProtocolOptions;
  166. CUrlOptions m_options;
  167. CUrlOptions m_protocolOptions;
  168. };