HttpServerBoxed 0.0.11 alpha
A simple http server for C# and .NET
Loading...
Searching...
No Matches
HSB.Server Class Reference

Public Member Functions

 Server (Configuration config)
 
void Start (bool openInBrowser=false)
 

Static Public Member Functions

static void Main ()
 

Detailed Description

Definition at line 15 of file Server.cs.

Constructor & Destructor Documentation

◆ Server()

HSB.Server.Server ( Configuration  config)

Definition at line 31 of file Server.cs.

32 {
33 int errorCode = 0;
34 if (!config.HideBranding)
35 Utils.PrintLogo();
36
37 if (config.Port == 0)
38 {
39 //if port is 0, we use a random port in the range 1024-65535
40 config.Port = (ushort)new Random().Next(1024, 65535);
41 }
42
43 this.config = config;
44 config.Debug.INFO("Starting logging...");
45
46 try
47 {
48
49
50 if (config.Address == "")
51 {
52 //address must be ANY
53 ipAddress = config.ListeningMode switch
54 {
55 IPMode.IPV4_ONLY => IPAddress.Any,
56 _ => IPAddress.IPv6Any,
57 };
58 }
59 else //note that in this case ListeningMode is NOT a valid parameter as it depends on the address
60 {
61 List<IPAddress> addresses = [.. Dns.GetHostAddresses(config.Address, AddressFamily.InterNetwork)];
62
63 //this fixes an error where user specifies an ipv4 address but want the server to listenes to BOTH or ipv6 only
64
65 if (addresses.Count != 0)
66 {
67 ipAddress = addresses.First();
68 config.ListeningMode = IPMode.IPV4_ONLY;
69 }
70 else
71 {
72 addresses = [.. Dns.GetHostAddresses(config.Address, AddressFamily.InterNetworkV6)];
73 if (addresses.Count != 0)
74 {
75 ipAddress = addresses.First();
76 config.ListeningMode = IPMode.IPV6_ONLY;
77 }
78 else
79 {
80 errorCode = (int)SERVER_ERRORS.ADDRESS_NOT_FOUND;
81 throw new Exception("Cannot found address to listen to");
82 }
83 }
84 }
85
86 //initialize the endpoints
87 localEndPoint = new(ipAddress, config.Port);
88 listener = new(ipAddress.AddressFamily,
89 SocketType.Stream, ProtocolType.Tcp);
90
91 SslConfiguration sslConf = config.SslSettings;
92
93
94 //if ssl is set and configuration is set to use two ports we start the sslListener
95 if ((sslConf.IsEnabled() || sslConf.IsDebugModeEnabled()) && sslConf.PortMode == SSL_PORT_MODE.DUAL_PORT)
96 {
97 sslLocalEndPoint = new(ipAddress, config.SslSettings.SslPort);
98 if (sslLocalEndPoint == null)
99 {
100 errorCode = (int)SERVER_ERRORS.CANNOT_CREATE_SSL_ENDPOINT;
101 throw new Exception("Cannot create SSL endpoint");
102 }
103 sslListener = new(ipAddress.AddressFamily,
104 SocketType.Stream, ProtocolType.Tcp);
105 if (sslListener == null)
106 {
107 errorCode = (int)SERVER_ERRORS.CANNOT_CREATE_SSL_LISTENER;
108 throw new Exception("Cannot create SSL listener");
109 }
110 if (sslConf.UseDebugCertificate)
111 {
112 sslCertificate = SslConfiguration.TryLoadDebugCertificate(c: config);
113 if (sslCertificate == null)
114 {
115 errorCode = (int)SERVER_ERRORS.CANNOT_LOAD_DEBUG_CERTIFICATE;
116 throw new Exception("Cannot load debug certificate, server cannot start with this configuration! Make sure openssl is installed");
117 }
118 }
119 else if (sslConf.IsEnabled())
120 sslCertificate = sslConf.GetCertificate();
121 }
122
123 if (config.ListeningMode == IPMode.ANY)
124 {
125 listener.DualMode = true;
126 if (config.SslSettings.IsEnabled())
127 sslListener!.DualMode = true;
128 }
129
130
131 if (sslConf.IsEnabled() || sslConf.IsDebugModeEnabled())
132 {
133 config.Debug.INFO("Server is running in SSL mode");
134
135 }
136 var prefix = "http";
137 if ((sslConf.IsEnabled() || sslConf.IsDebugModeEnabled()) && sslConf.PortMode == SSL_PORT_MODE.DUAL_PORT)
138 {
139 if (config.PublicURL == "")
140 config.Debug.INFO($"Listening at https://{sslLocalEndPoint}/");
141 else config.Debug.INFO($"Listening at https://{config.PublicURL}:{sslConf.SslPort}/");
142 }
143
144 else if ((sslConf.IsEnabled() || sslConf.IsDebugModeEnabled())&& sslConf.PortMode == SSL_PORT_MODE.SINGLE_PORT)
145 prefix += "s";
146
147 if (config.PublicURL == "")
148 config.Debug.INFO($"Listening at {prefix}://{localEndPoint}/");
149 else config.Debug.INFO($"Listening at {prefix}://{config.PublicURL}:{config.Port}/");
150
151 config.Debug.INFO("Server started");
152 }
153 catch (Exception e)
154 {
155 config.Debug.ERROR("An exception occurred while initializing the server ->\n" + e);
156 config.Debug.INFO("Server will now exit...");
157 Environment.Exit(-errorCode);
158 }
159 //end of the server initialization
160 }
bool HideBranding
Hide the HSB logo on startup.
string Address
The server listening address, ex : "127.0.0.1" or "192.168.1.2" or "" (for any address)
Debugger Debug
Holds all debug information and routines.
ushort Port
The server listening port.
IPMode ListeningMode
Set server listening mode to any, only ipv4 or only ipv6. This is valid only if the address is set to...
string PublicURL
When this field is set, it will be used for Unsecure SSL requests upgrade.
SslConfiguration SslSettings
Contains the SSL configuration properties.
SSL_PORT_MODE
This enum is used to determine if the server should listen on a single port for both HTTP and HTTPS o...
Definition SSLPortMode.cs:7
IPMode
Defines the listening mode of the server.
Definition IPMode.cs:8

Member Function Documentation

◆ Main()

static void HSB.Server.Main ( )
static

Definition at line 25 of file Server.cs.

26 {
27 Terminal.INFO("HSB-# has wrongfully been compiled has executable and will not run!");
28 Terminal.INFO("To run as standalone you must compile/execute the \"Standalone\" or the \"Launcher\" project");
29 Terminal.INFO("Check the documentation for more info (\"https://github.com/lorenzoconcas/HSB-Sharp\")");
30 }

◆ Start()

void HSB.Server.Start ( bool  openInBrowser = false)

Definition at line 162 of file Server.cs.

163 {
164
165 try
166 {
167 listener.Bind(localEndPoint);
168 listener.Listen(100);
169
170 var sslConf = config.SslSettings;
171
172 if (sslConf.IsEnabled() || sslConf.IsDebugModeEnabled())
173 { //sslListener and sslLocalEndPoint are not null because we checked in the constructor
174 sslListener!.Bind(sslLocalEndPoint!);
175 sslListener.Listen(100);
176 }
177
178 OpenInBrowserIfSet(openInBrowser, sslConf.IsEnabled(), sslConf.PortMode == SSL_PORT_MODE.DUAL_PORT ? sslLocalEndPoint! : localEndPoint);
179
180 //this makes the second port listen to SSL requests
181 if ((sslConf.IsEnabled() || sslConf.IsDebugModeEnabled()) && sslConf.PortMode == SSL_PORT_MODE.DUAL_PORT)
182 {
183 new Task(() =>
184 {
185 while (true)
186 {
187 Step(sslListener!, true);
188 }
189 }).Start();
190 }
191
192 //since the base port is always listening this is always executed
193 while (true)
194 {
195 //if ssl is enabled and single port is used
196 var sslMode = (sslConf.IsEnabled() || sslConf.IsDebugModeEnabled()) && sslConf.PortMode == SSL_PORT_MODE.SINGLE_PORT;
197
198 Step(listener, sslMode);
199 }
200 }
201 catch (Exception e)
202 {
203 Terminal.ERROR(e);
204 }
205 }

The documentation for this class was generated from the following file: