aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Net
diff options
context:
space:
mode:
authorLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-07-19 22:22:44 -0400
committerLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-07-19 22:22:44 -0400
commit80b3ad7bd20329e6a5bbf6eeb76af62c87434a7c (patch)
tree81ab455261cf30fab4b932215211d8cd0e57547a /MediaBrowser.Net
parent6fbd5cf46407a212fadb52eee00c7ac7690430ea (diff)
Moved the http server to it's own assembly. added comments and made other minor re-organizations.
Diffstat (limited to 'MediaBrowser.Net')
-rw-r--r--MediaBrowser.Net/CollectionExtensions.cs14
-rw-r--r--MediaBrowser.Net/Handlers/BaseEmbeddedResourceHandler.cs58
-rw-r--r--MediaBrowser.Net/Handlers/BaseHandler.cs111
-rw-r--r--MediaBrowser.Net/Handlers/BaseJsonHandler.cs11
-rw-r--r--MediaBrowser.Net/HttpServer.cs42
-rw-r--r--MediaBrowser.Net/MediaBrowser.Net.csproj66
-rw-r--r--MediaBrowser.Net/Properties/AssemblyInfo.cs36
-rw-r--r--MediaBrowser.Net/Request.cs18
-rw-r--r--MediaBrowser.Net/RequestContext.cs118
-rw-r--r--MediaBrowser.Net/StreamExtensions.cs19
-rw-r--r--MediaBrowser.Net/packages.config4
11 files changed, 497 insertions, 0 deletions
diff --git a/MediaBrowser.Net/CollectionExtensions.cs b/MediaBrowser.Net/CollectionExtensions.cs
new file mode 100644
index 000000000..a6b59904f
--- /dev/null
+++ b/MediaBrowser.Net/CollectionExtensions.cs
@@ -0,0 +1,14 @@
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Linq;
+
+namespace MediaBrowser.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.Net/Handlers/BaseEmbeddedResourceHandler.cs b/MediaBrowser.Net/Handlers/BaseEmbeddedResourceHandler.cs
new file mode 100644
index 000000000..d7f579d05
--- /dev/null
+++ b/MediaBrowser.Net/Handlers/BaseEmbeddedResourceHandler.cs
@@ -0,0 +1,58 @@
+using System;
+using System.IO;
+
+namespace MediaBrowser.Net.Handlers
+{
+ public abstract class BaseEmbeddedResourceHandler : BaseHandler
+ {
+ public BaseEmbeddedResourceHandler(string resourcePath)
+ : base()
+ {
+ ResourcePath = resourcePath;
+ }
+
+ 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";
+ }
+ }
+
+ protected override void WriteResponseToOutputStream(Stream stream)
+ {
+ GetEmbeddedResourceStream().CopyTo(stream);
+ }
+
+ protected abstract Stream GetEmbeddedResourceStream();
+ }
+}
diff --git a/MediaBrowser.Net/Handlers/BaseHandler.cs b/MediaBrowser.Net/Handlers/BaseHandler.cs
new file mode 100644
index 000000000..7a2bcffe9
--- /dev/null
+++ b/MediaBrowser.Net/Handlers/BaseHandler.cs
@@ -0,0 +1,111 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.IO;
+using System.IO.Compression;
+
+namespace MediaBrowser.Net.Handlers
+{
+ public abstract class BaseHandler
+ {
+ /// <summary>
+ /// Response headers
+ /// </summary>
+ public IDictionary<string, string> Headers = new Dictionary<string, string>();
+
+ /// <summary>
+ /// The action to write the response to the output stream
+ /// </summary>
+ public Action<Stream> WriteStream { get; set; }
+
+ /// <summary>
+ /// The original RequestContext
+ /// </summary>
+ public RequestContext RequestContext { get; set; }
+
+ /// <summary>
+ /// The original QueryString
+ /// </summary>
+ protected NameValueCollection QueryString
+ {
+ get
+ {
+ return RequestContext.Request.QueryString;
+ }
+ }
+
+ /// <summary>
+ /// Gets the MIME type to include in the response headers
+ /// </summary>
+ public abstract string ContentType { get; }
+
+ /// <summary>
+ /// Gets the status code to include in the response headers
+ /// </summary>
+ public virtual int StatusCode
+ {
+ get
+ {
+ return 200;
+ }
+ }
+
+ /// <summary>
+ /// Gets the cache duration to include in the response headers
+ /// </summary>
+ public virtual TimeSpan CacheDuration
+ {
+ get
+ {
+ return TimeSpan.FromTicks(0);
+ }
+ }
+
+ /// <summary>
+ /// Gets the last date modified of the content being returned, if this can be determined.
+ /// This will be used to invalidate the cache, so it's not needed if CacheDuration is 0.
+ /// </summary>
+ public virtual DateTime? LastDateModified
+ {
+ get
+ {
+ return null;
+ }
+ }
+
+ public virtual bool GzipResponse
+ {
+ get
+ {
+ return true;
+ }
+ }
+
+ public BaseHandler()
+ {
+ WriteStream = s =>
+ {
+ WriteReponse(s);
+ s.Close();
+ };
+ }
+
+ private void WriteReponse(Stream stream)
+ {
+ if (GzipResponse)
+ {
+ using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, false))
+ {
+ WriteResponseToOutputStream(gzipStream);
+ }
+ }
+ else
+ {
+ WriteResponseToOutputStream(stream);
+ }
+ }
+
+ protected abstract void WriteResponseToOutputStream(Stream stream);
+
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Net/Handlers/BaseJsonHandler.cs b/MediaBrowser.Net/Handlers/BaseJsonHandler.cs
new file mode 100644
index 000000000..725b89c32
--- /dev/null
+++ b/MediaBrowser.Net/Handlers/BaseJsonHandler.cs
@@ -0,0 +1,11 @@
+
+namespace MediaBrowser.Net.Handlers
+{
+ public abstract class BaseJsonHandler : BaseHandler
+ {
+ public override string ContentType
+ {
+ get { return "application/json"; }
+ }
+ }
+}
diff --git a/MediaBrowser.Net/HttpServer.cs b/MediaBrowser.Net/HttpServer.cs
new file mode 100644
index 000000000..aa24538d0
--- /dev/null
+++ b/MediaBrowser.Net/HttpServer.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Net;
+using System.Reactive.Linq;
+
+namespace MediaBrowser.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.Net/MediaBrowser.Net.csproj b/MediaBrowser.Net/MediaBrowser.Net.csproj
new file mode 100644
index 000000000..6f1b7cccf
--- /dev/null
+++ b/MediaBrowser.Net/MediaBrowser.Net.csproj
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{5DA08D1C-0D52-4B1B-AA66-E4A171D938F6}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>MediaBrowser.Net</RootNamespace>
+ <AssemblyName>MediaBrowser.Net</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <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" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="CollectionExtensions.cs" />
+ <Compile Include="Handlers\BaseEmbeddedResourceHandler.cs" />
+ <Compile Include="Handlers\BaseJsonHandler.cs" />
+ <Compile Include="HttpServer.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Request.cs" />
+ <Compile Include="RequestContext.cs" />
+ <Compile Include="Handlers\BaseHandler.cs" />
+ <Compile Include="StreamExtensions.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ No newline at end of file
diff --git a/MediaBrowser.Net/Properties/AssemblyInfo.cs b/MediaBrowser.Net/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..00964b6a2
--- /dev/null
+++ b/MediaBrowser.Net/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("MediaBrowser.Net")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("MediaBrowser.Net")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("eacc40b5-e24e-4467-8000-f40874048d45")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/MediaBrowser.Net/Request.cs b/MediaBrowser.Net/Request.cs
new file mode 100644
index 000000000..43f854233
--- /dev/null
+++ b/MediaBrowser.Net/Request.cs
@@ -0,0 +1,18 @@
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace MediaBrowser.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.Net/RequestContext.cs b/MediaBrowser.Net/RequestContext.cs
new file mode 100644
index 000000000..4c1350701
--- /dev/null
+++ b/MediaBrowser.Net/RequestContext.cs
@@ -0,0 +1,118 @@
+using System;
+using System.Linq;
+using System.Net;
+using MediaBrowser.Net.Handlers;
+
+namespace MediaBrowser.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(BaseHandler 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 (handler.GzipResponse)
+ {
+ Response.AddHeader("Content-Encoding", "gzip");
+ }
+
+ 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.Net/StreamExtensions.cs b/MediaBrowser.Net/StreamExtensions.cs
new file mode 100644
index 000000000..b4925cc7f
--- /dev/null
+++ b/MediaBrowser.Net/StreamExtensions.cs
@@ -0,0 +1,19 @@
+using System;
+using System.IO;
+using System.Reactive.Linq;
+
+namespace MediaBrowser.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.Net/packages.config b/MediaBrowser.Net/packages.config
new file mode 100644
index 000000000..47102a263
--- /dev/null
+++ b/MediaBrowser.Net/packages.config
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Rx-Main" version="1.0.11226" targetFramework="net45" />
+</packages> \ No newline at end of file