Browse code

db_mysql: database URL supports IPv6 address as hostname

- changed parsing of db_url to accept IPv6 address for hostname

Alexey Vasilyev authored on 04/09/2019 10:47:23
Showing 1 changed files
... ...
@@ -96,12 +96,13 @@ static int parse_mysql_uri(struct my_uri* res, str* uri)
96 96
 		ST_USER_HOST,  /* Username or hostname */
97 97
 		ST_PASS_PORT,  /* Password or port part */
98 98
 		ST_HOST,       /* Hostname part */
99
+		ST_HOST6,      /* Hostname part IPv6 */
99 100
 		ST_PORT,       /* Port part */
100 101
 		ST_DB          /* Database part */
101 102
 	};
102 103
 
103 104
 	enum state st;
104
-	int  i;
105
+	int  i, ipv6_flag=0;
105 106
 	const char* begin;
106 107
 	char* prev_token;
107 108
 
... ...
@@ -157,6 +158,11 @@ static int parse_mysql_uri(struct my_uri* res, str* uri)
157 157
 				begin = uri->s + i + 1;
158 158
 				break;
159 159
 
160
+			case '[':
161
+				st = ST_HOST6;
162
+				begin = uri->s + i + 1;
163
+				break;
164
+
160 165
 			case '/':
161 166
 				if (dupl_string(&res->host, begin, uri->s + i) < 0) goto err;
162 167
 				if (dupl_string(&res->database, uri->s + i + 1, uri->s + uri->len) < 0) goto err;
... ...
@@ -185,19 +191,33 @@ static int parse_mysql_uri(struct my_uri* res, str* uri)
185 185
 
186 186
 		case ST_HOST:
187 187
 			switch(uri->s[i]) {
188
+			case '[':
189
+				st = ST_HOST6;
190
+				begin = uri->s + i + 1;
191
+				break;
192
+
188 193
 			case ':':
189 194
 				st = ST_PORT;
190
-				if (dupl_string(&res->host, begin, uri->s + i) < 0) goto err;
195
+				if (dupl_string(&res->host, begin, uri->s + i - ipv6_flag) < 0) goto err;
191 196
 				begin = uri->s + i + 1;
192 197
 				break;
193 198
 
194 199
 			case '/':
195
-				if (dupl_string(&res->host, begin, uri->s + i) < 0) goto err;
200
+				if (dupl_string(&res->host, begin, uri->s + i - ipv6_flag) < 0) goto err;
196 201
 				if (dupl_string(&res->database, uri->s + i + 1, uri->s + uri->len) < 0) goto err;
197 202
 				return 0;
198 203
 			}
199 204
 			break;
200 205
 
206
+		case ST_HOST6:
207
+			switch(uri->s[i]) {
208
+			case ']':
209
+				ipv6_flag = 1;
210
+				st = ST_HOST;
211
+				break;
212
+			}
213
+			break;
214
+
201 215
 		case ST_PORT:
202 216
 			switch(uri->s[i]) {
203 217
 			case '/':