Skip to content

Commit

Permalink
Fix Bug: .NetFramework 4.8 ClientWebSocket Cann't Connect To .Net8 Ws…
Browse files Browse the repository at this point in the history
…Session #278
  • Loading branch information
chronoxor committed Dec 23, 2023
1 parent b6dad1b commit 03a81a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
9 changes: 0 additions & 9 deletions source/NetCoreServer/FileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,4 @@ public WriteLock(ReaderWriterLockSlim locker) : base(locker.ExitWriteLock)
locker.EnterWriteLock();
}
}

/// <summary>
/// String extensions utility class.
/// </summary>
public static class StringExtensions
{
public static string RemoveSuffix(this string str, char toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - 1) : str;
public static string RemoveSuffix(this string str, string toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - toRemove.Length) : str;
}
}
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>8.0.4.0</Version>
<Version>8.0.5.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2023 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
15 changes: 15 additions & 0 deletions source/NetCoreServer/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Linq;

namespace NetCoreServer
{
/// <summary>
/// String extensions utility class.
/// </summary>
public static class StringExtensions
{
public static string RemoveSuffix(this string self, char toRemove) => string.IsNullOrEmpty(self) ? self : (self.EndsWith(toRemove) ? self.Substring(0, self.Length - 1) : self);
public static string RemoveSuffix(this string self, string toRemove) => string.IsNullOrEmpty(self) ? self : (self.EndsWith(toRemove) ? self.Substring(0, self.Length - toRemove.Length) : self);
public static string RemoveWhiteSpace(this string self) => string.IsNullOrEmpty(self) ? self : new string(self.Where(c => !Char.IsWhiteSpace(c)).ToArray());
}
}
12 changes: 10 additions & 2 deletions source/NetCoreServer/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Threading;
using System.Linq;

namespace NetCoreServer
{
Expand Down Expand Up @@ -158,10 +159,17 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)

if (string.Compare(key, "Connection", StringComparison.OrdinalIgnoreCase) == 0)
{
if ((string.Compare(value, "Upgrade", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(value, "keep-alive, Upgrade", StringComparison.OrdinalIgnoreCase) != 0))
var values = value.Split(',').Select(str => str.Trim()).ToArray();
if ((values.Length == 0) ||
((values.Length == 1) && (string.Compare(values[0], "Upgrade", StringComparison.OrdinalIgnoreCase) != 0)) ||
((values.Length == 2) && !(
((string.Compare(values[0], "Upgrade", StringComparison.OrdinalIgnoreCase) == 0) && (string.Compare(values[1], "keep-alive", StringComparison.OrdinalIgnoreCase) == 0)) ||
((string.Compare(values[0], "keep-alive", StringComparison.OrdinalIgnoreCase) == 0) && (string.Compare(values[1], "Upgrade", StringComparison.OrdinalIgnoreCase) == 0))
)) ||
(values.Length > 2))
{
error = true;
response.MakeErrorResponse(400, "Invalid WebSocket handshaked request: 'Connection' header value must be 'Upgrade' or 'keep-alive, Upgrade'");
response.MakeErrorResponse(400, "Invalid WebSocket handshaked request: 'Connection' header value must be 'Upgrade' or 'Upgrade, keep-alive'");
break;
}

Expand Down

0 comments on commit 03a81a2

Please sign in to comment.