예제 #1
0
        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";
                }
            }
        }