static bool mongoc_uri_parse_userpass (mongoc_uri_t *uri, const char *str, const char **end) { bool ret = false; const char *end_userpass; const char *end_user; char *s; if ((s = scan_to_unichar(str, '@', "", &end_userpass))) { if ((uri->username = scan_to_unichar(s, ':', "", &end_user))) { uri->password = bson_strdup(end_user + 1); } else { uri->username = bson_strndup(str, end_userpass - str); uri->password = NULL; } mongoc_uri_do_unescape(&uri->username); mongoc_uri_do_unescape(&uri->password); *end = end_userpass + 1; bson_free(s); ret = true; } else { ret = true; } return ret; }
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; }
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; }
static bool mongoc_uri_parse_option (mongoc_uri_t *uri, const char *str) { int32_t v_int; const char *end_key; char *key; char *value; if (!(key = scan_to_unichar(str, '=', &end_key))) { return false; } value = bson_strdup(end_key + 1); mongoc_uri_do_unescape(&value); if (!strcasecmp(key, "connecttimeoutms") || !strcasecmp(key, "sockettimeoutms") || !strcasecmp(key, "maxpoolsize") || !strcasecmp(key, "minpoolsize") || !strcasecmp(key, "maxidletimems") || !strcasecmp(key, "waitqueuemultiple") || !strcasecmp(key, "waitqueuetimeoutms") || !strcasecmp(key, "wtimeoutms")) { v_int = strtol(value, NULL, 10); bson_append_int32(&uri->options, key, -1, v_int); } else if (!strcasecmp(key, "w")) { if (*value == '-' || isdigit(*value)) { v_int = strtol (value, NULL, 10); BSON_APPEND_INT32 (&uri->options, "w", v_int); } else if (0 == strcasecmp (value, "majority")) { BSON_APPEND_UTF8 (&uri->options, "w", "majority"); } else if (*value) { BSON_APPEND_UTF8 (&uri->options, "W", value); } } else if (!strcasecmp(key, "canonicalizeHostname") || !strcasecmp(key, "journal") || !strcasecmp(key, "safe") || !strcasecmp(key, "slaveok") || !strcasecmp(key, "ssl")) { bson_append_bool (&uri->options, key, -1, (0 == strcasecmp (value, "true")) || (0 == strcasecmp (value, "t")) || (0 == strcmp (value, "1"))); } else if (!strcasecmp(key, "readpreferencetags")) { mongoc_uri_parse_tags(uri, value, &uri->read_prefs); } else { bson_append_utf8(&uri->options, key, -1, value, -1); } bson_free(key); bson_free(value); return true; }
static bson_bool_t mongoc_uri_parse_option (mongoc_uri_t *uri, const char *str) { bson_int32_t v_int; const char *end_key; char *key; char *value; if (!(key = scan_to_unichar(str, '=', &end_key))) { return FALSE; } value = strdup(end_key + 1); mongoc_uri_do_unescape(&value); if (!strcasecmp(key, "connecttimeoutms") || !strcasecmp(key, "sockettimeoutms") || !strcasecmp(key, "maxpoolsize") || !strcasecmp(key, "minpoolsize") || !strcasecmp(key, "maxidletimems") || !strcasecmp(key, "waitqueuemultiple") || !strcasecmp(key, "waitqueuetimeoutms") || !strcasecmp(key, "wtimeoutms")) { v_int = strtol(value, NULL, 10); bson_append_int32(&uri->options, key, -1, v_int); } else if (!strcasecmp(key, "w")) { if (*value == '-' || isdigit(*value)) { v_int = strtol(value, NULL, 10); bson_append_int32(&uri->options, key, -1, v_int); } else { bson_append_utf8(&uri->options, key, -1, value, -1); } } else if (!strcasecmp(key, "journal") || !strcasecmp(key, "slaveok") || !strcasecmp(key, "ssl")) { bson_append_bool(&uri->options, key, -1, !strcmp(value, "true")); } else if (!strcasecmp(key, "readpreferencetags")) { mongoc_uri_parse_read_prefs(uri, value); } else { bson_append_utf8(&uri->options, key, -1, value, -1); } bson_free(key); bson_free(value); return TRUE; }
static bool mongoc_uri_parse_database (mongoc_uri_t *uri, const char *str, const char **end) { const char *end_database; if ((uri->database = scan_to_unichar(str, '?', &end_database))) { *end = end_database; } else if (*str) { uri->database = bson_strdup(str); *end = str + strlen(str); } mongoc_uri_do_unescape(&uri->database); return true; }
static bool mongoc_uri_parse_option (mongoc_uri_t *uri, const char *str) { int32_t v_int; const char *end_key; char *key = NULL; char *value = NULL; bool ret = false; if (!(key = scan_to_unichar(str, '=', "", &end_key))) { goto CLEANUP; } value = bson_strdup(end_key + 1); mongoc_uri_do_unescape(&value); if (!value) { /* do_unescape detected invalid UTF-8 and freed value */ goto CLEANUP; } if (mongoc_uri_option_is_int32(key)) { v_int = (int) strtol (value, NULL, 10); BSON_APPEND_INT32 (&uri->options, key, v_int); } else if (!strcasecmp(key, "w")) { if (*value == '-' || isdigit(*value)) { v_int = (int) strtol (value, NULL, 10); BSON_APPEND_INT32 (&uri->options, "w", v_int); } else if (0 == strcasecmp (value, "majority")) { BSON_APPEND_UTF8 (&uri->options, "w", "majority"); } else if (*value) { BSON_APPEND_UTF8 (&uri->options, "w", value); } } else if (mongoc_uri_option_is_bool(key)) { bson_append_bool (&uri->options, key, -1, (0 == strcasecmp (value, "true")) || (0 == strcasecmp (value, "t")) || (0 == strcmp (value, "1"))); } else if (!strcasecmp(key, "readpreferencetags")) { mongoc_uri_parse_tags(uri, value); } else if (!strcasecmp(key, "authmechanism") || !strcasecmp(key, "authsource")) { bson_append_utf8(&uri->credentials, key, -1, value, -1); } else if (!strcasecmp(key, "readconcernlevel")) { mongoc_read_concern_set_level (uri->read_concern, value); } else if (!strcasecmp(key, "authmechanismproperties")) { if (!mongoc_uri_parse_auth_mechanism_properties(uri, value)) { goto CLEANUP; } #ifdef MONGOC_EXPERIMENTAL_FEATURES } else if (!strcasecmp (key, "appname")) { if (!mongoc_uri_set_appname (uri, value)) { MONGOC_WARNING ("Cannot set appname: %s is invalid", value); goto CLEANUP; } #endif } else { bson_append_utf8(&uri->options, key, -1, value, -1); } ret = true; CLEANUP: bson_free(key); bson_free(value); return ret; }