commit
9556561a77
@ -1,20 +0,0 @@
|
||||
---
|
||||
name: Enhancement request
|
||||
about: Suggest an modification to an existing feature
|
||||
title: ''
|
||||
labels: enhancement
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->
|
||||
|
||||
**Describe the solution you'd like**
|
||||
<!-- A clear and concise description of what you want to happen. -->
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
<!-- A clear and concise description of any alternative solutions or features you've considered. -->
|
||||
|
||||
**Additional context**
|
||||
<!-- Add any other context or screenshots about the feature request here. -->
|
@ -1,14 +0,0 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest a new feature
|
||||
title: ''
|
||||
labels: feature
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the feature you'd like**
|
||||
<!-- A clear and concise description of what you want to happen. -->
|
||||
|
||||
**Additional context**
|
||||
<!-- Add any other context or screenshots about the feature request here. -->
|
@ -0,0 +1,22 @@
|
||||
# Number of days of inactivity before an issue becomes stale
|
||||
daysUntilStale: 90
|
||||
# Number of days of inactivity before a stale issue is closed
|
||||
daysUntilClose: 14
|
||||
# Issues with these labels will never be considered stale
|
||||
exemptLabels:
|
||||
- regression
|
||||
- security
|
||||
- dotnet-3.0-future
|
||||
- roadmap
|
||||
- future
|
||||
- feature
|
||||
- enhancement
|
||||
# Label to use when marking an issue as stale
|
||||
staleLabel: stale
|
||||
# Comment to post when marking an issue as stale. Set to `false` to disable
|
||||
markComment: >
|
||||
Issues go stale after 90d of inactivity. Mark the issue as fresh by adding a comment or commit. Stale issues close after an additional 14d of inactivity.
|
||||
If this issue is safe to close now please do so.
|
||||
If you have any questions you can reach us on [Matrix or Social Media](https://jellyfin.readthedocs.io/en/latest/getting-help/).
|
||||
# Comment to post when closing a stale issue. Set to `false` to disable
|
||||
closeComment: false
|
@ -1,132 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Authentication;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Cryptography;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using MediaBrowser.Model.Users;
|
||||
|
||||
namespace Emby.Server.Implementations.Library
|
||||
{
|
||||
public class DefaultPasswordResetProvider : IPasswordResetProvider
|
||||
{
|
||||
public string Name => "Default Password Reset Provider";
|
||||
|
||||
public bool IsEnabled => true;
|
||||
|
||||
private readonly string _passwordResetFileBase;
|
||||
private readonly string _passwordResetFileBaseDir;
|
||||
private readonly string _passwordResetFileBaseName = "passwordreset";
|
||||
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly ICryptoProvider _crypto;
|
||||
|
||||
public DefaultPasswordResetProvider(IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IUserManager userManager, ICryptoProvider cryptoProvider)
|
||||
{
|
||||
_passwordResetFileBaseDir = configurationManager.ApplicationPaths.ProgramDataPath;
|
||||
_passwordResetFileBase = Path.Combine(_passwordResetFileBaseDir, _passwordResetFileBaseName);
|
||||
_jsonSerializer = jsonSerializer;
|
||||
_userManager = userManager;
|
||||
_crypto = cryptoProvider;
|
||||
}
|
||||
|
||||
public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
|
||||
{
|
||||
SerializablePasswordReset spr;
|
||||
HashSet<string> usersreset = new HashSet<string>();
|
||||
foreach (var resetfile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{_passwordResetFileBaseName}*"))
|
||||
{
|
||||
using (var str = File.OpenRead(resetfile))
|
||||
{
|
||||
spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (spr.ExpirationDate < DateTime.Now)
|
||||
{
|
||||
File.Delete(resetfile);
|
||||
}
|
||||
else if (spr.Pin.Replace("-", "").Equals(pin.Replace("-", ""), StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
var resetUser = _userManager.GetUserByName(spr.UserName);
|
||||
if (resetUser == null)
|
||||
{
|
||||
throw new Exception($"User with a username of {spr.UserName} not found");
|
||||
}
|
||||
|
||||
await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
|
||||
usersreset.Add(resetUser.Name);
|
||||
File.Delete(resetfile);
|
||||
}
|
||||
}
|
||||
|
||||
if (usersreset.Count < 1)
|
||||
{
|
||||
throw new ResourceNotFoundException($"No Users found with a password reset request matching pin {pin}");
|
||||
}
|
||||
else
|
||||
{
|
||||
return new PinRedeemResult
|
||||
{
|
||||
Success = true,
|
||||
UsersReset = usersreset.ToArray()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ForgotPasswordResult> StartForgotPasswordProcess(MediaBrowser.Controller.Entities.User user, bool isInNetwork)
|
||||
{
|
||||
string pin = string.Empty;
|
||||
using (var cryptoRandom = System.Security.Cryptography.RandomNumberGenerator.Create())
|
||||
{
|
||||
byte[] bytes = new byte[4];
|
||||
cryptoRandom.GetBytes(bytes);
|
||||
pin = BitConverter.ToString(bytes);
|
||||
}
|
||||
|
||||
DateTime expireTime = DateTime.Now.AddMinutes(30);
|
||||
string filePath = _passwordResetFileBase + user.InternalId + ".json";
|
||||
SerializablePasswordReset spr = new SerializablePasswordReset
|
||||
{
|
||||
ExpirationDate = expireTime,
|
||||
Pin = pin,
|
||||
PinFile = filePath,
|
||||
UserName = user.Name
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (FileStream fileStream = File.OpenWrite(filePath))
|
||||
{
|
||||
_jsonSerializer.SerializeToStream(spr, fileStream);
|
||||
await fileStream.FlushAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Error serializing or writing password reset for {user.Name} to location: {filePath}", e);
|
||||
}
|
||||
|
||||
return new ForgotPasswordResult
|
||||
{
|
||||
Action = ForgotPasswordAction.PinCode,
|
||||
PinExpirationDate = expireTime,
|
||||
PinFile = filePath
|
||||
};
|
||||
}
|
||||
|
||||
private class SerializablePasswordReset : PasswordPinCreationResult
|
||||
{
|
||||
public string Pin { get; set; }
|
||||
|
||||
public string UserName { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Authentication;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Cryptography;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using MediaBrowser.Model.Users;
|
||||
|
||||
namespace Emby.Server.Implementations.Library
|
||||
{
|
||||
public class DefaultPasswordResetProvider : IPasswordResetProvider
|
||||
{
|
||||
public string Name => "Default Password Reset Provider";
|
||||
|
||||
public bool IsEnabled => true;
|
||||
|
||||
private readonly string _passwordResetFileBase;
|
||||
private readonly string _passwordResetFileBaseDir;
|
||||
private readonly string _passwordResetFileBaseName = "passwordreset";
|
||||
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly ICryptoProvider _crypto;
|
||||
|
||||
public DefaultPasswordResetProvider(IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IUserManager userManager, ICryptoProvider cryptoProvider)
|
||||
{
|
||||
_passwordResetFileBaseDir = configurationManager.ApplicationPaths.ProgramDataPath;
|
||||
_passwordResetFileBase = Path.Combine(_passwordResetFileBaseDir, _passwordResetFileBaseName);
|
||||
_jsonSerializer = jsonSerializer;
|
||||
_userManager = userManager;
|
||||
_crypto = cryptoProvider;
|
||||
}
|
||||
|
||||
public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
|
||||
{
|
||||
SerializablePasswordReset spr;
|
||||
HashSet<string> usersreset = new HashSet<string>();
|
||||
foreach (var resetfile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{_passwordResetFileBaseName}*"))
|
||||
{
|
||||
using (var str = File.OpenRead(resetfile))
|
||||
{
|
||||
spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (spr.ExpirationDate < DateTime.Now)
|
||||
{
|
||||
File.Delete(resetfile);
|
||||
}
|
||||
else if (spr.Pin.Replace("-", "").Equals(pin.Replace("-", ""), StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
var resetUser = _userManager.GetUserByName(spr.UserName);
|
||||
if (resetUser == null)
|
||||
{
|
||||
throw new Exception($"User with a username of {spr.UserName} not found");
|
||||
}
|
||||
|
||||
await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
|
||||
usersreset.Add(resetUser.Name);
|
||||
File.Delete(resetfile);
|
||||
}
|
||||
}
|
||||
|
||||
if (usersreset.Count < 1)
|
||||
{
|
||||
throw new ResourceNotFoundException($"No Users found with a password reset request matching pin {pin}");
|
||||
}
|
||||
else
|
||||
{
|
||||
return new PinRedeemResult
|
||||
{
|
||||
Success = true,
|
||||
UsersReset = usersreset.ToArray()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ForgotPasswordResult> StartForgotPasswordProcess(MediaBrowser.Controller.Entities.User user, bool isInNetwork)
|
||||
{
|
||||
string pin = string.Empty;
|
||||
using (var cryptoRandom = System.Security.Cryptography.RandomNumberGenerator.Create())
|
||||
{
|
||||
byte[] bytes = new byte[4];
|
||||
cryptoRandom.GetBytes(bytes);
|
||||
pin = BitConverter.ToString(bytes);
|
||||
}
|
||||
|
||||
DateTime expireTime = DateTime.Now.AddMinutes(30);
|
||||
string filePath = _passwordResetFileBase + user.InternalId + ".json";
|
||||
SerializablePasswordReset spr = new SerializablePasswordReset
|
||||
{
|
||||
ExpirationDate = expireTime,
|
||||
Pin = pin,
|
||||
PinFile = filePath,
|
||||
UserName = user.Name
|
||||
};
|
||||
|
||||
using (FileStream fileStream = File.OpenWrite(filePath))
|
||||
{
|
||||
_jsonSerializer.SerializeToStream(spr, fileStream);
|
||||
await fileStream.FlushAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return new ForgotPasswordResult
|
||||
{
|
||||
Action = ForgotPasswordAction.PinCode,
|
||||
PinExpirationDate = expireTime,
|
||||
PinFile = filePath
|
||||
};
|
||||
}
|
||||
|
||||
private class SerializablePasswordReset : PasswordPinCreationResult
|
||||
{
|
||||
public string Pin { get; set; }
|
||||
|
||||
public string UserName { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,167 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
|
||||
namespace Emby.Server.Implementations.Networking.IPNetwork
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods to convert <see cref="BigInteger"/>
|
||||
/// instances to hexadecimal, octal, and binary strings.
|
||||
/// </summary>
|
||||
public static class BigIntegerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="BigInteger"/> to a binary string.
|
||||
/// </summary>
|
||||
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="string"/> containing a binary
|
||||
/// representation of the supplied <see cref="BigInteger"/>.
|
||||
/// </returns>
|
||||
public static string ToBinaryString(this BigInteger bigint)
|
||||
{
|
||||
var bytes = bigint.ToByteArray();
|
||||
var idx = bytes.Length - 1;
|
||||
|
||||
// Create a StringBuilder having appropriate capacity.
|
||||
var base2 = new StringBuilder(bytes.Length * 8);
|
||||
|
||||
// Convert first byte to binary.
|
||||
var binary = Convert.ToString(bytes[idx], 2);
|
||||
|
||||
// Ensure leading zero exists if value is positive.
|
||||
if (binary[0] != '0' && bigint.Sign == 1)
|
||||
{
|
||||
base2.Append('0');
|
||||
}
|
||||
|
||||
// Append binary string to StringBuilder.
|
||||
base2.Append(binary);
|
||||
|
||||
// Convert remaining bytes adding leading zeros.
|
||||
for (idx--; idx >= 0; idx--)
|
||||
{
|
||||
base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
|
||||
}
|
||||
|
||||
return base2.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="BigInteger"/> to a hexadecimal string.
|
||||
/// </summary>
|
||||
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="string"/> containing a hexadecimal
|
||||
/// representation of the supplied <see cref="BigInteger"/>.
|
||||
/// </returns>
|
||||
public static string ToHexadecimalString(this BigInteger bigint)
|
||||
{
|
||||
return bigint.ToString("X");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="BigInteger"/> to a octal string.
|
||||
/// </summary>
|
||||
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="string"/> containing an octal
|
||||
/// representation of the supplied <see cref="BigInteger"/>.
|
||||
/// </returns>
|
||||
public static string ToOctalString(this BigInteger bigint)
|
||||
{
|
||||
var bytes = bigint.ToByteArray();
|
||||
var idx = bytes.Length - 1;
|
||||
|
||||
// Create a StringBuilder having appropriate capacity.
|
||||
var base8 = new StringBuilder(((bytes.Length / 3) + 1) * 8);
|
||||
|
||||
// Calculate how many bytes are extra when byte array is split
|
||||
// into three-byte (24-bit) chunks.
|
||||
var extra = bytes.Length % 3;
|
||||
|
||||
// If no bytes are extra, use three bytes for first chunk.
|
||||
if (extra == 0)
|
||||
{
|
||||
extra = 3;
|
||||
}
|
||||
|
||||
// Convert first chunk (24-bits) to integer value.
|
||||
int int24 = 0;
|
||||
for (; extra != 0; extra--)
|
||||
{
|
||||
int24 <<= 8;
|
||||
int24 += bytes[idx--];
|
||||
}
|
||||
|
||||
// Convert 24-bit integer to octal without adding leading zeros.
|
||||
var octal = Convert.ToString(int24, 8);
|
||||
|
||||
// Ensure leading zero exists if value is positive.
|
||||
if (octal[0] != '0')
|
||||
{
|
||||
if (bigint.Sign == 1)
|
||||
{
|
||||
base8.Append('0');
|
||||
}
|
||||
}
|
||||
|
||||
// Append first converted chunk to StringBuilder.
|
||||
base8.Append(octal);
|
||||
|
||||
// Convert remaining 24-bit chunks, adding leading zeros.
|
||||
for (; idx >= 0; idx -= 3)
|
||||
{
|
||||
int24 = (bytes[idx] << 16) + (bytes[idx - 1] << 8) + bytes[idx - 2];
|
||||
base8.Append(Convert.ToString(int24, 8).PadLeft(8, '0'));
|
||||
}
|
||||
|
||||
return base8.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Reverse a Positive BigInteger ONLY
|
||||
/// Bitwise ~ operator
|
||||
///
|
||||
/// Input : FF FF FF FF
|
||||
/// Width : 4
|
||||
/// Result : 00 00 00 00
|
||||
///
|
||||
///
|
||||
/// Input : 00 00 00 00
|
||||
/// Width : 4
|
||||
/// Result : FF FF FF FF
|
||||
///
|
||||
/// Input : FF FF FF FF
|
||||
/// Width : 8
|
||||
/// Result : FF FF FF FF 00 00 00 00
|
||||
///
|
||||
///
|
||||
/// Input : 00 00 00 00
|
||||
/// Width : 8
|
||||
/// Result : FF FF FF FF FF FF FF FF
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <returns></returns>
|
||||
public static BigInteger PositiveReverse(this BigInteger input, int width)
|
||||
{
|
||||
|
||||
var result = new List<byte>();
|
||||
var bytes = input.ToByteArray();
|
||||
var work = new byte[width];
|
||||
Array.Copy(bytes, 0, work, 0, bytes.Length - 1); // Length -1 : positive BigInteger
|
||||
|
||||
for (int i = 0; i < work.Length; i++)
|
||||
{
|
||||
result.Add((byte)(~work[i]));
|
||||
}
|
||||
result.Add(0); // positive BigInteger
|
||||
return new BigInteger(result.ToArray());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Emby.Server.Implementations.Networking.IPNetwork
|
||||
{
|
||||
public class IPAddressCollection : IEnumerable<IPAddress>, IEnumerator<IPAddress>
|
||||
{
|
||||
|
||||
private IPNetwork _ipnetwork;
|
||||
private BigInteger _enumerator;
|
||||
|
||||
internal IPAddressCollection(IPNetwork ipnetwork)
|
||||
{
|
||||
this._ipnetwork = ipnetwork;
|
||||
this._enumerator = -1;
|
||||
}
|
||||
|
||||
|
||||
#region Count, Array, Enumerator
|
||||
|
||||
public BigInteger Count => this._ipnetwork.Total;
|
||||
|
||||
public IPAddress this[BigInteger i]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (i >= this.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(i));
|
||||
}
|
||||
byte width = this._ipnetwork.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128;
|
||||
var ipn = this._ipnetwork.Subnet(width);
|
||||
return ipn[i].Network;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
IEnumerator<IPAddress> IEnumerable<IPAddress>.GetEnumerator()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
#region IEnumerator<IPNetwork> Members
|
||||
|
||||
public IPAddress Current => this[this._enumerator];
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// nothing to dispose
|
||||
return;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerator Members
|
||||
|
||||
object IEnumerator.Current => this.Current;
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
this._enumerator++;
|
||||
if (this._enumerator >= this.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
this._enumerator = -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,129 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Emby.Server.Implementations.Networking.IPNetwork
|
||||
{
|
||||
public class IPNetworkCollection : IEnumerable<IPNetwork>, IEnumerator<IPNetwork>
|
||||
{
|
||||
|
||||
private BigInteger _enumerator;
|
||||
private byte _cidrSubnet;
|
||||
private IPNetwork _ipnetwork;
|
||||
|
||||
private byte _cidr => this._ipnetwork.Cidr;
|
||||
|
||||
private BigInteger _broadcast => IPNetwork.ToBigInteger(this._ipnetwork.Broadcast);
|
||||
|
||||
private BigInteger _lastUsable => IPNetwork.ToBigInteger(this._ipnetwork.LastUsable);
|
||||
private BigInteger _network => IPNetwork.ToBigInteger(this._ipnetwork.Network);
|
||||
#if TRAVISCI
|
||||
public
|
||||
#else
|
||||
internal
|
||||
#endif
|
||||
IPNetworkCollection(IPNetwork ipnetwork, byte cidrSubnet)
|
||||
{
|
||||
|
||||
int maxCidr = ipnetwork.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? 32 : 128;
|
||||
if (cidrSubnet > maxCidr)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(cidrSubnet));
|
||||
}
|
||||
|
||||
if (cidrSubnet < ipnetwork.Cidr)
|
||||
{
|
||||
throw new ArgumentException("cidr");
|
||||
}
|
||||
|
||||
this._cidrSubnet = cidrSubnet;
|
||||
this._ipnetwork = ipnetwork;
|
||||
this._enumerator = -1;
|
||||
}
|
||||
|
||||
#region Count, Array, Enumerator
|
||||
|
||||
public BigInteger Count
|
||||
{
|
||||
get
|
||||
{
|
||||
var count = BigInteger.Pow(2, this._cidrSubnet - this._cidr);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public IPNetwork this[BigInteger i]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (i >= this.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(i));
|
||||
}
|
||||
|
||||
var last = this._ipnetwork.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6
|
||||
? this._lastUsable : this._broadcast;
|
||||
var increment = (last - this._network) / this.Count;
|
||||
var uintNetwork = this._network + ((increment + 1) * i);
|
||||
var ipn = new IPNetwork(uintNetwork, this._ipnetwork.AddressFamily, this._cidrSubnet);
|
||||
return ipn;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
IEnumerator<IPNetwork> IEnumerable<IPNetwork>.GetEnumerator()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
#region IEnumerator<IPNetwork> Members
|
||||
|
||||
public IPNetwork Current => this[this._enumerator];
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// nothing to dispose
|
||||
return;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerator Members
|
||||
|
||||
object IEnumerator.Current => this.Current;
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
this._enumerator++;
|
||||
if (this._enumerator >= this.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
this._enumerator = -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
Copyright (c) 2015, lduchosal
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
@ -1,647 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.Services;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Emby.Server.Implementations.SocketSharp
|
||||
{
|
||||
public partial class WebSocketSharpRequest : IHttpRequest
|
||||
{
|
||||
internal static string GetParameter(ReadOnlySpan<char> header, string attr)
|
||||
{
|
||||
int ap = header.IndexOf(attr.AsSpan(), StringComparison.Ordinal);
|
||||
if (ap == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ap += attr.Length;
|
||||
if (ap >= header.Length)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
char ending = header[ap];
|
||||
if (ending != '"')
|
||||
{
|
||||
ending = ' ';
|
||||
}
|
||||
|
||||
var slice = header.Slice(ap + 1);
|
||||
int end = slice.IndexOf(ending);
|
||||
if (end == -1)
|
||||
{
|
||||
return ending == '"' ? null : header.Slice(ap).ToString();
|
||||
}
|
||||
|
||||
return slice.Slice(0, end - ap - 1).ToString();
|
||||
}
|
||||
|
||||
private async Task LoadMultiPart(WebROCollection form)
|
||||
{
|
||||
string boundary = GetParameter(ContentType.AsSpan(), "; boundary=");
|
||||
if (boundary == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using (var requestStream = InputStream)
|
||||
{
|
||||
// DB: 30/01/11 - Hack to get around non-seekable stream and received HTTP request
|
||||
// Not ending with \r\n?
|
||||
var ms = new MemoryStream(32 * 1024);
|
||||
await requestStream.CopyToAsync(ms).ConfigureAwait(false);
|
||||
|
||||
var input = ms;
|
||||
ms.WriteByte((byte)'\r');
|
||||
ms.WriteByte((byte)'\n');
|
||||
|
||||
input.Position = 0;
|
||||
|
||||
// Uncomment to debug
|
||||
// var content = new StreamReader(ms).ReadToEnd();
|
||||
// Console.WriteLine(boundary + "::" + content);
|
||||
// input.Position = 0;
|
||||
|
||||
var multi_part = new HttpMultipart(input, boundary, ContentEncoding);
|
||||
|
||||
HttpMultipart.Element e;
|
||||
while ((e = multi_part.ReadNextElement()) != null)
|
||||
{
|
||||
if (e.Filename == null)
|
||||
{
|
||||
byte[] copy = new byte[e.Length];
|
||||
|
||||
input.Position = e.Start;
|
||||
await input.ReadAsync(copy, 0, (int)e.Length).ConfigureAwait(false);
|
||||
|
||||
form.Add(e.Name, (e.Encoding ?? ContentEncoding).GetString(copy, 0, copy.Length));
|
||||
}
|
||||
else
|
||||
{
|
||||
// We use a substream, as in 2.x we will support large uploads streamed to disk,
|
||||
files[e.Name] = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<QueryParamCollection> GetFormData()
|
||||
{
|
||||
var form = new WebROCollection();
|
||||
files = new Dictionary<string, HttpPostedFile>();
|
||||
|
||||
if (IsContentType("multipart/form-data"))
|
||||
{
|
||||
await LoadMultiPart(form).ConfigureAwait(false);
|
||||
}
|
||||
else if (IsContentType("application/x-www-form-urlencoded"))
|
||||
{
|
||||
await LoadWwwForm(form).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (validate_form && !checked_form)
|
||||
{
|
||||
checked_form = true;
|
||||
ValidateNameValueCollection("Form", form);
|
||||
}
|
||||
|
||||
return form;
|
||||
}
|
||||
|
||||
public string Accept => StringValues.IsNullOrEmpty(request.Headers[HeaderNames.Accept]) ? null : request.Headers[HeaderNames.Accept].ToString();
|
||||
|
||||
public string Authorization => StringValues.IsNullOrEmpty(request.Headers[HeaderNames.Authorization]) ? null : request.Headers[HeaderNames.Authorization].ToString();
|
||||
|
||||
protected bool validate_form { get; set; }
|
||||
protected bool checked_form { get; set; }
|
||||
|
||||
private static void ThrowValidationException(string name, string key, string value)
|
||||
{
|
||||
string v = "\"" + value + "\"";
|
||||
if (v.Length > 20)
|
||||
{
|
||||
v = v.Substring(0, 16) + "...\"";
|
||||
}
|
||||
|
||||
string msg = string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"A potentially dangerous Request.{0} value was detected from the client ({1}={2}).",
|
||||
name,
|
||||
key,
|
||||
v);
|
||||
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
private static void ValidateNameValueCollection(string name, QueryParamCollection coll)
|
||||
{
|
||||
if (coll == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var pair in coll)
|
||||
{
|
||||
var key = pair.Name;
|
||||
var val = pair.Value;
|
||||
if (val != null && val.Length > 0 && IsInvalidString(val))
|
||||
{
|
||||
ThrowValidationException(name, key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool IsInvalidString(string val)
|
||||
=> IsInvalidString(val, out var validationFailureIndex);
|
||||
|
||||
internal static bool IsInvalidString(string val, out int validationFailureIndex)
|
||||
{
|
||||
validationFailureIndex = 0;
|
||||
|
||||
int len = val.Length;
|
||||
if (len < 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
char current = val[0];
|
||||
for (int idx = 1; idx < len; idx++)
|
||||
{
|
||||
char next = val[idx];
|
||||
|
||||
// See http://secunia.com/advisories/14325
|
||||
if (current == '<' || current == '\xff1c')
|
||||
{
|
||||
if (next == '!' || next < ' '
|
||||
|| (next >= 'a' && next <= 'z')
|
||||
|| (next >= 'A' && next <= 'Z'))
|
||||
{
|
||||
validationFailureIndex = idx - 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (current == '&' && next == '#')
|
||||
{
|
||||
validationFailureIndex = idx - 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
current = next;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool IsContentType(string ct)
|
||||
{
|
||||
if (ContentType == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return ContentType.StartsWith(ct, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private async Task LoadWwwForm(WebROCollection form)
|
||||
{
|
||||
using (var input = InputStream)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
await input.CopyToAsync(ms).ConfigureAwait(false);
|
||||
ms.Position = 0;
|
||||
|
||||
using (var s = new StreamReader(ms, ContentEncoding))
|
||||
{
|
||||
var key = new StringBuilder();
|
||||
var value = new StringBuilder();
|
||||
int c;
|
||||
|
||||
while ((c = s.Read()) != -1)
|
||||
{
|
||||
if (c == '=')
|
||||
{
|
||||
value.Length = 0;
|
||||
while ((c = s.Read()) != -1)
|
||||
{
|
||||
if (c == '&')
|
||||
{
|
||||
AddRawKeyValue(form, key, value);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
value.Append((char)c);
|
||||
}
|
||||
}
|
||||
|
||||
if (c == -1)
|
||||
{
|
||||
AddRawKeyValue(form, key, value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (c == '&')
|
||||
{
|
||||
AddRawKeyValue(form, key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
key.Append((char)c);
|
||||
}
|
||||
}
|
||||
|
||||
if (c == -1)
|
||||
{
|
||||
AddRawKeyValue(form, key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value)
|
||||
{
|
||||
form.Add(WebUtility.UrlDecode(key.ToString()), WebUtility.UrlDecode(value.ToString()));
|
||||
|
||||
key.Length = 0;
|
||||
value.Length = 0;
|
||||
}
|
||||
|
||||
private Dictionary<string, HttpPostedFile> files;
|
||||
|
||||
private class WebROCollection : QueryParamCollection
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
var result = new StringBuilder();
|
||||
foreach (var pair in this)
|
||||
{
|
||||
if (result.Length > 0)
|
||||
{
|
||||
result.Append('&');
|
||||
}
|
||||
|
||||
var key = pair.Name;
|
||||
if (key != null && key.Length > 0)
|
||||
{
|
||||
result.Append(key);
|
||||
result.Append('=');
|
||||
}
|
||||
|
||||
result.Append(pair.Value);
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
private class HttpMultipart
|
||||
{
|
||||
|
||||
public class Element
|
||||
{
|
||||
public string ContentType { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Filename { get; set; }
|
||||
|
||||
public Encoding Encoding { get; set; }
|
||||
|
||||
public long Start { get; set; }
|
||||
|
||||
public long Length { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "ContentType " + ContentType + ", Name " + Name + ", Filename " + Filename + ", Start " +
|
||||
Start.ToString(CultureInfo.CurrentCulture) + ", Length " + Length.ToString(CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
|
||||
private const byte LF = (byte)'\n';
|
||||
|
||||
private const byte CR = (byte)'\r';
|
||||
|
||||
private Stream data;
|
||||
|
||||
private string boundary;
|
||||
|
||||
private byte[] boundaryBytes;
|
||||
|
||||
private byte[] buffer;
|
||||
|
||||
private bool atEof;
|
||||
|
||||
private Encoding encoding;
|
||||
|
||||
private StringBuilder sb;
|
||||
|
||||
// See RFC 2046
|
||||
// In the case of multipart entities, in which one or more different
|
||||
// sets of data are combined in a single body, a "multipart" media type
|
||||
// field must appear in the entity's header. The body must then contain
|
||||
// one or more body parts, each preceded by a boundary delimiter line,
|
||||
// and the last one followed by a closing boundary delimiter line.
|
||||
// After its boundary delimiter line, each body part then consists of a
|
||||
// header area, a blank line, and a body area. Thus a body part is
|
||||
// similar to an RFC 822 message in syntax, but different in meaning.
|
||||
|
||||
public HttpMultipart(Stream data, string b, Encoding encoding)
|
||||
{
|
||||
this.data = data;
|
||||
boundary = b;
|
||||
boundaryBytes = encoding.GetBytes(b);
|
||||
buffer = new byte[boundaryBytes.Length + 2]; // CRLF or '--'
|
||||
this.encoding = encoding;
|
||||
sb = new StringBuilder();
|
||||
}
|
||||
|
||||
public Element ReadNextElement()
|
||||
{
|
||||
if (atEof || ReadBoundary())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var elem = new Element();
|
||||
ReadOnlySpan<char> header;
|
||||
while ((header = ReadLine().AsSpan()).Length != 0)
|
||||
{
|
||||
if (header.StartsWith("Content-Disposition:".AsSpan(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
elem.Name = GetContentDispositionAttribute(header, "name");
|
||||
elem.Filename = StripPath(GetContentDispositionAttributeWithEncoding(header, "filename"));
|
||||
}
|
||||
else if (header.StartsWith("Content-Type:".AsSpan(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
elem.ContentType = header.Slice("Content-Type:".Length).Trim().ToString();
|
||||
elem.Encoding = GetEncoding(elem.ContentType);
|
||||
}
|
||||
}
|
||||
|
||||
long start = data.Position;
|
||||
elem.Start = start;
|
||||
long pos = MoveToNextBoundary();
|
||||
if (pos == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
elem.Length = pos - start;
|
||||
return elem;
|
||||
}
|
||||
|
||||
private string ReadLine()
|
||||
{
|
||||
// CRLF or LF are ok as line endings.
|
||||
bool got_cr = false;
|
||||
int b = 0;
|
||||
sb.Length = 0;
|
||||
while (true)
|
||||
{
|
||||
b = data.ReadByte();
|
||||
if (b == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (b == LF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
got_cr = b == CR;
|
||||
sb.Append((char)b);
|
||||
}
|
||||
|
||||
if (got_cr)
|
||||
{
|
||||
sb.Length--;
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static string GetContentDispositionAttribute(ReadOnlySpan<char> l, string name)
|
||||
{
|
||||
int idx = l.IndexOf((name + "=\"").AsSpan(), StringComparison.Ordinal);
|
||||
if (idx < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int begin = idx + name.Length + "=\"".Length;
|
||||
int end = l.Slice(begin).IndexOf('"');
|
||||
if (end < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (begin == end)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return l.Slice(begin, end - begin).ToString();
|
||||
}
|
||||
|
||||
private string GetContentDispositionAttributeWithEncoding(ReadOnlySpan<char> l, string name)
|
||||
{
|
||||
int idx = l.IndexOf((name + "=\"").AsSpan(), StringComparison.Ordinal);
|
||||
if (idx < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int begin = idx + name.Length + "=\"".Length;
|
||||
int end = l.Slice(begin).IndexOf('"');
|
||||
if (end < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (begin == end)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
ReadOnlySpan<char> temp = l.Slice(begin, end - begin);
|
||||
byte[] source = new byte[temp.Length];
|
||||
for (int i = temp.Length - 1; i >= 0; i--)
|
||||
{
|
||||
source[i] = (byte)temp[i];
|
||||
}
|
||||
|
||||
return encoding.GetString(source, 0, source.Length);
|
||||
}
|
||||
|
||||
private bool ReadBoundary()
|
||||
{
|
||||
try
|
||||
{
|
||||
string line;
|
||||
do
|
||||
{
|
||||
line = ReadLine();
|
||||
}
|
||||
while (line.Length == 0);
|
||||
|
||||
if (line[0] != '-' || line[1] != '-')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!line.EndsWith(boundary, StringComparison.Ordinal))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool CompareBytes(byte[] orig, byte[] other)
|
||||
{
|
||||
for (int i = orig.Length - 1; i >= 0; i--)
|
||||
{
|
||||
if (orig[i] != other[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private long MoveToNextBoundary()
|
||||
{
|
||||
long retval = 0;
|
||||
bool got_cr = false;
|
||||
|
||||
int state = 0;
|
||||
int c = data.ReadByte();
|
||||
while (true)
|
||||
{
|
||||
if (c == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (state == 0 && c == LF)
|
||||
{
|
||||
retval = data.Position - 1;
|
||||
if (got_cr)
|
||||
{
|
||||
retval--;
|
||||
}
|
||||
|
||||
state = 1;
|
||||
c = data.ReadByte();
|
||||
}
|
||||
else if (state == 0)
|
||||
{
|
||||
got_cr = c == CR;
|
||||
c = data.ReadByte();
|
||||
}
|
||||
else if (state == 1 && c == '-')
|
||||
{
|
||||
c = data.ReadByte();
|
||||
if (c == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (c != '-')
|
||||
{
|
||||
state = 0;
|
||||
got_cr = false;
|
||||
continue; // no ReadByte() here
|
||||
}
|
||||
|
||||
int nread = data.Read(buffer, 0, buffer.Length);
|
||||
int bl = buffer.Length;
|
||||
if (nread != bl)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!CompareBytes(boundaryBytes, buffer))
|
||||
{
|
||||
state = 0;
|
||||
data.Position = retval + 2;
|
||||
if (got_cr)
|
||||
{
|
||||
data.Position++;
|
||||
got_cr = false;
|
||||
}
|
||||
|
||||
c = data.ReadByte();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (buffer[bl - 2] == '-' && buffer[bl - 1] == '-')
|
||||
{
|
||||
atEof = true;
|
||||
}
|
||||
else if (buffer[bl - 2] != CR || buffer[bl - 1] != LF)
|
||||
{
|
||||
state = 0;
|
||||
data.Position = retval + 2;
|
||||
if (got_cr)
|
||||
{
|
||||
data.Position++;
|
||||
got_cr = false;
|
||||
}
|
||||
|
||||
c = data.ReadByte();
|
||||
continue;
|
||||
}
|
||||
|
||||
data.Position = retval + 2;
|
||||
if (got_cr)
|
||||
{
|
||||
data.Position++;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// state == 1
|
||||
state = 0; // no ReadByte() here
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
private static string StripPath(string path)
|
||||
{
|
||||
if (path == null || path.Length == 0)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
if (path.IndexOf(":\\", StringComparison.Ordinal) != 1
|
||||
&& !path.StartsWith("\\\\", StringComparison.Ordinal))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
return path.Substring(path.LastIndexOf('\\') + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.IO;
|
||||
using MediaBrowser.Model.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using IRequest = MediaBrowser.Model.Services.IRequest;
|
||||
|
||||
namespace Emby.Server.Implementations.SocketSharp
|
||||
{
|
||||
public class WebSocketSharpResponse : IResponse
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public WebSocketSharpResponse(ILogger logger, HttpResponse response)
|
||||
{
|
||||
_logger = logger;
|
||||
OriginalResponse = response;
|
||||
}
|
||||
|
||||
public HttpResponse OriginalResponse { get; }
|
||||
|
||||
public int StatusCode
|
||||
{
|
||||
get => OriginalResponse.StatusCode;
|
||||
set => OriginalResponse.StatusCode = value;
|
||||
}
|
||||
|
||||
public string StatusDescription { get; set; }
|
||||
|
||||
public string ContentType
|
||||
{
|
||||
get => OriginalResponse.ContentType;
|
||||
set => OriginalResponse.ContentType = value;
|
||||
}
|
||||
|
||||
public void AddHeader(string name, string value)
|
||||
{
|
||||
if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
ContentType = value;
|
||||
return;
|
||||
}
|
||||
|
||||
OriginalResponse.Headers.Add(name, value);
|
||||
}
|
||||
|
||||
public void Redirect(string url)
|
||||
{
|
||||
OriginalResponse.Redirect(url);
|
||||
}
|
||||
|
||||
public Stream OutputStream => OriginalResponse.Body;
|
||||
|
||||
public bool SendChunked { get; set; }
|
||||
|
||||
const int StreamCopyToBufferSize = 81920;
|
||||
public async Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, IFileSystem fileSystem, IStreamHelper streamHelper, CancellationToken cancellationToken)
|
||||
{
|
||||
var allowAsync = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
|
||||
//if (count <= 0)
|
||||
//{
|
||||
// allowAsync = true;
|
||||
//}
|
||||
|
||||
var fileOpenOptions = FileOpenOptions.SequentialScan;
|
||||
|
||||
if (allowAsync)
|
||||
{
|
||||
fileOpenOptions |= FileOpenOptions.Asynchronous;
|
||||
}
|
||||
|
||||
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
|
||||
|
||||
using (var fs = fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, fileShareMode, fileOpenOptions))
|
||||
{
|
||||
if (offset > 0)
|
||||
{
|
||||
fs.Position = offset;
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
await streamHelper.CopyToAsync(fs, OutputStream, count, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await fs.CopyToAsync(OutputStream, StreamCopyToBufferSize, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue