Browse code

db_postgres: 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 11:12:22
Showing 1 changed files
... ...
@@ -110,12 +110,13 @@ static int parse_postgres_uri(struct pg_uri *res, str *uri)
110 110
 		ST_USER_HOST, /* Username or hostname */
111 111
 		ST_PASS_PORT, /* Password or port part */
112 112
 		ST_HOST,	  /* Hostname part */
113
+		ST_HOST6,	  /* Hostname part IPv6 */
113 114
 		ST_PORT,	  /* Port part */
114 115
 		ST_DB		  /* Database part */
115 116
 	};
116 117
 
117 118
 	enum state st;
118
-	int i;
119
+	int i, ipv6_flag=0;
119 120
 	const char *begin;
120 121
 	char *prev_token;
121 122
 
... ...
@@ -173,6 +174,11 @@ static int parse_postgres_uri(struct pg_uri *res, str *uri)
173 173
 						begin = uri->s + i + 1;
174 174
 						break;
175 175
 
176
+					case '[':
177
+						st = ST_HOST6;
178
+						begin = uri->s + i + 1;
179
+						break;
180
+
176 181
 					case '/':
177 182
 						if(memchr(uri->s + i + 1, '/', uri->len - i - 1)
178 183
 								!= NULL)
... ...
@@ -213,9 +219,14 @@ static int parse_postgres_uri(struct pg_uri *res, str *uri)
213 213
 
214 214
 			case ST_HOST:
215 215
 				switch(uri->s[i]) {
216
+					case '[':
217
+						st = ST_HOST6;
218
+						begin = uri->s + i + 1;
219
+						break;
220
+
216 221
 					case ':':
217 222
 						st = ST_PORT;
218
-						if(dupl_string(&res->host, begin, uri->s + i) < 0)
223
+						if(dupl_string(&res->host, begin, uri->s + i - ipv6_flag) < 0)
219 224
 							goto err;
220 225
 						begin = uri->s + i + 1;
221 226
 						break;
... ...
@@ -224,7 +235,7 @@ static int parse_postgres_uri(struct pg_uri *res, str *uri)
224 224
 						if(memchr(uri->s + i + 1, '/', uri->len - i - 1)
225 225
 								!= NULL)
226 226
 							break;
227
-						if(dupl_string(&res->host, begin, uri->s + i) < 0)
227
+						if(dupl_string(&res->host, begin, uri->s + i - ipv6_flag) < 0)
228 228
 							goto err;
229 229
 						if(dupl_string(&res->database, uri->s + i + 1,
230 230
 								   uri->s + uri->len)
... ...
@@ -234,6 +245,15 @@ static int parse_postgres_uri(struct pg_uri *res, str *uri)
234 234
 				}
235 235
 				break;
236 236
 
237
+			case ST_HOST6:
238
+				switch(uri->s[i]) {
239
+					case ']':
240
+						ipv6_flag = 1;
241
+						st = ST_HOST;
242
+						break;
243
+				}
244
+				break;
245
+
237 246
 			case ST_PORT:
238 247
 				switch(uri->s[i]) {
239 248
 					case '/':