uri.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/uri.h
  3. // Purpose: wxURI - Class for parsing URIs
  4. // Author: Ryan Norton
  5. // Vadim Zeitlin (UTF-8 URI support, many other changes)
  6. // Created: 07/01/2004
  7. // Copyright: (c) 2004 Ryan Norton
  8. // 2008 Vadim Zeitlin
  9. // Licence: wxWindows Licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_URI_H_
  12. #define _WX_URI_H_
  13. #include "wx/defs.h"
  14. #include "wx/object.h"
  15. #include "wx/string.h"
  16. #include "wx/arrstr.h"
  17. // Host Type that the server component can be
  18. enum wxURIHostType
  19. {
  20. wxURI_REGNAME, // Host is a normal register name (www.mysite.com etc.)
  21. wxURI_IPV4ADDRESS, // Host is a version 4 ip address (192.168.1.100)
  22. wxURI_IPV6ADDRESS, // Host is a version 6 ip address [aa:aa:aa:aa::aa:aa]:5050
  23. wxURI_IPVFUTURE // Host is a future ip address (wxURI is unsure what kind)
  24. };
  25. // Component Flags
  26. enum wxURIFieldType
  27. {
  28. wxURI_SCHEME = 1,
  29. wxURI_USERINFO = 2,
  30. wxURI_SERVER = 4,
  31. wxURI_PORT = 8,
  32. wxURI_PATH = 16,
  33. wxURI_QUERY = 32,
  34. wxURI_FRAGMENT = 64
  35. };
  36. // Miscellaneous other flags
  37. enum wxURIFlags
  38. {
  39. wxURI_STRICT = 1
  40. };
  41. // Generic class for parsing URIs.
  42. //
  43. // See RFC 3986
  44. class WXDLLIMPEXP_BASE wxURI : public wxObject
  45. {
  46. public:
  47. wxURI();
  48. wxURI(const wxString& uri);
  49. // default copy ctor, assignment operator and dtor are ok
  50. bool Create(const wxString& uri);
  51. wxURI& operator=(const wxString& string)
  52. {
  53. Create(string);
  54. return *this;
  55. }
  56. bool operator==(const wxURI& uri) const;
  57. // various accessors
  58. bool HasScheme() const { return (m_fields & wxURI_SCHEME) != 0; }
  59. bool HasUserInfo() const { return (m_fields & wxURI_USERINFO) != 0; }
  60. bool HasServer() const { return (m_fields & wxURI_SERVER) != 0; }
  61. bool HasPort() const { return (m_fields & wxURI_PORT) != 0; }
  62. bool HasPath() const { return (m_fields & wxURI_PATH) != 0; }
  63. bool HasQuery() const { return (m_fields & wxURI_QUERY) != 0; }
  64. bool HasFragment() const { return (m_fields & wxURI_FRAGMENT) != 0; }
  65. const wxString& GetScheme() const { return m_scheme; }
  66. const wxString& GetPath() const { return m_path; }
  67. const wxString& GetQuery() const { return m_query; }
  68. const wxString& GetFragment() const { return m_fragment; }
  69. const wxString& GetPort() const { return m_port; }
  70. const wxString& GetUserInfo() const { return m_userinfo; }
  71. const wxString& GetServer() const { return m_server; }
  72. wxURIHostType GetHostType() const { return m_hostType; }
  73. // these functions only work if the user information part of the URI is in
  74. // the usual (but insecure and hence explicitly recommended against by the
  75. // RFC) "user:password" form
  76. wxString GetUser() const;
  77. wxString GetPassword() const;
  78. // combine all URI components into a single string
  79. //
  80. // BuildURI() returns the real URI suitable for use with network libraries,
  81. // for example, while BuildUnescapedURI() returns a string suitable to be
  82. // shown to the user.
  83. wxString BuildURI() const { return DoBuildURI(&wxURI::Nothing); }
  84. wxString BuildUnescapedURI() const { return DoBuildURI(&wxURI::Unescape); }
  85. // the escaped URI should contain only ASCII characters, including possible
  86. // escape sequences
  87. static wxString Unescape(const wxString& escapedURI);
  88. void Resolve(const wxURI& base, int flags = wxURI_STRICT);
  89. bool IsReference() const;
  90. protected:
  91. void Clear();
  92. // common part of BuildURI() and BuildUnescapedURI()
  93. wxString DoBuildURI(wxString (*funcDecode)(const wxString&)) const;
  94. // function which returns its argument unmodified, this is used by
  95. // BuildURI() to tell DoBuildURI() that nothing needs to be done with the
  96. // URI components
  97. static wxString Nothing(const wxString& value) { return value; }
  98. bool Parse(const char* uri);
  99. const char* ParseAuthority (const char* uri);
  100. const char* ParseScheme (const char* uri);
  101. const char* ParseUserInfo (const char* uri);
  102. const char* ParseServer (const char* uri);
  103. const char* ParsePort (const char* uri);
  104. const char* ParsePath (const char* uri);
  105. const char* ParseQuery (const char* uri);
  106. const char* ParseFragment (const char* uri);
  107. static bool ParseH16(const char*& uri);
  108. static bool ParseIPv4address(const char*& uri);
  109. static bool ParseIPv6address(const char*& uri);
  110. static bool ParseIPvFuture(const char*& uri);
  111. // should be called with i pointing to '%', returns the encoded character
  112. // following it or -1 if invalid and advances i past it (so that it points
  113. // to the last character consumed on return)
  114. static int DecodeEscape(wxString::const_iterator& i);
  115. // append next character pointer to by p to the string in an escaped form
  116. // and advance p past it
  117. //
  118. // if the next character is '%' and it's followed by 2 hex digits, they are
  119. // not escaped (again) by this function, this allows to keep (backwards-
  120. // compatible) ambiguity about the input format to wxURI::Create(): it can
  121. // be either already escaped or not
  122. void AppendNextEscaped(wxString& s, const char *& p);
  123. // convert hexadecimal digit to its value; return -1 if c isn't valid
  124. static int CharToHex(char c);
  125. // split an URI path string in its component segments (including empty and
  126. // "." ones, no post-processing is done)
  127. static wxArrayString SplitInSegments(const wxString& path);
  128. // various URI grammar helpers
  129. static bool IsUnreserved(char c);
  130. static bool IsReserved(char c);
  131. static bool IsGenDelim(char c);
  132. static bool IsSubDelim(char c);
  133. static bool IsHex(char c);
  134. static bool IsAlpha(char c);
  135. static bool IsDigit(char c);
  136. static bool IsEndPath(char c);
  137. wxString m_scheme;
  138. wxString m_path;
  139. wxString m_query;
  140. wxString m_fragment;
  141. wxString m_userinfo;
  142. wxString m_server;
  143. wxString m_port;
  144. wxURIHostType m_hostType;
  145. size_t m_fields;
  146. DECLARE_DYNAMIC_CLASS(wxURI)
  147. };
  148. #endif // _WX_URI_H_