// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
namespace Microsoft.AspNetCore.Server.Kestrel.Core
{
///
/// Provides programmatic configuration of Kestrel-specific features.
///
public class KestrelServerOptions
{
///
/// Configures the endpoints that Kestrel should listen to.
///
///
/// If this list is empty, the server.urls setting (e.g. UseUrls) is used.
///
internal List ListenOptions { get; } = new List();
///
/// Gets or sets whether the Server header should be included in each response.
///
///
/// Defaults to true.
///
public bool AddServerHeader { get; set; } = true;
///
/// Gets or sets a value that determines how Kestrel should schedule user callbacks.
///
/// The default mode is
public SchedulingMode ApplicationSchedulingMode { get; set; } = SchedulingMode.Default;
///
/// Gets or sets a value that controls whether synchronous IO is allowed for the and
///
///
/// Defaults to true.
///
public bool AllowSynchronousIO { get; set; } = true;
///
/// Enables the Listen options callback to resolve and use services registered by the application during startup.
/// Typically initialized by UseKestrel()"/>.
///
public IServiceProvider ApplicationServices { get; set; }
///
/// Provides access to request limit options.
///
public KestrelServerLimits Limits { get; } = new KestrelServerLimits();
///
/// Bind to given IP address and port.
///
public void Listen(IPAddress address, int port)
{
Listen(address, port, _ => { });
}
///
/// Bind to given IP address and port.
/// The callback configures endpoint-specific settings.
///
public void Listen(IPAddress address, int port, Action configure)
{
if (address == null)
{
throw new ArgumentNullException(nameof(address));
}
Listen(new IPEndPoint(address, port), configure);
}
///
/// Bind to given IP endpoint.
///
public void Listen(IPEndPoint endPoint)
{
Listen(endPoint, _ => { });
}
///
/// Bind to given IP address and port.
/// The callback configures endpoint-specific settings.
///
public void Listen(IPEndPoint endPoint, Action configure)
{
if (endPoint == null)
{
throw new ArgumentNullException(nameof(endPoint));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
var listenOptions = new ListenOptions(endPoint) { KestrelServerOptions = this };
configure(listenOptions);
ListenOptions.Add(listenOptions);
}
///
/// Bind to given Unix domain socket path.
///
public void ListenUnixSocket(string socketPath)
{
ListenUnixSocket(socketPath, _ => { });
}
///
/// Bind to given Unix domain socket path.
/// Specify callback to configure endpoint-specific settings.
///
public void ListenUnixSocket(string socketPath, Action configure)
{
if (socketPath == null)
{
throw new ArgumentNullException(nameof(socketPath));
}
if (socketPath.Length == 0 || socketPath[0] != '/')
{
throw new ArgumentException(CoreStrings.UnixSocketPathMustBeAbsolute, nameof(socketPath));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
var listenOptions = new ListenOptions(socketPath) { KestrelServerOptions = this };
configure(listenOptions);
ListenOptions.Add(listenOptions);
}
///
/// Open a socket file descriptor.
///
public void ListenHandle(ulong handle)
{
ListenHandle(handle, _ => { });
}
///
/// Open a socket file descriptor.
/// The callback configures endpoint-specific settings.
///
public void ListenHandle(ulong handle, Action configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
var listenOptions = new ListenOptions(handle) { KestrelServerOptions = this };
configure(listenOptions);
ListenOptions.Add(listenOptions);
}
}
}