Exemplo n.º 1
0
        void ProcessConnection(TcpClient client)
        {
            HttpRequest request = HttpRequest.Parse(client.Client, client.GetStream());



            if (request != null)
            {
                Logger.Log(request.Method + " " + request.Url, request);

                Controller ctrlr = Controllers["404"];

                string rUrl = request.Url.LocalPath.Remove(0, 1);

                if (Controllers.ContainsKey(rUrl))
                {
                    ctrlr = Controllers[rUrl];
                }
                else if (Controllers.ContainsKey(rUrl.Remove(rUrl.Length - 1, 1)))
                {
                    ctrlr = Controllers[rUrl.Remove(rUrl.Length - 1, 1)];
                }
                else
                {
                    List <Controller> matchedCtrl = new List <Controller>();

                    foreach (var ctrl in Controllers)
                    {
                        if (!ctrl.Value.ExactMatch && rUrl.StartsWith(ctrl.Key))
                        {
                            matchedCtrl.Add(ctrl.Value);
                        }
                    }

                    if (matchedCtrl.Count > 1)
                    {
                        List <int> dff = new List <int>();
                        Dictionary <int, Controller> rt = new Dictionary <int, Controller>();

                        foreach (var ctrl in matchedCtrl)
                        {
                            dff.Add(Compute(rUrl, ctrl.Route));
                            rt.Add(dff[dff.Count - 1], ctrl);
                        }

                        dff.Sort();

                        ctrlr = rt[dff[0]];
                    }
                    else if (matchedCtrl.Count == 1)
                    {
                        ctrlr = matchedCtrl[0];
                    }
                }

                Console.WriteLine("Route: " + ctrlr.Route);

                //OnConnectionReceived(client, request);

                var response = request.CreateRepsonse();

                int code = 500;

                try
                {
                    code = ctrlr.OnConnection(request, response);
                }
                catch (UnauthorizedAccessException ex)
                {
                    response.SetCode(403);
                }
                catch (Exception ex)
                {
                    Logger.LogWarning(ex.ToString());
                }



                if (response.RespCode == "UNKNOWN")
                {
                    response.SetCode(code);
                }

                response.Send();
            }
        }
Exemplo n.º 2
0
        void ProcessConnection(TcpClient client)
        {
            HttpRequest request = HttpRequest.Parse(client.Client, client.GetStream());

            if (request != null)
            {
                //Logger.Log(request.Method + " " + request.Url,request);

                Controller ctrlr = null;

                string rUrl = request.Url.LocalPath;

                if (Controllers.ContainsKey(rUrl))
                {
                    ctrlr = Controllers[rUrl];
                }
                else if (rUrl.EndsWith("/") && Controllers.ContainsKey(rUrl.Remove(rUrl.Length - 1, 1)))
                {
                    ctrlr = Controllers[rUrl.Remove(rUrl.Length - 1, 1)];
                }
                else
                {
                    List <Controller> matchedCtrl = new List <Controller>();

                    foreach (var ctrl in Controllers)
                    {
                        if (!ctrl.Value.ExactMatch && rUrl.StartsWith(ctrl.Key))
                        {
                            matchedCtrl.Add(ctrl.Value);
                        }
                    }

                    if (matchedCtrl.Count > 1)
                    {
                        List <int> dff = new List <int>();
                        Dictionary <int, Controller> rt = new Dictionary <int, Controller>();

                        foreach (var ctrl in matchedCtrl)
                        {
                            dff.Add(Compute(rUrl, ctrl.Route));
                            rt.Add(dff[dff.Count - 1], ctrl);
                        }

                        dff.Sort();

                        ctrlr = rt[dff[0]];
                    }
                    else if (matchedCtrl.Count == 1)
                    {
                        ctrlr = matchedCtrl[0];
                    }
                }

                var response = request.CreateRepsonse();
                response.AllowGzipCompression = _allowGzipCompression;

                if (ctrlr == null)
                {
                    response.SetBody(Http.GetFullCode(404), 404);
                }
                else
                {
                    try
                    {
                        ctrlr.OnConnection(request, response);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        response.SetBody("Unauthorized Access Error: " + ex.ToString(), 403);
                    }
                    catch (Exception ex)
                    {
                        //Logger.LogWarning();
                        response.SetBody("Internal Server Error: " + ex.ToString(), 500);
                    }

                    if (response.RespCode == "UNKNOWN")
                    {
                        response.SetBody("Internal error: Invalid or missing response code: " + ctrlr.GetType().Name);
                        response.SetCode(500);
                    }
                }

                response.Send();
            }
        }