using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace EGFramework{ public class EGHttpServer : IEGFramework, IModule { public HttpListener HttpServer { set; get; } public Encoding StringEncoding { set; get; } = Encoding.UTF8; public Dictionary ResponsePools { set; get; } = new Dictionary(); public Dictionary> ExecuteDelegates { set; get; } = new Dictionary>(); public void Init() { } /// /// if you are in Win7 or newest system, you should add prefix in urlacl by cmd for example: netsh http add urlacl url=http://+:6555/index/ user=Everyone /// /// /// public async void Listen(string prefix,Func responseFunc){ ExecuteDelegates.Add(prefix,responseFunc); if(!HttpListener.IsSupported){ return; } if(HttpServer == null){ HttpServer = new HttpListener(); } HttpServer.Prefixes.Add(prefix); if(!HttpServer.IsListening){ HttpServer.Start(); //GD.Print("Http listened in :" + prefix); while(true){ HttpListenerContext context = await HttpServer.GetContextAsync(); HttpListenerRequest request = context.Request; string responseKey = ""; ResponseMsg receivedMsgs = new ResponseMsg(); try { switch (request.HttpMethod) { case "POST": { Stream stream = context.Request.InputStream; StreamReader reader = new StreamReader(stream, Encoding.UTF8); // byte[] postBuffer = new byte[stream.Length]; // stream.Read(postBuffer,0,postBuffer.Length); string postString = reader.ReadToEnd(); receivedMsgs = new ResponseMsg(postString,new byte[1],"POST", ProtocolType.HttpServer); } break; case "GET": { NameValueCollection data = request.QueryString; Dictionary getDic = data.AllKeys.ToDictionary(k=>k,k=>data[k]); string getData = JsonConvert.SerializeObject(getDic); byte[] getBuffer = StringEncoding.GetBytes(getData); responseKey = "GET:"+this.GetDateTime().ToString(); receivedMsgs = new ResponseMsg(getData,getBuffer,responseKey, ProtocolType.HttpServer); //GD.Print("Received from "+receivedMsgs.sender+": "+receivedMsgs.stringData); } break; case "PUT": { Stream stream = context.Request.InputStream; StreamReader reader = new StreamReader(stream, Encoding.UTF8); byte[] postBuffer = new byte[stream.Length]; stream.Read(postBuffer,0,postBuffer.Length); string postData = reader.ReadToEnd(); receivedMsgs = new ResponseMsg(postData,postBuffer,"PUT", ProtocolType.HttpServer); } break; case "PATCH": { Stream stream = context.Request.InputStream; StreamReader reader = new StreamReader(stream, Encoding.UTF8); byte[] postBuffer = new byte[stream.Length]; stream.Read(postBuffer,0,postBuffer.Length); string postData = reader.ReadToEnd(); receivedMsgs = new ResponseMsg(postData,postBuffer,"PATCH", ProtocolType.HttpServer); } break; case "DELETE": { NameValueCollection data = request.QueryString; Dictionary getDic = data.AllKeys.ToDictionary(k=>k,k=>data[k]); string getData = JsonConvert.SerializeObject(getDic); byte[] getBuffer = StringEncoding.GetBytes(getData); receivedMsgs = new ResponseMsg(getData,getBuffer,"DELETE", ProtocolType.HttpServer); } break; } } catch (System.Exception e) { receivedMsgs.stringData = "Exception:"+e; throw; } HttpListenerResponse response = context.Response; response.AppendHeader("Access-Control-Allow-Origin", "*"); response.AppendHeader("Access-Control-Allow-Credentials", "true"); response.AppendHeader("Server", "MyIIS"); response.StatusCode = 200; byte[] buffer = StringEncoding.GetBytes("API not found!"); if(ExecuteDelegates.ContainsKey(request.Url.ToString()+"/")){ IRequest result = ExecuteDelegates[request.Url.ToString()+"/"].Invoke(receivedMsgs); buffer = StringEncoding.GetBytes(result.ToProtocolData()); } System.IO.Stream output = response.OutputStream; await output.WriteAsync(buffer, 0, buffer.Length); output.Close(); // if(!ResponsePools.ContainsKey("")){ // ResponsePools.Add("",response); // } // Response("","Hello world"); } } EG.Print("Server Overed"); } public IArchitecture GetArchitecture() { return EGArchitectureImplement.Interface; } } public static class CanGetEGHttpServerExtension{ public static EGHttpServer EGHttpServer(this IEGFramework self){ return self.GetModule(); } public static void EGHttpServerListen(this IEGFramework self ,string prefix,Func responseFunc){ self.GetModule().Listen(prefix,responseFunc); } } }