public ServerPreRequestExceptionEventArgs(ServerPreRequestHandlerEventArgs args, Exception exception)
 {
     this.PreRequestArgs = args;
     this.PreRequestArgs.ReturnStatus = HttpStatusCode.InternalServerError;
     this.Exception = exception;
     PrefillContent(this.Exception);
 }
示例#2
0
 protected virtual void OnPreResponseHandler(ServerPreRequestHandlerEventArgs e)
 {
     PreResponseHandler?.Invoke(this, e);
 }
示例#3
0
        private void TaskAction(object o)
        {
            HttpServer server = o as HttpServer;

            if (server == null)
            {
                throw new Exception("cannot resolve ServerObject!");
            }

            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, errors) => true;
            using (server.CurrentListener = new HttpListener())
            {
                foreach (string url in server.ListenerUrls)
                {
                    try
                    {
                        server.CurrentListener.Prefixes.Add(url);
                    }
                    catch (Exception e)
                    {
                        OnServerError(new ServerStartErrorEventArgs(e));
                    }
                }

                if (server.CurrentListener.Prefixes.Count <= 0)
                {
                    OnServerLog(new ServerStatusEventArgs("no prefixes added", 0));
                    server.CurrentListener.Close();
                    return;
                }

                try
                {
                    OnServerLog(new ServerStatusEventArgs("start server", 0));
                    server.CurrentListener.Start();
                    foreach (string prefixUrl in server.CurrentListener.Prefixes)
                    {
                        OnServerLog(new ServerStatusEventArgs("listen to:" + prefixUrl, 0));
                    }
                }
                catch (Exception e)
                {
                    server.CurrentListener.Close();
                    OnServerError(new ServerStartErrorEventArgs(e));
                    return;
                }

                try
                {
                    while (LoopEnable)
                    {
                        HttpListenerContext context = server.CurrentListener.GetContext();
                        string absPath = context.Request.Url.LocalPath;
                        bool   found   = CheckUrlAndGetPath(absPath, server.RootPath, server.IndexFiles, out var path);


                        using (HttpListenerResponse response = context.Response)
                        {
                            OnServerLog(new ServerStatusEventArgs("request:" + context.Request.Url, 0));
                            //CORS
                            response.Headers.Add("Access-Control-Allow-Origin:*");

                            ServerPreRequestHandlerEventArgs args = new ServerPreRequestHandlerEventArgs(context.Request);
                            try
                            {
                                try
                                {
                                    OnPreResponseHandler(args);
                                }
                                catch (Exception e)
                                {
                                    OnPreRequestException(new ServerPreRequestExceptionEventArgs(args, e));
                                }
                            }
                            catch (Exception e)
                            {
                                OnServerError(new ServerStartErrorEventArgs(e));
                                response.Close();
                                continue;
                            }
                            if (args.Handled)
                            {
                                if (args.AdditionalHeaders.Count > 0)
                                {
                                    response.Headers.Add(args.AdditionalHeaders);
                                }

                                response.ContentType = args.MimeType;
                                response.StatusCode  = (int)args.ReturnStatus;
                                byte[] bytes = Encoding.UTF8.GetBytes(args.ContentToSend);
                                response.OutputStream.Write(bytes, 0, bytes.Length);
                                response.Close();
                                continue;
                            }

                            if (HandleFileContent(found, server, absPath, response, path))
                            {
                                response.Close();
                                continue;
                            }


                            response.Close();
                        }
                    }

                    server.LoopEnable = false;

                    server.CurrentListener = null;
                }
                catch (Exception e)
                {
                    server.CurrentListener?.Close();
                    server.CurrentListener = null;
                    server.LoopEnable      = false;
                    OnServerError(new ServerStartErrorEventArgs(e));
                }

                this.CurrentListener?.Close();
            }
        }