Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bob Banks 16 posts 36 karma points
    Sep 19, 2013 @ 21:10
    Bob Banks
    0

    Trouble with 6.0.5?

    Installed the package without a problem on 6.0.5 and configured via document type.  Wasn't working.  So I brought in the source code and I see the event getting wired up in the constructor of EventsHandler but it never seems to fire.  Any ideas on what else to check? (useAspNetMasterPages is set to true)

     

     

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Sep 20, 2013 @ 09:16
    Lee Kelleher
    0

    Hi Bob,

    I haven't actually used it with v6.x (only because I haven't had a client requirement for it yet). It's concerning that it has stopped working.

    My next port of call (for me) would be to debug the Umbraco core source - to see why it's not triggering the event. Unfortunately my workload is full on for the next few weeks - so I wouldn't be able to look into it for a while (sorry).

    *If * you do dig deeper into this, please do share your findings - would be good for this package to work with v6.x+

    Thanks,
    - Lee

  • Dan White 206 posts 510 karma points c-trib
    Dec 02, 2013 @ 22:59
    Dan White
    1

    @Lee

    I've just tried it with 6.1.6 and can verify that it isn't working. 

    It doesn't look like UmbracoDefault.AfterRequestInit is being triggered. Likely due to the new MVC stuff.

    Off the top of my head, I'd look at doing it through a custom controller that executes on every request or hooking into stephans new routing stuff (https://github.com/umbraco/Umbraco4Docs/blob/master/Documentation/Reference/Request-Pipeline/inbound-pipeline.md). Do you think either of these approaches would be worth exploring?

  • Dan White 206 posts 510 karma points c-trib
    Dec 03, 2013 @ 01:40
    Dan White
    2

    Got it working with some quick changes using PublishedContentRequest.Prepared. It seemed to break the dashboard in the backend though. Likely due to upgrading the umbraco.dll. Anyway, just a quick proof of concept. Idk, if this is in the right direction or not.

    using System;
    using System.Web;
    using umbraco.NodeFactory;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Web.Routing;
    
    namespace Our.Umbraco.HttpsRedirect.Events
    {
        public class EventsHandler : ApplicationEventHandler
        {
            public EventsHandler()
            {
                PublishedContentRequest.Prepared += PublishedContentRequest_Prepared;
            }
    
            void PublishedContentRequest_Prepared(object sender, EventArgs e)
            {
                var context = HttpContext.Current;
                var request = sender as PublishedContentRequest;
                var url = request.Uri.ToString();
                var page = request.PublishedContent;
    
                // check if the port should be stripped.
                if (ShouldStripPort())
                    url = StripPortFromUrl(url, context.Request.Url);
    
                // check for matches
                if (HasMatch(page, request))
                {
                    // if the doc-type matches and is NOT on HTTPS...
                    if (!context.Request.IsSecureConnection)
                    {
                        // ... then redirect the URL to HTTPS.
                        PerformRedirect(url.Replace(Settings.HTTP, Settings.HTTPS), context);
                    }
    
                    return;
                }
    
                // otherwise if the URL is on HTTPS...
                if (context.Request.IsSecureConnection)
                {
                    // ... redirect the URL back to HTTP.
                    PerformRedirect(url.Replace(Settings.HTTPS, Settings.HTTP), context);
                    return;
                }
            }
    
            private static string StripPortFromUrl(string url, Uri contextUri)
            {
                return url.Replace(string.Format(":{0}", contextUri.Port), string.Empty);
            }
    
            private static bool ShouldStripPort()
            {
                return Settings.GetValueFromKey<bool>(Settings.AppKey_StripPort);
            }
    
            private static bool ShouldRedirectTemporary()
            {
                return Settings.GetValueFromKey<bool>(Settings.AppKey_UseTemporaryRedirects);
            }
    
            private static bool HasMatch(IPublishedContent page, PublishedContentRequest request)
            {
                return MatchesDocTypeAlias(page.DocumentTypeAlias)
                    || MatchesNodeId(page.Id)
                    || MatchesTemplate(request.TemplateAlias)
                    || MatchesPropertyValue((page.Id));
            }
    
            private static bool MatchesDocTypeAlias(string docTypeAlias)
            {
                return Settings.KeyContainsValue(Settings.AppKey_DocTypes, docTypeAlias);
            }
    
            private static bool MatchesNodeId(int pageId)
            {
                return Settings.KeyContainsValue(Settings.AppKey_PageIds, pageId);
            }
    
            private static bool MatchesTemplate(string templateAlias)
            {
                return Settings.KeyContainsValue(Settings.AppKey_Templates, templateAlias);
            }
    
            private static bool MatchesPropertyValue(int pageId)
            {
                var appSetting = Settings.GetValueFromKey(Settings.AppKey_Properties);
    
                if (string.IsNullOrEmpty(appSetting))
                    return false;
    
                var node = new Node(pageId);
                var items = appSetting.Split(new[] { Settings.COMMA }, StringSplitOptions.RemoveEmptyEntries);
    
                foreach (var item in items)
                {
                    var parts = item.Split(new[] { Settings.COLON }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 0)
                        continue;
    
                    var propertyAlias = parts[0];
                    var propertyValue = Settings.CHECKBOX_TRUE;
    
                    if (parts.Length > 1)
                        propertyValue = parts[1];
    
                    var property = node.GetProperty(propertyAlias);
                    if (property == null)
                        continue;
    
                    var match = string.Equals(property.Value, propertyValue, StringComparison.InvariantCultureIgnoreCase);
                    if (match)
                        return true;
                }
    
                return false;
            }
    
            private static void PerformRedirect(string targetUrl, HttpContext context)
            {
                if (ShouldRedirectTemporary())
                    context.Response.Redirect(targetUrl, true);
                else
                    context.Response.RedirectPermanent(targetUrl, true);
            }
        }
    }
  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Dec 03, 2013 @ 12:13
    Lee Kelleher
    0

    Hey Dan, thanks for making the effort to fix this for v6.1+ ... I haven't had a need for it personally, hence why it's been on the backburner for so long.

    Your fix looks good, I wasn't aware of the PublishedContentRequest.Prepared event (again only because I hadn't needed it before).

    How are you with git/GitHub? Feel free to send it as a pull-request - I'll merge it in.

    https://github.com/leekelleher/umbraco-https-redirect

    Thanks again! #h5yr

    Cheers, Lee.

  • Dan White 206 posts 510 karma points c-trib
    Dec 05, 2013 @ 02:43
    Dan White
    2

    Just submitted a pull-request.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Dec 05, 2013 @ 10:56
    Lee Kelleher
    0

    Excellent, thanks Dan! I'll take a look now.

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft