Browse code

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

Daniel-Constantin Mierla authored on 17/11/2015 08:14:07
Showing 1 changed files
... ...
@@ -55,6 +55,30 @@ static int dupl_string(char** dst, const char* begin, const char* end)
55 55
 	return 0;
56 56
 }
57 57
 
58
+/**
59
+ * Duplicate a string name (until a params separator found)
60
+ * \param dst destination
61
+ * \param begin start of the string
62
+ * \param end end of the string
63
+ */
64
+static int dupl_string_name(char** dst, const char* begin, const char* end)
65
+{
66
+	char *p;
67
+	if (*dst) pkg_free(*dst);
68
+
69
+	for(p=(char*)begin; p<end; p++) {
70
+		if(*p=='?') break;
71
+	}
72
+	*dst = pkg_malloc(p - begin + 1);
73
+	if ((*dst) == NULL) {
74
+		return -1;
75
+	}
76
+
77
+	memcpy(*dst, begin, p - begin);
78
+	(*dst)[p - begin] = '\0';
79
+	return 0;
80
+}
81
+
58 82
 
59 83
 /**
60 84
  * Parse a database URL of form 
... ...
@@ -151,7 +175,7 @@ static int parse_db_url(struct db_id* id, const str* url)
151 151
 
152 152
 			case '/':
153 153
 				if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
154
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
154
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
155 155
 				return 0;
156 156
 			}
157 157
 			break;
... ...
@@ -178,7 +202,7 @@ static int parse_db_url(struct db_id* id, const str* url)
178 178
 				id->host = prev_token;
179 179
 				prev_token = 0;
180 180
 				id->port = str2s(begin, url->s + i - begin, 0);
181
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
181
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
182 182
 				return 0;
183 183
 			}
184 184
 			break;
... ...
@@ -193,7 +217,7 @@ static int parse_db_url(struct db_id* id, const str* url)
193 193
 
194 194
 			case '/':
195 195
 				if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
196
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
196
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
197 197
 				return 0;
198 198
 			}
199 199
 			break;
... ...
@@ -202,7 +226,7 @@ static int parse_db_url(struct db_id* id, const str* url)
202 202
 			switch(url->s[i]) {
203 203
 			case '/':
204 204
 				id->port = str2s(begin, url->s + i - begin, 0);
205
-				if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
205
+				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
206 206
 				return 0;
207 207
 			}
208 208
 			break;