aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Common')
-rw-r--r--MediaBrowser.Common/Json/JsonSerializer.cs2
-rw-r--r--MediaBrowser.Common/MediaBrowser.Common.csproj10
-rw-r--r--MediaBrowser.Common/Net/CollectionExtensions.cs14
-rw-r--r--MediaBrowser.Common/Net/Handlers/JsonHandler.cs36
-rw-r--r--MediaBrowser.Common/Net/HttpServer.cs42
-rw-r--r--MediaBrowser.Common/Net/Request.cs18
-rw-r--r--MediaBrowser.Common/Net/RequestContext.cs103
-rw-r--r--MediaBrowser.Common/Net/Response.cs77
-rw-r--r--MediaBrowser.Common/Net/StreamExtensions.cs19
-rw-r--r--MediaBrowser.Common/Plugins/BasePlugin.cs7
-rw-r--r--MediaBrowser.Common/packages.config1
11 files changed, 322 insertions, 7 deletions
diff --git a/MediaBrowser.Common/Json/JsonSerializer.cs b/MediaBrowser.Common/Json/JsonSerializer.cs
index c9b8d8e85e..903482fc87 100644
--- a/MediaBrowser.Common/Json/JsonSerializer.cs
+++ b/MediaBrowser.Common/Json/JsonSerializer.cs
@@ -24,7 +24,7 @@ namespace MediaBrowser.Common.Json
}
}
}
-
+
public static void Serialize<T>(T o, string file)
{
using (StreamWriter streamWriter = new StreamWriter(file))
diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj
index 312f556b6c..9326bea8bb 100644
--- a/MediaBrowser.Common/MediaBrowser.Common.csproj
+++ b/MediaBrowser.Common/MediaBrowser.Common.csproj
@@ -35,6 +35,9 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
+ <Reference Include="System.Reactive">
+ <HintPath>..\packages\Rx-Main.1.0.11226\lib\Net4\System.Reactive.dll</HintPath>
+ </Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@@ -44,6 +47,13 @@
<ItemGroup>
<Compile Include="Events\GenericItemEventArgs.cs" />
<Compile Include="Json\JsonSerializer.cs" />
+ <Compile Include="Net\CollectionExtensions.cs" />
+ <Compile Include="Net\Handlers\JsonHandler.cs" />
+ <Compile Include="Net\HttpServer.cs" />
+ <Compile Include="Net\Request.cs" />
+ <Compile Include="Net\RequestContext.cs" />
+ <Compile Include="Net\Response.cs" />
+ <Compile Include="Net\StreamExtensions.cs" />
<Compile Include="Plugins\BasePluginConfiguration.cs" />
<Compile Include="Logging\BaseLogger.cs" />
<Compile Include="Logging\FileLogger.cs" />
diff --git a/MediaBrowser.Common/Net/CollectionExtensions.cs b/MediaBrowser.Common/Net/CollectionExtensions.cs
new file mode 100644
index 0000000000..98d24dfc04
--- /dev/null
+++ b/MediaBrowser.Common/Net/CollectionExtensions.cs
@@ -0,0 +1,14 @@
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Linq;
+
+namespace MediaBrowser.Common.Net
+{
+ public static class CollectionExtensions
+ {
+ public static IDictionary<string, IEnumerable<string>> ToDictionary(this NameValueCollection source)
+ {
+ return source.AllKeys.ToDictionary<string, string, IEnumerable<string>>(key => key, source.GetValues);
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Common/Net/Handlers/JsonHandler.cs b/MediaBrowser.Common/Net/Handlers/JsonHandler.cs
new file mode 100644
index 0000000000..da1234b4e7
--- /dev/null
+++ b/MediaBrowser.Common/Net/Handlers/JsonHandler.cs
@@ -0,0 +1,36 @@
+using System.IO;
+using System.IO.Compression;
+using MediaBrowser.Common.Json;
+
+namespace MediaBrowser.Common.Net.Handlers
+{
+ public abstract class JsonHandler : Response
+ {
+ public JsonHandler(RequestContext ctx)
+ : base(ctx)
+ {
+ Headers["Content-Encoding"] = "gzip";
+
+ WriteStream = s =>
+ {
+ WriteReponse(s);
+ s.Close();
+ };
+ }
+
+ public override string ContentType
+ {
+ get { return "application/json"; }
+ }
+
+ protected abstract object ObjectToSerialize { get; }
+
+ private void WriteReponse(Stream stream)
+ {
+ using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, false))
+ {
+ JsonSerializer.Serialize(ObjectToSerialize, gzipStream);
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Net/HttpServer.cs b/MediaBrowser.Common/Net/HttpServer.cs
new file mode 100644
index 0000000000..fad8d13eb9
--- /dev/null
+++ b/MediaBrowser.Common/Net/HttpServer.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Net;
+using System.Reactive.Linq;
+
+namespace MediaBrowser.Common.Net
+{
+ public class HttpServer : IObservable<RequestContext>, IDisposable
+ {
+ private readonly HttpListener listener;
+ private readonly IObservable<RequestContext> stream;
+
+ public HttpServer(string url)
+ {
+ listener = new HttpListener();
+ listener.Prefixes.Add(url);
+ listener.Start();
+ stream = ObservableHttpContext();
+ }
+
+ private IObservable<RequestContext> ObservableHttpContext()
+ {
+ return Observable.Create<RequestContext>(obs =>
+ Observable.FromAsyncPattern<HttpListenerContext>(listener.BeginGetContext,
+ listener.EndGetContext)()
+ .Select(c => new RequestContext(c))
+ .Subscribe(obs))
+ .Repeat()
+ .Retry()
+ .Publish()
+ .RefCount();
+ }
+ public void Dispose()
+ {
+ listener.Stop();
+ }
+
+ public IDisposable Subscribe(IObserver<RequestContext> observer)
+ {
+ return stream.Subscribe(observer);
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Common/Net/Request.cs b/MediaBrowser.Common/Net/Request.cs
new file mode 100644
index 0000000000..795c9c36ba
--- /dev/null
+++ b/MediaBrowser.Common/Net/Request.cs
@@ -0,0 +1,18 @@
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace MediaBrowser.Common.Net
+{
+ public class Request
+ {
+ public string HttpMethod { get; set; }
+ public IDictionary<string, IEnumerable<string>> Headers { get; set; }
+ public Stream InputStream { get; set; }
+ public string RawUrl { get; set; }
+ public int ContentLength
+ {
+ get { return int.Parse(Headers["Content-Length"].First()); }
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Common/Net/RequestContext.cs b/MediaBrowser.Common/Net/RequestContext.cs
new file mode 100644
index 0000000000..9a21b473de
--- /dev/null
+++ b/MediaBrowser.Common/Net/RequestContext.cs
@@ -0,0 +1,103 @@
+using System;
+using System.Linq;
+using System.Net;
+
+namespace MediaBrowser.Common.Net
+{
+ public class RequestContext
+ {
+ public HttpListenerRequest Request { get; private set; }
+ public HttpListenerResponse Response { get; private set; }
+
+ public RequestContext(HttpListenerContext context)
+ {
+ Response = context.Response;
+ Request = context.Request;
+ }
+
+ public void Respond(Response handler)
+ {
+ Response.AddHeader("Access-Control-Allow-Origin", "*");
+
+ foreach (var header in handler.Headers)
+ {
+ Response.AddHeader(header.Key, header.Value);
+ }
+
+ int statusCode = handler.StatusCode;
+ Response.ContentType = handler.ContentType;
+
+ TimeSpan cacheDuration = handler.CacheDuration;
+
+ if (Request.Headers.AllKeys.Contains("If-Modified-Since"))
+ {
+ DateTime ifModifiedSince;
+
+ if (DateTime.TryParse(Request.Headers["If-Modified-Since"].Replace(" GMT", string.Empty), out ifModifiedSince))
+ {
+ // If the cache hasn't expired yet just return a 304
+ if (IsCacheValid(ifModifiedSince, cacheDuration, handler.LastDateModified))
+ {
+ statusCode = 304;
+ }
+ }
+ }
+
+ Response.SendChunked = true;
+ Response.StatusCode = statusCode;
+
+ if (statusCode != 304)
+ {
+ if (cacheDuration.Ticks > 0)
+ {
+ CacheResponse(Response, cacheDuration, handler.LastDateModified);
+ }
+
+ handler.WriteStream(Response.OutputStream);
+ }
+ else
+ {
+ Response.OutputStream.Flush();
+ Response.OutputStream.Close();
+ }
+ }
+
+ private void CacheResponse(HttpListenerResponse response, TimeSpan duration, DateTime? dateModified)
+ {
+ DateTime lastModified = dateModified ?? DateTime.Now;
+
+ response.Headers[HttpResponseHeader.CacheControl] = "Public";
+ response.Headers[HttpResponseHeader.Expires] = DateTime.Now.Add(duration).ToString("r");
+ response.Headers[HttpResponseHeader.LastModified] = lastModified.ToString("r");
+ }
+
+ private bool IsCacheValid(DateTime ifModifiedSince, TimeSpan cacheDuration, DateTime? dateModified)
+ {
+ if (dateModified.HasValue)
+ {
+ DateTime lastModified = NormalizeDateForComparison(dateModified.Value);
+ ifModifiedSince = NormalizeDateForComparison(ifModifiedSince);
+
+ return lastModified <= ifModifiedSince;
+ }
+
+ DateTime cacheExpirationDate = ifModifiedSince.Add(cacheDuration);
+
+ if (DateTime.Now < cacheExpirationDate)
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ /// <summary>
+ /// When the browser sends the IfModifiedDate, it's precision is limited to seconds, so this will account for that
+ /// </summary>
+ private DateTime NormalizeDateForComparison(DateTime date)
+ {
+ return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
+ }
+
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Common/Net/Response.cs b/MediaBrowser.Common/Net/Response.cs
new file mode 100644
index 0000000000..44b4e29622
--- /dev/null
+++ b/MediaBrowser.Common/Net/Response.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.IO;
+
+namespace MediaBrowser.Common.Net
+{
+ public abstract class Response
+ {
+ protected RequestContext RequestContext { get; private set; }
+
+ protected NameValueCollection QueryString
+ {
+ get
+ {
+ return RequestContext.Request.QueryString;
+ }
+ }
+
+ public Response(RequestContext ctx)
+ {
+ RequestContext = ctx;
+
+ WriteStream = s => { };
+ Headers = new Dictionary<string, string>();
+ }
+
+ public abstract string ContentType { get; }
+
+ public virtual int StatusCode
+ {
+ get
+ {
+ return 200;
+ }
+ }
+
+ public virtual TimeSpan CacheDuration
+ {
+ get
+ {
+ return TimeSpan.FromTicks(0);
+ }
+ }
+
+ public virtual DateTime? LastDateModified
+ {
+ get
+ {
+ return null;
+ }
+ }
+
+ public IDictionary<string, string> Headers { get; set; }
+ public Action<Stream> WriteStream { get; set; }
+ }
+
+ /*public class ByteResponse : Response
+ {
+ public ByteResponse(byte[] bytes)
+ {
+ WriteStream = async s =>
+ {
+ await s.WriteAsync(bytes, 0, bytes.Length);
+ s.Close();
+ };
+ }
+ }
+
+ public class StringResponse : ByteResponse
+ {
+ public StringResponse(string message)
+ : base(Encoding.UTF8.GetBytes(message))
+ {
+ }
+ }*/
+} \ No newline at end of file
diff --git a/MediaBrowser.Common/Net/StreamExtensions.cs b/MediaBrowser.Common/Net/StreamExtensions.cs
new file mode 100644
index 0000000000..c10e458ada
--- /dev/null
+++ b/MediaBrowser.Common/Net/StreamExtensions.cs
@@ -0,0 +1,19 @@
+using System;
+using System.IO;
+using System.Reactive.Linq;
+
+namespace MediaBrowser.Common.Net
+{
+ public static class StreamExtensions
+ {
+ public static IObservable<byte[]> ReadBytes(this Stream stream, int count)
+ {
+ var buffer = new byte[count];
+ return Observable.FromAsyncPattern((cb, state) => stream.BeginRead(buffer, 0, count, cb, state), ar =>
+ {
+ stream.EndRead(ar);
+ return buffer;
+ })();
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Common/Plugins/BasePlugin.cs b/MediaBrowser.Common/Plugins/BasePlugin.cs
index fb37afa089..fc05cbc9a2 100644
--- a/MediaBrowser.Common/Plugins/BasePlugin.cs
+++ b/MediaBrowser.Common/Plugins/BasePlugin.cs
@@ -7,7 +7,7 @@ using MediaBrowser.Common.Json;
namespace MediaBrowser.Common.Plugins
{
- public abstract class BasePlugin<TConfigurationType> : IDisposable, IPlugin
+ public abstract class BasePlugin<TConfigurationType> : IPlugin
where TConfigurationType : BasePluginConfiguration, new()
{
public string Path { get; set; }
@@ -33,10 +33,6 @@ namespace MediaBrowser.Common.Plugins
protected abstract void InitInternal();
- public virtual void Dispose()
- {
- }
-
private TConfigurationType GetConfiguration()
{
if (!File.Exists(ConfigurationPath))
@@ -53,6 +49,5 @@ namespace MediaBrowser.Common.Plugins
string Path { get; set; }
void Init();
- void Dispose();
}
}
diff --git a/MediaBrowser.Common/packages.config b/MediaBrowser.Common/packages.config
index 9bfda38024..4f6bcdcffe 100644
--- a/MediaBrowser.Common/packages.config
+++ b/MediaBrowser.Common/packages.config
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="4.5.7" targetFramework="net45" />
+ <package id="Rx-Main" version="1.0.11226" targetFramework="net45" />
</packages> \ No newline at end of file