postgres-kit
postgres-kit copied to clipboard
Connection issue while URL contains "%"
Describe the bug
While my connection URL contains "%", e.g. postgres://user:2kf%[email protected]:5432/db1
-
-
URL(string:)
would return nilPostgresConfiguration(url:)
would failed,DatabaseConfigurationFactory.postgres()
too. -
If I change the URL, make % as %25 connection could be ok, but password is incorrect since
PostgresConfiguration(url:)
taks url.password and was not callremovingPercentEncoding
, actual password sent is2kf%25D
for this example. -
My workaround
func makePostgresConnection(url: URL) {
guard let host = url.host,
let user = url.user?.removingPercentEncoding,
let pass = url.password?.removingPercentEncoding else {
throw Error("informal connect string, no host or user or password")
}
let pgcfg = PostgresConfiguration(
hostname: host,
port: url.port ?? PostgresConfiguration.ianaPortNumber,
username: user,
password: pass,
// two lines below were copied from PostgresConfiguration(url:)
database: url.path.split(separator: "/").last.flatMap(String.init),
tlsConfiguration: url.query?.contains("ssl=true") == true || url.query?.contains("sslmode=require") == true ? .makeClientConfiguration() : nil)
return DatabaseConfigurationFactory.postgres(configuration: pgcfg, ...).make()
}
Expected behavior
- Use
removingPercentEncoding
while take user and password from the URL. - Find batter way convert url String to URL for password contains "%".