URL.cpp 11 KB

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