jellyfin-server/Emby.Server.Implementations/HttpServerFactory.cs

111 lines
3.4 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.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;
namespace Emby.Server.Implementations
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,
2017-05-24 19:12:55 +00:00
IServerConfigurationManager config,
2016-11-11 03:29:51 +00:00
INetworkManager networkmanager,
IMemoryStreamFactory streamProvider,
2017-05-24 19:12:55 +00:00
string serverName,
2016-11-11 03:29:51 +00:00
string defaultRedirectpath,
ITextEncoding textEncoding,
ISocketFactory socketFactory,
ICryptoProvider cryptoProvider,
IJsonSerializer json,
IXmlSerializer xml,
IEnvironmentInfo environment,
ICertificate certificate,
2017-03-12 19:27:26 +00:00
IFileSystem fileSystem,
bool enableDualModeSockets)
2016-11-11 03:29:51 +00:00
{
var logger = logManager.GetLogger("HttpServer");
return new HttpListenerHost(applicationHost,
2017-05-24 19:12:55 +00:00
logger,
config,
serverName,
defaultRedirectpath,
networkmanager,
streamProvider,
textEncoding,
socketFactory,
cryptoProvider,
json,
2016-11-11 03:29:51 +00:00
xml,
environment,
certificate,
new StreamFactory(),
GetParseFn,
2017-03-12 19:27:26 +00:00
enableDualModeSockets,
fileSystem);
2016-11-11 03:29:51 +00:00
}
private static Func<string, object> GetParseFn(Type propertyType)
{
return s => JsvReader.GetParseFn(propertyType)(s);
}
}
public class StreamFactory : IStreamFactory
{
2017-03-02 20:50:09 +00:00
public Stream CreateNetworkStream(IAcceptSocket acceptSocket, bool ownsSocket)
2016-11-11 03:29:51 +00:00
{
2017-03-02 20:50:09 +00:00
var netSocket = (NetAcceptSocket)acceptSocket;
2016-11-11 03:29:51 +00:00
2017-05-25 13:00:14 +00:00
return new SocketStream(netSocket.Socket, ownsSocket);
2016-11-11 03:29:51 +00:00
}
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; }
}
}