| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
// STL includes |
| 12 |
#include <algorithm> |
| 13 |
#include <map> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
#include "uriparser.hpp" |
| 17 |
|
| 18 |
#ifdef TEST |
| 19 |
#define TEST1 1 |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifdef TEST1 |
| 23 |
#include <iostream> |
| 24 |
#endif |
| 25 |
|
| 26 |
using namespace std; |
| 27 |
|
| 28 |
map<string, UriParser::Type> g_types; |
| 29 |
|
| 30 |
// Map construction using the initializer list is only available starting from C++11. |
| 31 |
// This dummy structure is used instead. |
| 32 |
struct UriParserInit |
| 33 |
{ |
| 34 |
UriParserInit() |
| 35 |
{ |
| 36 |
g_types["file"] = UriParser::FILE; |
| 37 |
g_types["udp"] = UriParser::UDP; |
| 38 |
g_types["tcp"] = UriParser::TCP; |
| 39 |
g_types["srt"] = UriParser::SRT; |
| 40 |
g_types["rtmp"] = UriParser::RTMP; |
| 41 |
g_types["http"] = UriParser::HTTP; |
| 42 |
g_types["rtp"] = UriParser::RTP; |
| 43 |
g_types[""] = UriParser::UNKNOWN; |
| 44 |
} |
| 45 |
} g_uriparser_init; |
| 46 |
|
| 47 |
UriParser::UriParser(const string& strUrl, DefaultExpect exp) |
| 48 |
: m_uriType(UNKNOWN) |
| 49 |
{ |
| 50 |
m_expect = exp; |
| 51 |
Parse(strUrl, exp); |
| 52 |
} |
| 53 |
|
| 54 |
UriParser::~UriParser(void) |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
string UriParser::makeUri() |
| 59 |
{ |
| 60 |
// Reassemble parts into the URI |
| 61 |
string prefix = ""; |
| 62 |
if (m_proto != "") |
| 63 |
{ |
| 64 |
prefix = m_proto + "://"; |
| 65 |
} |
| 66 |
|
| 67 |
std::ostringstream out; |
| 68 |
|
| 69 |
out << prefix << m_host; |
| 70 |
if ((m_port == "" || m_port == "0") && m_expect == EXPECT_FILE) |
| 71 |
{ |
| 72 |
// Do not add port |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
out << ":" << m_port; |
| 77 |
} |
| 78 |
|
| 79 |
if (m_path != "") |
| 80 |
{ |
| 81 |
if (m_path[0] != '/') |
| 82 |
out << "/"; |
| 83 |
out << m_path; |
| 84 |
} |
| 85 |
|
| 86 |
if (!m_mapQuery.empty()) |
| 87 |
{ |
| 88 |
out << "?"; |
| 89 |
|
| 90 |
query_it i = m_mapQuery.begin(); |
| 91 |
for (;;) |
| 92 |
{ |
| 93 |
out << i->first << "=" << i->second; |
| 94 |
++i; |
| 95 |
if (i == m_mapQuery.end()) |
| 96 |
break; |
| 97 |
out << "&"; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
m_origUri = out.str(); |
| 102 |
return m_origUri; |
| 103 |
} |
| 104 |
|
| 105 |
string UriParser::proto(void) const |
| 106 |
{ |
| 107 |
return m_proto; |
| 108 |
} |
| 109 |
|
| 110 |
UriParser::Type UriParser::type() const |
| 111 |
{ |
| 112 |
return m_uriType; |
| 113 |
} |
| 114 |
|
| 115 |
string UriParser::host(void) const |
| 116 |
{ |
| 117 |
return m_host; |
| 118 |
} |
| 119 |
|
| 120 |
string UriParser::port(void) const |
| 121 |
{ |
| 122 |
return m_port; |
| 123 |
} |
| 124 |
|
| 125 |
unsigned short int UriParser::portno(void) const |
| 126 |
{ |
| 127 |
// This returns port in numeric version. Fallback to 0. |
| 128 |
try |
| 129 |
{ |
| 130 |
int i = atoi(m_port.c_str()); |
| 131 |
if ( i <= 0 || i > 65535 ) |
| 132 |
return 0; |
| 133 |
return i; |
| 134 |
} |
| 135 |
catch (...) |
| 136 |
{ |
| 137 |
return 0; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
string UriParser::path(void) const |
| 142 |
{ |
| 143 |
return m_path; |
| 144 |
} |
| 145 |
|
| 146 |
string UriParser::queryValue(const string& strKey) const |
| 147 |
{ |
| 148 |
return m_mapQuery.at(strKey); |
| 149 |
} |
| 150 |
|
| 151 |
// NOTE: handles percent encoded single byte ASCII / Latin-1 characters but not unicode characters and encodings |
| 152 |
|
| 153 |
static string url_decode(const string& str) |
| 154 |
{ |
| 155 |
string ret; |
| 156 |
ret.reserve(str.size()); |
| 157 |
|
| 158 |
size_t end_idx = 0; |
| 159 |
|
| 160 |
for (;;) |
| 161 |
{ |
| 162 |
size_t idx = str.find('%', end_idx); |
| 163 |
if (idx == string::npos) |
| 164 |
break; |
| 165 |
|
| 166 |
if (idx + 2 >= str.size()) // bad percent encoding at the end |
| 167 |
break; |
| 168 |
|
| 169 |
ret += str.substr(end_idx, idx - end_idx); |
| 170 |
|
| 171 |
// make a "string" out of only two characters following % |
| 172 |
char tmp[3] = { str[idx+1], str[idx+2], '\0' }; |
| 173 |
char* endptr = 0; |
| 174 |
unsigned val = strtoul(tmp, &endptr, 16); |
| 175 |
if (endptr != &tmp[2]) |
| 176 |
{ |
| 177 |
// Processing was not correct. Skip these and proceed. |
| 178 |
ret += str[idx]; |
| 179 |
end_idx = idx + 1; |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
ret += char(val); |
| 184 |
end_idx = idx + 3; |
| 185 |
} |
| 186 |
|
| 187 |
// And again search anew since end_idx. |
| 188 |
} |
| 189 |
|
| 190 |
// Copy the rest of the string that wasn't processed. |
| 191 |
ret += str.substr(end_idx, str.size() - end_idx); |
| 192 |
|
| 193 |
return ret; |
| 194 |
} |
| 195 |
|
| 196 |
void UriParser::Parse(const string& strUrl, DefaultExpect exp) |
| 197 |
{ |
| 198 |
int iQueryStart = -1; |
| 199 |
|
| 200 |
size_t idx = strUrl.find("?"); |
| 201 |
if (idx != string::npos) |
| 202 |
{ |
| 203 |
m_host = strUrl.substr(0, idx); |
| 204 |
iQueryStart = (int)(idx + 1); |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
m_host = strUrl; |
| 209 |
} |
| 210 |
|
| 211 |
idx = m_host.find("://"); |
| 212 |
if (idx != string::npos) |
| 213 |
{ |
| 214 |
m_proto = m_host.substr(0, idx); |
| 215 |
transform(m_proto.begin(), m_proto.end(), m_proto.begin(), [](char c){ return tolower(c); }); |
| 216 |
m_host = m_host.substr(idx + 3, m_host.size() - (idx + 3)); |
| 217 |
} |
| 218 |
|
| 219 |
// Handle the IPv6 specification in square brackets. |
| 220 |
// This actually handles anything specified in [] so potentially |
| 221 |
// you can also specify the usual hostname here as well. If the |
| 222 |
// whole host results to have [] at edge positions, they are stripped, |
| 223 |
// otherwise they remain. In both cases the search for the colon |
| 224 |
// separating the port specification starts only after ]. |
| 225 |
const size_t i6pos = m_host.find("["); |
| 226 |
size_t i6end = string::npos; |
| 227 |
|
| 228 |
// Search for the "path" part only behind the closed bracket, |
| 229 |
// if both open and close brackets were found |
| 230 |
size_t path_since = 0; |
| 231 |
if (i6pos != string::npos) |
| 232 |
{ |
| 233 |
i6end = m_host.find("]", i6pos); |
| 234 |
if (i6end != string::npos) |
| 235 |
path_since = i6end; |
| 236 |
} |
| 237 |
|
| 238 |
idx = m_host.find("/", path_since); |
| 239 |
if (idx != string::npos) |
| 240 |
{ |
| 241 |
m_path = m_host.substr(idx, m_host.size() - idx); |
| 242 |
m_host = m_host.substr(0, idx); |
| 243 |
} |
| 244 |
|
| 245 |
// Check special things in the HOST entry. |
| 246 |
size_t atp = m_host.find('@'); |
| 247 |
if (atp != string::npos) |
| 248 |
{ |
| 249 |
string realhost = m_host.substr(atp+1); |
| 250 |
string prehost; |
| 251 |
if ( atp > 0 ) |
| 252 |
{ |
| 253 |
prehost = m_host.substr(0, atp-0); |
| 254 |
size_t colon = prehost.find(':'); |
| 255 |
if ( colon != string::npos ) |
| 256 |
{ |
| 257 |
string pw = prehost.substr(colon+1); |
| 258 |
string user; |
| 259 |
if ( colon > 0 ) |
| 260 |
user = prehost.substr(0, colon-0); |
| 261 |
m_mapQuery["user"] = user; |
| 262 |
m_mapQuery["password"] = pw; |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
m_mapQuery["user"] = prehost; |
| 267 |
} |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
m_mapQuery["multicast"] = "1"; |
| 272 |
} |
| 273 |
m_host = realhost; |
| 274 |
} |
| 275 |
|
| 276 |
bool stripbrackets = false; |
| 277 |
size_t hostend = 0; |
| 278 |
if (i6pos != string::npos) |
| 279 |
{ |
| 280 |
// IPv6 IP address. Find the terminating ] |
| 281 |
hostend = m_host.find("]", i6pos); |
| 282 |
idx = m_host.rfind(":"); |
| 283 |
if (hostend != string::npos) |
| 284 |
{ |
| 285 |
// Found the end. But not necessarily it was |
| 286 |
// at the beginning. If it was at the beginning, |
| 287 |
// strip them from the host name. |
| 288 |
|
| 289 |
size_t lasthost = idx; |
| 290 |
if (idx != string::npos && idx < hostend) |
| 291 |
{ |
| 292 |
idx = string::npos; |
| 293 |
lasthost = m_host.size(); |
| 294 |
} |
| 295 |
|
| 296 |
if (i6pos == 0 && hostend == lasthost - 1) |
| 297 |
{ |
| 298 |
stripbrackets = true; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
idx = m_host.rfind(":"); |
| 305 |
} |
| 306 |
|
| 307 |
if (idx != string::npos) |
| 308 |
{ |
| 309 |
m_port = m_host.substr(idx + 1, m_host.size() - (idx + 1)); |
| 310 |
|
| 311 |
// Extract host WITHOUT stripping brackets |
| 312 |
m_host = m_host.substr(0, idx); |
| 313 |
} |
| 314 |
|
| 315 |
if (stripbrackets) |
| 316 |
{ |
| 317 |
if (!hostend) |
| 318 |
hostend = m_host.size() - 1; |
| 319 |
m_host = m_host.substr(1, hostend - 1); |
| 320 |
} |
| 321 |
|
| 322 |
if ( m_port == "" && m_host != "" ) |
| 323 |
{ |
| 324 |
// Check if the host-but-no-port has specified |
| 325 |
// a single integer number. If so |
| 326 |
// We need to use C86 strtol, cannot use C++11 |
| 327 |
const char* beg = m_host.c_str(); |
| 328 |
const char* end = m_host.c_str() + m_host.size(); |
| 329 |
char* eos = 0; |
| 330 |
long val = strtol(beg, &eos, 10); |
| 331 |
if ( val > 0 && eos == end ) |
| 332 |
{ |
| 333 |
m_port = m_host; |
| 334 |
m_host = ""; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
string strQueryPair; |
| 339 |
while (iQueryStart > -1) |
| 340 |
{ |
| 341 |
idx = strUrl.find("&", iQueryStart); |
| 342 |
if (idx != string::npos) |
| 343 |
{ |
| 344 |
strQueryPair = strUrl.substr(iQueryStart, idx - iQueryStart); |
| 345 |
iQueryStart = (int)(idx + 1); |
| 346 |
} |
| 347 |
else |
| 348 |
{ |
| 349 |
strQueryPair = strUrl.substr(iQueryStart, strUrl.size() - iQueryStart); |
| 350 |
iQueryStart = (int)idx; |
| 351 |
} |
| 352 |
|
| 353 |
idx = strQueryPair.find("="); |
| 354 |
if (idx != string::npos) |
| 355 |
{ |
| 356 |
m_mapQuery[url_decode(strQueryPair.substr(0, idx))] = url_decode(strQueryPair.substr(idx + 1, strQueryPair.size() - (idx + 1))); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if (m_proto == "file") |
| 361 |
{ |
| 362 |
if ( m_path.size() > 3 && m_path.substr(0, 3) == "/./" ) |
| 363 |
m_path = m_path.substr(3); |
| 364 |
} |
| 365 |
|
| 366 |
// Post-parse fixes |
| 367 |
// Treat empty protocol as a file. In this case, merge the host and path. |
| 368 |
if (exp == EXPECT_FILE && m_proto == "" && m_port == "") |
| 369 |
{ |
| 370 |
m_proto = "file"; |
| 371 |
m_path = m_host + m_path; |
| 372 |
m_host = ""; |
| 373 |
} |
| 374 |
|
| 375 |
const auto proto_it = g_types.find(m_proto); |
| 376 |
// Default-constructed UNKNOWN will be used if not found. |
| 377 |
if (proto_it != g_types.end()) |
| 378 |
{ |
| 379 |
m_uriType = proto_it->second; |
| 380 |
} |
| 381 |
m_origUri = strUrl; |
| 382 |
} |
| 383 |
|
| 384 |
#ifdef TEST |
| 385 |
|
| 386 |
#include <vector> |
| 387 |
|
| 388 |
using namespace std; |
| 389 |
|
| 390 |
int main( int argc, char** argv ) |
| 391 |
{ |
| 392 |
if ( argc < 2 ) |
| 393 |
{ |
| 394 |
return 0; |
| 395 |
} |
| 396 |
UriParser parser (argv[1], UriParser::EXPECT_HOST); |
| 397 |
std::vector<std::string> args; |
| 398 |
|
| 399 |
if (argc > 2) |
| 400 |
{ |
| 401 |
copy(argv+2, argv+argc, back_inserter(args)); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
(void)argc; |
| 406 |
|
| 407 |
cout << "PARSING URL: " << argv[1] << endl; |
| 408 |
cout << "SCHEME INDEX: " << int(parser.type()) << endl; |
| 409 |
cout << "PROTOCOL: " << parser.proto() << endl; |
| 410 |
cout << "HOST: " << parser.host() << endl; |
| 411 |
cout << "PORT (string): " << parser.port() << endl; |
| 412 |
cout << "PORT (numeric): " << parser.portno() << endl; |
| 413 |
cout << "PATH: " << parser.path() << endl; |
| 414 |
cout << "PARAMETERS:\n"; |
| 415 |
for (auto& p: parser.parameters()) |
| 416 |
{ |
| 417 |
cout << "\t" << p.first << " = " << p.second << endl; |
| 418 |
} |
| 419 |
|
| 420 |
if (!args.empty()) |
| 421 |
{ |
| 422 |
for (string& s: args) |
| 423 |
{ |
| 424 |
vector<string> keyval; |
| 425 |
Split(s, '=', back_inserter(keyval)); |
| 426 |
if (keyval.size() < 2) |
| 427 |
keyval.push_back(""); |
| 428 |
parser[keyval[0]] = keyval[1]; |
| 429 |
} |
| 430 |
|
| 431 |
cout << "REASSEMBLED: " << parser.makeUri() << endl; |
| 432 |
} |
| 433 |
return 0; |
| 434 |
} |
| 435 |
|
| 436 |
#endif |