private static void FinalizeRejectedResponse(HttpListenerRequest request, HttpListenerResponse response, int statusCode, string statusDescription, Uri callbackUrl)
        {
            string remoteAddress = request.RemoteEndPoint.Address.ToString();

            try
            {
                response.StatusCode        = statusCode;
                response.StatusDescription = statusDescription;
            }
            finally
            {
                response.Close();
            }

            if (callbackUrl == null)
            {
                Supervisor.LogEvent(string.Format("Cancellation rejected from {0} due to: {1}.", remoteAddress, statusDescription), TraceEventType.Verbose);
            }
            else
            {
                Supervisor.LogEvent(string.Format("Cancellation rejected for {0} from {1} due to: {2}.", callbackUrl.OriginalString, remoteAddress, statusDescription), TraceEventType.Verbose);
            }
        }
Beispiel #2
0
 protected override void OnPause()
 {
     Supervisor.Pause();
     base.OnPause();
 }
Beispiel #3
0
 protected override void OnContinue()
 {
     Supervisor.Resume();
     base.OnContinue();
 }
        public void Process(HttpListenerRequest request, HttpListenerResponse response)
        {
            try
            {
                if (request.HttpMethod != "PUT" && request.HttpMethod != "DELETE")
                {
                    FinalizeRejectedResponse(request, response, 405, "Method Not Supported", null);
                    return;
                }

                if (request.QueryString.Count < 2)
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", null);
                    return;
                }

                Guid?guid = RetrieveGuidParameter(request);
                if (!guid.HasValue)
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", null);
                    return;
                }

                Uri url = RetrieveUrlParameter(request);
                if (url == null)
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", null);
                    return;
                }

                if (UrlContainsDangerousMarkup(url))
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", url);
                    return;
                }

                RevaleeUrlAuthorization authorization = Supervisor.Configuration.AuthorizedTargets.Match(url);
                if (authorization == null)
                {
                    // Keep all authorization errors homogeneous from the client perspective
                    RevaleeUrlAuthorization.ObfuscateExecutionTime();
                    FinalizeRejectedResponse(request, response, 401, "Unauthorized", url);
                    return;
                }

                if (!authorization.IsAuthorizedRequestSource(request.RemoteEndPoint.Address))
                {
                    // Keep all authorization errors homogeneous from the client perspective
                    RevaleeUrlAuthorization.ObfuscateExecutionTime();
                    FinalizeRejectedResponse(request, response, 401, "Unauthorized", url);
                    return;
                }

                if (Supervisor.Work.IsOverloaded)
                {
                    FinalizeRejectedResponse(request, response, 503, "Service Unavailable", url);
                    return;
                }

                RevaleeTask taskToCancel = RevaleeTask.Revive(DateTime.MinValue, url, DateTime.UtcNow, guid.Value, 0, null);
                Supervisor.State.CancelTask(taskToCancel);

                FinalizeAcceptedResponse(request, response, guid.Value, url);
                return;
            }
            catch (HttpListenerException hlex)
            {
                Supervisor.LogException(hlex, TraceEventType.Error, request.RawUrl);
                FinalizeRejectedResponse(request, response, 500, "Error Occurred", null);
                return;
            }
        }
        public void Process(HttpListenerRequest request, HttpListenerResponse response)
        {
            try
            {
                if (request.HttpMethod != "PUT")
                {
                    FinalizeRejectedResponse(request, response, 405, "Method Not Supported", null);
                    return;
                }

                if (request.QueryString.Count < 2)
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", null);
                    return;
                }

                Uri url = RetrieveUrlParameter(request);
                if (url == null)
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", null);
                    return;
                }

                DateTime?time = RetrieveTimeParameter(request);
                if (!time.HasValue)
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", url);
                    return;
                }

                if (UrlContainsDangerousMarkup(url))
                {
                    FinalizeRejectedResponse(request, response, 400, "Bad Request", url);
                    return;
                }

                RevaleeUrlAuthorization authorization = Supervisor.Configuration.AuthorizedTargets.Match(url);
                if (authorization == null)
                {
                    // Keep all authorization errors homogeneous from the client perspective
                    RevaleeUrlAuthorization.ObfuscateExecutionTime();
                    FinalizeRejectedResponse(request, response, 401, "Unauthorized", url);
                    return;
                }

                if (!authorization.IsAuthorizedRequestSource(request.RemoteEndPoint.Address))
                {
                    // Keep all authorization errors homogeneous from the client perspective
                    RevaleeUrlAuthorization.ObfuscateExecutionTime();
                    FinalizeRejectedResponse(request, response, 401, "Unauthorized", url);
                    return;
                }

                if (Supervisor.Work.IsOverloaded)
                {
                    FinalizeRejectedResponse(request, response, 503, "Service Unavailable", url);
                    return;
                }

                string authorizationCipher = RetrieveAuthorizationHeader(request);

                RevaleeTask newTask = new RevaleeTask(time.Value, url, authorization.RetryCount, authorizationCipher);
                Supervisor.State.AddTask(newTask);

                FinalizeAcceptedResponse(request, response, newTask);
                return;
            }
            catch (HttpListenerException hlex)
            {
                Supervisor.LogException(hlex, TraceEventType.Error, request.RawUrl);
                FinalizeRejectedResponse(request, response, 500, "Error Occurred", null);
                return;
            }
        }
        public static void Run()
        {
            Console.WriteLine("===] REVALEE [===  v{0}", GetVersionNumber());
            Console.WriteLine("                   {0}", GetCopyrightInformation());
            Console.WriteLine();

            if (!CheckInstallation())
            {
                return;
            }

            WriteStatusMessage("Initializing...");

            try
            {
                Supervisor.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("Revalee service cannot start due to the following critical error:");
                Console.WriteLine("*  {0}", ex.Message);
                Console.WriteLine();
                Console.WriteLine("Press any key to terminate.");
                Console.ReadKey(true);
                throw;
            }

            try
            {
                ClearStatusMessage();

                if (Supervisor.Configuration.ListenerPrefixes.Count == 0)
                {
                    Console.WriteLine("Revalee service is running but is not listening for callback requests.");
                }
                else if (Supervisor.Configuration.ListenerPrefixes.Count == 1)
                {
                    Console.WriteLine("Revalee service is running and listening on {0}.", Supervisor.Configuration.ListenerPrefixes[0]);
                }
                else
                {
                    Console.WriteLine("Revalee service is running and listening on:");

                    foreach (ListenerPrefix prefix in Supervisor.Configuration.ListenerPrefixes)
                    {
                        Console.WriteLine("    {0}", prefix);
                    }
                }

                Console.WriteLine("Press any key to terminate.");
                Console.ReadKey(true);
                Console.WriteLine();
            }
            finally
            {
                WriteStatusMessage("Service stopping...");

                try
                {
                    Supervisor.Stop();
                }
                catch (ThreadAbortException)
                {
                    // Ignore thread abort exceptions on shutdown
                }
                catch (ObjectDisposedException)
                {
                    // Ignore object disposed exceptions on shutdown
                }

                ClearStatusMessage();
                Console.WriteLine();
            }
        }