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 #include <string_view>
16 
17 #ifdef TARGET_WINDOWS
18 #undef SetPort // WIN32INCLUDES this is defined as SetPortA in WinSpool.h which is being included _somewhere_
19 #endif
20 
21 class CURL
22 {
23 public:
24  explicit CURL(std::string strURL) { Parse(std::move(strURL)); }
25 
26  CURL() = default;
27  virtual ~CURL(void);
28 
29  // explicit equals operator for std::string comparison
30  bool operator==(const std::string &url) const { return Get() == url; }
31 
32  void Reset();
33  void Parse(std::string strURL);
34  void SetFileName(std::string strFileName);
35  void SetHostName(std::string strHostName) { m_strHostName = std::move(strHostName); }
36 
37  void SetUserName(std::string strUserName) { m_strUserName = std::move(strUserName); }
38 
39  void SetDomain(std::string strDomain) { m_strDomain = std::move(strDomain); }
40 
41  void SetPassword(std::string strPassword) { m_strPassword = std::move(strPassword); }
42 
43  void SetProtocol(std::string strProtocol);
44  void SetOptions(std::string strOptions);
45  void SetProtocolOptions(std::string strOptions);
46  void SetPort(int port)
47  {
48  m_iPort = port;
49  }
50 
51  bool HasPort() const
52  {
53  return (m_iPort != 0);
54  }
55 
56  int GetPort() const
57  {
58  return m_iPort;
59  }
60 
61  const std::string& GetHostName() const
62  {
63  return m_strHostName;
64  }
65 
66  const std::string& GetDomain() const
67  {
68  return m_strDomain;
69  }
70 
71  const std::string& GetUserName() const
72  {
73  return m_strUserName;
74  }
75 
76  const std::string& GetPassWord() const
77  {
78  return m_strPassword;
79  }
80 
81  const std::string& GetFileName() const
82  {
83  return m_strFileName;
84  }
85 
86  const std::string& GetProtocol() const
87  {
88  return m_strProtocol;
89  }
90 
91  std::string GetTranslatedProtocol() const;
92 
93  const std::string& GetFileType() const
94  {
95  return m_strFileType;
96  }
97 
98  const std::string& GetShareName() const
99  {
100  return m_strShareName;
101  }
102 
103  const std::string& GetOptions() const
104  {
105  return m_strOptions;
106  }
107 
108  const std::string& GetProtocolOptions() const
109  {
110  return m_strProtocolOptions;
111  }
112 
113  std::string GetFileNameWithoutPath() const; /* return the filename excluding path */
114 
115  char GetDirectorySeparator() const;
116 
117  std::string Get() const;
118  std::string GetWithoutOptions() const;
119  std::string GetWithoutUserDetails(bool redact = false) const;
120  std::string GetWithoutFilename() const;
121  std::string GetRedacted() const;
122  static std::string GetRedacted(std::string path);
123  bool IsLocal() const;
124  bool IsLocalHost() const;
125  static bool IsFileOnly(const std::string &url);
126  static bool IsFullPath(const std::string &url);
127  static std::string Decode(std::string_view strURLData);
128  static std::string Encode(std::string_view strURLData);
129 
135  bool IsProtocol(const char *type) const
136  {
137  return IsProtocolEqual(m_strProtocol, type);
138  }
139 
147  static bool IsProtocolEqual(const std::string& protocol, const char *type);
148 
155  bool IsFileType(const char *type) const
156  {
157  return m_strFileType == type;
158  }
159 
160  void GetOptions(std::map<std::string, std::string> &options) const;
161  bool HasOption(const std::string &key) const;
162  bool GetOption(const std::string &key, std::string &value) const;
163  std::string GetOption(const std::string &key) const;
164  void SetOption(const std::string &key, const std::string &value);
165  void RemoveOption(const std::string &key);
166 
167  void GetProtocolOptions(std::map<std::string, std::string> &options) const;
168  bool HasProtocolOption(const std::string &key) const;
169  bool GetProtocolOption(const std::string &key, std::string &value) const;
170  std::string GetProtocolOption(const std::string &key) const;
171  void SetProtocolOption(const std::string &key, const std::string &value);
172  void RemoveProtocolOption(const std::string &key);
173 
174 protected:
175  int m_iPort = 0;
176  std::string m_strHostName;
177  std::string m_strShareName;
178  std::string m_strDomain;
179  std::string m_strUserName;
180  std::string m_strPassword;
181  std::string m_strFileName;
182  std::string m_strProtocol;
183  std::string m_strFileType;
184  std::string m_strOptions;
185  std::string m_strProtocolOptions;
186  CUrlOptions m_options;
187  CUrlOptions m_protocolOptions;
188 };
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:711
Definition: URL.h:21
static bool IsFullPath(const std::string &url)
return true if the url includes the full path
Definition: URL.cpp:643
static bool IsFileOnly(const std::string &url)
return true if there are no directories in the url.
Definition: URL.cpp:638
void Parse(std::string strURL)
Definition: URL.cpp:52
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:135
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:155
static std::string Encode(std::string_view strURLData)
Definition: URL.cpp:690