Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Npgsql/NpgsqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Transactions;
using Npgsql.Util;
using IsolationLevel = System.Data.IsolationLevel;
using System.Linq;

namespace Npgsql
{
Expand Down Expand Up @@ -141,7 +142,7 @@ void GetPoolAndSettings()
}

// Connection string hasn't been seen before. Parse it.
Settings = new NpgsqlConnectionStringBuilder(_connectionString);
Settings = ParseConnectionString(_connectionString);

if (!_countersInitialized)
{
Expand Down Expand Up @@ -182,6 +183,34 @@ void GetPoolAndSettings()
_pool = PoolManager.GetOrAdd(_connectionString, _pool);
}

NpgsqlConnectionStringBuilder ParseConnectionString(string connectionString)
{
return Uri.TryCreate(connectionString, UriKind.Absolute, out var uri)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also check that the scheme is postgres:// or postgresql://.

? ParseSettingsFromUri(uri)
: new NpgsqlConnectionStringBuilder(_connectionString);
}

NpgsqlConnectionStringBuilder ParseSettingsFromUri(Uri uri)
{
var builder = new NpgsqlConnectionStringBuilder();

var userInfo = uri.UserInfo?.Split(':');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll also need to unescape some values here.

var username = userInfo.ElementAtOrDefault(0);
var password = userInfo.ElementAtOrDefault(1);

var host = uri.Host;
var port = uri.Port;

var databaseName = uri.LocalPath?.TrimStart('/');

if (username != null) builder.Username = username;
if (password != null) builder.Password = password;
if (host != null) builder.Host = host;
if (port != default) builder.Port = port;

return builder;
}

Task Open(bool async, CancellationToken cancellationToken)
{
// This is an optimized path for when a connection can be taken from the pool
Expand Down