This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathHostConfiguration.cs
112 lines (101 loc) · 4.48 KB
/
HostConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
namespace Nancy.Hosting.Self
{
using System;
using System.Diagnostics;
/// <summary>
/// Host configuration for the self host.
/// </summary>
public sealed class HostConfiguration
{
/// <summary>
/// Gets or sets a property that determines if localhost URIs are
/// rewritten to http://+:port/ style URIs to allow for listening on
/// all IP addresses, but requiring either a URL reservation, or admin
/// access.
/// Defaults to true.
/// </summary>
public bool RewriteLocalhost { get; set; }
/// <summary>
/// Configuration around automatically creating URL reservations.
/// </summary>
public UrlReservations UrlReservations { get; set; }
/// <summary>
/// Gets or sets a property that determines if Transfer-Encoding: Chunked is allowed
/// for the response instead of Content-Length.
/// Defaults to true.
/// </summary>
public bool AllowChunkedEncoding { get; set; }
/// <summary>
/// Gets or sets a property that provides a callback to be called
/// if there is an unhandled exception in the self host.
/// Note: this will *not* be called for normal Nancy exceptions
/// that are handled by the Nancy handlers.
/// Defaults to writing to debug out.
/// </summary>
public Action<Exception> UnhandledExceptionCallback { get; set; }
/// <summary>
/// Gets or sets a property that determines whether client certificates
/// are enabled or not.
/// When set to true the self host will request a client certificate if the
/// request is running over SSL.
/// Defaults to false.
/// </summary>
public bool EnableClientCertificates { get; set; }
/// <summary>
/// Gets or sets a property determining if base URI matching can fall back to just
/// using Authority (Schema + Host + Port) as base URI if it cannot match anything in
/// the known list. This should only be set to True if you have issues with port forwarding
/// (e.g. on Azure).
/// Defaults to false.
/// </summary>
public bool AllowAuthorityFallback { get; set; }
/// <summary>
/// Gets or sets a property determining how many total connections the NancyHost can maintain simultaneously.
/// Higher values mean more connections can be maintained at a slower average response times; while fewer
/// connections will be rejected.
/// Lower values will result in fewer conections, yet will be maintained at a faster average response time.
/// Defaults to the approximate number of processor threads.
/// </summary>
public int MaximumConnectionCount { get; set; }
/// <summary>
/// Gets approximate processor thread count by halfing the Logical Core count to
/// account for hyper-threading.
/// </summary>
private static int ProcessorThreadCount
{
get
{
// Divide by 2 for hyper-threading, and good defaults.
var threadCount = Environment.ProcessorCount >> 1;
if (threadCount < 1)
{
// Ensure thread count is at least 1.
return 1;
}
return threadCount;
}
}
/// <summary>
/// Initializes the default configuration.
/// MaximumConnectionCount by default is half of the Logical Core count.
/// </summary>
/// <remarks>
/// If the system running NancyHost is not using hyper-threading you may want to consider
/// supplying your own values for MaximumConnectionCount as the default assumes
/// hyperthreading is being utilized.
/// </remarks>
public HostConfiguration()
{
this.RewriteLocalhost = true;
this.UrlReservations = new UrlReservations();
this.AllowChunkedEncoding = true;
this.UnhandledExceptionCallback = e =>
{
var message = string.Format("---\n{0}\n---\n", e);
Debug.Write(message);
};
this.EnableClientCertificates = false;
this.MaximumConnectionCount = ProcessorThreadCount;
}
}
}