diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-25 22:43:04 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-25 22:43:04 -0500 |
| commit | 2d06095447b972c8c7239277428e2c67c8b7ca86 (patch) | |
| tree | 14278bd4c0732ee962b73ff4845e5022e157a0a3 /MediaBrowser.ApiInteraction | |
| parent | 364fbb9e0c7586afa296ddd7d739df086f4c3533 (diff) | |
plugin security fixes and other abstractions
Diffstat (limited to 'MediaBrowser.ApiInteraction')
| -rw-r--r-- | MediaBrowser.ApiInteraction/ApiClient.cs | 19 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/AsyncHttpClient.cs | 2 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/BaseApiClient.cs | 65 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/DataSerializer.cs | 66 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/IAsyncHttpClient.cs | 2 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj | 13 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/NewtonsoftJsonSerializer.cs | 141 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/ServerDiscovery.cs | 63 | ||||
| -rw-r--r-- | MediaBrowser.ApiInteraction/packages.config | 3 |
9 files changed, 211 insertions, 163 deletions
diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index e7d4e6eff1..5268c7e1f7 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -46,6 +46,15 @@ namespace MediaBrowser.ApiInteraction } /// <summary> + /// Initializes a new instance of the <see cref="ApiClient" /> class. + /// </summary> + /// <param name="logger">The logger.</param> + public ApiClient(ILogger logger) + : this(logger, new AsyncHttpClient()) + { + } + + /// <summary> /// Sets the authorization header. /// </summary> /// <param name="header">The header.</param> @@ -67,7 +76,7 @@ namespace MediaBrowser.ApiInteraction throw new ArgumentNullException("url"); } - return HttpClient.GetStreamAsync(url, Logger, CancellationToken.None); + return HttpClient.GetAsync(url, Logger, CancellationToken.None); } /// <summary> @@ -364,7 +373,7 @@ namespace MediaBrowser.ApiInteraction var url = GetApiUrl("Plugins/" + plugin.Id + "/Assembly"); - return HttpClient.GetStreamAsync(url, Logger, CancellationToken.None); + return HttpClient.GetAsync(url, Logger, CancellationToken.None); } /// <summary> @@ -431,7 +440,7 @@ namespace MediaBrowser.ApiInteraction var url = GetApiUrl("Plugins/" + pluginId + "/ConfigurationFile"); - return await HttpClient.GetStreamAsync(url, Logger, CancellationToken.None).ConfigureAwait(false); + return await HttpClient.GetAsync(url, Logger, CancellationToken.None).ConfigureAwait(false); } /// <summary> @@ -963,7 +972,7 @@ namespace MediaBrowser.ApiInteraction const string contentType = "application/x-www-form-urlencoded"; - var postContent = DataSerializer.SerializeToJsonString(obj); + var postContent = SerializeToJson(obj); using (var stream = await HttpClient.PostAsync(url, contentType, postContent, Logger, CancellationToken.None).ConfigureAwait(false)) { @@ -991,7 +1000,7 @@ namespace MediaBrowser.ApiInteraction { url = AddDataFormat(url, serializationFormat); - return HttpClient.GetStreamAsync(url, Logger, CancellationToken.None); + return HttpClient.GetAsync(url, Logger, CancellationToken.None); } diff --git a/MediaBrowser.ApiInteraction/AsyncHttpClient.cs b/MediaBrowser.ApiInteraction/AsyncHttpClient.cs index ec8176d8a1..c6701cac08 100644 --- a/MediaBrowser.ApiInteraction/AsyncHttpClient.cs +++ b/MediaBrowser.ApiInteraction/AsyncHttpClient.cs @@ -45,7 +45,7 @@ namespace MediaBrowser.ApiInteraction /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{Stream}.</returns> /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception> - public async Task<Stream> GetStreamAsync(string url, ILogger logger, CancellationToken cancellationToken) + public async Task<Stream> GetAsync(string url, ILogger logger, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/MediaBrowser.ApiInteraction/BaseApiClient.cs b/MediaBrowser.ApiInteraction/BaseApiClient.cs index 242fc23591..4bd4ace508 100644 --- a/MediaBrowser.ApiInteraction/BaseApiClient.cs +++ b/MediaBrowser.ApiInteraction/BaseApiClient.cs @@ -2,6 +2,7 @@ using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Web; using System; using System.Collections.Generic; @@ -22,9 +23,22 @@ namespace MediaBrowser.ApiInteraction protected ILogger Logger { get; private set; } /// <summary> + /// Gets the protobuf serializer. + /// </summary> + /// <value>The protobuf serializer.</value> + public IProtobufSerializer ProtobufSerializer { get; private set; } + + /// <summary> + /// Gets the json serializer. + /// </summary> + /// <value>The json serializer.</value> + public IJsonSerializer JsonSerializer { get; private set; } + + /// <summary> /// Initializes a new instance of the <see cref="BaseApiClient" /> class. /// </summary> /// <param name="logger">The logger.</param> + /// <param name="jsonSerializer">The json serializer.</param> /// <exception cref="System.ArgumentNullException">logger</exception> protected BaseApiClient(ILogger logger) { @@ -33,9 +47,9 @@ namespace MediaBrowser.ApiInteraction throw new ArgumentNullException("logger"); } + JsonSerializer = new NewtonsoftJsonSerializer(); Logger = logger; - - DataSerializer.Configure(); + SerializationFormat = SerializationFormats.Json; } /// <summary> @@ -90,22 +104,11 @@ namespace MediaBrowser.ApiInteraction } } - private SerializationFormats _serializationFormat = SerializationFormats.Protobuf; /// <summary> /// Gets the default data format to request from the server /// </summary> /// <value>The serialization format.</value> - public SerializationFormats SerializationFormat - { - get - { - return _serializationFormat; - } - set - { - _serializationFormat = value; - } - } + public SerializationFormats SerializationFormat { get; set; } /// <summary> /// Resets the authorization header. @@ -790,7 +793,39 @@ namespace MediaBrowser.ApiInteraction protected T DeserializeFromStream<T>(Stream stream) where T : class { - return (T)DataSerializer.DeserializeFromStream(stream, SerializationFormat, typeof(T)); + return (T)DeserializeFromStream(stream, typeof(T), SerializationFormat); + } + + /// <summary> + /// Deserializes from stream. + /// </summary> + /// <param name="stream">The stream.</param> + /// <param name="type">The type.</param> + /// <param name="format">The format.</param> + /// <returns>System.Object.</returns> + /// <exception cref="System.NotImplementedException"></exception> + protected object DeserializeFromStream(Stream stream, Type type, SerializationFormats format) + { + if (format == SerializationFormats.Protobuf) + { + return ProtobufSerializer.DeserializeFromStream(stream, type); + } + if (format == SerializationFormats.Json) + { + return JsonSerializer.DeserializeFromStream(stream, type); + } + + throw new NotImplementedException(); + } + + /// <summary> + /// Serializers to json. + /// </summary> + /// <param name="obj">The obj.</param> + /// <returns>System.String.</returns> + protected string SerializeToJson(object obj) + { + return JsonSerializer.SerializeToString(obj); } /// <summary> diff --git a/MediaBrowser.ApiInteraction/DataSerializer.cs b/MediaBrowser.ApiInteraction/DataSerializer.cs deleted file mode 100644 index cc13d55c88..0000000000 --- a/MediaBrowser.ApiInteraction/DataSerializer.cs +++ /dev/null @@ -1,66 +0,0 @@ -using ProtoBuf; -using ProtoBuf.Meta; -using ServiceStack.Text; -using System; -using System.IO; - -namespace MediaBrowser.ApiInteraction -{ - /// <summary> - /// Class DataSerializer - /// </summary> - public static class DataSerializer - { - /// <summary> - /// Gets or sets the dynamically created serializer. - /// </summary> - /// <value>The dynamic serializer.</value> - public static TypeModel DynamicSerializer { get; set; } - - /// <summary> - /// Deserializes an object - /// </summary> - /// <param name="stream">The stream.</param> - /// <param name="format">The format.</param> - /// <param name="type">The type.</param> - /// <returns>System.Object.</returns> - /// <exception cref="System.NotImplementedException"></exception> - public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type) - { - if (format == SerializationFormats.Protobuf) - { - if (DynamicSerializer != null) - { - return DynamicSerializer.Deserialize(stream, null, type); - } - return Serializer.NonGeneric.Deserialize(type, stream); - } - if (format == SerializationFormats.Json) - { - return JsonSerializer.DeserializeFromStream(type, stream); - } - - throw new NotImplementedException(); - } - - /// <summary> - /// Serializes to json. - /// </summary> - /// <param name="obj">The obj.</param> - /// <returns>System.String.</returns> - public static string SerializeToJsonString(object obj) - { - return JsonSerializer.SerializeToString(obj, obj.GetType()); - } - - /// <summary> - /// Configures this instance. - /// </summary> - public static void Configure() - { - JsConfig.DateHandler = JsonDateHandler.ISO8601; - JsConfig.ExcludeTypeInfo = true; - JsConfig.IncludeNullValues = false; - } - } -} diff --git a/MediaBrowser.ApiInteraction/IAsyncHttpClient.cs b/MediaBrowser.ApiInteraction/IAsyncHttpClient.cs index edd11829a6..0837f150fa 100644 --- a/MediaBrowser.ApiInteraction/IAsyncHttpClient.cs +++ b/MediaBrowser.ApiInteraction/IAsyncHttpClient.cs @@ -24,7 +24,7 @@ namespace MediaBrowser.ApiInteraction /// <param name="logger">The logger.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{Stream}.</returns> - Task<Stream> GetStreamAsync(string url, ILogger logger, CancellationToken cancellationToken); + Task<Stream> GetAsync(string url, ILogger logger, CancellationToken cancellationToken); /// <summary> /// Deletes the async. diff --git a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj index 0d9938891f..a4a9091928 100644 --- a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj +++ b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj @@ -32,19 +32,13 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> - <Reference Include="protobuf-net, Version=2.0.0.621, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\protobuf-net.2.0.0.621\lib\net40\protobuf-net.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.Text, Version=3.9.37.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.Text.3.9.37\lib\net35\ServiceStack.Text.dll</HintPath> + <Reference Include="Newtonsoft.Json"> + <HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath> </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Net" /> <Reference Include="System.Net.Http" /> - <Reference Include="System.Net.Http.WebRequest" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> @@ -58,11 +52,10 @@ <Compile Include="AsyncHttpClient.cs" /> <Compile Include="BaseApiClient.cs" /> <Compile Include="ApiClient.cs" /> - <Compile Include="DataSerializer.cs" /> <Compile Include="IAsyncHttpClient.cs" /> + <Compile Include="NewtonsoftJsonSerializer.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="SerializationFormats.cs" /> - <Compile Include="ServerDiscovery.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj"> diff --git a/MediaBrowser.ApiInteraction/NewtonsoftJsonSerializer.cs b/MediaBrowser.ApiInteraction/NewtonsoftJsonSerializer.cs new file mode 100644 index 0000000000..f5b43669ea --- /dev/null +++ b/MediaBrowser.ApiInteraction/NewtonsoftJsonSerializer.cs @@ -0,0 +1,141 @@ +using MediaBrowser.Model.Serialization; +using Newtonsoft.Json; +using System; +using System.IO; + +namespace MediaBrowser.ApiInteraction +{ + /// <summary> + /// Class NewtonsoftJsonSerializer + /// </summary> + public class NewtonsoftJsonSerializer : IJsonSerializer + { + /// <summary> + /// Serializes to stream. + /// </summary> + /// <param name="obj">The obj.</param> + /// <param name="stream">The stream.</param> + /// <exception cref="System.NotImplementedException"></exception> + /// <exception cref="System.ArgumentNullException">obj</exception> + public void SerializeToStream(object obj, Stream stream) + { + throw new NotImplementedException(); + } + + /// <summary> + /// Deserializes from stream. + /// </summary> + /// <param name="stream">The stream.</param> + /// <param name="type">The type.</param> + /// <returns>System.Object.</returns> + public object DeserializeFromStream(Stream stream, Type type) + { + using (var streamReader = new StreamReader(stream)) + { + using (var jsonReader = new JsonTextReader(streamReader)) + { + return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type); + } + } + } + + /// <summary> + /// Deserializes from stream. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="stream">The stream.</param> + /// <returns>``0.</returns> + public T DeserializeFromStream<T>(Stream stream) + { + return (T)DeserializeFromStream(stream, typeof(T)); + } + + /// <summary> + /// Deserializes from string. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="text">The text.</param> + /// <returns>``0.</returns> + /// <exception cref="System.NotImplementedException"></exception> + public T DeserializeFromString<T>(string text) + { + throw new NotImplementedException(); + } + + /// <summary> + /// Deserializes from string. + /// </summary> + /// <param name="json">The json.</param> + /// <param name="type">The type.</param> + /// <returns>System.Object.</returns> + /// <exception cref="System.NotImplementedException"></exception> + public object DeserializeFromString(string json, Type type) + { + throw new NotImplementedException(); + } + + /// <summary> + /// Serializes to string. + /// </summary> + /// <param name="obj">The obj.</param> + /// <returns>System.String.</returns> + public string SerializeToString(object obj) + { + using (var streamWriter = new StringWriter()) + { + using (var jsonWriter = new JsonTextWriter((streamWriter))) + { + JsonSerializer.Create(new JsonSerializerSettings()).Serialize(jsonWriter, obj); + } + return streamWriter.ToString(); + } + } + + /// <summary> + /// Serializes to bytes. + /// </summary> + /// <param name="obj">The obj.</param> + /// <returns>System.Byte[][].</returns> + /// <exception cref="System.NotImplementedException"></exception> + public byte[] SerializeToBytes(object obj) + { + throw new NotImplementedException(); + } + + /// <summary> + /// Serializes to file. + /// </summary> + /// <param name="obj">The obj.</param> + /// <param name="file">The file.</param> + /// <exception cref="System.NotImplementedException"></exception> + /// <exception cref="System.ArgumentNullException">obj</exception> + public void SerializeToFile(object obj, string file) + { + throw new NotImplementedException(); + } + + /// <summary> + /// Deserializes from file. + /// </summary> + /// <param name="type">The type.</param> + /// <param name="file">The file.</param> + /// <returns>System.Object.</returns> + /// <exception cref="System.NotImplementedException"></exception> + public object DeserializeFromFile(Type type, string file) + { + throw new NotImplementedException(); + } + + /// <summary> + /// Deserializes from file. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="file">The file.</param> + /// <returns>``0.</returns> + /// <exception cref="System.NotImplementedException"></exception> + public T DeserializeFromFile<T>(string file) where T : class + { + throw new NotImplementedException(); + } + } +} diff --git a/MediaBrowser.ApiInteraction/ServerDiscovery.cs b/MediaBrowser.ApiInteraction/ServerDiscovery.cs deleted file mode 100644 index 99a65db5d7..0000000000 --- a/MediaBrowser.ApiInteraction/ServerDiscovery.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; - -namespace MediaBrowser.ApiInteraction -{ - public static class ServerDiscovery - { - /// <summary> - /// Attemps to discover the server within a local network - /// </summary> - public static async Task<IPEndPoint> DiscoverServer() - { - // Create a udp client - var client = new UdpClient(new IPEndPoint(IPAddress.Any, GetRandomUnusedPort())); - - // Construct the message the server is expecting - var bytes = Encoding.UTF8.GetBytes("who is MediaBrowserServer?"); - - // Send it - must be IPAddress.Broadcast, 7359 - var targetEndPoint = new IPEndPoint(IPAddress.Broadcast, 7359); - - // Send it - await client.SendAsync(bytes, bytes.Length, targetEndPoint).ConfigureAwait(false); - - // Get a result back - var result = await client.ReceiveAsync().ConfigureAwait(false); - - if (result.RemoteEndPoint.Port == targetEndPoint.Port) - { - // Convert bytes to text - var text = Encoding.UTF8.GetString(result.Buffer); - - // Expected response : MediaBrowserServer|192.168.1.1:1234 - // If the response is what we're expecting, proceed - if (text.StartsWith("mediabrowserserver", StringComparison.OrdinalIgnoreCase)) - { - text = text.Split('|')[1]; - - var vals = text.Split(':'); - - return new IPEndPoint(IPAddress.Parse(vals[0]), int.Parse(vals[1])); - } - } - - return null; - } - - /// <summary> - /// Gets a random port number that is currently available - /// </summary> - private static int GetRandomUnusedPort() - { - var listener = new TcpListener(IPAddress.Any, 0); - listener.Start(); - var port = ((IPEndPoint)listener.LocalEndpoint).Port; - listener.Stop(); - return port; - } - } -} diff --git a/MediaBrowser.ApiInteraction/packages.config b/MediaBrowser.ApiInteraction/packages.config index 14eb42cace..b82a8b0dde 100644 --- a/MediaBrowser.ApiInteraction/packages.config +++ b/MediaBrowser.ApiInteraction/packages.config @@ -1,5 +1,4 @@ <?xml version="1.0" encoding="utf-8"?> <packages> - <package id="protobuf-net" version="2.0.0.621" targetFramework="net45" /> - <package id="ServiceStack.Text" version="3.9.37" targetFramework="net45" /> + <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" /> </packages>
\ No newline at end of file |
