Browse code

lib: database URL supports IPv6 address as hostname

- changed parsing of db_url to accept IPv6 address for hostname

Alexey Vasilyev authored on 03/09/2019 13:48:02
Showing 1 changed files
... ...
@@ -102,12 +102,13 @@ static int parse_db_url(struct db_id* id, const str* url)
102 102
 		ST_USER_HOST,  /* Username or hostname */
103 103
 		ST_PASS_PORT,  /* Password or port part */
104 104
 		ST_HOST,       /* Hostname part */
105
+		ST_HOST6,      /* Hostname part IPv6 */
105 106
 		ST_PORT,       /* Port part */
106 107
 		ST_DB          /* Database part */
107 108
 	};
108 109
 
109 110
 	enum state st;
110
-	unsigned int len, i, j, a, foundanother;
111
+	unsigned int len, i, j, a, foundanother, ipv6_flag=0;
111 112
 	const char* begin;
112 113
 	char* prev_token;
113 114
 
... ...
@@ -190,6 +191,11 @@ static int parse_db_url(struct db_id* id, const str* url)
190 190
 				begin = url->s + i + 1;
191 191
 				break;
192 192
 
193
+			case '[':
194
+				st = ST_HOST6;
195
+				begin = url->s + i + 1;
196
+				break;
197
+
193 198
 			case '/':
194 199
 				if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
195 200
 				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
... ...
@@ -226,19 +232,33 @@ static int parse_db_url(struct db_id* id, const str* url)
226 226
 
227 227
 		case ST_HOST:
228 228
 			switch(url->s[i]) {
229
+			case '[':
230
+				st = ST_HOST6;
231
+				begin = url->s + i + 1;
232
+				break;
233
+
229 234
 			case ':':
230 235
 				st = ST_PORT;
231
-				if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
236
+				if (dupl_string(&id->host, begin, url->s + i - ipv6_flag) < 0) goto err;
232 237
 				begin = url->s + i + 1;
233 238
 				break;
234 239
 
235 240
 			case '/':
236
-				if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
241
+				if (dupl_string(&id->host, begin, url->s + i - ipv6_flag) < 0) goto err;
237 242
 				if (dupl_string_name(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
238 243
 				return 0;
239 244
 			}
240 245
 			break;
241 246
 
247
+		case ST_HOST6:
248
+			switch(url->s[i]) {
249
+			case ']':
250
+				ipv6_flag = 1;
251
+				st = ST_HOST;
252
+				break;
253
+			}
254
+			break;
255
+
242 256
 		case ST_PORT:
243 257
 			switch(url->s[i]) {
244 258
 			case '/':