some testing for get web gui requests

This commit is contained in:
Madpeter 2020-05-12 00:10:30 +01:00
parent cf73689e02
commit 95729fdaba

View file

@ -96,7 +96,7 @@ namespace BetterSecondBot.HttpServer
webui myUI = new webui(Config,Bot); webui myUI = new webui(Config,Bot);
while (Bot.KillMe == false) while (Bot.KillMe == false)
{ {
HttpListenerContext ctx = await listener.GetContextAsync(); HttpListenerContext ctx = await listener.GetContextAsync().ConfigureAwait(true);
HttpListenerRequest req = ctx.Request; HttpListenerRequest req = ctx.Request;
HttpListenerResponse resp = ctx.Response; HttpListenerResponse resp = ctx.Response;
string test = req.Url.AbsolutePath.Substring(1); string test = req.Url.AbsolutePath.Substring(1);
@ -110,12 +110,23 @@ namespace BetterSecondBot.HttpServer
resp.ContentType = "text/html"; resp.ContentType = "text/html";
if (req.HttpMethod == "GET") if (req.HttpMethod == "GET")
{ {
KeyValuePair<string, byte[]> replydata = myUI.process(http_args, req.Cookies); KeyValuePair<string, byte[]> replydata = myUI.Get_Process(http_args, req.Cookies);
resp.ContentType = replydata.Key; if (replydata.Value != null)
data = replydata.Value; {
resp.ContentLength64 = data.LongLength; resp.ContentType = replydata.Key;
await resp.OutputStream.WriteAsync(data, 0, data.Length); data = replydata.Value;
resp.Close(); resp.ContentLength64 = data.LongLength;
await resp.OutputStream.WriteAsync(data, 0, data.Length).ConfigureAwait(true);
resp.Close();
}
else
{
data = Encoding.UTF8.GetBytes("error with service");
resp.ContentLength64 = data.LongLength;
resp.ContentType = "text/html";
await resp.OutputStream.WriteAsync(data, 0, data.Length).ConfigureAwait(true);
resp.Close();
}
} }
else if (req.HttpMethod == "POST") else if (req.HttpMethod == "POST")
{ {
@ -136,50 +147,62 @@ namespace BetterSecondBot.HttpServer
} }
} }
bool accept_request = false;
string why_request_failed = "Bad signing request";
string command = "";
string arg = "";
string signing_code;
resp.StatusCode = 200; resp.StatusCode = 200;
if (post_args.ContainsKey("command") == true) KeyValuePair<bool, string> UI_process = myUI.Post_process(http_args,post_args);
if (UI_process.Key == false)
{ {
command = post_args["command"]; data = Encoding.UTF8.GetBytes(UI_process.Value);
if (post_args.ContainsKey("args") == true) resp.ContentLength64 = data.LongLength;
await resp.OutputStream.WriteAsync(data, 0, data.Length).ConfigureAwait(true);
}
else
{
bool accept_request = false;
string why_request_failed = "Bad signing request";
string command = "";
string arg = "";
string signing_code;
if (post_args.ContainsKey("command") == true)
{ {
arg = post_args["args"]; command = post_args["command"];
} if (post_args.ContainsKey("args") == true)
if (post_args.ContainsKey("code") == true)
{
signing_code = post_args["code"];
string raw = "" + command + "" + arg + "" + Config.Httpkey + "";
string hashcheck = helpers.GetSHA1(raw);
if (hashcheck == signing_code)
{ {
accept_request = true; arg = post_args["args"];
}
if (post_args.ContainsKey("code") == true)
{
signing_code = post_args["code"];
string raw = "" + command + "" + arg + "" + Config.Httpkey + "";
string hashcheck = helpers.GetSHA1(raw);
if (hashcheck == signing_code)
{
accept_request = true;
}
}
else
{
why_request_failed = "Post missing arg value";
} }
} }
else else
{ {
why_request_failed = "Post missing arg value"; why_request_failed = "Post missing command value";
} }
if (accept_request == true)
{
data = Encoding.UTF8.GetBytes(String.Join("###", post_controler.Call(command, arg)));
}
else
{
resp.StatusCode = 417;
data = Encoding.UTF8.GetBytes(why_request_failed);
}
resp.ContentLength64 = data.LongLength;
await resp.OutputStream.WriteAsync(data, 0, data.Length);
} }
else
{
why_request_failed = "Post missing command value";
}
if (accept_request == true)
{
data = Encoding.UTF8.GetBytes(String.Join("###", post_controler.Call(command, arg)));
}
else
{
resp.StatusCode = 417;
data = Encoding.UTF8.GetBytes(why_request_failed);
}
resp.ContentLength64 = data.LongLength;
await resp.OutputStream.WriteAsync(data, 0, data.Length);
resp.Close(); resp.Close();
} }
} }
@ -204,9 +227,16 @@ namespace BetterSecondBot.HttpServer
} }
if (ok == true) if (ok == true)
{ {
ConsoleLog.Status("Listening for connections on " + url + ""); try
Task listenTask = HandleIncomingConnections(); {
listenTask.GetAwaiter().GetResult(); ConsoleLog.Status("Listening for connections on " + url + "");
Task listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();
}
catch(Exception e)
{
ConsoleLog.Status("Http interface killed itself: "+e.ToString()+"");
}
} }
listener.Close(); listener.Close();
}) })