What is CDN?
The Content Delivery Network (CDN) is a media server where we can host a site's images, CSS, JavaScript, PDF and video files and so on. There will be two media servers, one for a HTTP request and one for a HTTPS request.
Using a CDN server provides the following advantages:
- Cached version of files.
- Decrease downloading time as files comes from a different server.
- Reduce the network traffic.
Let's understand how to implement the CDN and SSL CDN for all the media and graphic files for a legacy website (non MVC site). Suppose you want to implement the CDN for HTTP and HTTPS request and our media servers are like http://media.domain.com and https://securemedia.domain.com respectively. In a legacy site if you do a manual path update for each media and graphic file then it will be a very tough job and there will be a chance that the CDN and SSL CDN score will not be up to the benchmark.
Scenario 1: Suppose you have the master page in your ASP.NET application.
In your master page you can override the Render method. So the following is the code snippet for it.
- protected override void Render(System.Web.UI.HtmlTextWriter writer)
- {
- bool isCdnenabled = true;
- string cdnhost;
- if (isCdnenabled)
- {
- if (HttpContext.Current.Request.IsSecureConnection)
- {
- cdnhost = "https://securemedia.domain.com";
- }
- else if (HttpContext.Current.Request.Headers["ssl"] != null &&
- HttpContext.Current.Request.Headers["ssl"].ToLower() == "true")
- {
- cdnhost = "https://securemedia.domain.com";
- }
- else
- {
- cdnhost = "http://media.domain.com";
- }
- }
- else
- {
- cdnhost = "";
- }
-
- using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
- {
- base.Render(htmlwriter);
- string html = htmlwriter.InnerWriter.ToString();
- html=Regex.Replace(html, @"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}", string.Empty);
- html=Regex.Replace(html, @"[ \f\r\t\v]?([\n\xFE\xFF/{}[\];,<>*%&|^!~?:=])[\f\r\t\v]?", "$1");
- html = html.Replace(";\t", ";");
- html = html.Replace(";\n", ";");
- html = Regex.Replace(html, @"<title>[^\t\r\n[\]]</title>", "");
- html = Regex.Replace(html, @"<title></title>", "");
- string regExPatternForImgTags = @"[ \t]((src)|(href))=(?<o>(""|'))([^""']*)(\<o>)";
-
- string output = Regex.Replace(html, regExPatternForImgTags, (match) =>
- {
-
- string attr = match.Groups[1].Value;
- string quote = match.Groups[4].Value;
- string url = match.Groups[5].Value;
- string newUrl = url;
-
- if (!(newUrl.Contains("http://") || newUrl.Contains("https://")))
- {
- string urlCheck = NoQuery(url);
-
- if (urlCheck.EndsWith(".png") ||
- urlCheck.EndsWith(".jpg") ||
- urlCheck.EndsWith(".gif") ||
- urlCheck.EndsWith(".css") ||
- urlCheck.EndsWith(".ico") ||
- url.EndsWith(".js"))
- {
-
- if (url.Contains("/"))
- {
- newUrl = cdnhost + Regex.Replace(newUrl, @"(\.\./)+", "");
- }
- else
- {
- newUrl = cdnhost + newUrl;
- }
-
- return match.Value.Replace(url, newUrl);
- }
- }
-
-
- return match.Value;
- });
-
- writer.Write(output.Trim());
- }
-
-
- }
- public static string NoQuery(string url)
- {
- int index = url.IndexOf("?");
-
- if (index >= 0)
- return url.Substring(0, index);
-
- return url;
- }
Scenario 2: Suppose you don't have the master file in your application. Then you can create a HTTP Module and override the BeginRequest method and use the same code.