diff options
Diffstat (limited to 'MediaBrowser.Common/Extensions')
6 files changed, 189 insertions, 49 deletions
diff --git a/MediaBrowser.Common/Extensions/BaseExtensions.cs b/MediaBrowser.Common/Extensions/BaseExtensions.cs index db0514bb1..08964420e 100644 --- a/MediaBrowser.Common/Extensions/BaseExtensions.cs +++ b/MediaBrowser.Common/Extensions/BaseExtensions.cs @@ -1,37 +1,42 @@ using System; +using System.Security.Cryptography; +using System.Text; using System.Text.RegularExpressions; -using MediaBrowser.Model.Cryptography; namespace MediaBrowser.Common.Extensions { /// <summary> - /// Class BaseExtensions + /// Class BaseExtensions. /// </summary> public static class BaseExtensions { - public static ICryptoProvider CryptographyProvider { get; set; } - /// <summary> /// Strips the HTML. /// </summary> /// <param name="htmlString">The HTML string.</param> - /// <returns>System.String.</returns> + /// <returns><see cref="string" />.</returns> public static string StripHtml(this string htmlString) { // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net - const string pattern = @"<(.|\n)*?>"; + const string Pattern = @"<(.|\n)*?>"; - return Regex.Replace(htmlString, pattern, string.Empty).Trim(); + return Regex.Replace(htmlString, Pattern, string.Empty).Trim(); } /// <summary> - /// Gets the M d5. + /// Gets the Md5. /// </summary> - /// <param name="str">The STR.</param> - /// <returns>Guid.</returns> + /// <param name="str">The string.</param> + /// <returns><see cref="Guid" />.</returns> public static Guid GetMD5(this string str) { - return CryptographyProvider.GetMD5(str); +#pragma warning disable CA5351 + using (var provider = MD5.Create()) + { + return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str))); + } + +#pragma warning restore CA5351 } } } diff --git a/MediaBrowser.Common/Extensions/HttpContextExtensions.cs b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs new file mode 100644 index 000000000..1e5877c84 --- /dev/null +++ b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs @@ -0,0 +1,41 @@ +using System.Net; +using Microsoft.AspNetCore.Http; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Static class containing extension methods for <see cref="HttpContext"/>. + /// </summary> + public static class HttpContextExtensions + { + /// <summary> + /// Checks the origin of the HTTP context. + /// </summary> + /// <param name="context">The incoming HTTP context.</param> + /// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns> + public static bool IsLocal(this HttpContext context) + { + return (context.Connection.LocalIpAddress == null + && context.Connection.RemoteIpAddress == null) + || Equals(context.Connection.LocalIpAddress, context.Connection.RemoteIpAddress); + } + + /// <summary> + /// Extracts the remote IP address of the caller of the HTTP context. + /// </summary> + /// <param name="context">The HTTP context.</param> + /// <returns>The remote caller IP address.</returns> + public static IPAddress GetNormalizedRemoteIp(this HttpContext context) + { + // Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests) + var ip = context.Connection.RemoteIpAddress ?? IPAddress.Loopback; + + if (ip.IsIPv4MappedToIPv6) + { + ip = ip.MapToIPv4(); + } + + return ip; + } + } +} diff --git a/MediaBrowser.Common/Extensions/MethodNotAllowedException.cs b/MediaBrowser.Common/Extensions/MethodNotAllowedException.cs new file mode 100644 index 000000000..48e758ee4 --- /dev/null +++ b/MediaBrowser.Common/Extensions/MethodNotAllowedException.cs @@ -0,0 +1,26 @@ +using System; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Class MethodNotAllowedException. + /// </summary> + public class MethodNotAllowedException : Exception + { + /// <summary> + /// Initializes a new instance of the <see cref="MethodNotAllowedException" /> class. + /// </summary> + public MethodNotAllowedException() + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="MethodNotAllowedException" /> class. + /// </summary> + /// <param name="message">The message.</param> + public MethodNotAllowedException(string message) + : base(message) + { + } + } +} diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs new file mode 100644 index 000000000..08e01bfd6 --- /dev/null +++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs @@ -0,0 +1,80 @@ +using System; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Extension methods for <see cref="Process"/>. + /// </summary> + public static class ProcessExtensions + { + /// <summary> + /// Asynchronously wait for the process to exit. + /// </summary> + /// <param name="process">The process to wait for.</param> + /// <param name="timeout">The duration to wait before cancelling waiting for the task.</param> + /// <returns>True if the task exited normally, false if the timeout elapsed before the process exited.</returns> + /// <exception cref="InvalidOperationException">If <see cref="Process.EnableRaisingEvents"/> is not set to true for the process.</exception> + public static async Task<bool> WaitForExitAsync(this Process process, TimeSpan timeout) + { + using (var cancelTokenSource = new CancellationTokenSource(timeout)) + { + return await WaitForExitAsync(process, cancelTokenSource.Token).ConfigureAwait(false); + } + } + + /// <summary> + /// Asynchronously wait for the process to exit. + /// </summary> + /// <param name="process">The process to wait for.</param> + /// <param name="cancelToken">A <see cref="CancellationToken"/> to observe while waiting for the process to exit.</param> + /// <returns>True if the task exited normally, false if cancelled before the process exited.</returns> + public static async Task<bool> WaitForExitAsync(this Process process, CancellationToken cancelToken) + { + if (!process.EnableRaisingEvents) + { + throw new InvalidOperationException("EnableRisingEvents must be enabled to async wait for a task to exit."); + } + + // Add an event handler for the process exit event + var tcs = new TaskCompletionSource<bool>(); + process.Exited += (_, _) => tcs.TrySetResult(true); + + // Return immediately if the process has already exited + if (process.HasExitedSafe()) + { + return true; + } + + // Register with the cancellation token then await + using (var cancelRegistration = cancelToken.Register(() => tcs.TrySetResult(process.HasExitedSafe()))) + { + return await tcs.Task.ConfigureAwait(false); + } + } + + /// <summary> + /// Gets a value indicating whether the associated process has been terminated using + /// <see cref="Process.HasExited"/>. This is safe to call even if there is no operating system process + /// associated with the <see cref="Process"/>. + /// </summary> + /// <param name="process">The process to check the exit status for.</param> + /// <returns> + /// True if the operating system process referenced by the <see cref="Process"/> component has + /// terminated, or if there is no associated operating system process; otherwise, false. + /// </returns> + private static bool HasExitedSafe(this Process process) + { + try + { + return process.HasExited; + } + catch (InvalidOperationException) + { + return true; + } + } + } +} diff --git a/MediaBrowser.Common/Extensions/RateLimitExceededException.cs b/MediaBrowser.Common/Extensions/RateLimitExceededException.cs new file mode 100644 index 000000000..95802a462 --- /dev/null +++ b/MediaBrowser.Common/Extensions/RateLimitExceededException.cs @@ -0,0 +1,25 @@ +#pragma warning disable CS1591 + +using System; + +namespace MediaBrowser.Common.Extensions +{ + public class RateLimitExceededException : Exception + { + /// <summary> + /// Initializes a new instance of the <see cref="RateLimitExceededException" /> class. + /// </summary> + public RateLimitExceededException() + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="RateLimitExceededException" /> class. + /// </summary> + /// <param name="message">The message.</param> + public RateLimitExceededException(string message) + : base(message) + { + } + } +} diff --git a/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs b/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs index f62c65fd7..22130c5a1 100644 --- a/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs +++ b/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs @@ -3,7 +3,7 @@ using System; namespace MediaBrowser.Common.Extensions { /// <summary> - /// Class ResourceNotFoundException + /// Class ResourceNotFoundException. /// </summary> public class ResourceNotFoundException : Exception { @@ -12,7 +12,6 @@ namespace MediaBrowser.Common.Extensions /// </summary> public ResourceNotFoundException() { - } /// <summary> @@ -22,42 +21,6 @@ namespace MediaBrowser.Common.Extensions public ResourceNotFoundException(string message) : base(message) { - - } - } - - public class RemoteServiceUnavailableException : Exception - { - public RemoteServiceUnavailableException() - { - - } - - public RemoteServiceUnavailableException(string message) - : base(message) - { - - } - } - - public class RateLimitExceededException : Exception - { - /// <summary> - /// Initializes a new instance of the <see cref="RateLimitExceededException" /> class. - /// </summary> - public RateLimitExceededException() - { - - } - - /// <summary> - /// Initializes a new instance of the <see cref="RateLimitExceededException" /> class. - /// </summary> - /// <param name="message">The message.</param> - public RateLimitExceededException(string message) - : base(message) - { - } } } |
