public void OnWebHook(IAsyncResult ar) { HttpListenerContext CTX = null; try { CTX = listener.EndGetContext(ar); } catch (Exception e) { BotSession.Instance.Logger.info(log: "ERROR: Getting the end context for the listener failed"); return; } listener.BeginGetContext(OnWebHook, null); Stream body = CTX.Request.InputStream; StreamReader SR = new StreamReader(body, CTX.Request.ContentEncoding); string Response = SR.ReadToEnd(); if (!Directory.Exists("BotData/request_log")) { Directory.CreateDirectory("BotData/request_log"); } string RequestPath = CTX.Request.RawUrl; if (RequestPath.EndsWith("/")) { RequestPath = RequestPath.Substring(0, RequestPath.Length - 1); } string CustomReplyStr = ""; WebhookRegistry.HTTPResponseData reply = WebhookRegistry.Instance.RunCommand(RequestPath, Response, CTX.Request.Headers, CTX.Request.HttpMethod); CustomReplyStr = reply.ReplyString; byte[] buffer = Encoding.UTF8.GetBytes("\n" + CustomReplyStr); CTX.Response.ContentLength64 = buffer.Length; CTX.Response.AddHeader("Server", "1.8"); CTX.Response.StatusCode = reply.Status; if (reply.ReturnContentType != "" && reply.ReturnContentType != null) { CTX.Response.ContentType = reply.ReturnContentType; } Stream output = CTX.Response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); }
public WebhookRegistry.HTTPResponseData a_new_version_is_available(List <string> args, string body, string method, NameValueCollection headers) { if (MainConfiguration.Instance.SecretNewVerCode == body) { WebhookRegistry.HTTPResponseData hrd = new WebhookRegistry.HTTPResponseData(); hrd.ReplyString = "OK"; hrd.ReturnContentType = "text/plain"; hrd.Status = 200; BaseCommands.MH(Destinations.DEST_LOCAL, UUID.Zero, "Alert: A new version is available. Restart required"); BotSession.Instance.EnqueueExit = true; return(hrd); } else { WebhookRegistry.HTTPResponseData hrd = new WebhookRegistry.HTTPResponseData(); hrd.ReplyString = "Not authorized"; hrd.ReturnContentType = "text/plain"; hrd.Status = 500; return(hrd); } }