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
|
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// uri.cpp
//
// URI utility
//
#include "uri.h"
#include "sinsp.h"
#include <sstream>
#include <iomanip>
const std::string uri::SPECIAL_CHARS = "!#$&'()*+,/:;=?@[]";
const std::string uri::AMBIGUOUS_CHARS = " \"%-.<>\\^_`{|}~";
uri::uri(std::string str)
{
trim(str);
// parser does not handle missing host properly
bool no_host = false;
if(ci_find_substr(str, std::string("file:///")) == 0)
{
str.insert(7, "localhost");
no_host = true;
}
parsed_uri p_uri = parse_uri(str.c_str());
if(p_uri.error)
{
str = std::string("Invalid URI: [").append(str).append(1, ']');
throw sinsp_exception(str);
}
m_scheme = str.substr(p_uri.scheme_start, p_uri.scheme_end - p_uri.scheme_start);
std::transform(m_scheme.begin(), m_scheme.end(), m_scheme.begin(), ::tolower);
if(!no_host)
{
m_host = str.substr(p_uri.host_start, p_uri.host_end - p_uri.host_start);
std::transform(m_host.begin(), m_host.end(), m_host.begin(), ::tolower);
}
m_port = p_uri.port;
if(m_port == 0)
{
m_has_port = false;
m_port = get_well_known_port();
}
m_path = str.substr(p_uri.path_start, p_uri.path_end - p_uri.path_start);
m_query = str.substr(p_uri.query_start, p_uri.query_end - p_uri.query_start);
if(p_uri.user_info_end != p_uri.user_info_start)
{
std::string auth = str.substr(p_uri.user_info_start, p_uri.user_info_end - p_uri.user_info_start);
std::string::size_type pos = auth.find(':');
if(pos == std::string::npos)
{
throw sinsp_exception("Invalid credentials format.");
}
m_user = auth.substr(0, pos);
m_password = auth.substr(pos + 1);
}
}
void uri::check(std::string str)
{
trim(str);
// parser does not handle missing host properly
if(ci_find_substr(str, std::string("file:///")) == 0)
{
str.insert(7, "localhost");
}
parsed_uri p_uri = parse_uri(str.c_str());
if(p_uri.error)
{
str = std::string("Invalid URI: [").append(str).append(1, ']');
throw sinsp_exception(str);
}
if(p_uri.user_info_end != p_uri.user_info_start)
{
std::string auth = str.substr(p_uri.user_info_start, p_uri.user_info_end - p_uri.user_info_start);
std::string::size_type pos = auth.find(':');
if(pos == std::string::npos)
{
throw sinsp_exception("Invalid credentials format.");
}
}
}
int uri::get_well_known_port() const
{
if (!m_scheme.empty())
{
if(m_scheme == "http") { return 80; }
else if(m_scheme == "file") { return 0; }
else if(m_scheme == "https") { return 443; }
else if(m_scheme == "ftp") { return 21; }
else if(m_scheme == "ssh") { return 22; }
else if(m_scheme == "telnet") { return 23; }
else if(m_scheme == "nntp") { return 119; }
else if(m_scheme == "ldap") { return 389; }
else if(m_scheme == "rtsp") { return 554; }
else if(m_scheme == "sip") { return 5060; }
else if(m_scheme == "sips") { return 5061; }
else if(m_scheme == "xmpp") { return 5222; }
}
return 0;
}
void uri::set_path(const std::string& path)
{
uri u(*this);
u.m_path = path;
parsed_uri p_uri = parse_uri(u.to_string().c_str());
if(p_uri.error)
{
throw sinsp_exception(std::string("Invalid URI Path: [").append(path).append(1, ']'));
}
m_path = path;
}
std::string uri::to_string(bool show_creds) const
{
std::ostringstream ostr;
ostr << m_scheme << "://";
if(!m_user.empty())
{
if(show_creds)
{
ostr << m_user << ':' << m_password << '@';
}
else
{
ostr << "***:***@";
}
}
ostr << m_host;
if(m_port && m_has_port)
{
ostr << ':' << m_port;
}
ostr << m_path;
if(!m_query.empty())
{
ostr << '?' << m_query;
}
return ostr.str();
}
std::string uri::encode(const std::string& str, const std::string& reserved)
{
std::string encoded_str;
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
{
char c = *it;
if((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))
{
encoded_str += c;
}
else if (c <= 0x20 || c >= 0x7F ||
SPECIAL_CHARS.find(c) != std::string::npos ||
AMBIGUOUS_CHARS.find(c) != std::string::npos ||
reserved.find(c) != std::string::npos)
{
std::ostringstream ostr;
ostr << "%" << std::setfill('0') << std::setw(2) << std::uppercase << std::hex << ((unsigned) (unsigned char) c);
encoded_str.append(ostr.str());
}
else
{
encoded_str += c;
}
}
return encoded_str;
}
// URI-decodes the given string by replacing percent-encoded
// characters with the actual character. Returns the decoded string.
//
// When plus_as_space is true, non-encoded plus signs in the query are decoded as spaces.
// (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1)
std::string uri::decode(const std::string& str, bool plus_as_space)
{
std::string decoded_str;
bool in_query = false;
std::string::const_iterator it = str.begin();
std::string::const_iterator end = str.end();
while(it != end)
{
char c = *it++;
if(c == '?')
{
in_query = true;
}
// spaces may be encoded as plus signs in the query
if(in_query && plus_as_space && c == '+')
{
c = ' ';
}
else if(c == '%')
{
if (it == end)
{
throw sinsp_exception("URI encoding: no hex digit following percent sign in " + str);
}
char hi = *it++;
if (it == end)
{
throw sinsp_exception("URI encoding: two hex digits must follow percent sign in " + str);
}
char lo = *it++;
if (hi >= '0' && hi <= '9')
{
c = hi - '0';
}
else if (hi >= 'A' && hi <= 'F')
{
c = hi - 'A' + 10;
}
else if (hi >= 'a' && hi <= 'f')
{
c = hi - 'a' + 10;
}
else
{
throw sinsp_exception("URI encoding: not a hex digit found in " + str);
}
c *= 16;
if (lo >= '0' && lo <= '9')
{
c += lo - '0';
}
else if (lo >= 'A' && lo <= 'F')
{
c += lo - 'A' + 10;
}
else if (lo >= 'a' && lo <= 'f')
{
c += lo - 'a' + 10;
}
else
{
throw sinsp_exception("URI encoding: not a hex digit");
}
}
decoded_str += c;
}
return decoded_str;
}
bool uri::is(const std::string& proto)
{
return ci_compare::is_equal(m_scheme, proto);
}
|