ModdableWebServer 1.5.0

dotnet add package ModdableWebServer --version 1.5.0                
NuGet\Install-Package ModdableWebServer -Version 1.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ModdableWebServer" Version="1.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ModdableWebServer --version 1.5.0                
#r "nuget: ModdableWebServer, 1.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ModdableWebServer as a Cake Addin
#addin nuget:?package=ModdableWebServer&version=1.5.0

// Install ModdableWebServer as a Cake Tool
#tool nuget:?package=ModdableWebServer&version=1.5.0                

Moddable Web Server

About

This project is made because I used more than 2 project the NetCoreServer Library and each project is used to make a Server that support more C# DLL to be loaded.

I am really bad at making a Good Documentation. Please help!

Using the Library

HTTP(S)

What is ServerStruct?

You only Need to know you can get the Headers, URL Parameters, and can parse a Response to it.

About Headers & Parameters: Duplication is been reduced. (Please do not use same parameter in url.)
Key of it is lowercase

Response Creator

Creating response simple.

Make sure you adding like this:
Header
Cookie
Body

//here you create a simple 200 (OK) response.
ResponseCreator response = new();
//adding one Header to it.
response.SetHeader("key","value");
//adding multiple header to it.
response.SetHeaders(
	new Dictionary<string, string>() 
	{
		{ "key", "Value" }
	}
);

//adding content to it.
response.SetBody("Hello World?");

//Getting the HttpResponse.
_ = response.GetResponse();

//we can use the already made creator for start to make a new one.
response.New(404);

If you ever will need, you can use response.SetResponse, but not suggesting it.

Use of HTTPAttribute

[HTTP("Method", "Url")]

In Method can be used any HTTP/S Method.
GET, POST, HEAD, OPTION, DELETE is accepted.

In the Url you can use the full url, or making it as parameter
If you use parameter you can use ?{args} to parse every arg into the parameters

//url
[HTTP("GET", "/full/url")]

//parameter
[HTTP("GET", "/hey/{parameters}")]

//parameter and normal args
[HTTP("GET", "/hey/{parameters}?user={username}")]

//parameter and args
[HTTP("GET", "/hey/{parameters}?{args}")]

Make sure your Function is PUBLIC, STATIC, and must return a BOOL.

[HTTP("GET", "/test")]
public static bool test(HttpRequest request, ServerStruct serverStruct)
{
	//simple response making
	serverStruct.Response.MakeOkResponse();
	//And sending the response.
	serverStruct.SendResponse();
	//return is if the page exist or not. It will try to send 404 if false.
	return true;
}

Example or Return a false:

[HTTP("GET", "/throw404")]
public static bool throw_404(HttpRequest request, ServerStruct serverStruct)
{
	return false;
}

WS(S)

What is WebSocketStruct?

It contains:

  • Request of the Connection.
    • The url.
    • Headers
    • Parameters,
    • Body
  • Is Connected or not.
  • WebSocket Bytes Request (NULLABLE)
  • Two Session for each type (WS,WSS)
  • Enum, which is currently used.

Use of WSAttribute

In the Url you can use the full url, or making it as parameter

Make sure your Function is PUBLIC, STATIC, and must a VOID.

 [WS("/ws/{test}")]
 public static void ws(WebSocketStruct webStruct)
 {
	 //bunch of stuff for printing.
     Console.WriteLine(webStruct.IsConnected);
     Console.WriteLine(webStruct.Request.Url);
     Console.WriteLine(webStruct.WSRequest?.buffer);
     Console.WriteLine(webStruct.WSRequest?.offset);
     Console.WriteLine(webStruct.WSRequest?.size);
	 //answering in the websocket.
     webStruct.SendWebSocketText("test");
 }

Making a server.

Here you gonna see how we make a server and using it.

Certificate for Secure server.

Make sure you have a PFX file in a disk, and a password for that pfx file (The PFX file must use a password!)

Using Fields Neccesary:

//add this into the before the classname
using System.Security.Authentication;
using ModdableWebServer.Helper;

You can use any SslProtocol for this, I always use TLS 1.2

//Get Context with Validation
CertHelper.GetContext( SslProtocols.Tls12, "mypfx.pfx", "asecurepassword");

//Get Context with No Validation
CertHelper.GetContextNoValidate( SslProtocols.Tls12, "mypfx.pfx", "asecurepassword");

Creation

You can use any of it to make a sever

var context = CertHelper.GetContext( SslProtocols.Tls12, "mypfx.pfx", "asecurepassword");
var ws_server = new WS_Server("127.0.0.1", 6666);
var wss_server = new WSS_Server(context, "127.0.0.1", 6667);
var http_server = new HTTP_Server("127.0.0.1", 6668);
var https_server = new HTTPS_Server(context, "127.0.0.1", 6669);

Starting and Stopping.

Simply just use Start(), or Stop()

ws_server.Start();
ws_server.Stop();

Events

Each HTTP(S) server comes with event like:

public EventHandler<(HttpRequest request, string error)> ReceivedRequestError;
public EventHandler<SocketError> OnSocketError;
public EventHandler<HttpRequest> ReceivedFailed;
public event EventHandler<(string address, int port)> Started;
public event EventHandler Stopped;

WS(S) Only add one:

public EventHandler<string> WSError;

Adding our created Attributes to the Server

Merging (Will fail if other DLL already contains url/method)

// Loading Entry assembly contained HTTP/WS Attribute and merging.
ws_server.HTTP_AttributeToMethods.Merge(Assembly.GetEntryAssembly());
ws_server.WS_AttributeToMethods.Merge(Assembly.GetEntryAssembly());
http_server.AttributeToMethods.Merge(Assembly.GetEntryAssembly());

Overriding. It will replace already existing ones.

// Loading Entry assembly contained HTTP/WS Attribute and merging.
ws_server.HTTP_AttributeToMethods.Override(Assembly.GetEntryAssembly());
ws_server.WS_AttributeToMethods.Override(Assembly.GetEntryAssembly());
http_server.AttributeToMethods.Override(Assembly.GetEntryAssembly());

Assembly Difference

This is so Technical I needed to check like 5 times.

We have 4 Assembly: ModdableWebServer (MWS)
MyServerLib (Lib)
MyServerExtenstion (Extension)
MyServerConsole (Console)

Console starts a Lib.
Lib starts MWS.
Lib starts Extension.

Extenstion Contain a Class: EXT
Lib Contains a Class: LIBC
Console Contains a Class: CONC

GetEntryAssembly = Console
GetCallingAssembly On Extension = Lib
GetCallingAssembly On Lib = Console
GetExecutingAssembly On Lib = Lib
GetExecutingAssembly On Extension = Extension
GetExecutingAssembly On Console = Console
GetAssembly(typeof(EXT)) = Extension
GetAssembly(typeof(LIBC)) = Lib
GetAssembly(typeof(CONC)) = Console

Use the GetAssembly and TypeOf if you dont know what Assembly loaded what!

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on ModdableWebServer:

Repository Stars
SlejmUr/PayCheck3
PayDay 3 Server Emulator Attempt
Version Downloads Last updated
1.5.0 107 8/7/2024
1.4.0 166 3/16/2024
1.3.2 181 2/13/2024
1.3.1 138 2/2/2024
1.3.0 117 1/25/2024
1.2.1 189 1/1/2024
1.2.0 139 12/28/2023
1.1.1 354 10/16/2023
1.1.0.1 221 10/14/2023
1.1.0 176 10/14/2023
1.0.0 172 10/13/2023

1.5!