Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 7 additions & 11 deletions src/StackExchange.Redis/EndPointCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,14 @@ internal void SetDefaultPorts(int defaultPort)
{
for (int i = 0; i < Count; i++)
{
var endpoint = this[i];
var dns = endpoint as DnsEndPoint;
if (dns?.Port == 0)
switch (this[i])
{
this[i] = new DnsEndPoint(dns.Host, defaultPort, dns.AddressFamily);
continue;
}
var ip = endpoint as IPEndPoint;
if (ip?.Port == 0)
{
this[i] = new IPEndPoint(ip.Address, defaultPort);
continue;
case DnsEndPoint dns when dns.Port == 0:
this[i] = new DnsEndPoint(dns.Host, defaultPort, dns.AddressFamily);
break;
case IPEndPoint ip when ip.Port == 0:
this[i] = new IPEndPoint(ip.Address, defaultPort);
break;
}
}
}
Expand Down
33 changes: 24 additions & 9 deletions src/StackExchange.Redis/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Buffers.Text;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;

Expand Down Expand Up @@ -78,17 +79,21 @@ internal static string ToString(object value)

internal static string ToString(EndPoint endpoint)
{
if (endpoint is DnsEndPoint dns)
{
if (dns.Port == 0) return dns.Host;
return dns.Host + ":" + Format.ToString(dns.Port);
}
if (endpoint is IPEndPoint ip)
switch (endpoint)
{
if (ip.Port == 0) return ip.Address.ToString();
return ip.Address + ":" + Format.ToString(ip.Port);
case DnsEndPoint dns:
if (dns.Port == 0) return dns.Host;
return dns.Host + ":" + Format.ToString(dns.Port);
case IPEndPoint ip:
if (ip.Port == 0) return ip.Address.ToString();
return ip.Address + ":" + Format.ToString(ip.Port);
#if UNIX_SOCKET
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest:

Suggested change
#if UNIX_SOCKET
#if NETCOREAPP3_1_OR_GREATER

case UnixDomainSocketEndPoint uds:
return "!" + uds.ToString();
#endif
default:
return endpoint?.ToString() ?? "";
}
return endpoint?.ToString() ?? "";
}

internal static string ToStringHostOnly(EndPoint endpoint)
Expand Down Expand Up @@ -233,6 +238,16 @@ internal static EndPoint TryParseEndPoint(string addressWithPort)
string portPart = null;
if (string.IsNullOrEmpty(addressWithPort)) return null;

if (addressWithPort[0]=='!')
{
if (addressWithPort.Length == 1) return null;

#if UNIX_SOCKET
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#if UNIX_SOCKET
#if NETCOREAPP3_1_OR_GREATER

return new UnixDomainSocketEndPoint(addressWithPort.Substring(1));
#else
throw new PlatformNotSupportedException("Unix domain sockets require .NET Core 3 or above");
#endif
}
var lastColonIndex = addressWithPort.LastIndexOf(':');
if (lastColonIndex > 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/StackExchange.Redis/StackExchange.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<DefineConstants Condition="'$(TargetFramework)' != 'net461'">$(DefineConstants);VECTOR_SAFE</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' != 'net461' and '$(TargetFramework)' != 'net472' and '$(TargetFramework)' != 'netstandard2.0'">$(DefineConstants);UNIX_SOCKET</DefineConstants>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Recommend we define NETCOREAPP3_1_OR_GREATER here, which is landing in a future .NET 5.0 SDK (servicing release) we can remove: https://github.com/dotnet/designs/pull/164/files. This let's us cleanup nicely later.

</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions tests/StackExchange.Redis.Tests/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ public void ConfigurationOptionsIPv6Parsing(string configString, AddressFamily f
Assert.Equal(port, ep.Port);
}

[Fact]
public void CanParseAndFormatUnixDomainSocket()
{
const string ConfigString = "!/some/path,allowAdmin=True";
#if NET472
var ex = Assert.Throws<PlatformNotSupportedException>(() => ConfigurationOptions.Parse(ConfigString));
Assert.Equal("Unix domain sockets require .NET Core 3 or above", ex.Message);
#else
var config = ConfigurationOptions.Parse(ConfigString);
Assert.True(config.AllowAdmin);
var ep = Assert.IsType<UnixDomainSocketEndPoint>(Assert.Single(config.EndPoints));
Assert.Equal("/some/path", ep.ToString());
Assert.Equal(ConfigString, config.ToString());
#endif
}

[Fact]
public void TalkToNonsenseServer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="Microsoft.CSharp" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
</ItemGroup>
</Project>