Initial check-in

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti
2012-07-12 02:55:27 -04:00
commit b50f78e5da
93 changed files with 5325 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace MediaBrowser.Controller.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

@@ -0,0 +1,47 @@
using System;
using System.Net;
using System.Reactive.Linq;
namespace MediaBrowser.Controller.Net
{
public class HttpServer : IObservable<RequestContext>, IDisposable
{
private readonly HttpListener listener;
private readonly IObservable<RequestContext> stream;
public HttpServer(int port)
: this("http://+:" + port + "/")
{
}
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

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Controller.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

@@ -0,0 +1,37 @@
using System.Linq;
using System.Net;
using System.IO.Compression;
namespace MediaBrowser.Controller.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 response)
{
Response.AddHeader("Access-Control-Allow-Origin", "*");
foreach (var header in response.Headers)
{
Response.AddHeader(header.Key, header.Value);
}
Response.ContentType = response.ContentType;
Response.StatusCode = response.StatusCode;
Response.SendChunked = true;
GZipStream gzipStream = new GZipStream(Response.OutputStream, CompressionMode.Compress, false);
response.WriteStream(Response.OutputStream);
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace MediaBrowser.Controller.Net
{
public class Response
{
protected RequestContext RequestContext { get; private set; }
public Response(RequestContext ctx)
{
RequestContext = ctx;
WriteStream = s => { };
StatusCode = 200;
Headers = new Dictionary<string, string>();
CacheDuration = TimeSpan.FromTicks(0);
ContentType = "text/html";
}
public int StatusCode { get; set; }
public string ContentType { get; set; }
public IDictionary<string, string> Headers { get; set; }
public TimeSpan CacheDuration { 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

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reactive.Linq;
namespace MediaBrowser.Controller.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;
})();
}
}
}