aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Net
diff options
context:
space:
mode:
authorLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-07-12 23:50:50 -0400
committerLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-07-12 23:50:50 -0400
commit5f5f2838b1931a2cf426b778755a417cc662b46f (patch)
tree007597d0ba7bc2d992f909c9876744d80cc31b42 /MediaBrowser.Common/Net
parent572103cdfe9fdf15ac881a104537047b3ca928ea (diff)
moved the http server to common, added more http handlers
Diffstat (limited to 'MediaBrowser.Common/Net')
-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
7 files changed, 309 insertions, 0 deletions
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