postgres-kit icon indicating copy to clipboard operation
postgres-kit copied to clipboard

Connection issue while URL contains "%"

Open shawnclovie opened this issue 2 years ago • 0 comments

Describe the bug

While my connection URL contains "%", e.g. postgres://user:2kf%[email protected]:5432/db1 -

  • URL(string:) would return nil PostgresConfiguration(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 call removingPercentEncoding, actual password sent is 2kf%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 "%".

shawnclovie avatar Oct 14 '22 07:10 shawnclovie