Skip to content

Commit eab26dd

Browse files
Andrew WittenkevinAlbs
authored andcommitted
CDRIVER-3476 lower case options (#645)
* CDRIVER-3476 lower case options set with mongoc_uri_set_options_as_* functions
1 parent a12c4d7 commit eab26dd

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/libmongoc/src/mongoc/mongoc-uri.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ count_dots (const char *s)
123123
return n;
124124
}
125125

126+
static char *
127+
lowercase_str_new (const char *key)
128+
{
129+
char *ret = bson_strdup (key);
130+
mongoc_lowercase (key, ret);
131+
return ret;
132+
}
126133

127134
/* at least one character, and does not start with dot */
128135
static bool
@@ -2573,6 +2580,7 @@ _mongoc_uri_set_option_as_int32_with_error (mongoc_uri_t *uri,
25732580
const char *option;
25742581
const bson_t *options;
25752582
bson_iter_t iter;
2583+
char *option_lowercase = NULL;
25762584

25772585
option = mongoc_uri_canonicalize_option (option_orig);
25782586
/* Server Discovery and Monitoring Spec: "the driver MUST NOT permit users
@@ -2611,14 +2619,16 @@ _mongoc_uri_set_option_as_int32_with_error (mongoc_uri_t *uri,
26112619
return false;
26122620
}
26132621
}
2614-
2615-
if (!bson_append_int32 (&uri->options, option, -1, value)) {
2622+
option_lowercase = lowercase_str_new (option);
2623+
if (!bson_append_int32 (&uri->options, option_lowercase, -1, value)) {
2624+
bson_free (option_lowercase);
26162625
MONGOC_URI_ERROR (
26172626
error, "Failed to set URI option \"%s\" to %d", option_orig, value);
26182627

26192628
return false;
26202629
}
26212630

2631+
bson_free (option_lowercase);
26222632
return true;
26232633
}
26242634

@@ -2645,6 +2655,7 @@ _mongoc_uri_set_option_as_int32 (mongoc_uri_t *uri,
26452655
const char *option;
26462656
const bson_t *options;
26472657
bson_iter_t iter;
2658+
char *option_lowercase = NULL;
26482659

26492660
option = mongoc_uri_canonicalize_option (option_orig);
26502661
if ((options = mongoc_uri_get_options (uri)) &&
@@ -2657,7 +2668,9 @@ _mongoc_uri_set_option_as_int32 (mongoc_uri_t *uri,
26572668
}
26582669
}
26592670

2660-
bson_append_int32 (&uri->options, option, -1, value);
2671+
option_lowercase = lowercase_str_new (option);
2672+
bson_append_int32 (&uri->options, option_lowercase, -1, value);
2673+
bson_free (option_lowercase);
26612674
return true;
26622675
}
26632676

@@ -2795,6 +2808,7 @@ _mongoc_uri_set_option_as_int64_with_error (mongoc_uri_t *uri,
27952808
const char *option;
27962809
const bson_t *options;
27972810
bson_iter_t iter;
2811+
char *option_lowercase = NULL;
27982812

27992813
option = mongoc_uri_canonicalize_option (option_orig);
28002814

@@ -2814,15 +2828,17 @@ _mongoc_uri_set_option_as_int64_with_error (mongoc_uri_t *uri,
28142828
}
28152829
}
28162830

2817-
if (!bson_append_int64 (&uri->options, option, -1, value)) {
2831+
option_lowercase = lowercase_str_new (option);
2832+
if (!bson_append_int64 (&uri->options, option_lowercase, -1, value)) {
2833+
bson_free (option_lowercase);
28182834
MONGOC_URI_ERROR (error,
28192835
"Failed to set URI option \"%s\" to %" PRId64,
28202836
option_orig,
28212837
value);
28222838

28232839
return false;
28242840
}
2825-
2841+
bson_free (option_lowercase);
28262842
return true;
28272843
}
28282844

@@ -2893,6 +2909,7 @@ mongoc_uri_set_option_as_bool (mongoc_uri_t *uri,
28932909
bool value)
28942910
{
28952911
const char *option;
2912+
char *option_lowercase;
28962913
const bson_t *options;
28972914
bson_iter_t iter;
28982915

@@ -2912,8 +2929,9 @@ mongoc_uri_set_option_as_bool (mongoc_uri_t *uri,
29122929
return false;
29132930
}
29142931
}
2915-
2916-
bson_append_bool (&uri->options, option, -1, value);
2932+
option_lowercase = lowercase_str_new (option);
2933+
bson_append_bool (&uri->options, option_lowercase, -1, value);
2934+
bson_free (option_lowercase);
29172935
return true;
29182936
}
29192937

@@ -2989,6 +3007,7 @@ mongoc_uri_set_option_as_utf8 (mongoc_uri_t *uri,
29893007
{
29903008
const char *option;
29913009
size_t len;
3010+
char *option_lowercase = NULL;
29923011

29933012
option = mongoc_uri_canonicalize_option (option_orig);
29943013
BSON_ASSERT (option);
@@ -3005,7 +3024,10 @@ mongoc_uri_set_option_as_utf8 (mongoc_uri_t *uri,
30053024
if (!bson_strcasecmp (option, MONGOC_URI_APPNAME)) {
30063025
return mongoc_uri_set_appname (uri, value);
30073026
} else {
3008-
mongoc_uri_bson_append_or_replace_key (&uri->options, option, value);
3027+
option_lowercase = lowercase_str_new (option);
3028+
mongoc_uri_bson_append_or_replace_key (
3029+
&uri->options, option_lowercase, value);
3030+
bson_free (option_lowercase);
30093031
}
30103032

30113033
return true;

src/libmongoc/tests/test-mongoc-uri.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,21 @@ test_one_tls_option_enables_tls ()
27202720
}
27212721
}
27222722

2723+
static void
2724+
test_casing_options ()
2725+
{
2726+
mongoc_uri_t* uri;
2727+
bson_error_t error;
2728+
2729+
uri = mongoc_uri_new("mongodb://localhost:27017/");
2730+
mongoc_uri_set_option_as_bool (uri, "TLS", true);
2731+
mongoc_uri_parse_options(uri, "ssl=false", false, &error);
2732+
ASSERT_ERROR_CONTAINS(error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG,
2733+
"conflicts");
2734+
2735+
mongoc_uri_destroy(uri);
2736+
}
2737+
27232738
void
27242739
test_uri_install (TestSuite *suite)
27252740
{
@@ -2753,4 +2768,5 @@ test_uri_install (TestSuite *suite)
27532768
TestSuite_Add (suite,
27542769
"/Uri/one_tls_option_enables_tls",
27552770
test_one_tls_option_enables_tls);
2771+
TestSuite_Add(suite, "/Uri/options_casing", test_casing_options);
27562772
}

0 commit comments

Comments
 (0)