mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-07-15 18:53:27 +01:00
moved the http server to common, added more http handlers
This commit is contained in:
parent
572103cdfe
commit
5f5f2838b1
@@ -60,7 +60,7 @@ namespace MediaBrowser.Controller.Events
|
||||
public class PreBeginResolveEventArgs : EventArgs
|
||||
{
|
||||
public string Path { get; set; }
|
||||
public BaseItem Parent { get; set; }
|
||||
public Folder Parent { get; set; }
|
||||
|
||||
public bool Cancel { get; set; }
|
||||
|
||||
|
||||
@@ -7,11 +7,11 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Json;
|
||||
using MediaBrowser.Common.Logging;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Controller.Events;
|
||||
using MediaBrowser.Controller.IO;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Controller.Resolvers;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Entities;
|
||||
@@ -84,6 +84,7 @@ namespace MediaBrowser.Controller
|
||||
// Get users from users folder
|
||||
// Load root media folder
|
||||
Parallel.Invoke(ReloadUsers, ReloadRoot);
|
||||
var b = true;
|
||||
}
|
||||
|
||||
private void ReloadConfiguration()
|
||||
@@ -96,14 +97,6 @@ namespace MediaBrowser.Controller
|
||||
|
||||
private void ReloadPlugins()
|
||||
{
|
||||
if (Plugins != null)
|
||||
{
|
||||
Parallel.For(0, Plugins.Count(), i =>
|
||||
{
|
||||
Plugins.ElementAt(i).Dispose();
|
||||
});
|
||||
}
|
||||
|
||||
// Find plugins
|
||||
Plugins = PluginController.GetAllPlugins();
|
||||
|
||||
@@ -120,7 +113,7 @@ namespace MediaBrowser.Controller
|
||||
HttpServer.Dispose();
|
||||
}
|
||||
|
||||
HttpServer = new HttpServer(Configuration.HttpServerPortNumber);
|
||||
HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -133,16 +126,6 @@ namespace MediaBrowser.Controller
|
||||
ItemController.AddResovler<TBaseItemType, TResolverType>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a new BaseItem subclass
|
||||
/// </summary>
|
||||
public void RemoveBaseItemType<TBaseItemType, TResolverType>()
|
||||
where TBaseItemType : BaseItem, new()
|
||||
where TResolverType : BaseItemResolver<TBaseItemType>, new()
|
||||
{
|
||||
ItemController.RemoveResovler<TBaseItemType, TResolverType>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires when a path is about to be resolved, but before child folders and files
|
||||
/// have been collected from the file system.
|
||||
|
||||
@@ -25,18 +25,6 @@ namespace MediaBrowser.Controller.Library
|
||||
Resolvers.Insert(0, new TResolverType());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a new BaseItem resolver.
|
||||
/// </summary>
|
||||
public void RemoveResovler<TBaseItemType, TResolverType>()
|
||||
where TBaseItemType : BaseItem, new()
|
||||
where TResolverType : BaseItemResolver<TBaseItemType>, new()
|
||||
{
|
||||
IBaseItemResolver resolver = Resolvers.First(r => r.GetType() == typeof(TResolverType));
|
||||
|
||||
Resolvers.Remove(resolver);
|
||||
}
|
||||
|
||||
#region PreBeginResolvePath Event
|
||||
/// <summary>
|
||||
/// Fires when a path is about to be resolved, but before child folders and files
|
||||
|
||||
@@ -51,12 +51,6 @@
|
||||
<Compile Include="IO\Shortcut.cs" />
|
||||
<Compile Include="Library\ItemController.cs" />
|
||||
<Compile Include="Kernel.cs" />
|
||||
<Compile Include="Net\CollectionExtensions.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="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Resolvers\AudioResolver.cs" />
|
||||
<Compile Include="Resolvers\BaseItemResolver.cs" />
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
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))
|
||||
{
|
||||
}
|
||||
}*/
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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;
|
||||
})();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,9 @@ namespace MediaBrowser.Controller.Resolvers
|
||||
item.Path = args.Path;
|
||||
}
|
||||
|
||||
Folder parentFolder = args.Parent as Folder;
|
||||
|
||||
if (parentFolder != null)
|
||||
if (args.Parent != null)
|
||||
{
|
||||
item.Parent = parentFolder;
|
||||
item.Parent = args.Parent;
|
||||
}
|
||||
|
||||
item.Id = Kernel.GetMD5(item.Path);
|
||||
|
||||
Reference in New Issue
Block a user