[sr-dev] git:master:4da5d898: lib/srdb1: url parser extracts the db name before the url parameters

Daniel-Constantin Mierla miconda at gmail.com
Tue Nov 17 09:16:22 CET 2015


Module: kamailio
Branch: master
Commit: 4da5d898da9a0a8f2780dcdf864354098268a7e9
URL: https://github.com/kamailio/kamailio/commit/4da5d898da9a0a8f2780dcdf864354098268a7e9

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2015-11-17T09:16:06+01:00

lib/srdb1: url parser extracts the db name before the url parameters

- some database urls can have parameters after db name, like:
  driver://username:password@dbhost:port/dbname?params
- reported by Kelvin Chua for db_mongodb

---

Modified: lib/srdb1/db_id.c

---

Diff:  https://github.com/kamailio/kamailio/commit/4da5d898da9a0a8f2780dcdf864354098268a7e9.diff
Patch: https://github.com/kamailio/kamailio/commit/4da5d898da9a0a8f2780dcdf864354098268a7e9.patch

---

diff --git a/lib/srdb1/db_id.c b/lib/srdb1/db_id.c
index f1969c1..5a283f0 100644
--- a/lib/srdb1/db_id.c
+++ b/lib/srdb1/db_id.c
@@ -55,6 +55,30 @@ static int dupl_string(char** dst, const char* begin, const char* end)
 	return 0;
 }
 
+/**
+ * Duplicate a string name (until a params separator found)
+ * \param dst destination
+ * \param begin start of the string
+ * \param end end of the string
+ */
+static int dupl_string_name(char** dst, const char* begin, const char* end)
+{
+	char *p;
+	if (*dst) pkg_free(*dst);
+
+	for(p=(char*)begin; p<end; p++) {
+		if(*p=='?') break;
+	}
+	*dst = pkg_malloc(p - begin + 1);
+	if ((*dst) == NULL) {
+		return -1;
+	}
+
+	memcpy(*dst, begin, p - begin);
+	(*dst)[p - begin] = '\0';
+	return 0;
+}
+
 
 /**
  * Parse a database URL of form 
@@ -151,7 +175,7 @@ static int parse_db_url(struct db_id* id, const str* url)
 
 			case '/':
 				if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
 				return 0;
 			}
 			break;
@@ -178,7 +202,7 @@ static int parse_db_url(struct db_id* id, const str* url)
 				id->host = prev_token;
 				prev_token = 0;
 				id->port = str2s(begin, url->s + i - begin, 0);
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
 				return 0;
 			}
 			break;
@@ -193,7 +217,7 @@ static int parse_db_url(struct db_id* id, const str* url)
 
 			case '/':
 				if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
 				return 0;
 			}
 			break;
@@ -202,7 +226,7 @@ static int parse_db_url(struct db_id* id, const str* url)
 			switch(url->s[i]) {
 			case '/':
 				id->port = str2s(begin, url->s + i - begin, 0);
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
 				return 0;
 			}
 			break;




More information about the sr-dev mailing list