From a86b71899ec52c44ddc6c3018e8cc5e9d7ff4d62 Mon Sep 17 00:00:00 2001 From: Andrew Rabert Date: Thu, 27 Dec 2018 18:27:57 -0500 Subject: Add GPL modules --- MediaBrowser.Common/Net/HttpRequestOptions.cs | 157 ++++++++++++++++++++++++++ MediaBrowser.Common/Net/HttpResponseInfo.cs | 75 ++++++++++++ MediaBrowser.Common/Net/IHttpClient.cs | 59 ++++++++++ MediaBrowser.Common/Net/INetworkManager.cs | 66 +++++++++++ 4 files changed, 357 insertions(+) create mode 100644 MediaBrowser.Common/Net/HttpRequestOptions.cs create mode 100644 MediaBrowser.Common/Net/HttpResponseInfo.cs create mode 100644 MediaBrowser.Common/Net/IHttpClient.cs create mode 100644 MediaBrowser.Common/Net/INetworkManager.cs (limited to 'MediaBrowser.Common/Net') diff --git a/MediaBrowser.Common/Net/HttpRequestOptions.cs b/MediaBrowser.Common/Net/HttpRequestOptions.cs new file mode 100644 index 0000000000..c61e88c878 --- /dev/null +++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading; +using System.Text; + +namespace MediaBrowser.Common.Net +{ + /// + /// Class HttpRequestOptions + /// + public class HttpRequestOptions + { + /// + /// Gets or sets the URL. + /// + /// The URL. + public string Url { get; set; } + + public CompressionMethod? DecompressionMethod { get; set; } + + /// + /// Gets or sets the accept header. + /// + /// The accept header. + public string AcceptHeader + { + get { return GetHeaderValue("Accept"); } + set + { + RequestHeaders["Accept"] = value; + } + } + /// + /// Gets or sets the cancellation token. + /// + /// The cancellation token. + public CancellationToken CancellationToken { get; set; } + + /// + /// Gets or sets the resource pool. + /// + /// The resource pool. + public SemaphoreSlim ResourcePool { get; set; } + + /// + /// Gets or sets the user agent. + /// + /// The user agent. + public string UserAgent + { + get { return GetHeaderValue("User-Agent"); } + set + { + RequestHeaders["User-Agent"] = value; + } + } + + /// + /// Gets or sets the referrer. + /// + /// The referrer. + public string Referer { get; set; } + + /// + /// Gets or sets the host. + /// + /// The host. + public string Host { get; set; } + + /// + /// Gets or sets the progress. + /// + /// The progress. + public IProgress Progress { get; set; } + + /// + /// Gets or sets a value indicating whether [enable HTTP compression]. + /// + /// true if [enable HTTP compression]; otherwise, false. + public bool EnableHttpCompression { get; set; } + + public Dictionary RequestHeaders { get; private set; } + + public string RequestContentType { get; set; } + + public string RequestContent { get; set; } + public byte[] RequestContentBytes { get; set; } + + public bool BufferContent { get; set; } + + public bool LogRequest { get; set; } + public bool LogRequestAsDebug { get; set; } + public bool LogErrors { get; set; } + public bool LogResponse { get; set; } + public bool LogResponseHeaders { get; set; } + + public bool LogErrorResponseBody { get; set; } + public bool EnableKeepAlive { get; set; } + + public CacheMode CacheMode { get; set; } + public TimeSpan CacheLength { get; set; } + + public int TimeoutMs { get; set; } + public bool EnableDefaultUserAgent { get; set; } + + public bool AppendCharsetToMimeType { get; set; } + public string DownloadFilePath { get; set; } + + private string GetHeaderValue(string name) + { + string value; + + RequestHeaders.TryGetValue(name, out value); + + return value; + } + + /// + /// Initializes a new instance of the class. + /// + public HttpRequestOptions() + { + EnableHttpCompression = true; + + RequestHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); + + LogRequest = true; + LogErrors = true; + CacheMode = CacheMode.None; + + TimeoutMs = 20000; + } + + public void SetPostData(IDictionary values) + { + var strings = values.Keys.Select(key => string.Format("{0}={1}", key, values[key])); + var postContent = string.Join("&", strings.ToArray()); + + RequestContent = postContent; + RequestContentType = "application/x-www-form-urlencoded"; + } + } + + public enum CacheMode + { + None = 0, + Unconditional = 1 + } + + public enum CompressionMethod + { + Deflate, + Gzip + } +} diff --git a/MediaBrowser.Common/Net/HttpResponseInfo.cs b/MediaBrowser.Common/Net/HttpResponseInfo.cs new file mode 100644 index 0000000000..ed941a4474 --- /dev/null +++ b/MediaBrowser.Common/Net/HttpResponseInfo.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; + +namespace MediaBrowser.Common.Net +{ + /// + /// Class HttpResponseInfo + /// + public class HttpResponseInfo : IDisposable + { + /// + /// Gets or sets the type of the content. + /// + /// The type of the content. + public string ContentType { get; set; } + + /// + /// Gets or sets the response URL. + /// + /// The response URL. + public string ResponseUrl { get; set; } + + /// + /// Gets or sets the content. + /// + /// The content. + public Stream Content { get; set; } + + /// + /// Gets or sets the status code. + /// + /// The status code. + public HttpStatusCode StatusCode { get; set; } + + /// + /// Gets or sets the temp file path. + /// + /// The temp file path. + public string TempFilePath { get; set; } + + /// + /// Gets or sets the length of the content. + /// + /// The length of the content. + public long? ContentLength { get; set; } + + /// + /// Gets or sets the headers. + /// + /// The headers. + public Dictionary Headers { get; set; } + + private readonly IDisposable _disposable; + + public HttpResponseInfo(IDisposable disposable) + { + _disposable = disposable; + Headers = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + public HttpResponseInfo() + { + Headers = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + + public void Dispose() + { + if (_disposable != null) + { + _disposable.Dispose(); + } + } + } +} diff --git a/MediaBrowser.Common/Net/IHttpClient.cs b/MediaBrowser.Common/Net/IHttpClient.cs new file mode 100644 index 0000000000..cf55119653 --- /dev/null +++ b/MediaBrowser.Common/Net/IHttpClient.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Net +{ + /// + /// Interface IHttpClient + /// + public interface IHttpClient + { + /// + /// Gets the response. + /// + /// The options. + /// Task{HttpResponseInfo}. + Task GetResponse(HttpRequestOptions options); + + /// + /// Gets the specified options. + /// + /// The options. + /// Task{Stream}. + Task Get(HttpRequestOptions options); + + /// + /// Sends the asynchronous. + /// + /// The options. + /// The HTTP method. + /// Task{HttpResponseInfo}. + Task SendAsync(HttpRequestOptions options, string httpMethod); + + /// + /// Posts the specified options. + /// + /// The options. + /// Task{HttpResponseInfo}. + Task Post(HttpRequestOptions options); + + /// + /// Downloads the contents of a given url into a temporary location + /// + /// The options. + /// Task{System.String}. + /// progress + /// + Task GetTempFile(HttpRequestOptions options); + + /// + /// Gets the temporary file response. + /// + /// The options. + /// Task{HttpResponseInfo}. + Task GetTempFileResponse(HttpRequestOptions options); + } +} \ No newline at end of file diff --git a/MediaBrowser.Common/Net/INetworkManager.cs b/MediaBrowser.Common/Net/INetworkManager.cs new file mode 100644 index 0000000000..b2ff797bcf --- /dev/null +++ b/MediaBrowser.Common/Net/INetworkManager.cs @@ -0,0 +1,66 @@ +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Net; +using System.Collections.Generic; +using System; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Net +{ + public interface INetworkManager + { + event EventHandler NetworkChanged; + + /// + /// Gets a random port number that is currently available + /// + /// System.Int32. + int GetRandomUnusedTcpPort(); + + int GetRandomUnusedUdpPort(); + + Func LocalSubnetsFn { get; set; } + + /// + /// Returns MAC Address from first Network Card in Computer + /// + /// [string] MAC Address + List GetMacAddresses(); + + /// + /// Determines whether [is in private address space] [the specified endpoint]. + /// + /// The endpoint. + /// true if [is in private address space] [the specified endpoint]; otherwise, false. + bool IsInPrivateAddressSpace(string endpoint); + + /// + /// Gets the network shares. + /// + /// The path. + /// IEnumerable{NetworkShare}. + IEnumerable GetNetworkShares(string path); + + /// + /// Gets available devices within the domain + /// + /// PC's in the Domain + IEnumerable GetNetworkDevices(); + + /// + /// Determines whether [is in local network] [the specified endpoint]. + /// + /// The endpoint. + /// true if [is in local network] [the specified endpoint]; otherwise, false. + bool IsInLocalNetwork(string endpoint); + + IpAddressInfo[] GetLocalIpAddresses(); + + IpAddressInfo ParseIpAddress(string ipAddress); + + bool TryParseIpAddress(string ipAddress, out IpAddressInfo ipAddressInfo); + + Task GetHostAddressesAsync(string host); + + bool IsAddressInSubnets(string addressString, string[] subnets); + } +} \ No newline at end of file -- cgit v1.2.3