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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
#include "libfilezilla/uri.hpp"
#include "libfilezilla/encode.hpp"
#include "libfilezilla/iputils.hpp"
namespace fz {
namespace uri_chars {
constexpr std::string_view alpha{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" };
constexpr std::string_view digit{ "01234567890" };
constexpr std::string_view scheme{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-." };
}
uri::uri(std::string_view const& in, uri_parsing_flags flags)
{
if (!parse(in, flags)) {
clear();
}
}
void uri::clear()
{
*this = uri();
}
bool uri::parse(std::string_view in, uri_parsing_flags flags)
{
// Look for fragment
size_t pos = in.find('#');
if (pos != std::string::npos) {
fragment_ = in.substr(pos + 1);
in = in.substr(0, pos);
}
// Look for query
pos = in.find('?');
if (pos != std::string::npos) {
query_ = in.substr(pos + 1);
in = in.substr(0, pos);
}
// Do we have a scheme?
if (!in.empty() && uri_chars::alpha.find(in[0]) != std::string::npos) {
size_t scheme_delim = in.find_first_not_of(uri_chars::scheme, 1);
if (scheme_delim != std::string::npos && in[scheme_delim] == ':') {
scheme_ = in.substr(0, scheme_delim);
in = in.substr(scheme_delim + 1);
}
}
// Do we have authority?
if (!in.empty()) {
bool has_authority{};
if (in.size() >= 2 && in[0] == '/' && in[1] == '/') {
in = in.substr(2);
has_authority = true;
}
else if (flags == uri_parsing_flags::assume_authority) {
has_authority = true;
}
if (has_authority) {
size_t auth_delim = in.find('/');
std::string_view authority;
if (auth_delim != std::string::npos) {
authority = in.substr(0, auth_delim);
in = in.substr(auth_delim);
}
else {
authority = in;
in = std::string_view();
}
if (!parse_authority(authority)) {
return false;
}
}
}
if (!in.empty()) {
path_ = percent_decode_s(in);
if (path_.empty()) {
return false;
}
}
return true;
}
bool uri::parse_authority(std::string_view authority)
{
// Do we have userinfo?
size_t pos = authority.find('@');
if (pos != std::string::npos) {
std::string_view userinfo = authority.substr(0, pos);
authority = authority.substr(pos + 1);
pos = userinfo.find(':');
if (pos != std::string::npos) { // Slight inaccuracy: Empty password isn't handled well
user_ = percent_decode_s(userinfo.substr(0, pos));
if (user_.empty() && pos != 0) {
return false;
}
pass_ = percent_decode_s(userinfo.substr(pos + 1));
if (pass_.empty() && pos + 1 != userinfo.size()) {
return false;
}
}
else {
user_ = userinfo;
}
}
// Do we have port?
pos = authority.rfind(':');
if (pos != std::string::npos) {
if (authority.find_first_not_of(uri_chars::digit, pos + 1) == std::string::npos) {
port_ = to_integral<unsigned short>(authority.substr(pos + 1), 0);
if (!port_) {
return false;
}
authority = authority.substr(0, pos);
}
}
if (!authority.empty() && authority[0] == '[') {
if (authority.back() != ']') {
return false;
}
if (get_address_type(authority) != address_type::ipv6) {
return false;
}
}
host_ = percent_decode_s(authority);
if (host_.empty() && !authority.empty()) {
return false;
}
return true;
}
std::string uri::to_string(bool with_query) const
{
std::string ret;
if (!scheme_.empty()) {
ret += scheme_ + ":";
}
if (!host_.empty()) {
ret += "//";
ret += get_authority(true);
}
ret += percent_encode(path_, true);
if (with_query) {
if (!query_.empty()) {
ret += "?" + query_;
}
if (!fragment_.empty()) {
ret += "#" + fragment_;
}
}
return ret;
}
std::string uri::get_request(bool with_query) const
{
std::string ret = percent_encode(path_, true);
if (!ret.empty() && !query_.empty() && with_query) {
ret += "?";
ret += query_;
}
return ret;
}
std::string uri::get_authority(bool with_userinfo) const
{
std::string ret;
if (!host_.empty()) {
if (with_userinfo) {
ret += percent_encode(user_);
if (!pass_.empty()) {
ret += ":";
ret += percent_encode(pass_);
}
if (!user_.empty() || !pass_.empty()) {
ret += "@";
}
}
ret += percent_encode(host_);
if (port_ != 0) {
ret += ":";
ret += fz::to_string(port_);
}
}
return ret;
}
void uri::resolve(uri const& base)
{
if (!scheme_.empty() && scheme_ != base.scheme_) {
return;
}
scheme_ = base.scheme_;
if (!host_.empty()) {
return;
}
host_ = base.host_;
port_ = base.port_;
user_ = base.user_;
pass_ = base.pass_;
if (path_.empty()) {
path_ = base.path_;
if (query_.empty()) {
query_ = base.query_;
}
}
else {
if (path_[0] != '/') {
if (base.path_.empty() && !base.host_.empty()) {
path_ = "/" + path_;
}
else {
size_t pos = base.path_.rfind('/');
if (pos != std::string::npos) {
path_ = base.path_.substr(0, pos) + path_;
}
}
}
}
}
bool uri::empty() const
{
return host_.empty() && path_.empty();
}
bool uri::operator==(uri const& arg) const
{
return std::tie(scheme_, user_, pass_, host_, port_, path_, query_, fragment_) == std::tie(arg.scheme_, arg.user_, arg.pass_, arg.host_, arg.port_, arg.path_, arg.query_, arg.fragment_);
}
query_string::query_string(std::string_view const& raw, bool plus_is_space)
{
set(raw, plus_is_space);
}
query_string::query_string(std::pair<std::string, std::string> const& segment)
{
segments_[segment.first] = segment.second;
}
query_string::query_string(std::initializer_list<std::pair<std::string, std::string>> const& segments)
{
for (auto const& segment : segments) {
if (!segment.first.empty()) {
segments_[segment.first] = segment.second;
}
}
}
bool query_string::set(std::string_view const& raw, bool plus_is_space)
{
segments_.clear();
auto const tokens = strtok_view(raw, "&");
for (auto const& token : tokens) {
size_t pos = token.find('=');
if (!pos) {
segments_.clear();
return false;
}
std::string key = percent_decode_s(token.substr(0, pos), false, plus_is_space);
if (key.empty()) {
segments_.clear();
return false;
}
std::string value;
if (pos != std::string::npos) {
value = percent_decode_s(token.substr(pos + 1), false, plus_is_space);
if (value.empty() && pos + 1 != token.size()) {
segments_.clear();
return false;
}
}
segments_[key] = value;
}
return true;
}
std::string& query_string::operator[](std::string const& key)
{
return segments_[key];
}
void query_string::remove(std::string const& key)
{
auto it = segments_.find(key);
if (it != segments_.end()) {
segments_.erase(key);
}
}
std::string query_string::to_string(bool encode_slashes) const
{
std::string ret;
if (!segments_.empty()) {
for (auto const& segment : segments_) {
ret += percent_encode(segment.first, !encode_slashes);
ret += '=';
ret += percent_encode(segment.second, !encode_slashes);
ret += '&';
}
ret.pop_back();
}
return ret;
}
}
|