mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-07-02 04:12:53 +01:00
Moved the http server to it's own assembly. added comments and made other minor re-organizations.
This commit is contained in:
parent
6fbd5cf464
commit
80b3ad7bd2
@@ -2,11 +2,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Json;
|
||||
using MediaBrowser.Model.Users;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Users;
|
||||
|
||||
namespace MediaBrowser.Common.ApiInteraction
|
||||
{
|
||||
@@ -21,19 +22,20 @@ namespace MediaBrowser.Common.ApiInteraction
|
||||
WebClient = new WebClient();
|
||||
}
|
||||
|
||||
public async Task<DictionaryBaseItem> GetRootItem(Guid userId)
|
||||
public async Task<ApiBaseItemWrapper<ApiBaseItem>> GetRootItem(Guid userId)
|
||||
{
|
||||
string url = ApiUrl + "/item?userId=" + userId.ToString();
|
||||
|
||||
Stream stream = await WebClient.OpenReadTaskAsync(url);
|
||||
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
using (Stream stream = await WebClient.OpenReadTaskAsync(url))
|
||||
{
|
||||
return DictionaryBaseItem.FromApiOutput(gzipStream);
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return DeserializeBaseItemWrapper(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DictionaryBaseItem> GetItem(Guid id, Guid userId)
|
||||
public async Task<ApiBaseItemWrapper<ApiBaseItem>> GetItem(Guid id, Guid userId)
|
||||
{
|
||||
string url = ApiUrl + "/item?userId=" + userId.ToString();
|
||||
|
||||
@@ -42,11 +44,12 @@ namespace MediaBrowser.Common.ApiInteraction
|
||||
url += "&id=" + id.ToString();
|
||||
}
|
||||
|
||||
Stream stream = await WebClient.OpenReadTaskAsync(url);
|
||||
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
using (Stream stream = await WebClient.OpenReadTaskAsync(url))
|
||||
{
|
||||
return DictionaryBaseItem.FromApiOutput(gzipStream);
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return DeserializeBaseItemWrapper(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,11 +57,12 @@ namespace MediaBrowser.Common.ApiInteraction
|
||||
{
|
||||
string url = ApiUrl + "/users";
|
||||
|
||||
Stream stream = await WebClient.OpenReadTaskAsync(url);
|
||||
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
using (Stream stream = await WebClient.OpenReadTaskAsync(url))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(gzipStream);
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,11 +70,12 @@ namespace MediaBrowser.Common.ApiInteraction
|
||||
{
|
||||
string url = ApiUrl + "/genres?userId=" + userId.ToString();
|
||||
|
||||
Stream stream = await WebClient.OpenReadTaskAsync(url);
|
||||
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
using (Stream stream = await WebClient.OpenReadTaskAsync(url))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo>>(gzipStream);
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,11 +83,12 @@ namespace MediaBrowser.Common.ApiInteraction
|
||||
{
|
||||
string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
|
||||
|
||||
Stream stream = await WebClient.OpenReadTaskAsync(url);
|
||||
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
using (Stream stream = await WebClient.OpenReadTaskAsync(url))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<CategoryInfo>(gzipStream);
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<CategoryInfo>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,11 +96,12 @@ namespace MediaBrowser.Common.ApiInteraction
|
||||
{
|
||||
string url = ApiUrl + "/studios?userId=" + userId.ToString();
|
||||
|
||||
Stream stream = await WebClient.OpenReadTaskAsync(url);
|
||||
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
using (Stream stream = await WebClient.OpenReadTaskAsync(url))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo>>(gzipStream);
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,12 +109,20 @@ namespace MediaBrowser.Common.ApiInteraction
|
||||
{
|
||||
string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
|
||||
|
||||
Stream stream = await WebClient.OpenReadTaskAsync(url);
|
||||
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
using (Stream stream = await WebClient.OpenReadTaskAsync(url))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<CategoryInfo>(gzipStream);
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<CategoryInfo>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ApiBaseItemWrapper<ApiBaseItem> DeserializeBaseItemWrapper(Stream stream)
|
||||
{
|
||||
ApiBaseItemWrapper<ApiBaseItem> data = JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(stream);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,412 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MediaBrowser.Common.Json;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Users;
|
||||
using System.Linq;
|
||||
|
||||
namespace MediaBrowser.Common.ApiInteraction
|
||||
{
|
||||
public class DictionaryBaseItem : BaseItem
|
||||
{
|
||||
private Dictionary<string, object> Dictionary { get; set; }
|
||||
|
||||
public UserItemData UserItemData { get; set; }
|
||||
public IEnumerable<DictionaryBaseItem> Children { get; set; }
|
||||
|
||||
public DictionaryBaseItem(Dictionary<string, object> dictionary)
|
||||
{
|
||||
Dictionary = dictionary;
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("Name");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("Name", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ArtImagePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("ArtImagePath");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("ArtImagePath", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string AspectRatio
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("AspectRatio");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("AspectRatio", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string BannerImagePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("BannerImagePath");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("BannerImagePath", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string CustomPin
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("CustomPin");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("CustomPin", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string CustomRating
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("CustomRating");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("CustomRating", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string DisplayMediaType
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("DisplayMediaType");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("DisplayMediaType", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string LogoImagePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("LogoImagePath");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("LogoImagePath", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string OfficialRating
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("OfficialRating");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("OfficialRating", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string Overview
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("Overview");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("Overview", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string Path
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("Path");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("Path", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string PrimaryImagePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("PrimaryImagePath");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("PrimaryImagePath", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string SortName
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("SortName");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("SortName", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string Tagline
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("Tagline");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("Tagline", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string TrailerUrl
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("TrailerUrl");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("TrailerUrl", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override DateTime DateCreated
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetDateTime("DateCreated");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("DateCreated", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override DateTime DateModified
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetDateTime("DateModified");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("DateModified", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override float? UserRating
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetNullableFloat("UserRating");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("UserRating", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ThumbnailImagePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetString("ThumbnailImagePath");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("ThumbnailImagePath", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override int? ProductionYear
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetNullableInt("ProductionYear");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("ProductionYear", value);
|
||||
}
|
||||
}
|
||||
|
||||
public override TimeSpan? RunTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetNullableTimeSpan("RunTime");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("RunTime", value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsFolder
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetBool("IsFolder");
|
||||
}
|
||||
}
|
||||
|
||||
public override Guid Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetGuid("Id");
|
||||
}
|
||||
set
|
||||
{
|
||||
SetValue("Id", value);
|
||||
}
|
||||
}
|
||||
|
||||
public TimeSpan? GetNullableTimeSpan(string name)
|
||||
{
|
||||
string val = Dictionary[name] as string;
|
||||
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return TimeSpan.Parse(val);
|
||||
}
|
||||
|
||||
public int? GetNullableInt(string name)
|
||||
{
|
||||
string val = Dictionary[name] as string;
|
||||
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return int.Parse(val);
|
||||
}
|
||||
|
||||
public float? GetNullableFloat(string name)
|
||||
{
|
||||
string val = Dictionary[name] as string;
|
||||
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return float.Parse(val);
|
||||
}
|
||||
|
||||
public DateTime? GetNullableDateTime(string name)
|
||||
{
|
||||
string val = Dictionary[name] as string;
|
||||
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return DateTime.Parse(val);
|
||||
}
|
||||
|
||||
public DateTime GetDateTime(string name)
|
||||
{
|
||||
DateTime? val = GetNullableDateTime(name);
|
||||
|
||||
return val ?? DateTime.MinValue;
|
||||
}
|
||||
|
||||
public bool? GetNullableBool(string name)
|
||||
{
|
||||
string val = Dictionary[name] as string;
|
||||
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return val != "false";
|
||||
}
|
||||
|
||||
public Guid GetGuid(string name)
|
||||
{
|
||||
string val = GetString(name);
|
||||
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
return Guid.Empty;
|
||||
}
|
||||
|
||||
return Guid.Parse(val);
|
||||
}
|
||||
|
||||
public bool GetBool(string name)
|
||||
{
|
||||
bool? val = GetNullableBool(name);
|
||||
|
||||
return val ?? false;
|
||||
}
|
||||
|
||||
public string GetString(string name)
|
||||
{
|
||||
return Dictionary[name] as string;
|
||||
}
|
||||
|
||||
private void SetValue<T>(string name, T value)
|
||||
{
|
||||
Dictionary[name] = value;
|
||||
}
|
||||
|
||||
public static DictionaryBaseItem FromApiOutput(Stream stream)
|
||||
{
|
||||
Dictionary<string, object> data = JsonSerializer.DeserializeFromStream<Dictionary<string, object>>(stream);
|
||||
|
||||
string baseItem = data["Item"] as string;
|
||||
|
||||
DictionaryBaseItem item = new DictionaryBaseItem(JsonSerializer.DeserializeFromString<Dictionary<string, object>>(baseItem));
|
||||
|
||||
if (data.ContainsKey("UserItemData"))
|
||||
{
|
||||
item.UserItemData = JsonSerializer.DeserializeFromString<UserItemData>(data["UserItemData"].ToString());
|
||||
}
|
||||
|
||||
if (data.ContainsKey("Children"))
|
||||
{
|
||||
item.Children = JsonSerializer.DeserializeFromString<IEnumerable<Dictionary<string, object>>>(data["Children"].ToString()).Select(c => new DictionaryBaseItem(c));
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,24 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
|
||||
namespace MediaBrowser.Common.Json
|
||||
{
|
||||
public class JsonSerializer
|
||||
{
|
||||
public static void SerializeToStream<T>(T o, Stream stream)
|
||||
public static void SerializeToStream<T>(T obj, Stream stream)
|
||||
{
|
||||
Configure();
|
||||
|
||||
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(o, stream);
|
||||
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
|
||||
}
|
||||
|
||||
public static void SerializeToFile<T>(T o, string file)
|
||||
public static void SerializeToFile<T>(T obj, string file)
|
||||
{
|
||||
Configure();
|
||||
|
||||
using (StreamWriter streamWriter = new StreamWriter(file))
|
||||
{
|
||||
ServiceStack.Text.JsonSerializer.SerializeToWriter<T>(o, streamWriter);
|
||||
ServiceStack.Text.JsonSerializer.SerializeToWriter<T>(obj, streamWriter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,12 +45,11 @@ namespace MediaBrowser.Common.Json
|
||||
|
||||
return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(data);
|
||||
}
|
||||
|
||||
|
||||
private static void Configure()
|
||||
{
|
||||
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
||||
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
||||
ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Common.Logging
|
||||
{
|
||||
|
||||
@@ -35,9 +35,6 @@
|
||||
</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" />
|
||||
@@ -48,15 +45,6 @@
|
||||
<Compile Include="ApiInteraction\ApiController.cs" />
|
||||
<Compile Include="Events\GenericItemEventArgs.cs" />
|
||||
<Compile Include="Json\JsonSerializer.cs" />
|
||||
<Compile Include="ApiInteraction\DictionaryBaseItem.cs" />
|
||||
<Compile Include="Net\CollectionExtensions.cs" />
|
||||
<Compile Include="Net\Handlers\BaseEmbeddedResourceHandler.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" />
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
{
|
||||
}
|
||||
}*/
|
||||
}
|
||||
@@ -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;
|
||||
})();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using MediaBrowser.Common.Json;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
public class BasePluginConfiguration
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Rx-Main" version="1.0.11226" targetFramework="net45" />
|
||||
<package id="ServiceStack.Text" version="3.8.5" targetFramework="net45" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user