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
|
/*
* Copyright (C) 2012-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.
*/
#include "UrlOptions.h"
#include "URL.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
CUrlOptions::CUrlOptions() = default;
CUrlOptions::CUrlOptions(const std::string &options, const char *strLead /* = "" */)
: m_strLead(strLead)
{
AddOptions(options);
}
CUrlOptions::~CUrlOptions() = default;
std::string CUrlOptions::GetOptionsString(bool withLeadingSeparator /* = false */) const
{
std::string options;
for (const auto &opt : m_options)
{
if (!options.empty())
options += "&";
options += CURL::Encode(opt.first);
if (!opt.second.empty())
options += "=" + CURL::Encode(opt.second.asString());
}
if (withLeadingSeparator && !options.empty())
{
if (m_strLead.empty())
options = "?" + options;
else
options = m_strLead + options;
}
return options;
}
void CUrlOptions::AddOption(const std::string &key, const char *value)
{
if (key.empty() || value == NULL)
return;
return AddOption(key, std::string(value));
}
void CUrlOptions::AddOption(const std::string &key, const std::string &value)
{
if (key.empty())
return;
m_options[key] = value;
}
void CUrlOptions::AddOption(const std::string &key, int value)
{
if (key.empty())
return;
m_options[key] = value;
}
void CUrlOptions::AddOption(const std::string &key, float value)
{
if (key.empty())
return;
m_options[key] = value;
}
void CUrlOptions::AddOption(const std::string &key, double value)
{
if (key.empty())
return;
m_options[key] = value;
}
void CUrlOptions::AddOption(const std::string &key, bool value)
{
if (key.empty())
return;
m_options[key] = value;
}
void CUrlOptions::AddOptions(const std::string &options)
{
if (options.empty())
return;
std::string strOptions = options;
// if matching the preset leading str, remove from options.
if (!m_strLead.empty() && strOptions.compare(0, m_strLead.length(), m_strLead) == 0)
strOptions.erase(0, m_strLead.length());
else if (strOptions.at(0) == '?' || strOptions.at(0) == '#' || strOptions.at(0) == ';' || strOptions.at(0) == '|')
{
// remove leading ?, #, ; or | if present
if (!m_strLead.empty())
CLog::Log(LOGWARNING, "{}: original leading str {} overridden by {}", __FUNCTION__, m_strLead,
strOptions.at(0));
m_strLead = strOptions.at(0);
strOptions.erase(0, 1);
}
// split the options by & and process them one by one
for (const auto &option : StringUtils::Split(strOptions, "&"))
{
if (option.empty())
continue;
std::string key, value;
size_t pos = option.find('=');
key = CURL::Decode(option.substr(0, pos));
if (pos != std::string::npos)
value = CURL::Decode(option.substr(pos + 1));
// the key cannot be empty
if (!key.empty())
AddOption(key, value);
}
}
void CUrlOptions::AddOptions(const CUrlOptions &options)
{
m_options.insert(options.m_options.begin(), options.m_options.end());
}
void CUrlOptions::RemoveOption(const std::string &key)
{
if (key.empty())
return;
auto option = m_options.find(key);
if (option != m_options.end())
m_options.erase(option);
}
bool CUrlOptions::HasOption(const std::string &key) const
{
if (key.empty())
return false;
return m_options.find(key) != m_options.end();
}
bool CUrlOptions::GetOption(const std::string &key, CVariant &value) const
{
if (key.empty())
return false;
auto option = m_options.find(key);
if (option == m_options.end())
return false;
value = option->second;
return true;
}
|