lluri.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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=viewergpl$
  8. *
  9. * Copyright (c) 2006-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #ifndef LL_LLURI_H
  35. #define LL_LLURI_H
  36. #include <string>
  37. #include "llpreprocessor.h"
  38. class LLSD;
  39. // Note: LLURI instances are immutable.
  40. // See: http://www.ietf.org/rfc/rfc3986.txt
  41. class LLURI
  42. {
  43. protected:
  44. LOG_CLASS(LLURI);
  45. public:
  46. LLURI() = default;
  47. LLURI(const std::string& escaped_str);
  48. LLURI(const std::string& scheme, const std::string& user_name,
  49. const std::string& password, const std::string& host_name, U16 port,
  50. const std::string& escaped_path, const std::string& escaped_query);
  51. static LLURI buildHTTP(const std::string& prefix, const LLSD& path);
  52. // Prefix is either a full URL prefix of the form
  53. // "http://example.com:8080", or it can be simply a host and optional port
  54. // like "example.com" or "example.com:8080", in these cases, the "http://"
  55. // will be added
  56. static LLURI buildHTTP(const std::string& prefix, const LLSD& path,
  57. const LLSD& query);
  58. static LLURI buildHTTP(const std::string& host, const U32& port,
  59. const LLSD& path);
  60. static LLURI buildHTTP(const std::string& host, const U32& port,
  61. const LLSD& path, const LLSD& query);
  62. // Returns the whole URI, escaped as needed
  63. std::string asString() const;
  64. // These functions return parts of the decoded URI. The returned strings
  65. // are un-escaped as needed.
  66. // For all schemes. E.g. "http"; note the lack of a colon
  67. LL_INLINE const std::string& scheme() const
  68. {
  69. return mScheme;
  70. }
  71. std::string opaque() const; // Everything after the colon
  72. // For schemes that follow path like syntax (http, https, ftp)
  73. std::string authority() const; // Ex.: "host.com:80"
  74. std::string hostName() const; // Ex.: "host.com"
  75. std::string userName() const;
  76. std::string password() const;
  77. U16 hostPort() const; // Ex.: 80, will include implicit port
  78. bool defaultPort() const; // true if port is default for scheme
  79. LL_INLINE const std::string& escapedPath() const { return mEscapedPath; }
  80. std::string path() const; // Ex.: "/abc/def", includes leading slash
  81. LLSD pathArray() const; // above decoded into an array of strings
  82. std::string query() const; // Ex.: "x=34", section after "?"
  83. LL_INLINE const std::string& escapedQuery() const { return mEscapedQuery; }
  84. LLSD queryMap() const; // above decoded into a map
  85. static LLSD queryMap(std::string escaped_query_string);
  86. // Given a name value map, returns a serialized query string (such as
  87. // '?n1=v1&n2=v2&...'). 'query_map' is a map of name value. Every value
  88. // must be representable as a string.
  89. static std::string mapToQueryString(const LLSD& query_map);
  90. // Returns the rfc 1738 escaped URI or an empty string.
  91. // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  92. // 0123456789
  93. // -._~
  94. // See http://www.ietf.org/rfc/rfc1738.txt
  95. static std::string escape(const std::string& str);
  96. // Escapes symbols into stream
  97. static void encodeCharacter(std::ostream& ostr,
  98. std::string::value_type val);
  99. // Escapes a string with a specified set of allowed characters, URL-
  100. // encoding all the characters that are not in the allowed string.
  101. // 'is_allowed_sorted' is an optimization hint if allowed array is sorted.
  102. static std::string escape(const std::string& str,
  103. const std::string& allowed,
  104. bool is_allowed_sorted = false);
  105. // Break string into data part and path or sheme and escape path (if
  106. // present) and data. The data part is not allowed to have path related
  107. // symbols.
  108. static std::string escapePathAndData(const std::string& str);
  109. // Unescapes an escaped URI string.
  110. static std::string unescape(const std::string& str);
  111. private:
  112. // Only "http", "https", "ftp", and "secondlife" schemes are parsed;
  113. // secondlife scheme parses authority as "" and includes it as part of
  114. // the path, i.e.:
  115. // secondlife://app/login has mAuthority = "" and mPath = "/app/login"
  116. void parseAuthorityAndPathUsingOpaque();
  117. private:
  118. std::string mScheme;
  119. std::string mEscapedOpaque;
  120. std::string mEscapedAuthority;
  121. std::string mEscapedPath;
  122. std::string mEscapedQuery;
  123. };
  124. // This operator required for tut
  125. bool operator!=(const LLURI& first, const LLURI& second);
  126. #endif // LL_LLURI_H