public void Load() { loader.Load(); try { if (isLoad) { ControllerWorker controller = (ControllerWorker)loader.plugin.GetPluginRefObject(); Actions = controller._GetActions(); ActionsWS = controller._GetActionsWS(); } } catch { loader.Unload(); } }
void ResentContent(ref HelperClass helper, ref ControllerWorker controller, ref string bufS) { EchoClass ec = controller._GetNextContent(); while (ec.type != EchoClass.EchoType.End) { switch (ec.type) { case EchoClass.EchoType.String: bufS += (string)ec.param; break; case EchoClass.EchoType.Layout: ResentLayout(ref helper, ref controller, ref bufS, (string)ec.param, false); break; } //while (bufS.Length > 16000) { // byte[] buf = Encoding.UTF8.GetBytes(bufS.Substring(0, 16000)); // bufS = bufS.Remove(0, 16000); // helper.Context.Response.OutputStream.Write(buf, 0, buf.Length); //} ec = controller._GetNextContent(); } }
void ResentLayout(ref HelperClass helper, ref ControllerWorker controller, ref string bufS, string layoutName, bool content) { LayoutWorker layout = GetLayout(layoutName); GetStaticPlugins(layout, ref helper); layout._Work(helper); helper.GetData(layout._GetHelper()); EchoClass ec = layout._GetNextContent(); while (ec.type != EchoClass.EchoType.End) { switch (ec.type) { case EchoClass.EchoType.String: bufS += (string)ec.param; break; case EchoClass.EchoType.Layout: ResentLayout(ref helper, ref controller, ref bufS, (string)ec.param, content); break; case EchoClass.EchoType.Content: if (content) { ResentContent(ref helper, ref controller, ref bufS); } break; } //while (bufS.Length > 16000) { // byte[] buf = Encoding.UTF8.GetBytes(bufS.Substring(0, 16000)); // bufS = bufS.Remove(0, 16000); // helper.Context.Response.OutputStream.Write(buf, 0, buf.Length); //} ec = layout._GetNextContent(); } }
public void WorkWS(HelperClass _helper) { _helper.ContextWs.WebSocket.OnMessage += (sender, args) => { HelperClass helper = (HelperClass)WSclients[sender]; var json = Newtonsoft.Json.Linq.JObject.Parse(args.Data); if (!json.ContainsKey("path") || !json.ContainsKey("type") || !json.ContainsKey("body")) { helper.ContextWs.WebSocket.Close(WebSocketSharp.CloseStatusCode.InvalidData, "Invalid message content"); return; } string path = (string)json["path"]; Queue <string> pathSplit = new Queue <string>(path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)); if (pathSplit.Count == 0) { return; } ControllerTreeElement tree = controllerTree.SearchWS(ref pathSplit, out string action); if (tree != null) { ControllerWorker controller = (ControllerWorker)tree.plugin.GetPluginRefObject(); try { GetStaticPlugins(controller, ref helper); controller._WorkWS(helper, action, new object[] { (string)json["type"], (string)json["body"] }); helper.GetData(controller._GetHelper()); if (helper.returnType == ReturnType.DefaultContent) { string bufS = ""; ResentContent(ref helper, ref controller, ref bufS); helper.ContextWs.WebSocket.Send(bufS); bufS = null; } } catch (Exception e) { if (helper.ContextWs.WebSocket.IsAlive) { Log.Error("Ошибка обработки сообщения WebSocket", e); } } } }; _helper.ContextWs.WebSocket.OnClose += (sender, args) => { HelperClass helper = (HelperClass)WSclients[sender]; WSclients.Remove(sender); }; if (WebSocketInit) { PluginWorker httpInitPlugin = GetPlugin(WebSocketinitName); GetStaticPlugins(httpInitPlugin, ref _helper); httpInitPlugin._Work(_helper); _helper.GetData(httpInitPlugin._GetHelper()); if (_helper.Auth.Status) { WSclients.Add(_helper.ContextWs.WebSocket, _helper); _helper.ContextWs.WebSocket.Accept(); return; } } _helper.ContextWs.WebSocket.Close(WebSocketSharp.CloseStatusCode.Normal, "Unathorized"); }
public void Work(HelperClass _helper) { string path = $"{_helper.Context.Request.Url.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped)}"; Queue <string> pathSplit = new Queue <string>(path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)); if (pathSplit.Count == 0) { pathSplit.Enqueue("index"); } ControllerTreeElement tree = controllerTree.Search(ref pathSplit, out string action); if (tree != null) { ControllerWorker controller = (ControllerWorker)tree.plugin.GetPluginRefObject(); try { if (HTTPinit) { PluginWorker httpInitPlugin = GetPlugin(HTTPinitName); GetStaticPlugins(httpInitPlugin, ref _helper); httpInitPlugin._Work(_helper); _helper.GetData(httpInitPlugin._GetHelper()); } GetStaticPlugins(controller, ref _helper); controller._Work(_helper, action); _helper.GetData(controller._GetHelper()); _helper.Context.Response.Headers.Add(_helper.Responce.Headers); _helper.Context.Response.StatusCode = _helper.Responce.StatusCode; _helper.Context.Response.StatusDescription = _helper.Responce.StatusDescription; } catch (Exception e) { Log.Error("Внутренняя ошибка подключения", e); _helper.Answer500(e); } if (_helper.returnType == ReturnType.DefaultContent) { _helper.Context.Response.ContentType = _helper.Responce.ContentType; string bufS = ""; if (_helper.Render.isEnabled) { ResentLayout(ref _helper, ref controller, ref bufS, _helper.Render.layout, true); } else { ResentContent(ref _helper, ref controller, ref bufS); } //byte[] buf; // не работает в данной реализации HttpListenerContext //while (bufS.Length > 0) { // buf = Encoding.UTF8.GetBytes(bufS.Substring(0, bufS.Length > 16000 ? 16000 : bufS.Length)); // bufS = bufS.Remove(0, bufS.Length > 16000 ? 16000 : bufS.Length); // helper.Context.Response.OutputStream.Write(buf, 0, buf.Length); //} byte[] buf = Encoding.UTF8.GetBytes(bufS); _helper.Context.Response.ContentLength64 = buf.Length; _helper.Context.Response.OutputStream.Write(buf, 0, buf.Length); } else if (_helper.returnType == ReturnType.Info) { if (_helper.Responce.StatusCode / 100 == 3) { _helper.Context.Response.Redirect(_helper.Responce.RedirectLocation); } } if (_helper.WShelper.Acts.Count > 0) { if (_helper.WShelper.ActsForAll) { foreach (HelperClass WSclient in WSclients.Values) { foreach (WebSocketAct act in _helper.WShelper.Acts) { WSclient.ContextWs.WebSocket.Send(act.Body); } } } else { foreach (HelperClass WSclient in WSclients.Values) { foreach (WebSocketAct act in _helper.WShelper.Acts) { if (act.Recepients.Contains(WSclient.Auth.Login)) { WSclient.ContextWs.WebSocket.Send(act.Body); } } } } } } else { bool resFile = false; try { FileInfo fi = new FileInfo($"{baseDirectory.FullName}public{Path.DirectorySeparatorChar}{path.Replace('/', Path.DirectorySeparatorChar)}"); if (fi.Exists) { using (FileStream reader = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read)) { resFile = true; int pos = fi.Name.LastIndexOf('.') + 1; string format = fi.Name.Substring(pos, fi.Name.Length - pos); if (format == "css") { _helper.Context.Response.ContentType = "text/css"; } else if (format == "js") { _helper.Context.Response.ContentType = "text/javascript"; } _helper.Context.Response.ContentLength64 = reader.Length; _helper.Context.Response.Headers.Add(HttpResponseHeader.CacheControl, "max-age=86400"); reader.CopyTo(_helper.Context.Response.OutputStream, 16384); } } else { resFile = false; } } catch { resFile = false; } if (!resFile) { _helper.Context.Response.StatusCode = 404; _helper.Context.Response.StatusDescription = "Not found"; } } }