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 #.
Logging (console and syslog)
Middleware
Fast and Secure
&...
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 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
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");
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();