uri.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Copyright 2014 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include <bsoncxx/document/view.hpp>
  19. #include <bsoncxx/string/view_or_value.hpp>
  20. #include <mongocxx/read_concern.hpp>
  21. #include <mongocxx/read_preference.hpp>
  22. #include <mongocxx/write_concern.hpp>
  23. #include <mongocxx/config/prelude.hpp>
  24. namespace mongocxx {
  25. MONGOCXX_INLINE_NAMESPACE_BEGIN
  26. ///
  27. /// Class representing a MongoDB connection string URI.
  28. ///
  29. /// @todo CXX-2038 Use optionals for all option getters. Otherwise users cannot distinguish between
  30. /// a case of an empty option explicitly present in the URI and the option being omitted. Also
  31. /// change getters that return a std::string to return a string_view since a copy is not necessary.
  32. ///
  33. /// @see https://docs.mongodb.com/master/reference/connection-string/
  34. /// @see http://mongoc.org/libmongoc/current/mongoc_uri_t.html for more information on supported URI
  35. /// options.
  36. ///
  37. class MONGOCXX_API uri {
  38. public:
  39. struct host {
  40. std::string name;
  41. std::uint16_t port;
  42. std::int32_t family;
  43. };
  44. static const std::string k_default_uri;
  45. ///
  46. /// Constructs a uri from an optional MongoDB URI string. If no URI string is specified,
  47. /// uses the default URI string, 'mongodb://localhost:27017'.
  48. ///
  49. /// @see The documentation for the version of libmongoc used for the list of supported
  50. /// URI options. For the latest libmongoc release:
  51. /// http://mongoc.org/libmongoc/current/mongoc_uri_t.html
  52. ///
  53. /// @param uri_string
  54. /// String representing a MongoDB connection string URI, defaults to k_default_uri.
  55. ///
  56. uri(bsoncxx::string::view_or_value uri_string = k_default_uri);
  57. ///
  58. /// Move constructs a uri.
  59. ///
  60. uri(uri&&) noexcept;
  61. ///
  62. /// Move assigns a uri.
  63. ///
  64. uri& operator=(uri&&) noexcept;
  65. ///
  66. /// Destroys a uri.
  67. ///
  68. ~uri();
  69. ///
  70. /// Returns the authentication mechanism from the uri.
  71. ///
  72. /// @return A string representing the authentication mechanism.
  73. ///
  74. std::string auth_mechanism() const;
  75. ///
  76. /// Returns the authentication source from the uri.
  77. ///
  78. /// @return A string representing the authentication source.
  79. ///
  80. std::string auth_source() const;
  81. ///
  82. /// Returns the hosts from the uri.
  83. ///
  84. /// @return A vector of hosts.
  85. ///
  86. std::vector<host> hosts() const;
  87. ///
  88. /// Returns the database from the uri.
  89. ///
  90. /// @return A string with the name of the database.
  91. ///
  92. std::string database() const;
  93. ///
  94. /// Returns other uri options.
  95. ///
  96. /// Note, options are returned in the case they were presented.
  97. /// The URI mongodb://localhost/?appName=abc will return { "appName": "abc" }
  98. /// The URI mongodb://localhost/?appname=abc will return { "appname": "abc" }
  99. ///
  100. /// @return A document view containing other options.
  101. ///
  102. bsoncxx::document::view options() const;
  103. ///
  104. /// Returns the password from the uri.
  105. ///
  106. /// @return A string containing the supplied password.
  107. ///
  108. std::string password() const;
  109. ///
  110. /// Returns the read concern from the uri.
  111. ///
  112. /// @return A read_concern that represents what was specified in the uri.
  113. ///
  114. class read_concern read_concern() const;
  115. ///
  116. /// Returns the read preference from the uri.
  117. ///
  118. /// @return A read_preference that represents what was specified in the uri.
  119. ///
  120. class read_preference read_preference() const;
  121. ///
  122. /// Returns the replica set specified in the uri.
  123. ///
  124. /// @return A string representing the supplied replica set name.
  125. ///
  126. std::string replica_set() const;
  127. ///
  128. /// Returns the ssl parameter from the uri.
  129. ///
  130. /// @return Boolean that is @c true if ssl is enabled and @c false if not.
  131. ///
  132. /// @deprecated The tls() method should be used instead of this method.
  133. ///
  134. MONGOCXX_DEPRECATED bool ssl() const;
  135. ///
  136. /// Returns the tls parameter from the uri.
  137. ///
  138. /// @return Boolean that is @c true if tls is enabled and @c false if not.
  139. ///
  140. bool tls() const;
  141. ///
  142. /// Returns the uri in a string format.
  143. ///
  144. /// @return A string with the uri.
  145. ///
  146. std::string to_string() const;
  147. ///
  148. /// Returns the supplied username from the uri.
  149. ///
  150. /// @return A string with the username specified in the uri.
  151. ///
  152. std::string username() const;
  153. ///
  154. /// Returns the write concern specified in the uri.
  155. ///
  156. /// @return A write_concern that represents what was specified in the uri.
  157. ///
  158. class write_concern write_concern() const;
  159. ///
  160. /// Returns the value of the option "appname" if present in the uri.
  161. ///
  162. /// @return An optional stdx::string_view
  163. ///
  164. stdx::optional<stdx::string_view> appname() const;
  165. ///
  166. /// Returns the value of the option "authMechanismProperties" if present in the uri.
  167. ///
  168. /// @return An optional bsoncxx::document::view
  169. ///
  170. stdx::optional<bsoncxx::document::view> auth_mechanism_properties() const;
  171. ///
  172. /// Returns the list of compressors present in the uri or an empty list if "compressors" was not
  173. /// present or contained no valid compressors.
  174. ///
  175. /// @return A std::vector of stdx::string_view.
  176. ///
  177. std::vector<stdx::string_view> compressors() const;
  178. ///
  179. /// Returns the value of the option "connectTimeoutMS" if present in the uri.
  180. ///
  181. /// @return An optional std::int32_t
  182. ///
  183. stdx::optional<std::int32_t> connect_timeout_ms() const;
  184. ///
  185. /// Returns the value of the option "directConnection" if present in the uri.
  186. ///
  187. /// @return An optional bool
  188. ///
  189. stdx::optional<bool> direct_connection() const;
  190. ///
  191. /// Returns the value of the option "heartbeatFrequencyMS" if present in the uri.
  192. ///
  193. /// @return An optional std::int32_t
  194. ///
  195. stdx::optional<std::int32_t> heartbeat_frequency_ms() const;
  196. ///
  197. /// Returns the value of the option "localThresholdMS" if present in the uri.
  198. ///
  199. /// @return An optional std::int32_t
  200. ///
  201. stdx::optional<std::int32_t> local_threshold_ms() const;
  202. ///
  203. /// Returns the value of the option "maxPoolSize" if present in the uri.
  204. ///
  205. /// @return An optional std::int32_t
  206. ///
  207. stdx::optional<std::int32_t> max_pool_size() const;
  208. ///
  209. /// Returns the value of the option "retryReads" if present in the uri.
  210. ///
  211. /// @return An optional bool
  212. ///
  213. stdx::optional<bool> retry_reads() const;
  214. ///
  215. /// Returns the value of the option "retryWrites" if present in the uri.
  216. ///
  217. /// @return An optional bool
  218. ///
  219. stdx::optional<bool> retry_writes() const;
  220. ///
  221. /// Returns the value of the option "serverSelectionTimeoutMS" if present in the uri.
  222. ///
  223. /// @return An optional std::int32_t
  224. ///
  225. stdx::optional<std::int32_t> server_selection_timeout_ms() const;
  226. ///
  227. /// Returns the value of the option "serverSelectionTryOnce" if present in the uri.
  228. ///
  229. /// @return An optional bool
  230. ///
  231. stdx::optional<bool> server_selection_try_once() const;
  232. ///
  233. /// Returns the value of the option "socketTimeoutMS" if present in the uri.
  234. ///
  235. /// @return An optional std::int32_t
  236. ///
  237. stdx::optional<std::int32_t> socket_timeout_ms() const;
  238. ///
  239. /// Returns the value of the option "tlsAllowInvalidCertificates" if present in the uri.
  240. ///
  241. /// @return An optional bool
  242. ///
  243. stdx::optional<bool> tls_allow_invalid_certificates() const;
  244. ///
  245. /// Returns the value of the option "tlsAllowInvalidHostnames" if present in the uri.
  246. ///
  247. /// @return An optional bool
  248. ///
  249. stdx::optional<bool> tls_allow_invalid_hostnames() const;
  250. ///
  251. /// Returns the value of the option "tlsCAFile" if present in the uri.
  252. ///
  253. /// @return An optional stdx::string_view
  254. ///
  255. stdx::optional<stdx::string_view> tls_ca_file() const;
  256. ///
  257. /// Returns the value of the option "tlsCertificateKeyFile" if present in the uri.
  258. ///
  259. /// @return An optional stdx::string_view
  260. ///
  261. stdx::optional<stdx::string_view> tls_certificate_key_file() const;
  262. ///
  263. /// Returns the value of the option "tlsCertificateKeyFilePassword" if present in the uri.
  264. ///
  265. /// @return An optional stdx::string_view
  266. ///
  267. stdx::optional<stdx::string_view> tls_certificate_key_file_password() const;
  268. ///
  269. /// Returns the value of the option "tlsDisableCertificateRevocationCheck" if present in the
  270. /// uri.
  271. ///
  272. /// @return An optional bool
  273. ///
  274. stdx::optional<bool> tls_disable_certificate_revocation_check() const;
  275. ///
  276. /// Returns the value of the option "tlsDisableOCSPEndpointCheck" if present in the uri.
  277. ///
  278. /// @return An optional bool
  279. ///
  280. stdx::optional<bool> tls_disable_ocsp_endpoint_check() const;
  281. ///
  282. /// Returns the value of the option "tlsInsecure" if present in the uri.
  283. ///
  284. /// @return An optional bool
  285. ///
  286. stdx::optional<bool> tls_insecure() const;
  287. ///
  288. /// Returns the value of the option "waitQueueTimeoutMS" if present in the uri.
  289. ///
  290. /// @return An optional std::int32_t
  291. ///
  292. stdx::optional<std::int32_t> wait_queue_timeout_ms() const;
  293. ///
  294. /// Returns the value of the option "zlibCompressionLevel" if present in the uri.
  295. ///
  296. /// @return An optional std::int32_t
  297. ///
  298. stdx::optional<std::int32_t> zlib_compression_level() const;
  299. private:
  300. friend class client;
  301. friend class pool;
  302. class MONGOCXX_PRIVATE impl;
  303. MONGOCXX_PRIVATE uri(std::unique_ptr<impl>&& implementation);
  304. std::unique_ptr<impl> _impl;
  305. };
  306. MONGOCXX_INLINE_NAMESPACE_END
  307. } // namespace mongocxx
  308. #include <mongocxx/config/postlude.hpp>