4 Commits
2 changed files with 27 additions and 11 deletions
+8 -5
View File
@@ -1,5 +1,4 @@
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden // Allgemeine Informationen über eine Assembly werden über die folgenden
@@ -10,7 +9,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("BlubbFish")] [assembly: AssemblyCompany("BlubbFish")]
[assembly: AssemblyProduct("Bot-Utils")] [assembly: AssemblyProduct("Bot-Utils")]
[assembly: AssemblyCopyright("Copyright © 2018 - 12.03.2019")] [assembly: AssemblyCopyright("Copyright © 2018 - 21.04.2019")]
[assembly: AssemblyTrademark("© BlubbFish")] [assembly: AssemblyTrademark("© BlubbFish")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
@@ -32,8 +31,8 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, // Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben: // indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.6")] [assembly: AssemblyVersion("1.1.9")]
[assembly: AssemblyFileVersion("1.1.6")] [assembly: AssemblyFileVersion("1.1.9")]
/* /*
* 1.1.0 Remove Helper from Bot-Utils * 1.1.0 Remove Helper from Bot-Utils
@@ -42,5 +41,9 @@ using System.Runtime.InteropServices;
* 1.1.3 Variables parsing now as a String * 1.1.3 Variables parsing now as a String
* 1.1.4 add Woff as Binary type * 1.1.4 add Woff as Binary type
* 1.1.5 add a function to send an object as json directly * 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 (default resources),
* also put returntype boolean, add function that parse post params, if path is a dictionary try to load index.html
* 1.1.7 Restrucutre loading, so that all is init and after the listener is started, REQUEST_URL_HOST gives now host and port
* 1.1.8 Add logger to Webserver Class
* 1.1.9 Modify Output of SendFileResponse
*/ */
+16 -3
View File
@@ -15,11 +15,17 @@ namespace BlubbFish.Utils.IoT.Bots {
protected Dictionary<String, String> config; protected Dictionary<String, String> config;
protected static InIReader requests; protected static InIReader requests;
protected HttpListener httplistener; protected HttpListener httplistener;
protected ABackend databackend;
protected ProgramLogger logger = new ProgramLogger();
public Webserver(ABackend backend, Dictionary<String, String> settings, InIReader requestslookup) { public Webserver(ABackend backend, Dictionary<String, String> settings, InIReader requestslookup) {
this.config = settings; this.config = settings;
requests = requestslookup; requests = requestslookup;
backend.MessageIncomming += this.Backend_MessageIncomming; this.databackend = backend;
}
protected void StartListen() {
this.databackend.MessageIncomming += this.Backend_MessageIncomming;
this.httplistener = new HttpListener(); this.httplistener = new HttpListener();
this.httplistener.Prefixes.Add(this.config["prefix"]); this.httplistener.Prefixes.Add(this.config["prefix"]);
this.httplistener.Start(); this.httplistener.Start();
@@ -40,7 +46,7 @@ namespace BlubbFish.Utils.IoT.Bots {
}); });
} }
public static Boolean SendFileResponse(HttpListenerContext cont, String folder = "resources") { public static Boolean SendFileResponse(HttpListenerContext cont, String folder = "resources", Boolean printOutput = true) {
String restr = cont.Request.Url.PathAndQuery; String restr = cont.Request.Url.PathAndQuery;
if(restr.StartsWith("/")) { if(restr.StartsWith("/")) {
if(restr.IndexOf("?") != -1) { if(restr.IndexOf("?") != -1) {
@@ -65,6 +71,9 @@ namespace BlubbFish.Utils.IoT.Bots {
break; break;
} }
cont.Response.OutputStream.Write(output, 0, output.Length); cont.Response.OutputStream.Write(output, 0, output.Length);
if(printOutput) {
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
}
return true; return true;
} else { } else {
String file = File.ReadAllText(folder + "/" + restr); String file = File.ReadAllText(folder + "/" + restr);
@@ -74,7 +83,7 @@ namespace BlubbFish.Utils.IoT.Bots {
file = file.Replace("\"{%" + item.Key.ToUpper() + "%}\"", item.Value); file = file.Replace("\"{%" + item.Key.ToUpper() + "%}\"", item.Value);
} }
} }
file = file.Replace("{%REQUEST_URL_HOST%}", cont.Request.Url.Host); file = file.Replace("{%REQUEST_URL_HOST%}", cont.Request.Url.Host+":"+cont.Request.Url.Port);
Byte[] buf = Encoding.UTF8.GetBytes(file); Byte[] buf = Encoding.UTF8.GetBytes(file);
cont.Response.ContentLength64 = buf.Length; cont.Response.ContentLength64 = buf.Length;
switch(end) { switch(end) {
@@ -83,7 +92,9 @@ namespace BlubbFish.Utils.IoT.Bots {
break; break;
} }
cont.Response.OutputStream.Write(buf, 0, buf.Length); cont.Response.OutputStream.Write(buf, 0, buf.Length);
if(printOutput) {
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery); Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
}
return true; return true;
} }
} catch(Exception e) { } catch(Exception e) {
@@ -93,7 +104,9 @@ namespace BlubbFish.Utils.IoT.Bots {
} }
} }
} }
if(printOutput) {
Helper.WriteError("404 - " + cont.Request.Url.PathAndQuery + " not found!"); Helper.WriteError("404 - " + cont.Request.Url.PathAndQuery + " not found!");
}
cont.Response.StatusCode = 404; cont.Response.StatusCode = 404;
return false; return false;
} }