Uri.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2017 Facebook, Inc.
  3. * Copyright (c) 2017 Chukong Technologies
  4. * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Uri class is based on the original file here https://github.com/facebook/folly/blob/master/folly/Uri.cpp
  19. */
  20. #pragma once
  21. #include "platform/CCPlatformMacros.h"
  22. #include <string>
  23. #include <vector>
  24. #include <stdint.h>
  25. /**
  26. * @addtogroup network
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. namespace network {
  31. /**
  32. * Class representing a URI.
  33. *
  34. * Consider http://www.facebook.com/foo/bar?key=foo#anchor
  35. *
  36. * The URI is broken down into its parts: scheme ("http"), authority
  37. * (ie. host and port, in most cases: "www.facebook.com"), path
  38. * ("/foo/bar"), query ("key=foo") and fragment ("anchor"). The scheme is
  39. * lower-cased.
  40. *
  41. * If this Uri represents a URL, note that, to prevent ambiguity, the component
  42. * parts are NOT percent-decoded; you should do this yourself with
  43. * uriUnescape() (for the authority and path) and uriUnescape(...,
  44. * UriEscapeMode::QUERY) (for the query, but probably only after splitting at
  45. * '&' to identify the individual parameters).
  46. */
  47. class CC_DLL Uri
  48. {
  49. public:
  50. /**
  51. * Parse a Uri from a string. Throws std::invalid_argument on parse error.
  52. */
  53. static Uri parse(const std::string& str);
  54. /** Default constructor */
  55. Uri();
  56. /** Copy constructor */
  57. Uri(const Uri& o);
  58. /** Move constructor */
  59. Uri(Uri&& o);
  60. /** Copy assignment */
  61. Uri& operator=(const Uri& o);
  62. /** Move assignment */
  63. Uri& operator=(Uri&& o);
  64. /** Checks whether two Uri instances contain the same values */
  65. bool operator==(const Uri& o) const;
  66. /** Checks wether it's a valid URI */
  67. bool isValid() const { return _isValid; }
  68. /** Checks whether it's a SSL connection */
  69. bool isSecure() const { return _isSecure; }
  70. /** Gets the scheme name for this URI. */
  71. const std::string& getScheme() const { return _scheme; }
  72. /** Gets the user name with the specified URI. */
  73. const std::string& getUserName() const { return _username; }
  74. /** Gets the password with the specified URI. */
  75. const std::string& getPassword() const { return _password; }
  76. /**
  77. * Get host part of URI. If host is an IPv6 address, square brackets will be
  78. * returned, for example: "[::1]".
  79. */
  80. const std::string& getHost() const { return _host; }
  81. /**
  82. * Get host part of URI. If host is an IPv6 address, square brackets will not
  83. * be returned, for exmaple "::1"; otherwise it returns the same thing as
  84. * getHost().
  85. *
  86. * getHostName() is what one needs to call if passing the host to any other tool
  87. * or API that connects to that host/port; e.g. getaddrinfo() only understands
  88. * IPv6 host without square brackets
  89. */
  90. const std::string& getHostName() const { return _hostName; }
  91. /** Gets the port number of the URI. */
  92. uint16_t getPort() const { return _port; }
  93. /** Gets the path part of the URI. */
  94. const std::string& getPath() const { return _path; }
  95. /// Gets the path, query and fragment parts of the URI.
  96. const std::string& getPathEtc() const { return _pathEtc; }
  97. /** Gets the query part of the URI. */
  98. const std::string& getQuery() const { return _query; }
  99. /** Gets the fragment part of the URI */
  100. const std::string& getFragment() const { return _fragment; }
  101. /** Gets the authority part (userName, password, host and port) of the URI.
  102. * @note If the port number is a well-known port
  103. * number for the given scheme (e.g., 80 for http), it
  104. * is not included in the authority.
  105. */
  106. const std::string& getAuthority() const { return _authority; }
  107. /** Gets a string representation of the URI. */
  108. std::string toString() const;
  109. /**
  110. * Get query parameters as key-value pairs.
  111. * e.g. for URI containing query string: key1=foo&key2=&key3&=bar&=bar=
  112. * In returned list, there are 3 entries:
  113. * "key1" => "foo"
  114. * "key2" => ""
  115. * "key3" => ""
  116. * Parts "=bar" and "=bar=" are ignored, as they are not valid query
  117. * parameters. "=bar" is missing parameter name, while "=bar=" has more than
  118. * one equal signs, we don't know which one is the delimiter for key and
  119. * value.
  120. *
  121. * Note, this method is not thread safe, it might update internal state, but
  122. * only the first call to this method update the state. After the first call
  123. * is finished, subsequent calls to this method are thread safe.
  124. *
  125. * @return query parameter key-value pairs in a vector, each element is a
  126. * pair of which the first element is parameter name and the second
  127. * one is parameter value
  128. */
  129. const std::vector<std::pair<std::string, std::string>>& getQueryParams();
  130. /** Clears all parts of the URI. */
  131. void clear();
  132. private:
  133. bool doParse(const std::string& str);
  134. bool _isValid;
  135. bool _isSecure;
  136. std::string _scheme;
  137. std::string _username;
  138. std::string _password;
  139. std::string _host;
  140. std::string _hostName;
  141. bool _hasAuthority;
  142. uint16_t _port;
  143. std::string _authority;
  144. std::string _pathEtc;
  145. std::string _path;
  146. std::string _query;
  147. std::string _fragment;
  148. std::vector<std::pair<std::string, std::string>> _queryParams;
  149. };
  150. } // namespace network {
  151. NS_CC_END
  152. // end group
  153. /// @}