Ejemplo n.º 1
0
 /// <summary>
 /// see ref=<see cref="Open"/>
 /// HttpServer.HttpRequesting += OnRequest
 /// </summary>
 private void OnRequest(object sender, EventHttpRequestArgs e)
 {
     try
     {
         if (OnRequesting(e.Request, e.Response))
         {
             var item = Routes.GetAgent(e.Request.BaseUrl);
             if (item == null)
             {
                 BadGateway             result = new BadGateway("Cluster server unavailable");
                 EventResponseErrorArgs error  = new EventResponseErrorArgs(
                     e.Request, e.Response, result, BadGateway.CLUSTER_SERVER_UNAVAILABLE
                     );
                 OnResponseError(error);
             }
             else
             {
                 item.Execute(e.Request, e.Response);
             }
         }
     }
     catch (Exception e_)
     {
         if (HttpServer.EnableLog(BeetleX.EventArgs.LogType.Error))
         {
             HttpServer.Log(BeetleX.EventArgs.LogType.Error,
                            $"gateway process {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.BaseUrl} error {e_.Message}@{e_.StackTrace}");
         }
     }
     finally
     {
         e.Cancel = true;
     }
 }
Ejemplo n.º 2
0
 protected override void OnHttpRequest(object sender, EventHttpRequestArgs e)
 {
     if (e.Request.Url.IndexOf("/admin", StringComparison.OrdinalIgnoreCase) >= 0)
     {
         e.Response.Result(new JsonResult($"无权访问{e.Request.Url}"));
         e.Cancel = true;
     }
     else
     {
         base.OnHttpRequest(sender, e);
     }
 }
Ejemplo n.º 3
0
        private void OnRequest(object sender, EventHttpRequestArgs e)
        {
            var url = e.Request.BaseUrl;

            if (url.Length > 2 && url[1] == '_' && url[2] == '_')
            {
                return;
            }
            try
            {
                var ip = e.Request.RemoteIPAddress;
                HttpServer.RequestExecting();
                if (this.Pluginer.Requesting(e.Request, e.Response))
                {
                    var item = Routes.GetAgent(e.Request);
                    if (item == null)
                    {
                        EventResponseErrorArgs error = new EventResponseErrorArgs(
                            e.Request, e.Response, this, "Cluster server unavailable", Gateway.CLUSTER_SERVER_UNAVAILABLE
                            );
                        OnResponseError(error);
                    }
                    else
                    {
                        if (item.UrlRoute.Pluginer.Requesting(e.Request, e.Response))
                        {
                            item.Execute(e.Request, e.Response);
                        }
                    }
                }
            }
            catch (Exception e_)
            {
                if (HttpServer.EnableLog(BeetleX.EventArgs.LogType.Error))
                {
                    HttpServer.Log(BeetleX.EventArgs.LogType.Error,
                                   $"gateway process {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.BaseUrl} error {e_.Message}@{e_.StackTrace}");
                }
            }
            finally
            {
                e.Cancel = true;
            }
        }
Ejemplo n.º 4
0
 public void OnRequesting(object sender, EventHttpRequestArgs e)
 {
     if (e.Request.BaseUrl == "/plaintext")
     {
         e.Response.Result(plaintextResult);
     }
     else if (e.Request.BaseUrl == "/json")
     {
         var json = new SpanJsonResult(new JsonMessage {
             message = "Hello, World!"
         });
         e.Response.Result(json);
     }
     else
     {
         e.Response.Result(new NotFoundResult("url not found!"));
     }
     e.Cancel = true;
 }
Ejemplo n.º 5
0
 private void OnRequest(object sender, EventHttpRequestArgs e)
 {
     try
     {
         e.Cancel = true;
         var ip = e.Request.RemoteIPAddress;
         if (HttpServer.EnableLog(LogType.Info))
         {
             HttpServer.Log(LogType.Info, $"Gateway {e.Request.ID} {e.Request.Method} {e.Request.Url} request from {ip}");
         }
         HttpServer.RequestExecting();
         var result = this.Pluginer.Requesting(e.Request, e.Response);
         if (result.Item1)
         {
             var item = Routes.GetAgent(e.Request);
             if (item == null)
             {
                 if (HttpServer.EnableLog(LogType.Info))
                 {
                     HttpServer.Log(LogType.Info, $"Gateway {e.Request.ID} {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.Url} request cluster server unavailable");
                 }
                 EventResponseErrorArgs error = new EventResponseErrorArgs(
                     e.Request, e.Response, this, "Cluster server unavailable", Gateway.CLUSTER_SERVER_UNAVAILABLE
                     );
                 OnResponseError(error);
             }
             else
             {
                 result = item.UrlRoute.Pluginer.Requesting(e.Request, e.Response);
                 if (result.Item1)
                 {
                     if (HttpServer.EnableLog(LogType.Info))
                     {
                         HttpServer.Log(LogType.Info, $"Gateway {e.Request.ID} {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.Url} request {item.UrlRoute.Url}'s route executing");
                     }
                     if (IOQueue.Count > GatewayQueueSize)
                     {
                         Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(
                             e.Request, e.Response, this, $"The gateway queue overflow!", Gateway.GATEWAY_QUEUE_OVERFLOW);
                         this.OnResponseError(erea);
                     }
                     else
                     {
                         AddRequest(new Tuple <UrlRouteAgent, HttpRequest, HttpResponse>(item, e.Request, e.Response));
                     }
                 }
                 else
                 {
                     if (HttpServer.EnableLog(LogType.Info))
                     {
                         HttpServer.Log(LogType.Info, $"Gateway {e.Request.ID} {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.Url} request {item.UrlRoute.Url}'s route executing cancel!");
                     }
                     e.Cancel = result.Item2 == ResultType.Completed;
                     return;
                 }
             }
         }
         else
         {
             if (HttpServer.EnableLog(LogType.Info))
             {
                 HttpServer.Log(LogType.Info, $"Gateway {e.Request.ID} {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.Url} request gateway executing cancel!");
             }
             e.Cancel = result.Item2 == ResultType.Completed;
             return;
         }
     }
     catch (Exception e_)
     {
         if (HttpServer.EnableLog(BeetleX.EventArgs.LogType.Error))
         {
             HttpServer.Log(BeetleX.EventArgs.LogType.Error,
                            $"Gateway {e.Request.ID} {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.BaseUrl} process error {e_.Message}@{e_.StackTrace}");
         }
     }
 }
Ejemplo n.º 6
0
 private void OnRequest(object sender, EventHttpRequestArgs e)
 {
     try
     {
         e.Cancel = true;
         var ip = e.Request.RemoteIPAddress;
         if (HttpServer.EnableLog(LogType.Info))
         {
             HttpServer.Log(LogType.Info, $"gateway {e.Request.Method} {e.Request.Url} request from {ip}");
         }
         HttpServer.RequestExecting();
         var result = this.Pluginer.Requesting(e.Request, e.Response);
         if (result.Item1)
         {
             var item = Routes.GetAgent(e.Request);
             if (item == null)
             {
                 if (HttpServer.EnableLog(LogType.Info))
                 {
                     HttpServer.Log(LogType.Info, $"gateway {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.Url} request cluster server unavailable");
                 }
                 EventResponseErrorArgs error = new EventResponseErrorArgs(
                     e.Request, e.Response, this, "Cluster server unavailable", Gateway.CLUSTER_SERVER_UNAVAILABLE
                     );
                 OnResponseError(error);
             }
             else
             {
                 result = item.UrlRoute.Pluginer.Requesting(e.Request, e.Response);
                 if (result.Item1)
                 {
                     if (HttpServer.EnableLog(LogType.Info))
                     {
                         HttpServer.Log(LogType.Info, $"gateway {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.Url} request {item.UrlRoute.Url}'s route executing");
                     }
                     item.Execute(e.Request, e.Response);
                 }
                 else
                 {
                     if (HttpServer.EnableLog(LogType.Info))
                     {
                         HttpServer.Log(LogType.Info, $"gateway {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.Url} request {item.UrlRoute.Url}'s route executing cancel!");
                     }
                     e.Cancel = result.Item2 == ResultType.Completed;
                     return;
                 }
             }
         }
         else
         {
             e.Cancel = result.Item2 == ResultType.Completed;
             return;
         }
     }
     catch (Exception e_)
     {
         if (HttpServer.EnableLog(BeetleX.EventArgs.LogType.Error))
         {
             HttpServer.Log(BeetleX.EventArgs.LogType.Error,
                            $"gateway process {e.Request.RemoteIPAddress} {e.Request.Method} {e.Request.BaseUrl} error {e_.Message}@{e_.StackTrace}");
         }
     }
 }