PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/lluri.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 177 lines | 68 code | 20 blank | 89 comment | 0 complexity | 72b9ce1c397ea38ac1369bcbd9902094 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lluri.h
  3. * @author Phoenix
  4. * @date 2006-02-05
  5. * @brief Declaration of the URI class.
  6. *
  7. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #ifndef LL_LLURI_H
  29. #define LL_LLURI_H
  30. #include <string>
  31. class LLSD;
  32. class LLUUID;
  33. class LLApp;
  34. /**
  35. *
  36. * LLURI instances are immutable
  37. * See: http://www.ietf.org/rfc/rfc3986.txt
  38. *
  39. */
  40. class LL_COMMON_API LLURI
  41. {
  42. public:
  43. LLURI();
  44. LLURI(const std::string& escaped_str);
  45. LLURI(const std::string& scheme,
  46. const std::string& userName,
  47. const std::string& password,
  48. const std::string& hostName,
  49. U16 hostPort,
  50. const std::string& escapedPath,
  51. const std::string& escapedQuery);
  52. // construct from escaped string, as would be transmitted on the net
  53. ~LLURI();
  54. static LLURI buildHTTP(
  55. const std::string& prefix,
  56. const LLSD& path);
  57. static LLURI buildHTTP(
  58. const std::string& prefix,
  59. const LLSD& path,
  60. const LLSD& query);
  61. ///< prefix is either a full URL prefix of the form
  62. /// "http://example.com:8080", or it can be simply a host and
  63. /// optional port like "example.com" or "example.com:8080", in
  64. /// these cases, the "http://" will be added
  65. static LLURI buildHTTP(
  66. const std::string& host,
  67. const U32& port,
  68. const LLSD& path);
  69. static LLURI buildHTTP(
  70. const std::string& host,
  71. const U32& port,
  72. const LLSD& path,
  73. const LLSD& query);
  74. std::string asString() const;
  75. ///< the whole URI, escaped as needed
  76. /** @name Parts of a URI */
  77. //@{
  78. // These functions return parts of the decoded URI. The returned
  79. // strings are un-escaped as needed
  80. // for all schemes
  81. std::string scheme() const; ///< ex.: "http", note lack of colon
  82. std::string opaque() const; ///< everything after the colon
  83. // for schemes that follow path like syntax (http, https, ftp)
  84. std::string authority() const; // ex.: "host.com:80"
  85. std::string hostName() const; // ex.: "host.com"
  86. std::string userName() const;
  87. std::string password() const;
  88. U16 hostPort() const; // ex.: 80, will include implicit port
  89. BOOL defaultPort() const; // true if port is default for scheme
  90. const std::string& escapedPath() const { return mEscapedPath; }
  91. std::string path() const; // ex.: "/abc/def", includes leading slash
  92. LLSD pathArray() const; // above decoded into an array of strings
  93. std::string query() const; // ex.: "x=34", section after "?"
  94. const std::string& escapedQuery() const { return mEscapedQuery; }
  95. LLSD queryMap() const; // above decoded into a map
  96. static LLSD queryMap(std::string escaped_query_string);
  97. /**
  98. * @brief given a name value map, return a serialized query string.
  99. *
  100. * @param query_map a map of name value. every value must be
  101. * representable as a string.
  102. * @return Returns an url query string of '?n1=v1&n2=v2&...'
  103. */
  104. static std::string mapToQueryString(const LLSD& query_map);
  105. /** @name Escaping Utilities */
  106. //@{
  107. /**
  108. * @brief Escape the string passed except for unreserved
  109. *
  110. * ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  111. * 0123456789
  112. * -._~
  113. *
  114. * @see http://www.ietf.org/rfc/rfc1738.txt
  115. *
  116. * @param str The raw URI to escape.
  117. * @return Returns the rfc 1738 escaped uri or an empty string.
  118. */
  119. static std::string escape(const std::string& str);
  120. /**
  121. * @brief Escape a string with a specified set of allowed characters.
  122. *
  123. * Escape a string by urlencoding all the characters that aren't
  124. * in the allowed string.
  125. * @param str The raw URI to escape.
  126. * @param allowed Character array of allowed characters
  127. * @param is_allowed_sorted Optimization hint if allowed array is sorted.
  128. * @return Returns the escaped uri or an empty string.
  129. */
  130. static std::string escape(
  131. const std::string& str,
  132. const std::string& allowed,
  133. bool is_allowed_sorted = false);
  134. /**
  135. * @brief unescape an escaped URI string.
  136. *
  137. * @param str The escped URI to unescape.
  138. * @return Returns the unescaped uri or an empty string.
  139. */
  140. static std::string unescape(const std::string& str);
  141. //@}
  142. private:
  143. // only "http", "https", "ftp", and "secondlife" schemes are parsed
  144. // secondlife scheme parses authority as "" and includes it as part of
  145. // the path. See lluri_tut.cpp
  146. // i.e. secondlife://app/login has mAuthority = "" and mPath = "/app/login"
  147. void parseAuthorityAndPathUsingOpaque();
  148. std::string mScheme;
  149. std::string mEscapedOpaque;
  150. std::string mEscapedAuthority;
  151. std::string mEscapedPath;
  152. std::string mEscapedQuery;
  153. };
  154. // this operator required for tut
  155. LL_COMMON_API bool operator!=(const LLURI& first, const LLURI& second);
  156. #endif // LL_LLURI_H