mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-20 00:55:13 +01:00
Initial check-in
This commit is contained in:
14
MediaBrowser.Controller/Net/CollectionExtensions.cs
Normal file
14
MediaBrowser.Controller/Net/CollectionExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
MediaBrowser.Controller/Net/HttpServer.cs
Normal file
47
MediaBrowser.Controller/Net/HttpServer.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
MediaBrowser.Controller/Net/Request.cs
Normal file
18
MediaBrowser.Controller/Net/Request.cs
Normal 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()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
37
MediaBrowser.Controller/Net/RequestContext.cs
Normal file
37
MediaBrowser.Controller/Net/RequestContext.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
49
MediaBrowser.Controller/Net/Response.cs
Normal file
49
MediaBrowser.Controller/Net/Response.cs
Normal 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))
|
||||
{
|
||||
}
|
||||
}*/
|
||||
}
|
||||
20
MediaBrowser.Controller/Net/StreamExtensions.cs
Normal file
20
MediaBrowser.Controller/Net/StreamExtensions.cs
Normal 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;
|
||||
})();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user