-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathPgConnectionUriParser.java
234 lines (210 loc) · 8.27 KB
/
PgConnectionUriParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (C) 2017 Julien Viet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.vertx.pgclient.impl;
import io.vertx.core.json.JsonObject;
import io.vertx.pgclient.SslMode;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.parseInt;
import static java.lang.String.format;
/**
* This is a parser for parsing connection URIs of PostgreSQL.
* Based on PostgreSQL 11: postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
*
* @author Billy Yuan <[email protected]>
*/
public class PgConnectionUriParser {
private static final String SCHEME_DESIGNATOR_REGEX = "postgre(s|sql)://"; // URI scheme designator
private static final String USER_INFO_REGEX = "((?<userinfo>[a-zA-Z0-9\\-._~%!*]+(:[a-zA-Z0-9\\-._~%!*]+)?)@)?"; // user name and password
private static final String NET_LOCATION_REGEX = "(?<netloc>[0-9.]+|\\[[a-zA-Z0-9:]+]|[a-zA-Z0-9\\-._~%]+)?"; // ip v4/v6 address, host, domain socket address TODO multi-host not supported yet
private static final String PORT_REGEX = "(:(?<port>\\d+))?"; // port
private static final String DATABASE_REGEX = "(/(?<database>[a-zA-Z0-9\\-._~%!*]+))?"; // database name
private static final String PARAMS_REGEX = "(\\?(?<params>.*))?"; // parameters
private static final Pattern SCHEME_DESIGNATOR_PATTERN = Pattern.compile("^" + SCHEME_DESIGNATOR_REGEX);
private static final Pattern FULL_URI_PATTERN = Pattern.compile("^"
+ SCHEME_DESIGNATOR_REGEX
+ USER_INFO_REGEX
+ NET_LOCATION_REGEX
+ PORT_REGEX
+ DATABASE_REGEX
+ PARAMS_REGEX
+ "$");
public static JsonObject parse(String connectionUri) {
return parse(connectionUri, true);
}
public static JsonObject parse(String connectionUri, boolean exact) {
try {
Matcher matcher = SCHEME_DESIGNATOR_PATTERN.matcher(connectionUri);
if (matcher.find() || exact) {
JsonObject configuration = new JsonObject();
doParse(connectionUri, configuration);
return configuration;
} else {
return null;
}
} catch (Exception e) {
throw new IllegalArgumentException("Cannot parse invalid connection URI: " + connectionUri, e);
}
}
// execute the parsing process and store options in the configuration
private static void doParse(String connectionUri, JsonObject configuration) {
Matcher matcher = FULL_URI_PATTERN.matcher(connectionUri);
if (matcher.matches()) {
// parse the user and password
parseUserAndPassword(matcher.group("userinfo"), configuration);
// parse the IP address/host/unix domainSocket address
parseNetLocation(matcher.group("netloc"), configuration);
// parse the port
parsePort(matcher.group("port"), configuration);
// parse the database name
parseDatabaseName(matcher.group("database"), configuration);
// parse the parameters
parseParameters(matcher.group("params"), configuration);
} else {
throw new IllegalArgumentException("Wrong syntax of connection URI");
}
}
private static void parseUserAndPassword(String userInfo, JsonObject configuration) {
if (userInfo == null || userInfo.isEmpty()) {
return;
}
if (occurExactlyOnce(userInfo, ":")) {
int index = userInfo.indexOf(":");
String user = userInfo.substring(0, index);
if (user.isEmpty()) {
throw new IllegalArgumentException("Can not only specify the password without a concrete user");
}
String password = userInfo.substring(index + 1);
configuration.put("user", decodeUrl(user));
configuration.put("password", decodeUrl(password));
} else if (!userInfo.contains(":")) {
configuration.put("user", decodeUrl(userInfo));
} else {
throw new IllegalArgumentException("Can not use multiple delimiters to delimit user and password");
}
}
private static void parseNetLocation(String hostInfo, JsonObject configuration) {
if (hostInfo == null || hostInfo.isEmpty()) {
return;
}
parseNetLocationValue(decodeUrl(hostInfo), configuration);
}
private static void parsePort(String portInfo, JsonObject configuration) {
if (portInfo == null || portInfo.isEmpty()) {
return;
}
int port;
try {
port = parseInt(decodeUrl(portInfo));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The port must be a valid integer");
}
if (port > 65535 || port <= 0) {
throw new IllegalArgumentException("The port can only range in 1-65535");
}
configuration.put("port", port);
}
private static void parseDatabaseName(String databaseInfo, JsonObject configuration) {
if (databaseInfo == null || databaseInfo.isEmpty()) {
return;
}
configuration.put("database", decodeUrl(databaseInfo));
}
private static void parseParameters(String parametersInfo, JsonObject configuration) {
if (parametersInfo == null || parametersInfo.isEmpty()) {
return;
}
Map<String, String> properties = new HashMap<>();
for (String parameterPair : parametersInfo.split("&")) {
if (parameterPair.isEmpty()) {
continue;
}
int indexOfDelimiter = parameterPair.indexOf("=");
if (indexOfDelimiter < 0) {
throw new IllegalArgumentException(format("Missing delimiter '=' of parameters \"%s\" in the part \"%s\"", parametersInfo, parameterPair));
} else {
String key = parameterPair.substring(0, indexOfDelimiter).toLowerCase();
String value = decodeUrl(parameterPair.substring(indexOfDelimiter + 1).trim());
switch (key) {
case "port":
parsePort(value, configuration);
break;
case "host":
parseNetLocationValue(value, configuration);
break;
case "hostaddr":
configuration.put("host", value);
break;
case "user":
configuration.put("user", value);
break;
case "password":
configuration.put("password", value);
break;
case "dbname":
configuration.put("database", value);
break;
case "sslmode":
configuration.put("sslMode", SslMode.of(value));
break;
case "application_name":
properties.put("application_name", value);
break;
case "fallback_application_name":
properties.put("fallback_application_name", value);
break;
case "search_path":
properties.put("search_path", value);
break;
case "options":
properties.put("options", value);
break;
default:
configuration.put(key, value);
break;
}
}
}
if (!properties.isEmpty()) {
configuration.put("properties", properties);
}
}
private static void parseNetLocationValue(String hostValue, JsonObject configuration) {
if (isRegardedAsIpv6Address(hostValue)) {
configuration.put("host", hostValue.substring(1, hostValue.length() - 1));
} else {
configuration.put("host", hostValue);
}
}
private static boolean isRegardedAsIpv6Address(String hostAddress) {
return hostAddress.startsWith("[") && hostAddress.endsWith("]");
}
private static String decodeUrl(String url) {
try {
return URLDecoder.decode(url, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("The connection uri contains unknown characters that can not be resolved.");
}
}
private static boolean occurExactlyOnce(String uri, String character) {
return uri.contains(character) && uri.indexOf(character) == uri.lastIndexOf(character);
}
}