aboutsummaryrefslogtreecommitdiff
path: root/xbmc/URL.h
blob: ae84335cc74ff3348bedd39c94fa48b407259e5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "utils/UrlOptions.h"

#include <stdlib.h>
#include <string>
#include <string_view>

#ifdef TARGET_WINDOWS
#undef SetPort // WIN32INCLUDES this is defined as SetPortA in WinSpool.h which is being included _somewhere_
#endif

class CURL
{
public:
  explicit CURL(std::string strURL) { Parse(std::move(strURL)); }

  CURL() = default;
  virtual ~CURL(void);

  // explicit equals operator for std::string comparison
  bool operator==(const std::string &url) const { return Get() == url; }

  void Reset();
  void Parse(std::string strURL);
  void SetFileName(std::string strFileName);
  void SetHostName(std::string strHostName) { m_strHostName = std::move(strHostName); }

  void SetUserName(std::string strUserName) { m_strUserName = std::move(strUserName); }

  void SetDomain(std::string strDomain) { m_strDomain = std::move(strDomain); }

  void SetPassword(std::string strPassword) { m_strPassword = std::move(strPassword); }

  void SetProtocol(std::string strProtocol);
  void SetOptions(std::string strOptions);
  void SetProtocolOptions(std::string strOptions);
  void SetPort(int port)
  {
    m_iPort = port;
  }

  bool HasPort() const
  {
    return (m_iPort != 0);
  }

  int GetPort() const
  {
    return m_iPort;
  }

  const std::string& GetHostName() const
  {
    return m_strHostName;
  }

  const std::string& GetDomain() const
  {
    return m_strDomain;
  }

  const std::string& GetUserName() const
  {
    return m_strUserName;
  }

  const std::string& GetPassWord() const
  {
    return m_strPassword;
  }

  const std::string& GetFileName() const
  {
    return m_strFileName;
  }

  const std::string& GetProtocol() const
  {
    return m_strProtocol;
  }

  std::string GetTranslatedProtocol() const;

  const std::string& GetFileType() const
  {
    return m_strFileType;
  }

  const std::string& GetShareName() const
  {
      return m_strShareName;
  }

  const std::string& GetOptions() const
  {
    return m_strOptions;
  }

  const std::string& GetProtocolOptions() const
  {
    return m_strProtocolOptions;
  }

  std::string GetFileNameWithoutPath() const; /* return the filename excluding path */

  char GetDirectorySeparator() const;

  std::string Get() const;
  std::string GetWithoutOptions() const;
  std::string GetWithoutUserDetails(bool redact = false) const;
  std::string GetWithoutFilename() const;
  std::string GetRedacted() const;
  static std::string GetRedacted(std::string path);
  bool IsLocal() const;
  bool IsLocalHost() const;
  static bool IsFileOnly(const std::string &url); ///< return true if there are no directories in the url.
  static bool IsFullPath(const std::string &url); ///< return true if the url includes the full path
  static std::string Decode(std::string_view strURLData);
  static std::string Encode(std::string_view strURLData);

  /*! \brief Check whether a URL is a given URL scheme.
   Comparison is case-insensitive as per RFC1738
   \param type a lower-case scheme name, e.g. "smb".
   \return true if the url is of the given scheme, false otherwise.
   */
  bool IsProtocol(const char *type) const
  {
    return IsProtocolEqual(m_strProtocol, type);
  }

  /*! \brief 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.
   \param protocol a lower-case scheme name, e.g. "ftp"
   \param type a lower-case scheme name, e.g. "smb".
   \return true if the url is of the given scheme, false otherwise.
   */
  static bool IsProtocolEqual(const std::string& protocol, const char *type);

  /*! \brief Check whether a URL is a given filetype.
   Comparison is effectively case-insensitive as both the parameter
   and m_strFileType are lower-case.
   \param type a lower-case filetype, e.g. "mp3".
   \return true if the url is of the given filetype, false otherwise.
   */
  bool IsFileType(const char *type) const
  {
    return m_strFileType == type;
  }

  void GetOptions(std::map<std::string, std::string> &options) const;
  bool HasOption(const std::string &key) const;
  bool GetOption(const std::string &key, std::string &value) const;
  std::string GetOption(const std::string &key) const;
  void SetOption(const std::string &key, const std::string &value);
  void RemoveOption(const std::string &key);

  void GetProtocolOptions(std::map<std::string, std::string> &options) const;
  bool HasProtocolOption(const std::string &key) const;
  bool GetProtocolOption(const std::string &key, std::string &value) const;
  std::string GetProtocolOption(const std::string &key) const;
  void SetProtocolOption(const std::string &key, const std::string &value);
  void RemoveProtocolOption(const std::string &key);

protected:
  int m_iPort = 0;
  std::string m_strHostName;
  std::string m_strShareName;
  std::string m_strDomain;
  std::string m_strUserName;
  std::string m_strPassword;
  std::string m_strFileName;
  std::string m_strProtocol;
  std::string m_strFileType;
  std::string m_strOptions;
  std::string m_strProtocolOptions;
  CUrlOptions m_options;
  CUrlOptions m_protocolOptions;
};