2 Commits
3 changed files with 31 additions and 12 deletions
+1
View File
@@ -37,6 +37,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
+1 -1
View File
@@ -42,5 +42,5 @@ using System.Runtime.InteropServices;
* 1.1.3 Variables parsing now as a String
* 1.1.4 add Woff as Binary type
* 1.1.5 add a function to send an object as json directly
* 1.1.6 rename functions and make SendFileResponse with a parameter for the folder
* 1.1.6 rename functions and make SendFileResponse with a parameter for the folder,
*/
+29 -11
View File
@@ -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<String, String> 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) {
@@ -50,8 +48,8 @@ namespace BlubbFish.Utils.IoT.Bots
} else {
restr = restr.Substring(1);
}
if(restr == "") {
restr = "index.html";
if(Directory.Exists(folder + "/" + restr)) {
restr += "/index.html";
}
String end = restr.IndexOf('.') != -1 ? restr.Substring(restr.IndexOf('.') + 1) : "";
if(File.Exists(folder + "/" + restr)) {
@@ -111,6 +109,26 @@ namespace BlubbFish.Utils.IoT.Bots
return false;
}
public static Dictionary<String, String> 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<String, String> ret = new Dictionary<String, String>();
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<String, String>();
}
public void Dispose() {
this.httplistener.Stop();
this.httplistener.Close();