public void Apply(UrlPolicy policy, HttpContext httpContext)
        {
            var rule = policy.SeoSettings.CanonicalHostNameRule;

            if (rule == CanonicalHostNameRule.NoRule)
            {
                return;
            }

            if (httpContext.Connection.IsLocal())
            {
                // Allows testing of "localtest.me"
                return;
            }

            var hasWww = policy.Host.Value.StartsWith("www.", StringComparison.OrdinalIgnoreCase);

            if (rule == CanonicalHostNameRule.OmitWww && hasWww)
            {
                policy.Host.Modify(policy.Host.Value.Substring(4));
            }
            else if (rule == CanonicalHostNameRule.RequireWww && !hasWww)
            {
                policy.Host.Modify("www." + policy.Host.Value);
            }
        }
예제 #2
0
 public CommonController(
     SmartDbContext db,
     IGeoCountryLookup countryLookup,
     ICookieConsentManager cookieConsentManager,
     Lazy <IMediaService> mediaService,
     ILanguageService languageService,
     UrlPolicy urlPolicy,
     IWebHelper webHelper,
     IThemeContext themeContext,
     IThemeRegistry themeRegistry,
     ThemeSettings themeSettings,
     SeoSettings seoSettings,
     LocalizationSettings localizationSettings,
     PrivacySettings privacySettings)
 {
     _db                   = db;
     _countryLookup        = countryLookup;
     _cookieConsentManager = cookieConsentManager;
     _mediaService         = mediaService;
     _languageService      = languageService;
     _urlPolicy            = urlPolicy;
     _webHelper            = webHelper;
     _themeContext         = themeContext;
     _themeRegistry        = themeRegistry;
     _themeSettings        = themeSettings;
     _seoSettings          = seoSettings;
     _localizationSettings = localizationSettings;
     _privacySettings      = privacySettings;
 }
예제 #3
0
 public CommonController(
     SmartDbContext db,
     Lazy <IMediaService> mediaService,
     UrlPolicy urlPolicy,
     IThemeContext themeContext,
     IThemeRegistry themeRegistry,
     ThemeSettings themeSettings,
     LocalizationSettings localizationSettings)
 {
     _db            = db;
     _mediaService  = mediaService;
     _urlPolicy     = urlPolicy;
     _themeContext  = themeContext;
     _themeRegistry = themeRegistry;
     _themeSettings = themeSettings;
 }
예제 #4
0
        public virtual UrlPolicy GetUrlPolicy()
        {
            if (_urlPolicy == null)
            {
                var request = _httpContextAccessor?.HttpContext?.Request;
                if (request == null)
                {
                    throw new InvalidOperationException("A valid HttpContext instance is required for successful URL policy creation.");
                }

                _urlPolicy = new UrlPolicy(request)
                {
                    DefaultCultureCode   = _languageService.GetMasterLanguageSeoCode(),
                    LocalizationSettings = _localizationSettings,
                    SeoSettings          = _seoSettings,
                    WorkingLanguage      = _workContext.WorkingLanguage
                };
            }

            return(_urlPolicy);
        }
예제 #5
0
        public void Apply(UrlPolicy policy, HttpContext httpContext)
        {
            // Don't redirect on localhost if not allowed.
            if (httpContext.Connection.IsLocal())
            {
                if (!httpContext.RequestServices.GetService <SecuritySettings>().UseSslOnLocalhost)
                {
                    return;
                }
            }

            // Only redirect for GET requests, otherwise the browser might not propagate
            // the verb and request body correctly.
            if (!HttpMethods.IsGet(httpContext.Request.Method))
            {
                return;
            }

            var currentConnectionSecured = httpContext.RequestServices.GetService <IWebHelper>().IsCurrentConnectionSecured();
            var currentStore             = httpContext.RequestServices.GetService <IStoreContext>().CurrentStore;
            var supportsHttps            = currentStore.SupportsHttps();
            var requireHttps             = currentStore.ForceSslForAllPages;

            if (policy.Endpoint != null && supportsHttps && !requireHttps)
            {
                // Check if RequireSslAttribute is present in endpoint metadata
                requireHttps = policy.Endpoint.Metadata.GetMetadata <RequireSslAttribute>() != null;
            }

            requireHttps = requireHttps && supportsHttps;

            if (requireHttps && !currentConnectionSecured)
            {
                policy.Scheme.Modify(Uri.UriSchemeHttps);
            }
            else if (!requireHttps && currentConnectionSecured)
            {
                policy.Scheme.Modify(Uri.UriSchemeHttp);
            }
        }
예제 #6
0
        private static PathInfo GetCurrentPathInfo(ActionContext actionContext)
        {
            var cacheKey = "CurrentPathInfoForSitemapMatching";

            return(actionContext.HttpContext.GetItem(cacheKey, () =>
            {
                var urlPolicy = actionContext.HttpContext.RequestServices.GetRequiredService <UrlPolicy>();
                var urlHelper = actionContext.HttpContext.RequestServices.GetRequiredService <IUrlHelper>();

                var info = new PathInfo
                {
                    RequestPath = UrlPolicy.CombineSegments(urlPolicy.PathBase, urlPolicy.Path.Value)
                };

                // Path generated by Routing.
                var routeValues = actionContext.RouteData.Values;

                // We pushed matching UrlRecord instance to DataTokens in SlugRouteTransformer.
                var urlRecord = actionContext.RouteData.DataTokens["UrlRecord"] as UrlRecord;
                if (urlRecord != null)
                {
                    var routeName = urlRecord.EntityName;

                    // Also slug has been pushed to current RouteValues in SlugRouteTransformer class.
                    if (routeValues.ContainsKey(SlugRouteTransformer.SlugRouteKey))
                    {
                        info.RoutePath = urlHelper.RouteUrl(routeName, new { SeName = routeValues[SlugRouteTransformer.SlugRouteKey] });
                    }
                }
                else
                {
                    info.RoutePath = urlHelper.RouteUrl(routeValues);
                }

                return info;
            }));
        }
예제 #7
0
 public ErrorController(UrlPolicy urlPolicy, ILoggerFactory loggerFactory)
 {
     _urlPolicy     = urlPolicy;
     _loggerFactory = loggerFactory;
 }
예제 #8
0
 /// <summary>
 /// Indicates whether URLs may be logged in case of an error.
 /// </summary>
 public static bool CanLogError(this UrlPolicy policy)
 {
     return(policy.CanLog() || policy == UrlPolicy.LoadError);
 }
예제 #9
0
 /// <summary>
 /// Indicates whether URLs may be logged normally.
 /// </summary>
 public static bool CanLog(this UrlPolicy policy)
 {
     return(policy == UrlPolicy.Always || policy == UrlPolicy.BeforeTitle);
 }
예제 #10
0
        public void Apply(UrlPolicy policy, HttpContext httpContext)
        {
            if (policy.Endpoint == null)
            {
                return;
            }

            if (!policy.LocalizationSettings.SeoFriendlyUrlsForLanguagesEnabled || !HttpMethods.IsGet(policy.Method))
            {
                // Handle only GET requests and when config says so.
                return;
            }

            var localizedRouteMetadata = policy.Endpoint.Metadata.OfType <LocalizedRouteMetadata>().FirstOrDefault();

            if (localizedRouteMetadata == null)
            {
                // Handle only localizable routes
                return;
            }

            var workingLanguage = policy.WorkingLanguage;
            var invalidBehavior = policy.LocalizationSettings.InvalidLanguageRedirectBehaviour;
            var defaultBehavior = policy.LocalizationSettings.DefaultLanguageRedirectBehaviour;

            if (policy.Culture.HasValue)
            {
                var languageService = httpContext.RequestServices.GetService <ILanguageService>();
                if (!languageService.IsPublishedLanguage(policy.Culture))
                {
                    // Language is not defined in system or not assigned to store
                    if (invalidBehavior == InvalidLanguageRedirectBehaviour.ReturnHttp404)
                    {
                        var cultureCodeReplacement = defaultBehavior == DefaultLanguageRedirectBehaviour.PrependSeoCodeAndRedirect
                            ? workingLanguage.GetTwoLetterISOLanguageName()
                            : string.Empty;

                        policy.Culture.Modify(cultureCodeReplacement);
                        policy.IsInvalidUrl = true;
                    }
                    else if (invalidBehavior == InvalidLanguageRedirectBehaviour.FallbackToWorkingLanguage)
                    {
                        policy.Culture.Modify(defaultBehavior == DefaultLanguageRedirectBehaviour.StripSeoCode
                            ? string.Empty
                            : workingLanguage.GetTwoLetterISOLanguageName());
                    }
                }
                else // Not a published language
                {
                    // Redirect default language (if desired)
                    if (policy.Culture == policy.DefaultCultureCode && defaultBehavior == DefaultLanguageRedirectBehaviour.StripSeoCode)
                    {
                        policy.Culture.Strip();
                    }
                }
            }
            else // No culture present
            {
                // Keep default language prefixless (if desired)
                if (!(workingLanguage.UniqueSeoCode == policy.DefaultCultureCode && (int)(defaultBehavior) > 0))
                {
                    // Add language code to URL
                    policy.Culture.Modify(workingLanguage.UniqueSeoCode);
                }
            }
        }
예제 #11
0
 public void SetUp()
 {
     _inputPolicy = MockRepository.GenerateStub <IRouteInputPolicy>();
     _policy      = new UrlPolicy(c => true);
 }