jellyfin/Emby.Server.Core/HttpServerFactory.cs

108 lines
3.2 KiB
C#
Raw Normal View History

2016-11-11 03:29:51 +00:00
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Emby.Common.Implementations.Net;
using Emby.Server.Implementations.HttpServer;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Cryptography;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.System;
using MediaBrowser.Model.Text;
using ServiceStack.Text.Jsv;
using SocketHttpListener.Primitives;
2016-11-11 17:33:10 +00:00
namespace Emby.Server.Core
2016-11-11 03:29:51 +00:00
{
/// <summary>
/// Class ServerFactory
/// </summary>
public static class HttpServerFactory
{
/// <summary>
/// Creates the server.
/// </summary>
/// <returns>IHttpServer.</returns>
public static IHttpServer CreateServer(IServerApplicationHost applicationHost,
ILogManager logManager,
IServerConfigurationManager config,
INetworkManager networkmanager,
IMemoryStreamFactory streamProvider,
string serverName,
string defaultRedirectpath,
ITextEncoding textEncoding,
ISocketFactory socketFactory,
ICryptoProvider cryptoProvider,
IJsonSerializer json,
IXmlSerializer xml,
IEnvironmentInfo environment,
ICertificate certificate)
{
var logger = logManager.GetLogger("HttpServer");
return new HttpListenerHost(applicationHost,
logger,
config,
serverName,
defaultRedirectpath,
networkmanager,
streamProvider,
textEncoding,
socketFactory,
cryptoProvider,
json,
xml,
environment,
certificate,
new StreamFactory(),
GetParseFn);
}
private static Func<string, object> GetParseFn(Type propertyType)
{
return s => JsvReader.GetParseFn(propertyType)(s);
}
}
public class StreamFactory : IStreamFactory
{
public Stream CreateNetworkStream(ISocket socket, bool ownsSocket)
{
var netSocket = (NetSocket)socket;
return new NetworkStream(netSocket.Socket, ownsSocket);
}
public Task AuthenticateSslStreamAsServer(Stream stream, ICertificate certificate)
{
var sslStream = (SslStream)stream;
var cert = (Certificate)certificate;
return sslStream.AuthenticateAsServerAsync(cert.X509Certificate);
}
public Stream CreateSslStream(Stream innerStream, bool leaveInnerStreamOpen)
{
return new SslStream(innerStream, leaveInnerStreamOpen);
}
}
public class Certificate : ICertificate
{
public Certificate(X509Certificate x509Certificate)
{
X509Certificate = x509Certificate;
}
public X509Certificate X509Certificate { get; private set; }
}
}