2
0

URL.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 "../../Include/RmlUi/Core/Log.h"
  29. #include "../../Include/RmlUi/Core/StringUtilities.h"
  30. #include "../../Include/RmlUi/Core/URL.h"
  31. #include <string.h>
  32. namespace Rml {
  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. else
  205. parent_dir_start_pos += 1;
  206. // Strip out the parent dir and the /..
  207. path.erase(parent_dir_start_pos, parent_dir_pos - parent_dir_start_pos + 4);
  208. // We've altered the URL, mark it dirty
  209. url_dirty = true;
  210. }
  211. }
  212. // Find the file name. This is the string after the trailing slash of the
  213. // path, and just before the extension.
  214. const char* extension_begin = strrchr(file_name_begin, '.');
  215. if (nullptr == extension_begin)
  216. {
  217. file_name = file_name_begin;
  218. extension = "";
  219. }
  220. else
  221. {
  222. file_name = String(file_name_begin, extension_begin);
  223. extension = extension_begin + 1;
  224. }
  225. return true;
  226. }
  227. // Returns the entire URL.
  228. const String& URL::GetURL() const
  229. {
  230. if (url_dirty)
  231. ConstructURL();
  232. return url;
  233. }
  234. // Sets the URL's protocol.
  235. bool URL::SetProtocol(const String& _protocol)
  236. {
  237. protocol = _protocol;
  238. url_dirty = true;
  239. return true;
  240. }
  241. // Returns the protocol this URL is utilising.
  242. const String& URL::GetProtocol() const
  243. {
  244. return protocol;
  245. }
  246. /// Sets the URL's login
  247. bool URL::SetLogin( const String& _login )
  248. {
  249. login = _login;
  250. url_dirty = true;
  251. return true;
  252. }
  253. /// Returns the URL's login
  254. const String& URL::GetLogin() const
  255. {
  256. return login;
  257. }
  258. /// Sets the URL's password
  259. bool URL::SetPassword(const String& _password)
  260. {
  261. password = _password;
  262. url_dirty = true;
  263. return true;
  264. }
  265. /// Returns the URL's password
  266. const String& URL::GetPassword() const
  267. {
  268. return password;
  269. }
  270. // Sets the URL's host.
  271. bool URL::SetHost(const String& _host)
  272. {
  273. host = _host;
  274. url_dirty = true;
  275. return true;
  276. }
  277. // Returns the URL's host.
  278. const String& URL::GetHost() const
  279. {
  280. return host;
  281. }
  282. // Sets the URL's port number.
  283. bool URL::SetPort(int _port)
  284. {
  285. port = _port;
  286. url_dirty = true;
  287. return true;
  288. }
  289. // Returns the URL's port number.
  290. int URL::GetPort() const
  291. {
  292. return port;
  293. }
  294. // Sets the URL's path.
  295. bool URL::SetPath(const String& _path)
  296. {
  297. path = _path;
  298. url_dirty = true;
  299. return true;
  300. }
  301. // Prefixes the URL's existing path with the given prefix.
  302. bool URL::PrefixPath(const String& prefix)
  303. {
  304. // If there's no trailing slash on the end of the prefix, add one.
  305. if (!prefix.empty() &&
  306. prefix[prefix.size() - 1] != '/')
  307. path = prefix + "/" + path;
  308. else
  309. path = prefix + path;
  310. url_dirty = true;
  311. return true;
  312. }
  313. // Returns the URL's path.
  314. const String& URL::GetPath() const
  315. {
  316. return path;
  317. }
  318. // Sets the URL's file name.
  319. bool URL::SetFileName(const String& _file_name)
  320. {
  321. file_name = _file_name;
  322. url_dirty = true;
  323. return true;
  324. }
  325. // Returns the URL's file name.
  326. const String& URL::GetFileName() const
  327. {
  328. return file_name;
  329. }
  330. // Sets the URL's file extension.
  331. bool URL::SetExtension(const String& _extension)
  332. {
  333. extension = _extension;
  334. url_dirty = true;
  335. return true;
  336. }
  337. // Returns the URL's file extension.
  338. const String& URL::GetExtension() const
  339. {
  340. return extension;
  341. }
  342. // Gets the current parameters
  343. const URL::Parameters& URL::GetParameters() const
  344. {
  345. return parameters;
  346. }
  347. // Set an individual parameter
  348. void URL::SetParameter(const String& key, const String& value)
  349. {
  350. parameters[key] = value;
  351. url_dirty = true;
  352. }
  353. // Set all parameters
  354. void URL::SetParameters(const Parameters& _parameters)
  355. {
  356. parameters = _parameters;
  357. url_dirty = true;
  358. }
  359. // Clear the parameters
  360. void URL::ClearParameters()
  361. {
  362. parameters.clear();
  363. }
  364. // Returns the URL's path, file name and extension.
  365. String URL::GetPathedFileName() const
  366. {
  367. String pathed_file_name = path;
  368. // Append the file name.
  369. pathed_file_name += file_name;
  370. // Append the extension.
  371. if (!extension.empty())
  372. {
  373. pathed_file_name += ".";
  374. pathed_file_name += extension;
  375. }
  376. return pathed_file_name;
  377. }
  378. String URL::GetQueryString() const
  379. {
  380. String query_string;
  381. int count = 0;
  382. for ( Parameters::const_iterator itr = parameters.begin(); itr != parameters.end(); ++itr )
  383. {
  384. query_string += ( count == 0 ) ? "" : "&";
  385. query_string += UrlEncode((*itr).first);
  386. query_string += "=";
  387. query_string += UrlEncode((*itr).second);
  388. count++;
  389. }
  390. return query_string;
  391. }
  392. // Less-than operator for use as a key in STL containers.
  393. bool URL::operator<(const URL& rhs) const
  394. {
  395. if (url_dirty)
  396. ConstructURL();
  397. if (rhs.url_dirty)
  398. rhs.ConstructURL();
  399. return url < rhs.url;
  400. }
  401. void URL::ConstructURL() const
  402. {
  403. url = "";
  404. // Append the protocol.
  405. if (!protocol.empty() && !host.empty())
  406. {
  407. url = protocol;
  408. url += "://";
  409. }
  410. // Append login and password
  411. if (!login.empty())
  412. {
  413. url += login ;
  414. if (!password.empty())
  415. {
  416. url += ":" ;
  417. url += password ;
  418. }
  419. url += "@" ;
  420. }
  421. RMLUI_ASSERTMSG( password.empty() || ( !password.empty() && !login.empty() ), "Can't have a password without a login!" );
  422. // Append the host.
  423. url += host;
  424. // Only check ports if there is some host/protocol part
  425. if ( !url.empty() )
  426. {
  427. if (port > 0)
  428. {
  429. RMLUI_ASSERTMSG( !host.empty(), "Can't have a port without a host!" );
  430. char port_string[16];
  431. sprintf(port_string, ":%d/", port);
  432. url += port_string;
  433. }
  434. else
  435. {
  436. url += "/";
  437. }
  438. }
  439. // Append the path.
  440. if (!path.empty())
  441. {
  442. url += path;
  443. }
  444. // Append the file name.
  445. url += file_name;
  446. // Append the extension.
  447. if (!extension.empty())
  448. {
  449. url += ".";
  450. url += extension;
  451. }
  452. // Append parameters
  453. if (!parameters.empty())
  454. {
  455. url += "?";
  456. url += GetQueryString();
  457. }
  458. url_dirty = false;
  459. }
  460. String URL::UrlEncode(const String &value)
  461. {
  462. String encoded;
  463. char hex[4] = {0,0,0,0};
  464. encoded.clear();
  465. const char *value_c = value.c_str();
  466. for (String::size_type i = 0; value_c[i]; i++)
  467. {
  468. char c = value_c[i];
  469. if (IsUnreservedChar(c))
  470. encoded += c;
  471. else
  472. {
  473. sprintf(hex, "%%%02X", c);
  474. encoded += hex;
  475. }
  476. }
  477. return encoded;
  478. }
  479. String URL::UrlDecode(const String &value)
  480. {
  481. String decoded;
  482. decoded.clear();
  483. const char *value_c = value.c_str();
  484. String::size_type value_len = value.size();
  485. for (String::size_type i = 0; i < value_len; i++)
  486. {
  487. char c = value_c[i];
  488. if (c == '+')
  489. {
  490. decoded += ' ';
  491. }
  492. else if (c == '%')
  493. {
  494. char *endp;
  495. String t = value.substr(i+1, 2);
  496. int ch = strtol(t.c_str(), &endp, 16);
  497. if (*endp == '\0')
  498. decoded += char(ch);
  499. else
  500. decoded += t;
  501. i += 2;
  502. }
  503. else
  504. {
  505. decoded += c;
  506. }
  507. }
  508. return decoded;
  509. }
  510. bool URL::IsUnreservedChar(const char in)
  511. {
  512. switch (in)
  513. {
  514. case '0': case '1': case '2': case '3': case '4':
  515. case '5': case '6': case '7': case '8': case '9':
  516. case 'a': case 'b': case 'c': case 'd': case 'e':
  517. case 'f': case 'g': case 'h': case 'i': case 'j':
  518. case 'k': case 'l': case 'm': case 'n': case 'o':
  519. case 'p': case 'q': case 'r': case 's': case 't':
  520. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  521. case 'A': case 'B': case 'C': case 'D': case 'E':
  522. case 'F': case 'G': case 'H': case 'I': case 'J':
  523. case 'K': case 'L': case 'M': case 'N': case 'O':
  524. case 'P': case 'Q': case 'R': case 'S': case 'T':
  525. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  526. case '-': case '.': case '_': case '~':
  527. return true;
  528. default:
  529. break;
  530. }
  531. return false;
  532. }
  533. } // namespace Rml