More Muc Than You Can Handle

It Is Good To Laugh At Your Old Code

One of the things I love about my job is the ability to see myself grow as a developer. I’m glad I can laugh at my old code because if I can’t then I’m definitely not improving my skillset.

Here’s some code from around 3 years ago:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
using System;
using System.Web;
using System.Configuration;

using System.Text;
using System.Text.RegularExpressions;
using CBC.Radio3.Core.Web;

namespace CBC.Radio3.Core
{
    public class UrlRewriteHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        // Override the ProcessRequest method.
          public void ProcessRequest(HttpContext context) 
          {
            string path = (context.Request.QueryString["url"] == null) 
                            ? string.Empty 
                            : Convert.ToString(context.Request.QueryString["url"]);
            
            string sendToUrl = path;
                        
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
            context.Trace.Write("ModuleRewriter", "Url Requested " + path);

            for(int i = 0; i < rules.Count; i++)
            {
                string lookFor = rules[i].LookFor;
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                if (re.IsMatch(path))
                {
                    sendToUrl = SiteInfo.ApplicationPath + re.Replace(path, rules[i].SendTo);
                    context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
                    
                    string referer               = context.Request.Headers["Referer"] ?? string.Empty;
                    string permalink             = path;
                    RewriterRuleType ruleType     = UrlRewriteHandler.GetRewriterRuleType(path); 
                    string siteDomain            = SiteInfo.Hostname;
                    
                    if ( !IsBotRequest(context) && !referer.Contains( siteDomain ))
                    {
                        PermalinkLogger.Instance().Log(permalink, ruleType, referer);
                    }
                    
                    context.Response.Redirect(sendToUrl);
                }
            }
            
            // We haven't found a match so return a 404
            context.Response.Status        = "404 Not Found";
            context.Response.StatusCode = 404;
          }
 

          private static bool IsBotRequest(HttpContext context)
          {
              return context.Request.Browser.Crawler;
          }

          public bool IsReusable { get { return true; } }
          
          
        public static RewriterRuleType GetRewriterRuleType(string permalink)
        {
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

            for(int i = 0; i < rules.Count; i++)
            {
                string lookFor = rules[i].LookFor;
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                if (re.IsMatch(permalink))
                {
                    return rules[i].RuleType;
                }
            }
            
            return RewriterRuleType.Undefined;
        } 
        
        public static string GetRuleDataXml(string permalink)
        {
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

            for(int i = 0; i < rules.Count; i++)
            {
                string lookFor = rules[i].LookFor;
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                if (re.IsMatch(permalink))
                {
                    string xmlData = re.Replace(permalink, rules[i].NodeForm);
                    return xmlData;
                }
            }
            
            return string.Empty;        
        }        
   }
}

Here’s what the code I used to replace it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Web;
using CBC.Radio3.Permalinks.Common;

namespace CBC.Radio3.Permalinks.HttpHandlers
{
    public class PermalinkProcessingHandler : IHttpHandler
    {
        private readonly IPermalinkParser permalinkParser;

        public PermalinkProcessingHandler()
            : this(new PermalinkParser())
        {
            
        }

        public PermalinkProcessingHandler(IPermalinkParser permalinkParser)
        {
            this.permalinkParser = permalinkParser;
        }

        public void ProcessRequest(HttpContext context) 
        {
            string permalink = context.Request.QueryString["url"] ?? string.Empty;

            if (!string.IsNullOrEmpty(permalink) && permalinkParser.CanParse(permalink))
            {
                string redirectUrl = permalinkParser.GetRedirectUrlFrom(permalink);
                context.Response.Redirect(redirectUrl);
            }

            context.Response.Status        = "404 Not Found";
            context.Response.StatusCode = 404;
        }

        public bool IsReusable { get { return true; } }
    }
}