Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merged pull request #196

  • Loading branch information...
commit 5d5590e45c9f60901cf6e583bd34d8698182c738 2 parents e8c84a9 + ebd6f81
@derickr derickr authored
View
15 mcon/parse.c
@@ -29,7 +29,7 @@ mongo_servers* mongo_parse_init(void)
int mongo_parse_server_spec(mongo_con_manager *manager, mongo_servers *servers, char *spec, char **error_message)
{
char *pos; /* Pointer to current parsing position */
- char *tmp_user = NULL, *tmp_pass = NULL; /* Stores parsed user/pw to be copied to each server struct */
+ char *tmp_user = NULL, *tmp_pass = NULL, *tmp_database = NULL; /* Stores parsed user/password/database to be copied to each server struct */
char *host_start, *host_end, *last_slash, *port_start, *db_start, *db_end;
int i, is_hostname;
@@ -179,21 +179,26 @@ int mongo_parse_server_spec(mongo_con_manager *manager, mongo_servers *servers,
}
}
}
+
+ /* Handling database name */
if (db_start) {
- char *tmp_db = strndup(db_start, db_end - db_start);
- mongo_manager_log(manager, MLOG_PARSE, MLOG_INFO, "- Found database name '%s'", tmp_db);
- free(tmp_db);
+ tmp_database = strndup(db_start, db_end - db_start);
+ mongo_manager_log(manager, MLOG_PARSE, MLOG_INFO, "- Found database name '%s'", tmp_database);
+ } else if (tmp_user && tmp_pass) {
+ mongo_manager_log(manager, MLOG_PARSE, MLOG_INFO, "- No database name found for an authenticated connection. Using 'admin' as default database");
+ tmp_database = strdup("admin");
}
/* Update all servers with user, password and dbname */
for (i = 0; i < servers->count; i++) {
servers->server[i]->username = tmp_user ? strdup(tmp_user) : NULL;
servers->server[i]->password = tmp_pass ? strdup(tmp_pass) : NULL;
- servers->server[i]->db = db_start ? strndup(db_start, db_end-db_start) : NULL;
+ servers->server[i]->db = tmp_database ? strdup(tmp_database) : NULL;
}
free(tmp_user);
free(tmp_pass);
+ free(tmp_database);
return 0;
}
View
19 mcon/tests/parse-test.c
@@ -45,16 +45,19 @@ int main(void)
parse_test("mongodb://derick:test@host1,host2:123/database");
parse_test("mongodb://host1,host2:123/database");
/* Specifying options */
- parse_test("mongodb://derick:test@host1,host2:123/database?option1=foo");
- parse_test("mongodb://derick:test@host1,host2:123/?option2=bar");
- parse_test("mongodb://derick:test@host1,host2:123?option3=bar");
- parse_test("mongodb://derick:test@host1,host2:123/?option4");
- parse_test("mongodb://derick:test@host1,host2:123/?option5=bar;option6=baz");
- parse_test("mongodb://derick:test@host1,host2:123/?option7=bar&option8=baz");
- parse_test("mongodb://derick:test@host1,host2:123/?option9=bar;option10");
- parse_test("mongodb://derick:test@host1,host2:123/?option11;option12=baz");
+ parse_test("mongodb://derick:test@host1,host2:123/database?readPreference=secondary_preferred");
+ parse_test("mongodb://derick:test@host1,host2:123/?readPreference=secondary");
+ parse_test("mongodb://derick:test@host1,host2:123?timeout=4");
+ parse_test("mongodb://derick:test@host1,host2:123/?readPreference");
+ parse_test("mongodb://derick:test@host1,host2:123/?readPreference=primary;timeout=baz");
+ parse_test("mongodb://derick:test@host1,host2:123/?readPreference=primary&timeout=baz");
+ parse_test("mongodb://derick:test@host1,host2:123/?readPreference=primary;timeout");
+ parse_test("mongodb://derick:test@host1,host2:123/?readPreference;timeout=baz");
/* Specific options */
parse_test("mongodb://host1/?replicaSet=testset");
+ parse_test("mongodb://foo:bar@primary:14000/database?replicaSet=seta");
+ parse_test("mongodb://foo:bar@primary:14000/database/?replicaSet=seta");
+
return 0;
}
View
72 tests/others/mongo-default-auth-db.phpt
@@ -0,0 +1,72 @@
+--TEST--
+Test for PHP-498: Check default database for authentication
+--SKIPIF--
+<?php require_once dirname(__FILE__) . "/skipif.inc"; ?>
+--FILE--
+<?php
+require_once dirname(__FILE__) . "/../debug.inc";
+require_once dirname(__FILE__) . "/../utils.inc";
+
+$dsns = array(
+ "mongodb://admin:admin@whisky",
+ "mongodb://foo:bar@localhost/?replicaSet=seta",
+ "mongodb://foo:bar@primary,secondary/?replicaSet=seta",
+ "mongodb://foo:bar@primary:14000/database?replicaSet=seta",
+ "mongodb://foo:bar@primary:14000/database/?replicaSet=seta",
+);
+
+foreach ($dsns as $dsn) {
+ echo $dsn, "\n";
+ $m = new Mongo($dsn, array('connect' => false));
+ echo "\n";
+}
+?>
+--EXPECTF--
+mongodb://admin:admin@whisky
+PARSE INFO: Parsing mongodb://admin:admin@whisky
+PARSE INFO: - Found user 'admin' and a password
+PARSE INFO: - Found node: whisky:27017
+PARSE INFO: - Connection type: STANDALONE
+PARSE INFO: - No database name found for an authenticated connection. Using 'admin' as default database
+PARSE INFO: - Found unknown connection string option 'connect' with value ''
+
+mongodb://foo:bar@localhost/?replicaSet=seta
+PARSE INFO: Parsing mongodb://foo:bar@localhost/?replicaSet=seta
+PARSE INFO: - Found user 'foo' and a password
+PARSE INFO: - Found node: localhost:27017
+PARSE INFO: - Connection type: STANDALONE
+PARSE INFO: - Found option 'replicaSet': 'seta'
+PARSE INFO: - Switching connection type: REPLSET
+PARSE INFO: - No database name found for an authenticated connection. Using 'admin' as default database
+PARSE INFO: - Found unknown connection string option 'connect' with value ''
+
+mongodb://foo:bar@primary,secondary/?replicaSet=seta
+PARSE INFO: Parsing mongodb://foo:bar@primary,secondary/?replicaSet=seta
+PARSE INFO: - Found user 'foo' and a password
+PARSE INFO: - Found node: primary:27017
+PARSE INFO: - Found node: secondary:27017
+PARSE INFO: - Connection type: MULTIPLE
+PARSE INFO: - Found option 'replicaSet': 'seta'
+PARSE INFO: - Switching connection type: REPLSET
+PARSE INFO: - No database name found for an authenticated connection. Using 'admin' as default database
+PARSE INFO: - Found unknown connection string option 'connect' with value ''
+
+mongodb://foo:bar@primary:14000/database?replicaSet=seta
+PARSE INFO: Parsing mongodb://foo:bar@primary:14000/database?replicaSet=seta
+PARSE INFO: - Found user 'foo' and a password
+PARSE INFO: - Found node: primary:14000
+PARSE INFO: - Connection type: STANDALONE
+PARSE INFO: - Found option 'replicaSet': 'seta'
+PARSE INFO: - Switching connection type: REPLSET
+PARSE INFO: - Found database name 'database'
+PARSE INFO: - Found unknown connection string option 'connect' with value ''
+
+mongodb://foo:bar@primary:14000/database/?replicaSet=seta
+PARSE INFO: Parsing mongodb://foo:bar@primary:14000/database/?replicaSet=seta
+PARSE INFO: - Found user 'foo' and a password
+PARSE INFO: - Found node: primary:14000
+PARSE INFO: - Connection type: STANDALONE
+PARSE INFO: - Found option 'replicaSet': 'seta'
+PARSE INFO: - Switching connection type: REPLSET
+PARSE INFO: - Found database name 'database/'
+PARSE INFO: - Found unknown connection string option 'connect' with value ''
Please sign in to comment.
Something went wrong with that request. Please try again.