|
| 1 | +diff --git a/Sources/CLibMongoC/mongoc/mongoc-topology.c b/Sources/CLibMongoC/mongoc/mongoc-topology.c |
| 2 | +index 987f98e4e..7b9136bfc 100644 |
| 3 | +--- a/Sources/CLibMongoC/mongoc/mongoc-topology.c |
| 4 | ++++ a/Sources/CLibMongoC/mongoc/mongoc-topology.c |
| 5 | +@@ -210,6 +210,8 @@ mongoc_topology_new (const mongoc_uri_t *uri, bool single_threaded) |
| 6 | + uint32_t id; |
| 7 | + const mongoc_host_list_t *hl; |
| 8 | + mongoc_rr_data_t rr_data; |
| 9 | ++ bool has_directconnection; |
| 10 | ++ bool directconnection; |
| 11 | + |
| 12 | + BSON_ASSERT (uri); |
| 13 | + |
| 14 | +@@ -328,12 +330,35 @@ mongoc_topology_new (const mongoc_uri_t *uri, bool single_threaded) |
| 15 | + |
| 16 | + /* |
| 17 | + * Set topology type from URI: |
| 18 | +- * - if we've got a replicaSet name, initialize to RS_NO_PRIMARY |
| 19 | +- * - otherwise, if the seed list has a single host, initialize to SINGLE |
| 20 | ++ * + if directConnection=true |
| 21 | ++ * - whether or not we have a replicaSet name, initialize to SINGLE |
| 22 | ++ * (directConnect with SRV or multiple hosts triggers a URI parse error) |
| 23 | ++ * + if directConnection=false |
| 24 | ++ * - if we've got a replicaSet name, initialize to RS_NO_PRIMARY |
| 25 | ++ * - otherwise, initialize to UNKNOWN |
| 26 | ++ * + if directConnection was not specified in the URI (old behavior) |
| 27 | ++ * - if we've got a replicaSet name, initialize to RS_NO_PRIMARY |
| 28 | ++ * - otherwise, if the seed list has a single host, initialize to SINGLE |
| 29 | + * - everything else gets initialized to UNKNOWN |
| 30 | + */ |
| 31 | ++ has_directconnection = mongoc_uri_has_option ( |
| 32 | ++ uri, MONGOC_URI_DIRECTCONNECTION); |
| 33 | ++ directconnection = has_directconnection && |
| 34 | ++ mongoc_uri_get_option_as_bool (uri, MONGOC_URI_DIRECTCONNECTION, false); |
| 35 | + hl = mongoc_uri_get_hosts (topology->uri); |
| 36 | +- if (mongoc_uri_get_replica_set (topology->uri)) { |
| 37 | ++ if (service && !has_directconnection) { |
| 38 | ++ init_type = MONGOC_TOPOLOGY_UNKNOWN; |
| 39 | ++ } else if (has_directconnection) { |
| 40 | ++ if (directconnection) { |
| 41 | ++ init_type = MONGOC_TOPOLOGY_SINGLE; |
| 42 | ++ } else { |
| 43 | ++ if (mongoc_uri_get_replica_set (topology->uri)) { |
| 44 | ++ init_type = MONGOC_TOPOLOGY_RS_NO_PRIMARY; |
| 45 | ++ } else { |
| 46 | ++ init_type = MONGOC_TOPOLOGY_UNKNOWN; |
| 47 | ++ } |
| 48 | ++ } |
| 49 | ++ } else if (mongoc_uri_get_replica_set (topology->uri)) { |
| 50 | + init_type = MONGOC_TOPOLOGY_RS_NO_PRIMARY; |
| 51 | + } else { |
| 52 | + if (hl && hl->next) { |
| 53 | +diff --git a/Sources/CLibMongoC/mongoc/mongoc-uri.c b/Sources/CLibMongoC/mongoc/mongoc-uri.c |
| 54 | +index 8e6fa149e..31ac05272 100644 |
| 55 | +--- a/Sources/CLibMongoC/mongoc/mongoc-uri.c |
| 56 | ++++ b/Sources/CLibMongoC/mongoc/mongoc-uri.c |
| 57 | +@@ -702,6 +702,14 @@ mongoc_uri_bson_append_or_replace_key (bson_t *options, |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | ++bool |
| 62 | ++mongoc_uri_has_option (const mongoc_uri_t *uri, const char *key) |
| 63 | ++{ |
| 64 | ++ bson_iter_t iter; |
| 65 | ++ |
| 66 | ++ return bson_iter_init_find_case (&iter, &uri->options, key); |
| 67 | ++} |
| 68 | ++ |
| 69 | + bool |
| 70 | + mongoc_uri_option_is_int32 (const char *key) |
| 71 | + { |
| 72 | +@@ -731,6 +739,7 @@ bool |
| 73 | + mongoc_uri_option_is_bool (const char *key) |
| 74 | + { |
| 75 | + return !strcasecmp (key, MONGOC_URI_CANONICALIZEHOSTNAME) || |
| 76 | ++ !strcasecmp (key, MONGOC_URI_DIRECTCONNECTION) || |
| 77 | + !strcasecmp (key, MONGOC_URI_JOURNAL) || |
| 78 | + !strcasecmp (key, MONGOC_URI_RETRYREADS) || |
| 79 | + !strcasecmp (key, MONGOC_URI_RETRYWRITES) || |
| 80 | +@@ -1390,6 +1399,41 @@ mongoc_uri_finalize_auth (mongoc_uri_t *uri, |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | ++static bool |
| 85 | ++mongoc_uri_finalize_directconnection (mongoc_uri_t *uri, bson_error_t *error) |
| 86 | ++{ |
| 87 | ++ bool directconnection = false; |
| 88 | ++ |
| 89 | ++ directconnection = |
| 90 | ++ mongoc_uri_get_option_as_bool (uri, MONGOC_URI_DIRECTCONNECTION, false); |
| 91 | ++ if (!directconnection) { |
| 92 | ++ return true; |
| 93 | ++ } |
| 94 | ++ |
| 95 | ++ /* URI options spec: "The driver MUST report an error if the |
| 96 | ++ * directConnection=true URI option is specified with an SRV URI, because |
| 97 | ++ * the URI may resolve to multiple hosts. The driver MUST allow specifying |
| 98 | ++ * directConnection=false URI option with an SRV URI." */ |
| 99 | ++ if (uri->is_srv) { |
| 100 | ++ MONGOC_URI_ERROR ( |
| 101 | ++ error, "%s", "SRV URI not allowed with directConnection option"); |
| 102 | ++ return false; |
| 103 | ++ } |
| 104 | ++ |
| 105 | ++ /* URI options spec: "The driver MUST report an error if the |
| 106 | ++ * directConnection=true URI option is specified with multiple seeds." */ |
| 107 | ++ if (uri->hosts && uri->hosts->next) { |
| 108 | ++ MONGOC_URI_ERROR ( |
| 109 | ++ error, |
| 110 | ++ "%s", |
| 111 | ++ "Multiple seeds not allowed with directConnection option"); |
| 112 | ++ return false; |
| 113 | ++ } |
| 114 | ++ |
| 115 | ++ return true; |
| 116 | ++ |
| 117 | ++} |
| 118 | ++ |
| 119 | + static bool |
| 120 | + mongoc_uri_parse_before_slash (mongoc_uri_t *uri, |
| 121 | + const char *before_slash, |
| 122 | +@@ -1502,6 +1546,10 @@ mongoc_uri_parse (mongoc_uri_t *uri, const char *str, bson_error_t *error) |
| 123 | + goto error; |
| 124 | + } |
| 125 | + |
| 126 | ++ if (!mongoc_uri_finalize_directconnection (uri, error)) { |
| 127 | ++ goto error; |
| 128 | ++ } |
| 129 | ++ |
| 130 | + bson_free (before_slash); |
| 131 | + return true; |
| 132 | + |
| 133 | +diff --git a/Sources/CLibMongoC/include/CLibMongoC_mongoc-uri.h b/Sources/CLibMongoC/include/CLibMongoC_mongoc-uri.h |
| 134 | +index a190b1f71..e08b7d43f 100644 |
| 135 | +--- a/Sources/CLibMongoC/include/CLibMongoC_mongoc-uri.h |
| 136 | ++++ b/Sources/CLibMongoC/include/CLibMongoC_mongoc-uri.h |
| 137 | +@@ -40,6 +40,7 @@ |
| 138 | + #define MONGOC_URI_CANONICALIZEHOSTNAME "canonicalizehostname" |
| 139 | + #define MONGOC_URI_CONNECTTIMEOUTMS "connecttimeoutms" |
| 140 | + #define MONGOC_URI_COMPRESSORS "compressors" |
| 141 | ++#define MONGOC_URI_DIRECTCONNECTION "directconnection" |
| 142 | + #define MONGOC_URI_GSSAPISERVICENAME "gssapiservicename" |
| 143 | + #define MONGOC_URI_HEARTBEATFREQUENCYMS "heartbeatfrequencyms" |
| 144 | + #define MONGOC_URI_JOURNAL "journal" |
| 145 | +@@ -120,6 +121,8 @@ mongoc_uri_get_password (const mongoc_uri_t *uri); |
| 146 | + MONGOC_EXPORT (bool) |
| 147 | + mongoc_uri_set_password (mongoc_uri_t *uri, const char *password); |
| 148 | + MONGOC_EXPORT (bool) |
| 149 | ++mongoc_uri_has_option (const mongoc_uri_t *uri, const char *key); |
| 150 | ++MONGOC_EXPORT (bool) |
| 151 | + mongoc_uri_option_is_int32 (const char *key); |
| 152 | + MONGOC_EXPORT (bool) |
| 153 | + mongoc_uri_option_is_int64 (const char *key); |
0 commit comments