Moved the http server to it's own assembly. added comments and made other minor re-organizations.

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti
2012-07-19 22:22:44 -04:00
parent 6fbd5cf464
commit 80b3ad7bd2
67 changed files with 806 additions and 964 deletions

View File

@@ -1,14 +0,0 @@
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);
}
}
}

View File

@@ -1,70 +0,0 @@
using System.IO;
using System.IO.Compression;
using System;
namespace MediaBrowser.Common.Net.Handlers
{
public abstract class BaseEmbeddedResourceHandler : Response
{
public BaseEmbeddedResourceHandler(RequestContext ctx, string resourcePath)
: base(ctx)
{
ResourcePath = resourcePath;
Headers["Content-Encoding"] = "gzip";
WriteStream = s =>
{
WriteReponse(s);
s.Close();
};
}
protected string ResourcePath { get; set; }
public override string ContentType
{
get
{
string extension = Path.GetExtension(ResourcePath);
if (extension.EndsWith("jpeg", StringComparison.OrdinalIgnoreCase) || extension.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
{
return "image/jpeg";
}
else if (extension.EndsWith("png", StringComparison.OrdinalIgnoreCase))
{
return "image/png";
}
else if (extension.EndsWith("ico", StringComparison.OrdinalIgnoreCase))
{
return "image/ico";
}
else if (extension.EndsWith("js", StringComparison.OrdinalIgnoreCase))
{
return "application/x-javascript";
}
else if (extension.EndsWith("css", StringComparison.OrdinalIgnoreCase))
{
return "text/css";
}
else if (extension.EndsWith("html", StringComparison.OrdinalIgnoreCase))
{
return "text/html; charset=utf-8";
}
return "text/plain; charset=utf-8";
}
}
private void WriteReponse(Stream stream)
{
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, false))
{
GetEmbeddedResourceStream().CopyTo(gzipStream);
}
}
protected abstract Stream GetEmbeddedResourceStream();
}
}

View File

@@ -1,36 +0,0 @@
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.SerializeToStream(ObjectToSerialize, gzipStream);
}
}
}
}

View File

@@ -1,42 +0,0 @@
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);
}
}
}

View File

@@ -1,18 +0,0 @@
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()); }
}
}
}

View File

@@ -1,112 +0,0 @@
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 string LocalPath
{
get
{
return Request.Url.LocalPath;
}
}
public RequestContext(HttpListenerContext context)
{
Response = context.Response;
Request = context.Request;
}
public void Respond(Response handler)
{
Response.AddHeader("Access-Control-Allow-Origin", "*");
Response.KeepAlive = true;
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);
}
}
}

View File

@@ -1,77 +0,0 @@
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))
{
}
}*/
}

View File

@@ -1,19 +0,0 @@
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;
})();
}
}
}