<commit_before>﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.Mvc.Filters;
using IFilterProvider = Orchard.Mvc.Filters.IFilterProvider;

namespace Orchard.Exceptions.Filters {
    public class UnhandledExceptionFilter : FilterProvider, IActionFilter {
        private readonly IExceptionPolicy _exceptionPolicy;
        private readonly IOrchardServices _orchardServices;
        private readonly Lazy<IEnumerable<IFilterProvider>> _filterProviders;

        public UnhandledExceptionFilter(
            IExceptionPolicy exceptionPolicy,
            IOrchardServices orchardServices,
            Lazy<IEnumerable<IFilterProvider>> filters) {
            _exceptionPolicy = exceptionPolicy;
            _orchardServices = orchardServices;
            _filterProviders = filters;
            Logger = NullLogger.Instance;
        }

        public ILogger Logger { get; set; }

        public void OnActionExecuting(ActionExecutingContext filterContext) {
        }

        public void OnActionExecuted(ActionExecutedContext filterContext) {
            if (!filterContext.ExceptionHandled && filterContext.Exception != null) {
                if (_exceptionPolicy.HandleException(this, filterContext.Exception)) {
                    var shape = _orchardServices.New.ErrorPage();
                    shape.Message = filterContext.Exception.Message;
                    shape.Exception = filterContext.Exception;

                    filterContext.ExceptionHandled = true;

                    // inform exception filters of the exception that was suppressed
                    var filterInfo = new FilterInfo();
                    foreach (var filterProvider in _filterProviders.Value) {
                        filterProvider.AddFilters(filterInfo);
                    }

                    var exceptionContext = new ExceptionContext(filterContext.Controller.ControllerContext, filterContext.Exception);
                    foreach (var exceptionFilter in filterInfo.ExceptionFilters) {
                        exceptionFilter.OnException(exceptionContext);
                    }

                    if (exceptionContext.ExceptionHandled) {
                        filterContext.Result = exceptionContext.Result;
                    }
                    else {
                        filterContext.Result = new ShapeResult(filterContext.Controller, shape);
                        filterContext.RequestContext.HttpContext.Response.StatusCode = 500;
                    }
                }
            }

            if (filterContext.Result is HttpNotFoundResult) {
                var model = _orchardServices.New.NotFound();
                var request = filterContext.RequestContext.HttpContext.Request;
                var url = request.RawUrl;

                // If the url is relative then replace with Requested path
                model.RequestedUrl = request.Url.OriginalString.Contains(url) & request.Url.OriginalString != url ?
                    request.Url.OriginalString : url;

                // Dont get the user stuck in a 'retry loop' by
                // allowing the Referrer to be the same as the Request
                model.ReferrerUrl = request.UrlReferrer != null &&
                    request.UrlReferrer.OriginalString != model.RequestedUrl ?
                    request.UrlReferrer.OriginalString : null;

                filterContext.Result = new ShapeResult(filterContext.Controller, model);
                filterContext.RequestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }
        }
    }
}<commit_msg>Prevent IIS 7 classic mode from handling custom error/not found page<commit_after>﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.Mvc.Filters;
using IFilterProvider = Orchard.Mvc.Filters.IFilterProvider;

namespace Orchard.Exceptions.Filters {
    public class UnhandledExceptionFilter : FilterProvider, IActionFilter {
        private readonly IExceptionPolicy _exceptionPolicy;
        private readonly IOrchardServices _orchardServices;
        private readonly Lazy<IEnumerable<IFilterProvider>> _filterProviders;

        public UnhandledExceptionFilter(
            IExceptionPolicy exceptionPolicy,
            IOrchardServices orchardServices,
            Lazy<IEnumerable<IFilterProvider>> filters) {
            _exceptionPolicy = exceptionPolicy;
            _orchardServices = orchardServices;
            _filterProviders = filters;
            Logger = NullLogger.Instance;
        }

        public ILogger Logger { get; set; }

        public void OnActionExecuting(ActionExecutingContext filterContext) {
        }

        public void OnActionExecuted(ActionExecutedContext filterContext) {
            if (!filterContext.ExceptionHandled && filterContext.Exception != null) {
                if (_exceptionPolicy.HandleException(this, filterContext.Exception)) {
                    var shape = _orchardServices.New.ErrorPage();
                    shape.Message = filterContext.Exception.Message;
                    shape.Exception = filterContext.Exception;

                    filterContext.ExceptionHandled = true;

                    // inform exception filters of the exception that was suppressed
                    var filterInfo = new FilterInfo();
                    foreach (var filterProvider in _filterProviders.Value) {
                        filterProvider.AddFilters(filterInfo);
                    }

                    var exceptionContext = new ExceptionContext(filterContext.Controller.ControllerContext, filterContext.Exception);
                    foreach (var exceptionFilter in filterInfo.ExceptionFilters) {
                        exceptionFilter.OnException(exceptionContext);
                    }

                    if (exceptionContext.ExceptionHandled) {
                        filterContext.Result = exceptionContext.Result;
                    }
                    else {
                        filterContext.Result = new ShapeResult(filterContext.Controller, shape);
                        filterContext.RequestContext.HttpContext.Response.StatusCode = 500;

                        // prevent IIS 7.0 classic mode from handling the 404/500 itself
                        filterContext.RequestContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                    }
                }
            }

            if (filterContext.Result is HttpNotFoundResult) {
                var model = _orchardServices.New.NotFound();
                var request = filterContext.RequestContext.HttpContext.Request;
                var url = request.RawUrl;

                // If the url is relative then replace with Requested path
                model.RequestedUrl = request.Url.OriginalString.Contains(url) & request.Url.OriginalString != url ?
                    request.Url.OriginalString : url;

                // Dont get the user stuck in a 'retry loop' by
                // allowing the Referrer to be the same as the Request
                model.ReferrerUrl = request.UrlReferrer != null &&
                    request.UrlReferrer.OriginalString != model.RequestedUrl ?
                    request.UrlReferrer.OriginalString : null;

                filterContext.Result = new ShapeResult(filterContext.Controller, model);
                filterContext.RequestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;

                // prevent IIS 7.0 classic mode from handling the 404/500 itself
                filterContext.RequestContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.06489863572612521, 'fraction_numerical': 0.002932551319648094, 'mean_word_length': 2.009976976208749, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2273378', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1706, 'n_words': 406}