Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Draft76Handler.cs hacktoberfest-accepted #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
54 changes: 36 additions & 18 deletions src/Fleck/Handlers/Draft76Handler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
Expand All @@ -21,40 +22,56 @@ public static IHandler Create(WebSocketHttpRequest request, Action<string> onMes
ReceiveData = data => ReceiveData(onMessage, data)
};
}

public static void ReceiveData(Action<string> onMessage, List<byte> data)
{
while (data.Count > 0)
{
if (data[0] != Start)
{
FleckLog.Error("Invalid frame start.");
throw new WebSocketException(WebSocketStatusCodes.InvalidFramePayloadData);

}

var endIndex = data.IndexOf(End);
if (endIndex < 0)
{
FleckLog.Warn("End marker not found, waiting for more data.");
return;

}

if (endIndex > MaxSize)
{
FleckLog.Error("Message too big.");
throw new WebSocketException(WebSocketStatusCodes.MessageTooBig);

}

var bytes = data.Skip(1).Take(endIndex - 1).ToArray();

data.RemoveRange(0, endIndex + 1);

var message = Encoding.UTF8.GetString(bytes);

onMessage(message);
}
}

public static byte[] FrameText(string data)
{
//Utilizing ArrayPool<T> reduces heap allocation
//by reusing arrays from a pool. This can improve performance
//by reducing the amount and frequency
//of garbage collections.
byte[] bytes = Encoding.UTF8.GetBytes(data);
// wrap the array with the wrapper bytes
var wrappedBytes = new byte[bytes.Length + 2];
wrappedBytes[0] = Start;
wrappedBytes[wrappedBytes.Length - 1] = End;
Array.Copy(bytes, 0, wrappedBytes, 1, bytes.Length);
return wrappedBytes;
var wrappedBytes = ArrayPool<byte>.Shared.Rent(bytes.Length + 2);

try
{
wrappedBytes[0] = Start;
wrappedBytes[wrappedBytes.Length - 1] = End;
Array.Copy(bytes, 0, wrappedBytes, 1, bytes.Length);
return wrappedBytes;
}
finally
{
ArrayPool<byte>.Shared.Return(wrappedBytes);
}
}

public static byte[] Handshake(WebSocketHttpRequest request, string subProtocol)
Expand Down Expand Up @@ -86,7 +103,6 @@ public static byte[] Handshake(WebSocketHttpRequest request, string subProtocol)

return byteResponse;
}

public static byte[] CalculateAnswerBytes(string key1, string key2, ArraySegment<byte> challenge)
{
byte[] result1Bytes = ParseKey(key1);
Expand All @@ -96,10 +112,12 @@ public static byte[] CalculateAnswerBytes(string key1, string key2, ArraySegment
Array.Copy(result1Bytes, 0, rawAnswer, 0, 4);
Array.Copy(result2Bytes, 0, rawAnswer, 4, 4);
Array.Copy(challenge.Array, challenge.Offset, rawAnswer, 8, 8);

return MD5.Create().ComputeHash(rawAnswer);
//Replaced MD5 with more secure SHA-256, since MD5 is considered cryptographically unsuitable
using (var sha256 = SHA256.Create())
{
return sha256.ComputeHash(rawAnswer);
}
}

private static byte[] ParseKey(string key)
{
int spaces = key.Count(x => x == ' ');
Expand Down