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

This class contains all the settings of the server. More...

Public Member Functions

 Configuration ()
 Creates a default fail-safe configuration (still, the port could be in use)
 
 Configuration (string content)
 Instantiate configuration from a json file (content passed as string)
 
 Configuration (string address, ushort port, string staticPath, Debugger? debugInfo=null, IPMode ipMode=IPMode.ANY, int requestMaxSize=KILOBYTE, ulong? defaultSessionExpirationTime=null, SslConfiguration? sslConfiguration=null)
 Instantiate a configuration with the minimal settings.
 
void SaveToJson (string path)
 Save the current configuration to a json file.
 
void GET (string path, Delegate func)
 Map a function to a path that will reply with a GET response.
 
void POST (string path, Delegate func)
 Map a function to a path that will reply with a POST response.
 
void HEAD (string path, Delegate func)
 Map a function to a path that will reply with a HEAD response.
 
void PUT (string path, Delegate func)
 Map a function to a path that will reply with a HEAD response.
 
void DELETE (string path, Delegate func)
 Map a function to a path that will reply with a DELETE response.
 
void PATCH (string path, Delegate func)
 Map a function to a path that will reply with a PATCH response.
 
void TRACE (string path, Delegate func)
 Map a function to a path that will reply with a TRACE response.
 
void OPTIONS (string path, Delegate func)
 Map a function to a path that will reply with a OPTIONS response.
 
void CONNECT (string path, Delegate func)
 Map a function to a path that will reply with a CONNECT response.
 
void AddSharedObject (string name, object o)
 Add an object shared between all servlet.
 
object GetSharedObject (string name)
 Get an object shared between all servlet.
 
void RemoveSharedObject (string name)
 Remove an object shared between all servlet.
 
void AddCustomGlobalHeader (string name, string value)
 Add an HTTP Response header that will be added to ALL the responses.
 
void RemoveCustomGlobalHeader (string name)
 Remove a global HTTP Response header previously added.
 
string GetCustomGlobalHeader (string name)
 Gets the value of a global HTTP Response header previously added.
 
void AddCustomGlobalCookie (Cookie cookie)
 Add (Or replaces) a cookie that will be added to ALL the responses.
 
void RemoveCustomGlobalCookie (string name)
 Remove a global cookie previously added.
 
Cookie GetCustomGlobalCookie (string name)
 Gets the value of a global cookie previously added.
 
List< string > GetRawArguments ()
 
void HideBrandingOnStartup ()
 
override string ToString ()
 String representing the configuration.
 
List< Tuple< string, string > > GetAllRoutes ()
 

Static Public Member Functions

static Configuration LoadFromJson (string path)
 Instantiate configuration from a json file passed as parameter.
 

Public Attributes

string Address
 The server listening address, ex : "127.0.0.1" or "192.168.1.2" or "" (for any address)
 
ushort Port
 The server listening port.
 
string PublicURL = ""
 When this field is set, it will be used for Unsecure SSL requests upgrade.
 
string StaticFolderPath
 Indicates the location where all static files will be searched and served from.
 
Debugger Debug
 Holds all debug information and routines.
 
int RequestMaxSize
 Specifies the size in bytes of the buffer that will contain the HTTP request.
 
bool HideBranding = false
 Hide the HSB logo on startup.
 
ulong DefaultSessionExpirationTime
 Sets the expiration time of the session.
 
string CustomServerName = ""
 When set, the server will use this name instead of the default one (default is: HSB-#/assembly_version (os_version))
 
bool IPAutoblock = false
 If this is set, the server will block the IP of the client if they try to access unsafe paths.
 
BLOCK_MODE BlockMode = BLOCK_MODE.NONE
 Setting this to BlockMode.WhiteList will make the server accept only requests from ip presents in ip_whitelist.txt if set to BlockMode.BlackList will ban requests from ip presents in ip_blacklist.txt.
 
List< string > PermanentIPList = []
 This list contains all the IP addresses that will be allowed/denied to access the server, it's behavior depends on the blockMode If BlockMode is set to BlockMode.OKLIST, only the IP addresses in this list will be allowed to access the server If BlockMode is set to BlockMode.BANLIST, the IP addresses in this list will be banned from accessing the server.
 
bool ServeEmbeddedResource = false
 If set to true, the server will try to search for the requested resource in the assembly resources if fails to find it, the usual chain of execution will be followed.
 
string EmbeddedResourcePrefix = ""
 If ServeEmbeddedResource is set to true, this will be prepended to the requested resource ex: if the requested resource is /index.html and the prefix is set to "www" the server will search for the resource in the assembly resources at www/index.html.
 
SslConfiguration SslSettings
 Contains the SSL configuration properties.
 

Static Public Attributes

const int KILOBYTE = 1024
 Defines the size of a kilobyte in bytes, useful to set the requestMaxSize.
 
const int MEGABYTE = KILOBYTE * KILOBYTE
 Defines the size of a megabyte in bytes, useful to set the requestMaxSize.
 

Protected Attributes

Dictionary< string, object > sharedObjects = []
 Useful to share objects between servlets without using the singleton technique.
 
Dictionary< string, string > customGlobalHeaders = []
 headers added to ANY response
 
Dictionary< string, CookiecustomGlobalCookies = []
 Cookies added to ANY response.
 

Properties

IPMode ListeningMode [get, set]
 Set server listening mode to any, only ipv4 or only ipv6. This is valid only if the address is set to "".
 
Dictionary< string, string > CustomGlobalHeaders [get]
 Gets all global HTTP Response headers.
 
Dictionary< string, CookieCustomGlobalCookies [get]
 Gets all global cookies.
 

Detailed Description

This class contains all the settings of the server.

Definition at line 11 of file Configuration.cs.

Constructor & Destructor Documentation

◆ Configuration() [1/3]

HSB.Configuration.Configuration ( )

Creates a default fail-safe configuration (still, the port could be in use)

Definition at line 122 of file Configuration.cs.

123 {
124 Address = "";
125 Port = 8080;
126 StaticFolderPath = ""; //no static file support if not set
127 Debug = new Debugger();
128 RequestMaxSize = KILOBYTE; //max 1KB Requests default
129 ListeningMode = IPMode.ANY; //listen to both ipv6 and ipv4
130 //default one day
131 DefaultSessionExpirationTime = (ulong)TimeSpan.FromDays(1).Ticks;
132 SslSettings = new SslConfiguration();
133 }
string StaticFolderPath
Indicates the location where all static files will be searched and served from.
ulong DefaultSessionExpirationTime
Sets the expiration time of the session.
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...
int RequestMaxSize
Specifies the size in bytes of the buffer that will contain the HTTP request.
const int KILOBYTE
Defines the size of a kilobyte in bytes, useful to set the requestMaxSize.
SslConfiguration SslSettings
Contains the SSL configuration properties.
IPMode
Defines the listening mode of the server.
Definition IPMode.cs:8

◆ Configuration() [2/3]

HSB.Configuration.Configuration ( string  content)

Instantiate configuration from a json file (content passed as string)

Parameters
jsonContentThe content of the JSON file

Definition at line 139 of file Configuration.cs.

140 {
141 using var doc = JsonDocument.Parse(content);
142 var root = doc.RootElement;
143 string lastProp = "";
144 try
145 {
146
147 lastProp = "Address"; Address = root.GetProperty("Address").GetString() ?? "";
148 lastProp = "Port"; Port = root.GetProperty("Port").GetUInt16();
149 lastProp = "PublicURL"; PublicURL = root.GetProperty("PublicURL").GetString() ?? "";
150 lastProp = "StaticFolderPath"; StaticFolderPath = root.GetProperty("StaticFolderPath").GetString() ?? "";
151 lastProp = "Debug"; Debug = Debugger.FromJson(root.GetProperty("Debug"));
152 lastProp = "SslSettings"; SslSettings = SslConfiguration.FromJSON(root.GetProperty("SslSettings"));
153 lastProp = "RequestMaxSize"; RequestMaxSize = root.GetProperty("Port").GetInt32();
154 lastProp = "BlockMode"; BlockMode = (BLOCK_MODE)root.GetProperty("BlockMode").GetInt32();
155 lastProp = "HideBranding"; HideBranding = root.GetProperty("HideBranding").GetBoolean();
156 lastProp = "IPAutoblock"; IPAutoblock = root.GetProperty("IPAutoblock").GetBoolean();
157 lastProp = "ListeningMode"; ListeningMode = (IPMode)root.GetProperty(nameof(ListeningMode)).GetInt32();
158 lastProp = "CustomServerName"; CustomServerName = root.GetProperty("CustomServerName").GetString() ?? "";
159 lastProp = "ServeEmbeddedResource"; ServeEmbeddedResource = root.GetProperty("ServeEmbeddedResource").GetBoolean();
160 lastProp = "EmbeddedResourcePrefix"; EmbeddedResourcePrefix = root.GetProperty("EmbeddedResourcePrefix").GetString() ?? "";
161 lastProp = "DefaultSessionExpirationTime"; DefaultSessionExpirationTime = root.GetProperty("DefaultSessionExpirationTime").GetUInt64();
162
163 lastProp = "PermanentIPList";
164 foreach (var item in root.GetProperty("PermanentIPList").EnumerateArray())
165 {
166 string? v = item.GetString();
167 if (v != null)
168 {
169 PermanentIPList.Add(v!);
170 }
171 }
172
173
174
175 }
176 catch (Exception e)
177 {
178 Terminal.ERROR("Cannot parse configuration file");
179 Terminal.ERROR(e);
180 Terminal.ERROR($"Last property that was being parsed: {lastProp}");
181 Environment.Exit(1);
182 }
183 }
bool IPAutoblock
If this is set, the server will block the IP of the client if they try to access unsafe paths.
List< string > PermanentIPList
This list contains all the IP addresses that will be allowed/denied to access the server,...
bool HideBranding
Hide the HSB logo on startup.
string CustomServerName
When set, the server will use this name instead of the default one (default is: HSB-#/assembly_versio...
BLOCK_MODE BlockMode
Setting this to BlockMode.WhiteList will make the server accept only requests from ip presents in ip_...
string EmbeddedResourcePrefix
If ServeEmbeddedResource is set to true, this will be prepended to the requested resource ex: if the ...
string PublicURL
When this field is set, it will be used for Unsecure SSL requests upgrade.
bool ServeEmbeddedResource
If set to true, the server will try to search for the requested resource in the assembly resources if...
BLOCK_MODE
Defines how to block requests.
Definition BlockMode.cs:7

◆ Configuration() [3/3]

HSB.Configuration.Configuration ( string  address,
ushort  port,
string  staticPath,
Debugger debugInfo = null,
IPMode  ipMode = IPMode::ANY,
int  requestMaxSize = KILOBYTE,
ulong?  defaultSessionExpirationTime = null,
SslConfiguration sslConfiguration = null 
)

Instantiate a configuration with the minimal settings.

Parameters
addressListening address (es: "127.0.0.1" or "192.168.1.2" or "" for any)
portListening port
staticPathPath of the static folder
debugInfoClass holding debugging information
IPv4OnlySets whether or not listen only to ipv6 addresses

Definition at line 194 of file Configuration.cs.

195 {
196 Address = address;
197 Port = port;
198 StaticFolderPath = staticPath;
199 Debug = debugInfo ?? new Debugger();
200 ListeningMode = ipMode;
201 //default 1KB max requests
202 RequestMaxSize = requestMaxSize;
203 //default one day
204 DefaultSessionExpirationTime = defaultSessionExpirationTime ?? (ulong)TimeSpan.FromDays(1).Ticks;
205 SslSettings = sslConfiguration ?? new SslConfiguration();
206
207 }

Member Function Documentation

◆ AddCustomGlobalCookie()

void HSB.Configuration.AddCustomGlobalCookie ( Cookie  cookie)

Add (Or replaces) a cookie that will be added to ALL the responses.

Parameters
nameName of the cookie
valueValue of the cookie

Definition at line 336 of file Configuration.cs.

337 {
338 customGlobalCookies.Remove(cookie.name);
339 customGlobalCookies.Add(cookie.name, cookie);
340 }
Dictionary< string, Cookie > customGlobalCookies
Cookies added to ANY response.

◆ AddCustomGlobalHeader()

void HSB.Configuration.AddCustomGlobalHeader ( string  name,
string  value 
)

Add an HTTP Response header that will be added to ALL the responses.

Parameters
nameName of the header
valueValue of the header

◆ AddSharedObject()

void HSB.Configuration.AddSharedObject ( string  name,
object  o 
)

Add an object shared between all servlet.

Parameters
nameName of the object
oObject to share

◆ CONNECT()

void HSB.Configuration.CONNECT ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a CONNECT response.

Parameters
pathMapping
funcFunction that will handle the request

◆ DELETE()

void HSB.Configuration.DELETE ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a DELETE response.

Parameters
pathMapping
funcFunction that will handle the request

◆ GET()

void HSB.Configuration.GET ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a GET response.

Parameters
pathMapping
funcFunction that will handle the request

◆ GetAllRoutes()

List< Tuple< string, string > > HSB.Configuration.GetAllRoutes ( )

Definition at line 389 of file Configuration.cs.

390 {
391 List<Tuple<string, string>> routes = [];
392 expressMapping.ForEach(m => routes.Add(new(m.Item1, m.Item2.Item1.ToString())));
393 var staticRoutes = Server.CollectStaticRoutes();
394 staticRoutes.ToList().ForEach(m => routes.Add(new(m.Key.Item1, "")));
395 return routes;
396 }

◆ GetCustomGlobalCookie()

Cookie HSB.Configuration.GetCustomGlobalCookie ( string  name)

Gets the value of a global cookie previously added.

Parameters
nameName of the cookie

◆ GetCustomGlobalHeader()

string HSB.Configuration.GetCustomGlobalHeader ( string  name)

Gets the value of a global HTTP Response header previously added.

Parameters
nameName of the header

◆ GetSharedObject()

object HSB.Configuration.GetSharedObject ( string  name)

Get an object shared between all servlet.

Parameters
nameName of the shared object

◆ HEAD()

void HSB.Configuration.HEAD ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a HEAD response.

Parameters
pathMapping
funcFunction that will handle the request

◆ LoadFromJson()

static Configuration HSB.Configuration.LoadFromJson ( string  path)
static

Instantiate configuration from a json file passed as parameter.

Parameters
path
Returns

Definition at line 214 of file Configuration.cs.

215 {
216 string content = File.ReadAllText(path);
217 return new Configuration(content);
218 }
Configuration()
Creates a default fail-safe configuration (still, the port could be in use)

◆ OPTIONS()

void HSB.Configuration.OPTIONS ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a OPTIONS response.

Parameters
pathMapping
funcFunction that will handle the request

◆ PATCH()

void HSB.Configuration.PATCH ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a PATCH response.

Parameters
pathMapping
funcFunction that will handle the request

◆ POST()

void HSB.Configuration.POST ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a POST response.

Parameters
pathMapping
funcFunction that will handle the request

◆ PUT()

void HSB.Configuration.PUT ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a HEAD response.

Parameters
pathMapping
funcFunction that will handle the request

◆ RemoveCustomGlobalCookie()

void HSB.Configuration.RemoveCustomGlobalCookie ( string  name)

Remove a global cookie previously added.

Parameters
nameName of the cookie

◆ RemoveCustomGlobalHeader()

void HSB.Configuration.RemoveCustomGlobalHeader ( string  name)

Remove a global HTTP Response header previously added.

Parameters
nameName of the header

◆ RemoveSharedObject()

void HSB.Configuration.RemoveSharedObject ( string  name)

Remove an object shared between all servlet.

Parameters
nameName of the shared object

◆ SaveToJson()

void HSB.Configuration.SaveToJson ( string  path)

Save the current configuration to a json file.

Parameters
path

Definition at line 223 of file Configuration.cs.

224 {
225
226 string json = JsonSerializer.Serialize(this, jserializerOptions);
227 File.WriteAllText(path, json);
228 }

◆ ToString()

override string HSB.Configuration.ToString ( )

String representing the configuration.

Returns

Definition at line 365 of file Configuration.cs.

366 {
367 string str = $"Current configuration:\nListening address and port: {Address}:{Port}";
368 if (StaticFolderPath == "")
369 str += "\nStatic folder is not set";
370 else
371 str += $"\nStatic folder path: {StaticFolderPath}";
372
373 if (expressMapping.Count != 0)
374 {
375 str += "\nExpressJS-Like routing map:";
376 expressMapping.ForEach(m => str += $"\nPath : {m.Item1} -> {m.Item2.Item2.Method.Name}");
377 }
378
379 var staticRoutes = Server.CollectStaticRoutes();
380
381 if (staticRoutes.Count != 0)
382 {
383 str += "\nStatic routes:";
384 staticRoutes.ToList().ForEach(m => str += $"\nPath : {m.Key.Item1} -> {m.Value.Name}");
385 }
386 return str;
387 }

◆ TRACE()

void HSB.Configuration.TRACE ( string  path,
Delegate  func 
)

Map a function to a path that will reply with a TRACE response.

Parameters
pathMapping
funcFunction that will handle the request

Member Data Documentation

◆ Address

string HSB.Configuration.Address

The server listening address, ex : "127.0.0.1" or "192.168.1.2" or "" (for any address)

Definition at line 21 of file Configuration.cs.

◆ BlockMode

BLOCK_MODE HSB.Configuration.BlockMode = BLOCK_MODE.NONE

Setting this to BlockMode.WhiteList will make the server accept only requests from ip presents in ip_whitelist.txt if set to BlockMode.BlackList will ban requests from ip presents in ip_blacklist.txt.

Definition at line 90 of file Configuration.cs.

◆ customGlobalCookies

Dictionary<string, Cookie> HSB.Configuration.customGlobalCookies = []
protected

Cookies added to ANY response.

Definition at line 69 of file Configuration.cs.

◆ customGlobalHeaders

Dictionary<string, string> HSB.Configuration.customGlobalHeaders = []
protected

headers added to ANY response

Definition at line 65 of file Configuration.cs.

◆ CustomServerName

string HSB.Configuration.CustomServerName = ""

When set, the server will use this name instead of the default one (default is: HSB-#/assembly_version (os_version))

Definition at line 81 of file Configuration.cs.

◆ Debug

Debugger HSB.Configuration.Debug

Holds all debug information and routines.

Definition at line 41 of file Configuration.cs.

◆ DefaultSessionExpirationTime

ulong HSB.Configuration.DefaultSessionExpirationTime

Sets the expiration time of the session.

Definition at line 73 of file Configuration.cs.

◆ EmbeddedResourcePrefix

string HSB.Configuration.EmbeddedResourcePrefix = ""

If ServeEmbeddedResource is set to true, this will be prepended to the requested resource ex: if the requested resource is /index.html and the prefix is set to "www" the server will search for the resource in the assembly resources at www/index.html.

Definition at line 108 of file Configuration.cs.

◆ HideBranding

bool HSB.Configuration.HideBranding = false

Hide the HSB logo on startup.

Definition at line 57 of file Configuration.cs.

◆ IPAutoblock

bool HSB.Configuration.IPAutoblock = false

If this is set, the server will block the IP of the client if they try to access unsafe paths.

Definition at line 85 of file Configuration.cs.

◆ KILOBYTE

const int HSB.Configuration.KILOBYTE = 1024
static

Defines the size of a kilobyte in bytes, useful to set the requestMaxSize.

Definition at line 49 of file Configuration.cs.

◆ MEGABYTE

const int HSB.Configuration.MEGABYTE = KILOBYTE * KILOBYTE
static

Defines the size of a megabyte in bytes, useful to set the requestMaxSize.

Definition at line 53 of file Configuration.cs.

◆ PermanentIPList

List<string> HSB.Configuration.PermanentIPList = []

This list contains all the IP addresses that will be allowed/denied to access the server, it's behavior depends on the blockMode If BlockMode is set to BlockMode.OKLIST, only the IP addresses in this list will be allowed to access the server If BlockMode is set to BlockMode.BANLIST, the IP addresses in this list will be banned from accessing the server.

Note that IPv6 and IPv4 are considered different ips!

Definition at line 97 of file Configuration.cs.

◆ Port

ushort HSB.Configuration.Port

The server listening port.

Definition at line 25 of file Configuration.cs.

◆ PublicURL

string HSB.Configuration.PublicURL = ""

When this field is set, it will be used for Unsecure SSL requests upgrade.

Definition at line 29 of file Configuration.cs.

◆ RequestMaxSize

int HSB.Configuration.RequestMaxSize

Specifies the size in bytes of the buffer that will contain the HTTP request.

Definition at line 45 of file Configuration.cs.

◆ ServeEmbeddedResource

bool HSB.Configuration.ServeEmbeddedResource = false

If set to true, the server will try to search for the requested resource in the assembly resources if fails to find it, the usual chain of execution will be followed.

Definition at line 102 of file Configuration.cs.

◆ sharedObjects

Dictionary<string, object> HSB.Configuration.sharedObjects = []
protected

Useful to share objects between servlets without using the singleton technique.

Definition at line 61 of file Configuration.cs.

◆ SslSettings

SslConfiguration HSB.Configuration.SslSettings

Contains the SSL configuration properties.

Definition at line 117 of file Configuration.cs.

◆ StaticFolderPath

string HSB.Configuration.StaticFolderPath

Indicates the location where all static files will be searched and served from.

Definition at line 37 of file Configuration.cs.

Property Documentation

◆ CustomGlobalCookies

Dictionary<string, Cookie> HSB.Configuration.CustomGlobalCookies
get

Gets all global cookies.

Definition at line 355 of file Configuration.cs.

◆ CustomGlobalHeaders

Dictionary<string, string> HSB.Configuration.CustomGlobalHeaders
get

Gets all global HTTP Response headers.

Definition at line 328 of file Configuration.cs.

◆ ListeningMode

IPMode HSB.Configuration.ListeningMode
getset

Set server listening mode to any, only ipv4 or only ipv6. This is valid only if the address is set to "".

Definition at line 33 of file Configuration.cs.

33{ get; set; }

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