jellyfin-server/SocketHttpListener/PayloadData.cs

131 lines
3.0 KiB
C#
Raw Normal View History

2016-11-11 19:55:12 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace SocketHttpListener
{
internal class PayloadData : IEnumerable<byte>
2016-11-11 19:55:12 +00:00
{
#region Private Fields
private byte[] _applicationData;
private byte[] _extensionData;
private bool _masked;
2016-11-11 19:55:12 +00:00
#endregion
#region Public Const Fields
public const ulong MaxLength = long.MaxValue;
#endregion
#region Public Constructors
public PayloadData()
: this(new byte[0], new byte[0], false)
{
}
public PayloadData(byte[] applicationData)
: this(new byte[0], applicationData, false)
{
}
2016-11-11 19:55:12 +00:00
public PayloadData(string applicationData)
: this(new byte[0], Encoding.UTF8.GetBytes(applicationData), false)
{
}
public PayloadData(byte[] applicationData, bool masked)
: this(new byte[0], applicationData, masked)
{
}
public PayloadData(byte[] extensionData, byte[] applicationData, bool masked)
{
_extensionData = extensionData;
_applicationData = applicationData;
_masked = masked;
}
2016-11-11 19:55:12 +00:00
#endregion
#region Internal Properties
2016-11-11 19:55:12 +00:00
internal bool ContainsReservedCloseStatusCode =>
_applicationData.Length > 1 &&
_applicationData.SubArray(0, 2).ToUInt16(ByteOrder.Big).IsReserved();
2016-11-11 19:55:12 +00:00
#endregion
2016-11-11 19:55:12 +00:00
#region Public Properties
2016-11-11 19:55:12 +00:00
public byte[] ApplicationData => _applicationData;
2016-11-11 19:55:12 +00:00
public byte[] ExtensionData => _extensionData;
2016-11-11 19:55:12 +00:00
public bool IsMasked => _masked;
2016-11-11 19:55:12 +00:00
public ulong Length => (ulong)(_extensionData.Length + _applicationData.Length);
2016-11-11 19:55:12 +00:00
#endregion
2016-11-11 19:55:12 +00:00
#region Private Methods
2016-11-11 19:55:12 +00:00
private static void mask(byte[] src, byte[] key)
{
for (long i = 0; i < src.Length; i++)
src[i] = (byte)(src[i] ^ key[i % 4]);
}
2016-11-11 19:55:12 +00:00
#endregion
2016-11-11 19:55:12 +00:00
#region Public Methods
2016-11-11 19:55:12 +00:00
public IEnumerator<byte> GetEnumerator()
{
foreach (byte b in _extensionData)
yield return b;
foreach (byte b in _applicationData)
yield return b;
}
public void Mask(byte[] maskingKey)
{
if (_extensionData.Length > 0)
mask(_extensionData, maskingKey);
if (_applicationData.Length > 0)
mask(_applicationData, maskingKey);
_masked = !_masked;
}
public byte[] ToByteArray()
{
return _extensionData.Length > 0
? new List<byte>(this).ToArray()
: _applicationData;
}
public override string ToString()
{
return BitConverter.ToString(ToByteArray());
}
#endregion
#region Explicitly Implemented Interface Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
2016-11-11 19:55:12 +00:00
}
}