From b3d60ad1ae21611e579808ce2d6bbe5c0be82fc5 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Tue, 2 Apr 2019 23:31:28 +0200 Subject: [PATCH] SendFileResponse as now the default parameter folder = "resources" add a function GetPostParams --- Bot-Utils/Bot-Utils.csproj | 1 + Bot-Utils/Webserver.cs | 36 +++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Bot-Utils/Bot-Utils.csproj b/Bot-Utils/Bot-Utils.csproj index a4bd1c4..d00ac9c 100644 --- a/Bot-Utils/Bot-Utils.csproj +++ b/Bot-Utils/Bot-Utils.csproj @@ -37,6 +37,7 @@ + diff --git a/Bot-Utils/Webserver.cs b/Bot-Utils/Webserver.cs index 954f5ef..8208b32 100644 --- a/Bot-Utils/Webserver.cs +++ b/Bot-Utils/Webserver.cs @@ -1,17 +1,15 @@ -using BlubbFish.Utils.IoT.Connector; -using BlubbFish.Utils.IoT.Events; -using LitJson; -using System; +using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net; using System.Text; using System.Threading; -using System.Threading.Tasks; +using System.Web; +using BlubbFish.Utils.IoT.Connector; +using BlubbFish.Utils.IoT.Events; +using LitJson; -namespace BlubbFish.Utils.IoT.Bots -{ +namespace BlubbFish.Utils.IoT.Bots { public abstract class Webserver { protected Dictionary config; @@ -42,7 +40,7 @@ namespace BlubbFish.Utils.IoT.Bots }); } - public static Boolean SendFileResponse(HttpListenerContext cont, String folder) { + public static Boolean SendFileResponse(HttpListenerContext cont, String folder = "resources") { String restr = cont.Request.Url.PathAndQuery; if(restr.StartsWith("/")) { if(restr.IndexOf("?") != -1) { @@ -111,6 +109,26 @@ namespace BlubbFish.Utils.IoT.Bots return false; } + public static Dictionary GetPostParams(HttpListenerRequest req) { + if(req.HttpMethod == "POST") { + if(req.HasEntityBody) { + StreamReader reader = new StreamReader(req.InputStream, req.ContentEncoding); + String rawData = reader.ReadToEnd(); + req.InputStream.Close(); + reader.Close(); + Dictionary ret = new Dictionary(); + foreach(String param in rawData.Split('&')) { + String[] kvPair = param.Split('='); + if(!ret.ContainsKey(kvPair[0])) { + ret.Add(kvPair[0], HttpUtility.UrlDecode(kvPair[1])); + } + } + return ret; + } + } + return new Dictionary(); + } + public void Dispose() { this.httplistener.Stop(); this.httplistener.Close();