90 lines
3.3 KiB
C#
90 lines
3.3 KiB
C#
|
|
// Decompiled with JetBrains decompiler
|
|||
|
|
// Type: Sensormat.HttpServer
|
|||
|
|
// Assembly: Sensormat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
|
|||
|
|
// MVID: 49795081-3467-4846-904D-9A360FB2FA3C
|
|||
|
|
// Assembly location: \\192.168.178.26\Freigabe\Sensormat.dll
|
|||
|
|
|
|||
|
|
using System;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
#nullable disable
|
|||
|
|
namespace Sensormat
|
|||
|
|
{
|
|||
|
|
public class HttpServer
|
|||
|
|
{
|
|||
|
|
public HttpListener listener;
|
|||
|
|
private string URL;
|
|||
|
|
|
|||
|
|
public HttpServer.HttpServerIncomingRequestCallBack httpServerIncomingRequest { get; set; }
|
|||
|
|
|
|||
|
|
public async Task HandleIncomingConnections()
|
|||
|
|
{
|
|||
|
|
bool runServer = true;
|
|||
|
|
while (runServer)
|
|||
|
|
{
|
|||
|
|
HttpListenerContext contextAsync = await this.listener.GetContextAsync();
|
|||
|
|
HttpListenerRequest request = contextAsync.Request;
|
|||
|
|
HttpListenerResponse resp = contextAsync.Response;
|
|||
|
|
Console.WriteLine(request.Url.ToString());
|
|||
|
|
Console.WriteLine(request.HttpMethod);
|
|||
|
|
Console.WriteLine(request.UserHostName);
|
|||
|
|
Console.WriteLine(request.UserAgent);
|
|||
|
|
Console.WriteLine();
|
|||
|
|
HttpServerIncomingRequestCallBackParameter e = new HttpServerIncomingRequestCallBackParameter()
|
|||
|
|
{
|
|||
|
|
StatusCode = 200,
|
|||
|
|
StatusDescription = "",
|
|||
|
|
Result = "",
|
|||
|
|
URL = this.URL,
|
|||
|
|
Request = request,
|
|||
|
|
AbsolutePath = request.Url.AbsolutePath.Trim('/')
|
|||
|
|
};
|
|||
|
|
if (request.QueryString != null)
|
|||
|
|
{
|
|||
|
|
foreach (string allKey in request.QueryString.AllKeys)
|
|||
|
|
e.Query.Add(allKey, request.QueryString[allKey]);
|
|||
|
|
}
|
|||
|
|
if (this.httpServerIncomingRequest != null)
|
|||
|
|
this.httpServerIncomingRequest(e);
|
|||
|
|
byte[] bytes = Encoding.UTF8.GetBytes(e.Result);
|
|||
|
|
resp.ContentType = "text/html";
|
|||
|
|
resp.ContentEncoding = Encoding.UTF8;
|
|||
|
|
resp.ContentLength64 = (long)bytes.Length;
|
|||
|
|
resp.StatusCode = e.StatusCode;
|
|||
|
|
resp.StatusDescription = e.StatusDescription;
|
|||
|
|
await resp.OutputStream.WriteAsync(bytes, 0, bytes.Length);
|
|||
|
|
resp.Close();
|
|||
|
|
resp = (HttpListenerResponse)null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string getValue(string ID) => "OK " + ID;
|
|||
|
|
|
|||
|
|
private string setValue(string ID, string Parameter) => "setValue " + ID + " " + Parameter;
|
|||
|
|
|
|||
|
|
public HttpServer(string url)
|
|||
|
|
{
|
|||
|
|
this.listener = new HttpListener();
|
|||
|
|
this.listener.Prefixes.Add(url);
|
|||
|
|
this.listener.Start();
|
|||
|
|
Console.WriteLine("Listening for connections on {0}", (object)url);
|
|||
|
|
this.URL = url;
|
|||
|
|
this.HandleIncomingConnections();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool Test() => !string.IsNullOrEmpty(new WebClient().DownloadString(this.URL));
|
|||
|
|
|
|||
|
|
public void Release()
|
|||
|
|
{
|
|||
|
|
if (this.listener == null)
|
|||
|
|
return;
|
|||
|
|
this.listener.Close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public delegate void HttpServerIncomingRequestCallBack(
|
|||
|
|
HttpServerIncomingRequestCallBackParameter e);
|
|||
|
|
}
|
|||
|
|
}
|