Skip to content

Commit 08feba0

Browse files
committed
Merge pull request #1146
2 parents 2bc52f9 + 09a8b87 commit 08feba0

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

php_phongo.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,31 @@ static bool php_phongo_uri_finalize_auth(mongoc_uri_t* uri) /* {{{ */
14351435
return true;
14361436
} /* }}} */
14371437

1438+
static bool php_phongo_uri_finalize_directconnection(mongoc_uri_t* uri) /* {{{ */
1439+
{
1440+
const mongoc_host_list_t* hosts;
1441+
1442+
if (!mongoc_uri_get_option_as_bool(uri, MONGOC_URI_DIRECTCONNECTION, false)) {
1443+
return true;
1444+
}
1445+
1446+
/* Per the URI options spec, directConnection conflicts with multiple hosts
1447+
* and SRV URIs, which may resolve to multiple hosts. */
1448+
if (!strncmp(mongoc_uri_get_string(uri), "mongodb+srv://", 14)) {
1449+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: SRV URI not allowed with directConnection option.");
1450+
return false;
1451+
}
1452+
1453+
hosts = mongoc_uri_get_hosts(uri);
1454+
1455+
if (hosts && hosts->next) {
1456+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: Multiple seeds not allowed with directConnection option.");
1457+
return false;
1458+
}
1459+
1460+
return true;
1461+
} /* }}} */
1462+
14381463
static bool php_phongo_uri_finalize_tls(mongoc_uri_t* uri) /* {{{ */
14391464
{
14401465
const bson_t* options;
@@ -1676,12 +1701,17 @@ static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options)
16761701
}
16771702
}
16781703

1679-
// Finalize auth options
1704+
/* Validate any interactions between URI options */
16801705
if (!php_phongo_uri_finalize_auth(uri)) {
16811706
/* Exception should already have been thrown */
16821707
return false;
16831708
}
16841709

1710+
if (!php_phongo_uri_finalize_directconnection(uri)) {
1711+
/* Exception should already have been thrown */
1712+
return false;
1713+
}
1714+
16851715
return true;
16861716
} /* }}} */
16871717

@@ -2502,7 +2532,7 @@ static bool php_phongo_extract_handshake_data(zval* driver, const char* key, cha
25022532
zval* zvalue;
25032533

25042534
if (!php_array_exists(driver, key)) {
2505-
*value = NULL;
2535+
*value = NULL;
25062536
*value_len = 0;
25072537

25082538
return true;
@@ -2523,7 +2553,7 @@ static bool php_phongo_extract_handshake_data(zval* driver, const char* key, cha
25232553

25242554
static char* php_phongo_concat_handshake_data(const char* default_value, const char* custom_value, size_t custom_value_len)
25252555
{
2526-
char* ret;
2556+
char* ret;
25272557
/* Length of the returned value needs to include the trailing null byte */
25282558
size_t ret_len = strlen(default_value) + 1;
25292559

@@ -2552,7 +2582,7 @@ static void php_phongo_handshake_data_append(const char* name, size_t name_len,
25522582
char* full_platform;
25532583

25542584
php_version_string_len = strlen(PHP_VERSION);
2555-
php_version_string = ecalloc(sizeof(char*), 4 + php_version_string_len);
2585+
php_version_string = ecalloc(sizeof(char*), 4 + php_version_string_len);
25562586
snprintf(php_version_string, 4 + php_version_string_len, "PHP %s", PHP_VERSION);
25572587

25582588
driver_name = php_phongo_concat_handshake_data("ext-mongodb:PHP", name, name_len);
@@ -2583,7 +2613,7 @@ static void php_phongo_set_handshake_data(zval* driverOptions)
25832613
size_t platform_len = 0;
25842614

25852615
if (driverOptions && php_array_existsc(driverOptions, "driver")) {
2586-
zval* driver = php_array_fetchc(driverOptions, "driver");
2616+
zval* driver = php_array_fetchc(driverOptions, "driver");
25872617

25882618
if (Z_TYPE_P(driver) != IS_ARRAY) {
25892619
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"driver\" driver option to be an array, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(driver));
@@ -3489,7 +3519,7 @@ static zend_class_entry* php_phongo_fetch_internal_class(const char* class_name,
34893519
static HashTable* php_phongo_std_get_gc(zval* object, zval** table, int* n) /* {{{ */
34903520
{
34913521
*table = NULL;
3492-
*n = 0;
3522+
*n = 0;
34933523
return zend_std_get_properties(object);
34943524
} /* }}} */
34953525

tests/manager/manager-ctor-directconnection-error-001.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ require_once __DIR__ . '/../utils/tools.php';
77

88
echo throws(function() {
99
$manager = new \MongoDB\Driver\Manager('mongodb://a.example.com,b.example.com/?directConnection=true');
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n\n";
11+
12+
echo throws(function() {
13+
$manager = new \MongoDB\Driver\Manager('mongodb://a.example.com,b.example.com', ['directConnection' => true]);
1014
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1115

1216
?>
@@ -15,4 +19,7 @@ echo throws(function() {
1519
--EXPECT--
1620
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
1721
Failed to parse MongoDB URI: 'mongodb://a.example.com,b.example.com/?directConnection=true'. Multiple seeds not allowed with directConnection option.
22+
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Failed to parse URI options: Multiple seeds not allowed with directConnection option.
1825
===DONE===

tests/manager/manager-ctor-directconnection-error-002.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ require_once __DIR__ . '/../utils/tools.php';
77

88
echo throws(function() {
99
$manager = new \MongoDB\Driver\Manager('mongodb+srv://a.example.com/?directConnection=true');
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n\n";
11+
12+
echo throws(function() {
13+
$manager = new \MongoDB\Driver\Manager('mongodb+srv://a.example.com', ['directConnection' => true]);
1014
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1115

1216
?>
@@ -15,4 +19,7 @@ echo throws(function() {
1519
--EXPECT--
1620
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
1721
Failed to parse MongoDB URI: 'mongodb+srv://a.example.com/?directConnection=true'. SRV URI not allowed with directConnection option.
22+
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Failed to parse URI options: SRV URI not allowed with directConnection option.
1825
===DONE===

0 commit comments

Comments
 (0)