URL.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef ROCKETCOREURL_H
  28. #define ROCKETCOREURL_H
  29. #include "Header.h"
  30. #include "String.h"
  31. #include <map>
  32. namespace Rocket {
  33. namespace Core {
  34. /**
  35. @author Peter Curry
  36. */
  37. class ROCKETCORE_API URL
  38. {
  39. public:
  40. /// Constructs an empty URL.
  41. URL();
  42. /// Constructs a new URL from the given string.
  43. URL(const String& url);
  44. /// Constructs a new URL from the given string. A little more scripting
  45. /// engine friendly.
  46. URL(const char* url);
  47. /// Destroys the URL.
  48. ~URL();
  49. /// Assigns a new URL to the object. This will return false if the URL
  50. /// is malformed.
  51. bool SetURL(const String& url);
  52. /// Returns the entire URL.
  53. const String& GetURL() const;
  54. /// Sets the URL's protocol.
  55. bool SetProtocol(const String& protocol);
  56. /// Returns the protocol this URL is utilising.
  57. const String& GetProtocol() const;
  58. /// Sets the URL's login
  59. bool SetLogin( const String& login );
  60. /// Returns the URL's login
  61. const String& GetLogin() const;
  62. /// Sets the URL's password
  63. bool SetPassword( const String& password );
  64. /// Returns the URL's password
  65. const String& GetPassword() const;
  66. /// Sets the URL's host.
  67. bool SetHost(const String& host);
  68. /// Returns the URL's host.
  69. const String& GetHost() const;
  70. /// Sets the URL's port number.
  71. bool SetPort(int port);
  72. /// Returns the URL's port number.
  73. int GetPort() const;
  74. /// Sets the URL's path.
  75. bool SetPath(const String& path);
  76. /// Prefixes the URL's existing path with the given prefix.
  77. bool PrefixPath(const String& prefix);
  78. /// Returns the URL's path.
  79. const String& GetPath() const;
  80. /// Sets the URL's file name.
  81. bool SetFileName(const String& file_name);
  82. /// Returns the URL's file name.
  83. const String& GetFileName() const;
  84. /// Sets the URL's file extension.
  85. bool SetExtension(const String& extension);
  86. /// Returns the URL's file extension.
  87. const String& GetExtension() const;
  88. /// Access the url parameters
  89. typedef std::map< String, String > Parameters;
  90. const Parameters& GetParameters() const;
  91. void SetParameter(const String& name, const String& value);
  92. void SetParameters( const Parameters& parameters );
  93. void ClearParameters();
  94. /// Returns the URL's path, file name and extension.
  95. String GetPathedFileName() const;
  96. /// Builds and returns a url query string ( key=value&key2=value2 )
  97. String GetQueryString() const;
  98. /// Less-than operator for use as a key in STL containers.
  99. bool operator<(const URL& rhs) const;
  100. /// Since URLs often contain characters outside the ASCII set,
  101. /// the URL has to be converted into a valid ASCII format and back.
  102. static String UrlEncode(const String &value);
  103. static String UrlDecode(const String &value);
  104. private:
  105. void ConstructURL() const;
  106. /// Portable character check (remember EBCDIC). Do not use isalnum() because
  107. /// its behavior is altered by the current locale.
  108. /// See http://tools.ietf.org/html/rfc3986#section-2.3
  109. /// (copied from libcurl sources)
  110. static bool IsUnreservedChar(const char c);
  111. mutable String url;
  112. String protocol;
  113. String login;
  114. String password;
  115. String host;
  116. String path;
  117. String file_name;
  118. String extension;
  119. Parameters parameters;
  120. int port;
  121. mutable int url_dirty;
  122. };
  123. }
  124. }
  125. #endif