bool mongoc_uri_parse_host (mongoc_uri_t *uri, const char *str) { uint16_t port; const char *end_host; char *hostname; if (*str == '[' && strchr (str, ']')) { return mongoc_uri_parse_host6 (uri, str); } if ((hostname = scan_to_unichar(str, ':', "?/,", &end_host))) { end_host++; if (!mongoc_uri_parse_port(&port, end_host)) { bson_free (hostname); return false; } } else { hostname = bson_strdup(str); port = MONGOC_DEFAULT_PORT; } mongoc_uri_do_unescape(&hostname); if (!hostname) { /* invalid */ bson_free (hostname); return false; } mongoc_uri_append_host(uri, hostname, port); bson_free(hostname); return true; }
static bool mongoc_uri_parse_host6 (mongoc_uri_t *uri, const char *str) { uint16_t port = MONGOC_DEFAULT_PORT; const char *portstr; const char *end_host; char *hostname; if ((portstr = strrchr (str, ':')) && !strstr (portstr, "]")) { if (!mongoc_uri_parse_port(&port, portstr + 1)) { return false; } } hostname = scan_to_unichar (str + 1, ']', "", &end_host); mongoc_uri_do_unescape (&hostname); if (!hostname) { return false; } mongoc_uri_append_host (uri, hostname, port); bson_free (hostname); return true; }
mongoc_uri_t * mongoc_uri_copy (const mongoc_uri_t *uri) { mongoc_uri_t *copy; mongoc_host_list_t *iter; BSON_ASSERT (uri); copy = (mongoc_uri_t *)bson_malloc0(sizeof (*copy)); copy->str = bson_strdup (uri->str); copy->username = bson_strdup (uri->username); copy->password = bson_strdup (uri->password); copy->database = bson_strdup (uri->database); copy->read_prefs = mongoc_read_prefs_copy (uri->read_prefs); copy->read_concern = mongoc_read_concern_copy (uri->read_concern); copy->write_concern = mongoc_write_concern_copy (uri->write_concern); for (iter = uri->hosts; iter; iter = iter->next) { mongoc_uri_append_host (copy, iter->host, iter->port); } bson_copy_to (&uri->options, ©->options); bson_copy_to (&uri->credentials, ©->credentials); return copy; }
static bool mongoc_uri_parse_host (mongoc_uri_t *uri, const char *str) { uint16_t port; const char *end_host; char *hostname; if (*str == '[' && strchr (str, ']')) { return mongoc_uri_parse_host6 (uri, str); } if ((hostname = scan_to_unichar(str, ':', &end_host))) { end_host++; if (!isdigit(*end_host)) { bson_free(hostname); return false; } #ifdef _MSC_VER sscanf_s (end_host, "%hu", &port); #else sscanf (end_host, "%hu", &port); #endif } else { hostname = bson_strdup(str); port = MONGOC_DEFAULT_PORT; } mongoc_uri_do_unescape(&hostname); mongoc_uri_append_host(uri, hostname, port); bson_free(hostname); return true; }
static bool mongoc_uri_parse_host6 (mongoc_uri_t *uri, const char *str) { uint16_t port = 27017; const char *portstr; const char *end_host; char *hostname; if ((portstr = strrchr (str, ':')) && !strstr (portstr, "]")) { #ifdef _MSC_VER sscanf_s (portstr, ":%hu", &port); #else sscanf (portstr, ":%hu", &port); #endif } hostname = scan_to_unichar (str + 1, ']', &end_host); mongoc_uri_do_unescape (&hostname); mongoc_uri_append_host (uri, hostname, port); bson_free (hostname); return true; }
static bson_bool_t mongoc_uri_parse_host (mongoc_uri_t *uri, const char *str) { bson_uint16_t port; const char *end_host; char *hostname; if ((hostname = scan_to_unichar(str, ':', &end_host))) { end_host++; if (!isdigit(*end_host)) { bson_free(hostname); return FALSE; } sscanf(end_host, "%hu", &port); } else { hostname = bson_strdup(str); port = MONGOC_DEFAULT_PORT; } mongoc_uri_do_unescape(&hostname); mongoc_uri_append_host(uri, hostname, port); bson_free(hostname); return TRUE; }