uri.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: uri.h
  3. // Purpose: interface of wxURI
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Host type of URI returned from wxURI::GetHostType().
  9. */
  10. enum wxURIHostType
  11. {
  12. wxURI_REGNAME, ///< Host is a normal register name (@c "www.mysite.com").
  13. wxURI_IPV4ADDRESS, ///< Host is a version 4 ip address (@c "192.168.1.100").
  14. wxURI_IPV6ADDRESS, ///< Host is a version 6 ip address (@c "[aa:aa:aa:aa::aa:aa]:5050").
  15. wxURI_IPVFUTURE ///< Host is a future ip address, wxURI is unsure what kind.
  16. };
  17. /**
  18. @class wxURI
  19. wxURI is used to extract information from a URI (Uniform Resource Identifier).
  20. For information about URIs, see RFC 3986 (http://www.ietf.org/rfc/rfc3986.txt).
  21. In short, a URL is a URI. In other words, URL is a subset of a URI - all
  22. acceptable URLs are also acceptable URIs.
  23. wxURI automatically escapes invalid characters in a string, so there is no
  24. chance of wxURI "failing" on construction/creation.
  25. wxURI supports copy construction and standard assignment operators. wxURI
  26. can also be inherited from to provide further functionality.
  27. To obtain individual components you can use one of the GetXXX() methods.
  28. However, you should check HasXXX() before calling a get method, which
  29. determines whether or not the component referred to by the method is
  30. defined according to RFC 2396. Consider an undefined component equivalent
  31. to a @NULL C string.
  32. Example:
  33. @code
  34. // protocol will hold the http protocol (i.e. "http")
  35. wxString protocol;
  36. wxURI myuri("http://mysite.com");
  37. if( myuri.HasScheme() )
  38. protocol = myuri.GetScheme();
  39. @endcode
  40. @note On URIs with a "file" scheme wxURI does not parse the userinfo,
  41. server, or port portion. This is to keep compatibility with
  42. wxFileSystem, the old wxURL, and older url specifications.
  43. @library{wxbase}
  44. @category{net}
  45. @see wxURL
  46. */
  47. class wxURI : public wxObject
  48. {
  49. public:
  50. /**
  51. Creates an empty URI.
  52. */
  53. wxURI();
  54. /**
  55. Constructor for quick creation.
  56. @param uri
  57. URI (Uniform Resource Identifier) to initialize with.
  58. */
  59. wxURI(const wxString& uri);
  60. /**
  61. Copies this URI from another URI.
  62. @param uri
  63. URI (Uniform Resource Identifier) to initialize with.
  64. */
  65. wxURI(const wxURI& uri);
  66. /**
  67. Builds the URI from its individual components and adds proper
  68. separators.
  69. If the URI is not a reference or is not resolved, the URI that is
  70. returned is the same one passed to the constructor or Create().
  71. */
  72. wxString BuildURI() const;
  73. /**
  74. Builds the URI from its individual components, adds proper separators,
  75. and returns escape sequences to normal characters.
  76. @note It is preferred to call this over Unescape(BuildURI()) since
  77. BuildUnescapedURI() performs some optimizations over the plain
  78. method.
  79. */
  80. wxString BuildUnescapedURI() const;
  81. /**
  82. Creates this URI from the @a uri string.
  83. Returns @true if this instance was correctly initialized.
  84. @param uri
  85. String to initialize from.
  86. */
  87. bool Create(const wxString& uri);
  88. /**
  89. Obtains the fragment of this URI.
  90. The fragment of a URI is the last value of the URI, and is the value
  91. after a "#" character after the path of the URI.
  92. @c "http://mysite.com/mypath#<fragment>"
  93. */
  94. const wxString& GetFragment() const;
  95. /**
  96. Obtains the host type of this URI, which is one of wxURIHostType.
  97. */
  98. wxURIHostType GetHostType() const;
  99. /**
  100. Returns the password part of the userinfo component of this URI. Note
  101. that this is explicitly depreciated by RFC 1396 and should generally be
  102. avoided if possible.
  103. @c "http://<user>:<password>@mysite.com/mypath"
  104. */
  105. wxString GetPassword() const;
  106. /**
  107. Returns the (normalized) path of the URI.
  108. The path component of a URI comes directly after the scheme component
  109. if followed by zero or one slashes ('/'), or after the server/port
  110. component.
  111. Absolute paths include the leading '/' character.
  112. @c "http://mysite.com<path>"
  113. */
  114. const wxString& GetPath() const;
  115. /**
  116. Returns a string representation of the URI's port.
  117. The Port of a URI is a value after the server, and must come after a
  118. colon (:).
  119. @c "http://mysite.com:<port>"
  120. @note You can easily get the numeric value of the port by using
  121. wxAtoi() or wxString::Format().
  122. */
  123. const wxString& GetPort() const;
  124. /**
  125. Returns the Query component of the URI.
  126. The query component is what is commonly passed to a cgi application,
  127. and must come after the path component, and after a '?' character.
  128. @c "http://mysite.com/mypath?<query>"
  129. */
  130. const wxString& GetQuery() const;
  131. /**
  132. Returns the Scheme component of the URI.
  133. The first part of the URI.
  134. @c "<scheme>://mysite.com"
  135. */
  136. const wxString& GetScheme() const;
  137. /**
  138. Returns the Server component of the URI.
  139. The server of the URI can be a server name or a type of IP address. See
  140. GetHostType() for the possible values for the host type of the server
  141. component.
  142. @c "http://<server>/mypath"
  143. */
  144. const wxString& GetServer() const;
  145. /**
  146. Returns the username part of the userinfo component of this URI. Note
  147. that this is explicitly depreciated by RFC 1396 and should generally be
  148. avoided if possible.
  149. @c "http://<user>:<password>@mysite.com/mypath"
  150. */
  151. wxString GetUser() const;
  152. /**
  153. Returns the UserInfo component of the URI.
  154. The component of a URI before the server component that is postfixed by
  155. a '@' character.
  156. @c "http://<userinfo>@mysite.com/mypath"
  157. */
  158. const wxString& GetUserInfo() const;
  159. /**
  160. Returns @true if the Fragment component of the URI exists.
  161. */
  162. bool HasFragment() const;
  163. /**
  164. Returns @true if the Path component of the URI exists.
  165. */
  166. bool HasPath() const;
  167. /**
  168. Returns @true if the Port component of the URI exists.
  169. */
  170. bool HasPort() const;
  171. /**
  172. Returns @true if the Query component of the URI exists.
  173. */
  174. bool HasQuery() const;
  175. /**
  176. Returns @true if the Scheme component of the URI exists.
  177. */
  178. bool HasScheme() const;
  179. /**
  180. Returns @true if the Server component of the URI exists.
  181. */
  182. bool HasServer() const;
  183. /**
  184. Returns @true if the User component of the URI exists.
  185. */
  186. bool HasUserInfo() const;
  187. /**
  188. Returns @true if a valid [absolute] URI, otherwise this URI is a URI
  189. reference and not a full URI, and this function returns @false.
  190. */
  191. bool IsReference() const;
  192. /**
  193. Inherits this URI from a base URI - components that do not exist in
  194. this URI are copied from the base, and if this URI's path is not an
  195. absolute path (prefixed by a '/'), then this URI's path is merged with
  196. the base's path.
  197. For instance, resolving "../mydir" from "http://mysite.com/john/doe"
  198. results in the scheme (http) and server ("mysite.com") being copied
  199. into this URI, since they do not exist. In addition, since the path of
  200. this URI is not absolute (does not begin with '/'), the path of the
  201. base's is merged with this URI's path, resulting in the URI
  202. "http://mysite.com/john/mydir".
  203. @param base
  204. Base URI to inherit from. Must be a full URI and not a reference.
  205. @param flags
  206. Currently either wxURI_STRICT or 0, in non-strict mode some
  207. compatibility layers are enabled to allow loopholes from RFCs prior
  208. to 2396.
  209. */
  210. void Resolve(const wxURI& base, int flags = wxURI_STRICT);
  211. /**
  212. Translates all escape sequences (normal characters and returns the result.
  213. If you want to unescape an entire wxURI, use BuildUnescapedURI()
  214. instead, as it performs some optimizations over this method.
  215. @param uri
  216. String with escaped characters to convert.
  217. */
  218. static wxString Unescape(const wxString& uri);
  219. /**
  220. Compares this URI to another URI, and returns @true if this URI equals
  221. @a uricomp, otherwise it returns @false.
  222. @param uricomp
  223. URI to compare to.
  224. */
  225. bool operator==(const wxURI& uricomp) const;
  226. };