Skip to content

xiosec/micro-web-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Micro Web Server xiosec - micro-web-server

stars - micro-web-server forks - micro-web-server GitHub release License issues - micro-web-server

A micro web server for a restful api and etc...
This project is for my associate thesis.
In this project, we have tried to design a simple tool so that programmers of other languages ​​such as Python can set up their web server with C #.

Languages and Tools

bootstrap csharp css3 dotnet git html5 javascript mssql nginx nodejs typescript

Capabilities

Logging (console and syslog)
Middleware
Fast and Secure &...

Example

public static void Info(Requests requests, Response response)
{
    Dictionary<string, string> myInfo = new Dictionary<string, string>()
    {
        {"name",requests.getArg("name","null")},
        {"age",requests.getArg("age","null") },
        {"github","https://github.com/xiosec" },
    };
    response.sendJson(myInfo, 200);

}
        
static void Main(string[] args)
{
    ConsoleLog consoleLog = new ConsoleLog();
    Dictionary<string, Action<Requests, Response>> urlPatterns = new Dictionary<string, Action<Requests, Response>>()
    {
        {@"^\/info\?name\=[a-z]+\&age=\d+$", Info},
    };

    Server server = new Server(IPAddress.Parse("127.0.0.1"), 8080, 10, urlPatterns, consoleLog);
    if (server.Start())
    {
        consoleLog.Informational("Started");
    }
}
http://127.0.0.1:8080/info?name=xiosec&age=19

Request

Request Information:

//request method (GET,POST,PUT,DELETE,...)
request.requestInfo["method"]

//request full path
request.requestInfo["path"]

//request http version
request.requestInfo["httpVersion"]

Headers:

//get header value with key
request.getHeader(key,defaultValue)

Cookies:

//get cookie value with key
request.getCookie(key,defaultValue)

URL Parameters:

//get URL parameter with key
request.getArg(key,defaultValue)

Authorization:

request.getAuthHeader() -> {"type","key"}

Body:

//Receive body value as a string
request.body

Response

Headers:

//set header
response.header["key"]="value"

Cookies:

//set cookie
response.cookie["key"]="value"

Content type and Status code:

response.extensions["name"]
response.statusCode["code"]

Send response:

response.send200Ok(Content , contentType)
response.send(Content , statusCode , contentType)
response.sendJson(Content , statusCode)
response.sendNotFound(Content , contentType)

Security:

//Set xss protection header
response.setSecurityHeader()
//The text generates a secure response
response.safeResponse(response)

Redirect:

//redirect to path
response.redirect("/path");

Middleware

An example of setting up a middleware:

public static (Requests,Response) AccessControllMiddleware(Requests requests, Response response)
{
    response.header["Access-Control-Allow-Origin"] = "*";
    response.header["Access-Control-Allow-Headers"] = "Content-Type, Content-Length, Accept-Encoding";
    return (requests, response);
}

...

server.Middlewares.Add(AccessControlMiddleware);
server.Start();

License

Released under MIT by @xiosec.