You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
6.4 KiB
140 lines
6.4 KiB
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<string,HttpListenerResponse> ResponsePools { set; get; } = new Dictionary<string, HttpListenerResponse>(); |
|
public void Init() |
|
{ |
|
|
|
} |
|
|
|
/// <summary> |
|
/// 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 |
|
/// </summary> |
|
/// <param name="prefix"></param> |
|
/// <returns></returns> |
|
public async void Listen(string prefix){ |
|
if(!HttpListener.IsSupported){ |
|
return; |
|
} |
|
if(HttpServer == null){ |
|
HttpServer = new HttpListener(); |
|
} |
|
HttpServer.Prefixes.Add(prefix); |
|
if(!HttpServer.IsListening){ |
|
HttpServer.Start(); |
|
HttpListenerContext context = await HttpServer.GetContextAsync(); |
|
|
|
HttpListenerRequest request = context.Request; |
|
string responseKey = ""; |
|
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(); |
|
ResponseMsg receivedMsgs = new ResponseMsg(postString,postBuffer,"GET:"+context.User.ToString(), ProtocolType.HttpServer); |
|
this.EGOnReceivedData(receivedMsgs); |
|
} |
|
break; |
|
case "GET": |
|
{ |
|
NameValueCollection data = request.QueryString; |
|
Dictionary<string,string> getDic = data.AllKeys.ToDictionary(k=>k,k=>data[k]); |
|
string getData = JsonConvert.SerializeObject(getDic); |
|
byte[] getBuffer = StringEncoding.GetBytes(getData); |
|
responseKey = "GET:"+this.GetTimeStamp().ToString(); |
|
ResponseMsg receivedMsgs = new ResponseMsg(getData,getBuffer,responseKey, ProtocolType.HttpServer); |
|
this.EGOnReceivedData(receivedMsgs); |
|
} |
|
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(); |
|
ResponseMsg receivedMsgs = new ResponseMsg(postData,postBuffer,"PUT:"+context.User.ToString(), ProtocolType.HttpServer); |
|
this.EGOnReceivedData(receivedMsgs); |
|
} |
|
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(); |
|
ResponseMsg receivedMsgs = new ResponseMsg(postData,postBuffer,"PATCH:"+context.User.ToString(), ProtocolType.HttpServer); |
|
this.EGOnReceivedData(receivedMsgs); |
|
} |
|
break; |
|
case "DELETE": |
|
{ |
|
NameValueCollection data = request.QueryString; |
|
Dictionary<string,string> getDic = data.AllKeys.ToDictionary(k=>k,k=>data[k]); |
|
string getData = JsonConvert.SerializeObject(getDic); |
|
byte[] getBuffer = StringEncoding.GetBytes(getData); |
|
ResponseMsg receivedMsgs = new ResponseMsg(getData,getBuffer,"DELETE:"+context.User.ToString(), ProtocolType.HttpServer); |
|
this.EGOnReceivedData(receivedMsgs); |
|
} |
|
break; |
|
} |
|
HttpListenerResponse response = context.Response; |
|
response.AppendHeader("Access-Control-Allow-Origin", "*"); |
|
response.AppendHeader("Access-Control-Allow-Credentials", "true"); |
|
response.AppendHeader("Server", "MyIIS"); |
|
response.StatusCode = 200; |
|
|
|
ResponsePools.Add("",response); |
|
|
|
} |
|
} |
|
|
|
public async void Response(string responseKey,string responseString = "",byte[] responseByte = null){ |
|
#region 返回应答 |
|
// Construct a response. |
|
byte[] buffer = StringEncoding.GetBytes(responseString); |
|
// Get a response stream and write the response to it. |
|
ResponsePools[responseKey].ContentLength64 = buffer.Length; |
|
System.IO.Stream output = ResponsePools[responseKey].OutputStream; |
|
await output.WriteAsync(buffer, 0, buffer.Length); |
|
// You must close the output stream. |
|
output.Close(); |
|
#endregion |
|
} |
|
|
|
public IArchitecture GetArchitecture() |
|
{ |
|
return EGArchitectureImplement.Interface; |
|
} |
|
|
|
} |
|
|
|
public static class CanGetEGHttpServerExtension{ |
|
public static EGHttpServer EGHttpServer(this IEGFramework self){ |
|
return self.GetModule<EGHttpServer>(); |
|
} |
|
|
|
public static void EGHttpServerListen(this IEGFramework self ,string prefix){ |
|
self.GetModule<EGHttpServer>().Listen(prefix); |
|
} |
|
} |
|
|
|
}
|
|
|