xbmc
URL.h
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 
9 #pragma once
10 
11 #include "utils/UrlOptions.h"
12 
13 #include <stdlib.h>
14 #include <string>
15 
16 #ifdef TARGET_WINDOWS
17 #undef SetPort // WIN32INCLUDES this is defined as SetPortA in WinSpool.h which is being included _somewhere_
18 #endif
19 
20 class CURL
21 {
22 public:
23  explicit CURL(const std::string& strURL)
24  {
25  Parse(strURL);
26  }
27 
28  CURL() = default;
29  virtual ~CURL(void);
30 
31  // explicit equals operator for std::string comparison
32  bool operator==(const std::string &url) const { return Get() == url; }
33 
34  void Reset();
35  void Parse(const std::string& strURL);
36  void SetFileName(const std::string& strFileName);
37  void SetHostName(const std::string& strHostName)
38  {
39  m_strHostName = strHostName;
40  }
41 
42  void SetUserName(const std::string& strUserName)
43  {
44  m_strUserName = strUserName;
45  }
46 
47  void SetDomain(const std::string& strDomain)
48  {
49  m_strDomain = strDomain;
50  }
51 
52  void SetPassword(const std::string& strPassword)
53  {
54  m_strPassword = strPassword;
55  }
56 
57  void SetProtocol(const std::string& strProtocol);
58  void SetOptions(const std::string& strOptions);
59  void SetProtocolOptions(const std::string& strOptions);
60  void SetPort(int port)
61  {
62  m_iPort = port;
63  }
64 
65  bool HasPort() const
66  {
67  return (m_iPort != 0);
68  }
69 
70  int GetPort() const
71  {
72  return m_iPort;
73  }
74 
75  const std::string& GetHostName() const
76  {
77  return m_strHostName;
78  }
79 
80  const std::string& GetDomain() const
81  {
82  return m_strDomain;
83  }
84 
85  const std::string& GetUserName() const
86  {
87  return m_strUserName;
88  }
89 
90  const std::string& GetPassWord() const
91  {
92  return m_strPassword;
93  }
94 
95  const std::string& GetFileName() const
96  {
97  return m_strFileName;
98  }
99 
100  const std::string& GetProtocol() const
101  {
102  return m_strProtocol;
103  }
104 
105  const std::string GetTranslatedProtocol() const;
106 
107  const std::string& GetFileType() const
108  {
109  return m_strFileType;
110  }
111 
112  const std::string& GetShareName() const
113  {
114  return m_strShareName;
115  }
116 
117  const std::string& GetOptions() const
118  {
119  return m_strOptions;
120  }
121 
122  const std::string& GetProtocolOptions() const
123  {
124  return m_strProtocolOptions;
125  }
126 
127  const std::string GetFileNameWithoutPath() const; /* return the filename excluding path */
128 
129  char GetDirectorySeparator() const;
130 
131  std::string Get() const;
132  std::string GetWithoutOptions() const;
133  std::string GetWithoutUserDetails(bool redact = false) const;
134  std::string GetWithoutFilename() const;
135  std::string GetRedacted() const;
136  static std::string GetRedacted(const std::string& path);
137  bool IsLocal() const;
138  bool IsLocalHost() const;
139  static bool IsFileOnly(const std::string &url);
140  static bool IsFullPath(const std::string &url);
141  static std::string Decode(const std::string& strURLData);
142  static std::string Encode(const std::string& strURLData);
143 
149  bool IsProtocol(const char *type) const
150  {
151  return IsProtocolEqual(m_strProtocol, type);
152  }
153 
161  static bool IsProtocolEqual(const std::string& protocol, const char *type);
162 
169  bool IsFileType(const char *type) const
170  {
171  return m_strFileType == type;
172  }
173 
174  void GetOptions(std::map<std::string, std::string> &options) const;
175  bool HasOption(const std::string &key) const;
176  bool GetOption(const std::string &key, std::string &value) const;
177  std::string GetOption(const std::string &key) const;
178  void SetOption(const std::string &key, const std::string &value);
179  void RemoveOption(const std::string &key);
180 
181  void GetProtocolOptions(std::map<std::string, std::string> &options) const;
182  bool HasProtocolOption(const std::string &key) const;
183  bool GetProtocolOption(const std::string &key, std::string &value) const;
184  std::string GetProtocolOption(const std::string &key) const;
185  void SetProtocolOption(const std::string &key, const std::string &value);
186  void RemoveProtocolOption(const std::string &key);
187 
188 protected:
189  int m_iPort = 0;
190  std::string m_strHostName;
191  std::string m_strShareName;
192  std::string m_strDomain;
193  std::string m_strUserName;
194  std::string m_strPassword;
195  std::string m_strFileName;
196  std::string m_strProtocol;
197  std::string m_strFileType;
198  std::string m_strOptions;
199  std::string m_strProtocolOptions;
200  CUrlOptions m_options;
201  CUrlOptions m_protocolOptions;
202 };
static bool IsProtocolEqual(const std::string &protocol, const char *type)
Check whether a URL protocol is a given URL scheme. Both parameters MUST be lower-case. Typically this would be called using the result of TranslateProtocol() which enforces this for protocol.
Definition: URL.cpp:707
static std::string Encode(const std::string &strURLData)
Definition: URL.cpp:685
Definition: URL.h:20
static bool IsFullPath(const std::string &url)
return true if the url includes the full path
Definition: URL.cpp:638
static bool IsFileOnly(const std::string &url)
return true if there are no directories in the url.
Definition: URL.cpp:633
void Parse(const std::string &strURL)
Definition: URL.cpp:47
bool IsProtocol(const char *type) const
Check whether a URL is a given URL scheme. Comparison is case-insensitive as per RFC1738.
Definition: URL.h:149
Definition: UrlOptions.h:16
bool IsFileType(const char *type) const
Check whether a URL is a given filetype. Comparison is effectively case-insensitive as both the param...
Definition: URL.h:169