URL.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "../../Include/RmlUi/Core/URL.h"
  30. #include <stdio.h>
  31. namespace Rml {
  32. namespace Core {
  33. const char* DEFAULT_PROTOCOL = "file";
  34. // Constructs an Empty URL.
  35. URL::URL()
  36. {
  37. port = 0;
  38. url_dirty = false;
  39. }
  40. // Constructs a new URL from the given string.
  41. URL::URL(const String& _url)
  42. {
  43. port = 0;
  44. RMLUI_VERIFY(SetURL(_url));
  45. }
  46. // Constructs a new URL from the given string.
  47. URL::URL(const char* _url)
  48. {
  49. port = 0;
  50. RMLUI_VERIFY(SetURL(_url));
  51. }
  52. // Destroys the URL.
  53. URL::~URL()
  54. {
  55. }
  56. // Assigns a new URL to the object.
  57. bool URL::SetURL(const String& _url)
  58. {
  59. url_dirty = false;
  60. url = _url;
  61. // Make sure an Empty URL is completely Empty.
  62. if (url.empty())
  63. {
  64. protocol.clear();
  65. login.clear();
  66. password.clear();
  67. host.clear();
  68. port = 0;
  69. path.clear();
  70. file_name.clear();
  71. extension.clear();
  72. return true;
  73. }
  74. // Find the protocol. This consists of the string appearing before the
  75. // '://' token (ie, file://, http://).
  76. const char* host_begin = strchr(_url.c_str(), ':');
  77. if (nullptr != host_begin)
  78. {
  79. protocol = String(_url.c_str(), host_begin);
  80. if (0 != strncmp(host_begin, "://", 3))
  81. {
  82. char malformed_terminator[4] = {0, 0, 0, 0};
  83. strncpy(malformed_terminator, host_begin, 3);
  84. Log::Message(Log::LT_ERROR, "Malformed protocol identifier found in URL %s; expected %s://, found %s%s.\n", _url.c_str(), protocol.c_str(), protocol.c_str(), malformed_terminator);
  85. return false;
  86. }
  87. host_begin += 3;
  88. }
  89. else
  90. {
  91. protocol = DEFAULT_PROTOCOL;
  92. host_begin = _url.c_str();
  93. }
  94. // We only want to look for a host if a protocol was specified.
  95. const char* path_begin;
  96. if (host_begin != _url.c_str())
  97. {
  98. // Find the host. This is the string appearing after the protocol or after
  99. // the username:password combination, and terminated either with a colon,
  100. // if a port is specified, or a forward slash if there is no port.
  101. // Check for a login pair
  102. const char* at_symbol = strchr( host_begin, '@' );
  103. if ( at_symbol )
  104. {
  105. String login_password;
  106. login_password = String( host_begin, at_symbol );
  107. host_begin = at_symbol + 1;
  108. const char* password_ptr = strchr( login_password.c_str(), ':' );
  109. if ( password_ptr )
  110. {
  111. login = String( login_password.c_str(), password_ptr );
  112. password = String( password_ptr + 1 );
  113. }
  114. else
  115. {
  116. login = login_password;
  117. }
  118. }
  119. // Get the host portion
  120. path_begin = strchr(host_begin, '/');
  121. // Search for the colon in the host name, which will indicate a port.
  122. const char* port_begin = strchr(host_begin, ':');
  123. if (nullptr != port_begin && (nullptr == path_begin || port_begin < path_begin))
  124. {
  125. if (1 != sscanf(port_begin, ":%d", &port))
  126. {
  127. Log::Message(Log::LT_ERROR, "Malformed port number found in URL %s.\n", _url.c_str());
  128. return false;
  129. }
  130. host = String(host_begin, port_begin);
  131. // Don't continue if there is no path.
  132. if (nullptr == path_begin)
  133. {
  134. return true;
  135. }
  136. // Increment the path string past the trailing slash.
  137. ++path_begin;
  138. }
  139. else
  140. {
  141. port = -1;
  142. if (nullptr == path_begin)
  143. {
  144. host = host_begin;
  145. return true;
  146. }
  147. else
  148. {
  149. // Assign the host name, then increment the path string past the
  150. // trailing slash.
  151. host = String(host_begin, path_begin);
  152. ++path_begin;
  153. }
  154. }
  155. }
  156. else
  157. {
  158. path_begin = _url.c_str();
  159. }
  160. // Check for parameters
  161. String path_segment;
  162. const char* parameters = strchr(path_begin, '?');
  163. if ( parameters )
  164. {
  165. // Pull the path segment out, so further processing doesn't read the parameters
  166. path_segment = String(path_begin, parameters);
  167. path_begin = path_segment.c_str();
  168. // Loop through all parameters, loading them
  169. StringList parameter_list;
  170. StringUtilities::ExpandString( parameter_list, parameters + 1, '&' );
  171. for ( size_t i = 0; i < parameter_list.size(); i++ )
  172. {
  173. // Split into key and value
  174. StringList key_value;
  175. StringUtilities::ExpandString( key_value, parameter_list[i], '=' );
  176. key_value[0] = UrlDecode(key_value[0]);
  177. if ( key_value.size() == 2 )
  178. this->parameters[key_value[0]] = UrlDecode(key_value[1]);
  179. else
  180. this->parameters[key_value[0]] = "";
  181. }
  182. }
  183. // Find the path. This is the string appearing after the host, terminated
  184. // by the last forward slash.
  185. const char* file_name_begin = strrchr(path_begin, '/');
  186. if (nullptr == file_name_begin)
  187. {
  188. // No path!
  189. file_name_begin = path_begin;
  190. path = "";
  191. }
  192. else
  193. {
  194. // Copy the path including the trailing slash.
  195. path = String(path_begin, ++file_name_begin);
  196. // Normalise the path, stripping any ../'s from it
  197. size_t parent_dir_pos = String::npos;
  198. while ((parent_dir_pos = path.find("/..")) != String::npos && parent_dir_pos != 0)
  199. {
  200. // Find the start of the parent directory.
  201. size_t parent_dir_start_pos = path.rfind('/', parent_dir_pos - 1);
  202. if (parent_dir_start_pos == String::npos)
  203. parent_dir_start_pos = 0;
  204. // Strip out the parent dir and the /..
  205. path.erase(parent_dir_start_pos, parent_dir_pos - parent_dir_start_pos + 3);
  206. // We've altered the URL, mark it dirty
  207. url_dirty = true;
  208. }
  209. }
  210. // Find the file name. This is the string after the trailing slash of the
  211. // path, and just before the extension.
  212. const char* extension_begin = strrchr(file_name_begin, '.');
  213. if (nullptr == extension_begin)
  214. {
  215. file_name = file_name_begin;
  216. extension = "";
  217. }
  218. else
  219. {
  220. file_name = String(file_name_begin, extension_begin);
  221. extension = extension_begin + 1;
  222. }
  223. return true;
  224. }
  225. // Returns the entire URL.
  226. const String& URL::GetURL() const
  227. {
  228. if (url_dirty)
  229. ConstructURL();
  230. return url;
  231. }
  232. // Sets the URL's protocol.
  233. bool URL::SetProtocol(const String& _protocol)
  234. {
  235. protocol = _protocol;
  236. url_dirty = true;
  237. return true;
  238. }
  239. // Returns the protocol this URL is utilising.
  240. const String& URL::GetProtocol() const
  241. {
  242. return protocol;
  243. }
  244. /// Sets the URL's login
  245. bool URL::SetLogin( const String& _login )
  246. {
  247. login = _login;
  248. url_dirty = true;
  249. return true;
  250. }
  251. /// Returns the URL's login
  252. const String& URL::GetLogin() const
  253. {
  254. return login;
  255. }
  256. /// Sets the URL's password
  257. bool URL::SetPassword(const String& _password)
  258. {
  259. password = _password;
  260. url_dirty = true;
  261. return true;
  262. }
  263. /// Returns the URL's password
  264. const String& URL::GetPassword() const
  265. {
  266. return password;
  267. }
  268. // Sets the URL's host.
  269. bool URL::SetHost(const String& _host)
  270. {
  271. host = _host;
  272. url_dirty = true;
  273. return true;
  274. }
  275. // Returns the URL's host.
  276. const String& URL::GetHost() const
  277. {
  278. return host;
  279. }
  280. // Sets the URL's port number.
  281. bool URL::SetPort(int _port)
  282. {
  283. port = _port;
  284. url_dirty = true;
  285. return true;
  286. }
  287. // Returns the URL's port number.
  288. int URL::GetPort() const
  289. {
  290. return port;
  291. }
  292. // Sets the URL's path.
  293. bool URL::SetPath(const String& _path)
  294. {
  295. path = _path;
  296. url_dirty = true;
  297. return true;
  298. }
  299. // Prefixes the URL's existing path with the given prefix.
  300. bool URL::PrefixPath(const String& prefix)
  301. {
  302. // If there's no trailing slash on the end of the prefix, add one.
  303. if (!prefix.empty() &&
  304. prefix[prefix.size() - 1] != '/')
  305. path = prefix + "/" + path;
  306. else
  307. path = prefix + path;
  308. url_dirty = true;
  309. return true;
  310. }
  311. // Returns the URL's path.
  312. const String& URL::GetPath() const
  313. {
  314. return path;
  315. }
  316. // Sets the URL's file name.
  317. bool URL::SetFileName(const String& _file_name)
  318. {
  319. file_name = _file_name;
  320. url_dirty = true;
  321. return true;
  322. }
  323. // Returns the URL's file name.
  324. const String& URL::GetFileName() const
  325. {
  326. return file_name;
  327. }
  328. // Sets the URL's file extension.
  329. bool URL::SetExtension(const String& _extension)
  330. {
  331. extension = _extension;
  332. url_dirty = true;
  333. return true;
  334. }
  335. // Returns the URL's file extension.
  336. const String& URL::GetExtension() const
  337. {
  338. return extension;
  339. }
  340. // Gets the current parameters
  341. const URL::Parameters& URL::GetParameters() const
  342. {
  343. return parameters;
  344. }
  345. // Set an individual parameter
  346. void URL::SetParameter(const String& key, const String& value)
  347. {
  348. parameters[key] = value;
  349. url_dirty = true;
  350. }
  351. // Set all parameters
  352. void URL::SetParameters(const Parameters& _parameters)
  353. {
  354. parameters = _parameters;
  355. url_dirty = true;
  356. }
  357. // Clear the parameters
  358. void URL::ClearParameters()
  359. {
  360. parameters.clear();
  361. }
  362. // Returns the URL's path, file name and extension.
  363. String URL::GetPathedFileName() const
  364. {
  365. String pathed_file_name = path;
  366. // Append the file name.
  367. pathed_file_name += file_name;
  368. // Append the extension.
  369. if (!extension.empty())
  370. {
  371. pathed_file_name += ".";
  372. pathed_file_name += extension;
  373. }
  374. return pathed_file_name;
  375. }
  376. String URL::GetQueryString() const
  377. {
  378. String query_string;
  379. int count = 0;
  380. for ( Parameters::const_iterator itr = parameters.begin(); itr != parameters.end(); ++itr )
  381. {
  382. query_string += ( count == 0 ) ? "" : "&";
  383. query_string += UrlEncode((*itr).first);
  384. query_string += "=";
  385. query_string += UrlEncode((*itr).second);
  386. count++;
  387. }
  388. return query_string;
  389. }
  390. // Less-than operator for use as a key in STL containers.
  391. bool URL::operator<(const URL& rhs) const
  392. {
  393. if (url_dirty)
  394. ConstructURL();
  395. if (rhs.url_dirty)
  396. rhs.ConstructURL();
  397. return url < rhs.url;
  398. }
  399. void URL::ConstructURL() const
  400. {
  401. url = "";
  402. // Append the protocol.
  403. if (!protocol.empty() && !host.empty())
  404. {
  405. url = protocol;
  406. url += "://";
  407. }
  408. // Append login and password
  409. if (!login.empty())
  410. {
  411. url += login ;
  412. if (!password.empty())
  413. {
  414. url += ":" ;
  415. url += password ;
  416. }
  417. url += "@" ;
  418. }
  419. RMLUI_ASSERTMSG( password.empty() || ( !password.empty() && !login.empty() ), "Can't have a password without a login!" );
  420. // Append the host.
  421. url += host;
  422. // Only check ports if there is some host/protocol part
  423. if ( !url.empty() )
  424. {
  425. if (port > 0)
  426. {
  427. RMLUI_ASSERTMSG( !host.empty(), "Can't have a port without a host!" );
  428. char port_string[16];
  429. sprintf(port_string, ":%d/", port);
  430. url += port_string;
  431. }
  432. else
  433. {
  434. url += "/";
  435. }
  436. }
  437. // Append the path.
  438. if (!path.empty())
  439. {
  440. url += path;
  441. }
  442. // Append the file name.
  443. url += file_name;
  444. // Append the extension.
  445. if (!extension.empty())
  446. {
  447. url += ".";
  448. url += extension;
  449. }
  450. // Append parameters
  451. if (!parameters.empty())
  452. {
  453. url += "?";
  454. url += GetQueryString();
  455. }
  456. url_dirty = false;
  457. }
  458. String URL::UrlEncode(const String &value)
  459. {
  460. String encoded;
  461. char hex[4] = {0,0,0,0};
  462. encoded.clear();
  463. const char *value_c = value.c_str();
  464. for (String::size_type i = 0; value_c[i]; i++)
  465. {
  466. char c = value_c[i];
  467. if (IsUnreservedChar(c))
  468. encoded += c;
  469. else
  470. {
  471. sprintf(hex, "%%%02X", c);
  472. encoded += hex;
  473. }
  474. }
  475. return encoded;
  476. }
  477. String URL::UrlDecode(const String &value)
  478. {
  479. String decoded;
  480. decoded.clear();
  481. const char *value_c = value.c_str();
  482. String::size_type value_len = value.size();
  483. for (String::size_type i = 0; i < value_len; i++)
  484. {
  485. char c = value_c[i];
  486. if (c == '+')
  487. {
  488. decoded += ' ';
  489. }
  490. else if (c == '%')
  491. {
  492. char *endp;
  493. String t = value.substr(i+1, 2);
  494. int ch = strtol(t.c_str(), &endp, 16);
  495. if (*endp == '\0')
  496. decoded += char(ch);
  497. else
  498. decoded += t;
  499. i += 2;
  500. }
  501. else
  502. {
  503. decoded += c;
  504. }
  505. }
  506. return decoded;
  507. }
  508. bool URL::IsUnreservedChar(const char in)
  509. {
  510. switch (in)
  511. {
  512. case '0': case '1': case '2': case '3': case '4':
  513. case '5': case '6': case '7': case '8': case '9':
  514. case 'a': case 'b': case 'c': case 'd': case 'e':
  515. case 'f': case 'g': case 'h': case 'i': case 'j':
  516. case 'k': case 'l': case 'm': case 'n': case 'o':
  517. case 'p': case 'q': case 'r': case 's': case 't':
  518. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  519. case 'A': case 'B': case 'C': case 'D': case 'E':
  520. case 'F': case 'G': case 'H': case 'I': case 'J':
  521. case 'K': case 'L': case 'M': case 'N': case 'O':
  522. case 'P': case 'Q': case 'R': case 'S': case 'T':
  523. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  524. case '-': case '.': case '_': case '~':
  525. return true;
  526. default:
  527. break;
  528. }
  529. return false;
  530. }
  531. }
  532. }