using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using EmbedIO.Utilities;
namespace EmbedIO
{
///
/// Provides extension methods for .
///
public static class WebServerOptionsExtensions
{
///
/// Adds a URL prefix.
///
/// The on which this method is called.
/// The URL prefix.
/// with added.
/// is .
/// The configuration of is locked.
/// is .
///
/// is the empty string.
/// - or -
/// is already registered.
///
public static WebServerOptions WithUrlPrefix(this WebServerOptions @this, string urlPrefix)
{
@this.AddUrlPrefix(urlPrefix);
return @this;
}
///
/// Adds zero or more URL prefixes.
///
/// The on which this method is called.
/// An enumeration of URL prefixes to add.
/// with every non- element
/// of added.
/// is .
/// The configuration of is locked.
/// is .
///
/// One or more of the elements of is the empty string.
/// - or -
/// One or more of the elements of is already registered.
///
public static WebServerOptions WithUrlPrefixes(this WebServerOptions @this, IEnumerable urlPrefixes)
{
foreach (var urlPrefix in Validate.NotNull(nameof(urlPrefixes), urlPrefixes))
@this.AddUrlPrefix(urlPrefix);
return @this;
}
///
/// Adds zero or more URL prefixes.
///
/// The on which this method is called.
/// An array of URL prefixes to add.
/// with every non- element
/// of added.
/// is .
/// The configuration of is locked.
/// is .
///
/// One or more of the elements of is the empty string.
/// - or -
/// One or more of the elements of is already registered.
///
public static WebServerOptions WithUrlPrefixes(this WebServerOptions @this, params string[] urlPrefixes)
=> WithUrlPrefixes(@this, urlPrefixes as IEnumerable);
///
/// Sets the type of HTTP listener.
///
/// The on which this method is called.
/// The type of HTTP listener.
/// with its Mode property
/// set to .
/// is .
/// The configuration of is locked.
public static WebServerOptions WithMode(this WebServerOptions @this, HttpListenerMode value)
{
@this.Mode = value;
return @this;
}
///
/// Sets the type of HTTP listener to .
///
/// The on which this method is called.
/// with its Mode property
/// set to .
/// is .
/// The configuration of is locked.
public static WebServerOptions WithEmbedIOHttpListener(this WebServerOptions @this)
{
@this.Mode = HttpListenerMode.EmbedIO;
return @this;
}
///
/// Sets the type of HTTP listener to .
///
/// The on which this method is called.
/// with its Mode property
/// set to .
/// is .
/// The configuration of is locked.
public static WebServerOptions WithMicrosoftHttpListener(this WebServerOptions @this)
{
@this.Mode = HttpListenerMode.Microsoft;
return @this;
}
///
/// Sets the X.509 certificate to use for SSL connections.
///
/// The on which this method is called.
/// The X.509 certificate to use for SSL connections.
/// with its Certificate property
/// set to .
/// is .
/// The configuration of is locked.
public static WebServerOptions WithCertificate(this WebServerOptions @this, X509Certificate2 value)
{
@this.Certificate = value;
return @this;
}
///
/// Sets the thumbprint of the X.509 certificate to use for SSL connections.
///
/// The on which this method is called.
/// The thumbprint of the X.509 certificate to use for SSL connections.
/// with its CertificateThumbprint property
/// set to .
/// is .
/// The configuration of is locked.
public static WebServerOptions WithCertificateThumbprint(this WebServerOptions @this, string value)
{
@this.CertificateThumbprint = value;
return @this;
}
///
/// Sets a value indicating whether to automatically load the X.509 certificate.
///
/// The on which this method is called.
/// If , automatically load the X.509 certificate.
/// with its AutoLoadCertificate property
/// set to .
/// is .
/// The configuration of is locked.
/// is
/// and the underlying operating system is not Windows.
public static WebServerOptions WithAutoLoadCertificate(this WebServerOptions @this, bool value)
{
@this.AutoLoadCertificate = value;
return @this;
}
///
/// Instructs a instance to automatically load the X.509 certificate.
///
/// The on which this method is called.
/// with its AutoLoadCertificate property
/// set to .
/// is .
/// The configuration of is locked.
/// The underlying operating system is not Windows.
public static WebServerOptions WithAutoLoadCertificate(this WebServerOptions @this)
{
@this.AutoLoadCertificate = true;
return @this;
}
///
/// Instructs a instance to not load the X.509 certificate automatically .
///
/// The on which this method is called.
/// with its AutoLoadCertificate property
/// set to .
/// is .
/// The configuration of is locked.
public static WebServerOptions WithoutAutoLoadCertificate(this WebServerOptions @this)
{
@this.AutoLoadCertificate = false;
return @this;
}
///
/// Sets a value indicating whether to automatically bind the X.509 certificate
/// to the port used for HTTPS.
///
/// The on which this method is called.
/// If , automatically bind the X.509 certificate
/// to the port used for HTTPS.
/// with its AutoRegisterCertificate property
/// set to .
/// is .
/// The configuration of is locked.
/// is
/// and the underlying operating system is not Windows.
public static WebServerOptions WithAutoRegisterCertificate(this WebServerOptions @this, bool value)
{
@this.AutoRegisterCertificate = value;
return @this;
}
///
/// Instructs a instance to automatically bind the X.509 certificate
/// to the port used for HTTPS.
///
/// The on which this method is called.
/// with its AutoRegisterCertificate property
/// set to .
/// is .
/// The configuration of is locked.
/// The underlying operating system is not Windows.
public static WebServerOptions WithAutoRegisterCertificate(this WebServerOptions @this)
{
@this.AutoRegisterCertificate = true;
return @this;
}
///
/// Instructs a instance to not bind the X.509 certificate automatically.
///
/// The on which this method is called.
/// with its AutoRegisterCertificate property
/// set to .
/// is .
/// The configuration of is locked.
public static WebServerOptions WithoutAutoRegisterCertificate(this WebServerOptions @this)
{
@this.AutoRegisterCertificate = false;
return @this;
}
///
/// Sets a value indicating the X.509 certificate store where to load the certificate from.
///
/// The on which this method is called.
/// One of the constants.
/// with its StoreName property
/// set to .
/// is .
/// The configuration of is locked.
///
public static WebServerOptions WithStoreName(this WebServerOptions @this, StoreName value)
{
@this.StoreName = value;
return @this;
}
///
/// Sets a value indicating the location of the X.509 certificate store where to load the certificate from.
///
/// The on which this method is called.
/// One of the constants.
/// with its StoreLocation property
/// set to .
/// is .
/// The configuration of is locked.
///
public static WebServerOptions WithStoreLocation(this WebServerOptions @this, StoreLocation value)
{
@this.StoreLocation = value;
return @this;
}
///
/// Sets the name and location of the X.509 certificate store where to load the certificate from.
///
/// The on which this method is called.
/// One of the constants.
/// One of the constants.
/// with its StoreName property
/// set to and its StoreLocation property
/// set to .
/// is .
/// The configuration of is locked.
///
///
public static WebServerOptions WithStore(this WebServerOptions @this, StoreName name, StoreLocation location)
{
@this.StoreName = name;
@this.StoreLocation = location;
return @this;
}
}
}