Uri.h 5.7 KB

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