Esempio n. 1
0
        /// <summary>
        /// Start this RequestWorker. Meant to run in a separate thread.
        /// </summary>
        public void Run()
        {
            Log.Info(Thread.CurrentThread.Name + " now running.");

            do
            {
                ContextProvider Context = Queue.Take();
                Stopwatch       S       = new Stopwatch();
                S.Start();
                RequestProvider  Request  = Context.Request;
                ResponseProvider Response = Context.Response;

                Log.Info("Processing " + Request.HttpMethod + " request for " + Request.Url.LocalPath);

                #region Redirect
                //Resolve redirects, if any
                string URL = Redirect.Resolve(Request.Url.LocalPath.ToLower());
                //If null is returned, an infinite loop was detected.
                if (URL == null)
                {
                    Log.Error("Couldn't resolve URL; infinite redirection loop. URL: " + Request.Url.LocalPath.ToLower());
                    Response.Send(Utils.GetErrorPage(HttpStatusCode.LoopDetected, "An infinite loop was detected while trying to access the specified URL."), HttpStatusCode.LoopDetected);
                    continue;
                }
                //Remove trailing /
                if (URL.EndsWith('/') && URL.Length > 1)
                {
                    URL = URL.Remove(URL.Length - 1);
                }

                //Redirect if necessary
                if (URL != Request.Url.LocalPath.ToLower())
                {
                    Log.Info("Request redirected to " + URL);
                    Response.Redirect(URL);
                    Response.Send(HttpStatusCode.PermanentRedirect);
                    continue;
                }
                #endregion

                //Find this request's target
                Type T = FindEndpoint(Request);
                if (T != null)
                {
                    ProcessEndpoint(T, Context);
                }
                else
                {
                    //No endpoint was found, so see if a resource exists at this address instead.
                    ProcessResource(Request.Url.LocalPath.ToLower(), Context);
                }
                long TimeSpent = S.ElapsedMilliseconds;
                Log.Info("Operation complete. Took " + TimeSpent + "ms");
                if (TimeSpent >= 250)
                {
                    Log.Warning("An operation took too long to complete. Took " + TimeSpent + " ms, should be less than 250ms");
                }
                RequestTimeWatcher?.Update(TimeSpent);
                Listener.QueueSizeWatcher?.Update(RequestWorker.Queue.Count);
            } while (!Debug || Queue.Count != 0);
            //Connection.Close();
        }