Advertisement
Guest User

ImageResizer.Plugins.AzureCDN

a guest
Apr 23rd, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using ImageResizer.Configuration;
  5. using System.Text.RegularExpressions;
  6. using System.Collections.Specialized;
  7. using System.Web;
  8. using ImageResizer.Util;
  9.  
  10. namespace ImageResizer.Plugins.AzureCDN {
  11.     /// <summary>
  12.     /// Allows querystrings to be expressed with '/' or ';' instead of '?', allow the querystring to survive the AzureCDN guillotine.
  13.     /// Since IIS can't stand '&' symbols in the path, you have to replace both '?' and '&' with ';'
  14.     /// Later I hope to include control adapters to automate the process.
  15.     /// </summary>
  16.     public class AzureCDNPlugin : IPlugin {
  17.  
  18.         public AzureCDNPlugin() { }
  19.  
  20.         Config c;
  21.  
  22.         protected string redirectThrough = null;
  23.         protected bool redirectPermanent = false;
  24.         public IPlugin Install(Configuration.Config c) {
  25.             this.c = c;
  26.             c.Plugins.add_plugin(this);
  27.             c.Pipeline.PostAuthorizeRequestStart += Pipeline_PostAuthorizeRequestStart;
  28.             c.Pipeline.RewriteDefaults += Pipeline_RewriteDefaults;
  29.             redirectThrough = c.get("azurecdn.redirectthrough", null);
  30.             redirectPermanent = c.get("azurecdn.permanentRedirect", false);
  31.             return this;
  32.         }
  33.  
  34.         void Pipeline_PostAuthorizeRequestStart(System.Web.IHttpModule sender, System.Web.HttpContext context) {
  35.             if (redirectThrough != null && c.Pipeline.ModifiedQueryString.Count > 0) {
  36.                 //It wasn't a AzureCDN URL - it had a normal querystring
  37.                 context.Items[c.Pipeline.ModifiedPathKey + ".hadquery"] = true;
  38.             }
  39.         }
  40.  
  41.         void Pipeline_RewriteDefaults(System.Web.IHttpModule sender, System.Web.HttpContext context, IUrlEventArgs e) {
  42.             ///Handle redirectThrough behavior
  43.             if (redirectThrough != null && context.Items[c.Pipeline.ModifiedPathKey + ".hadquery"] != null) {
  44.                 //It had a querystring originally - which means the request didn't come from AzureCDN, it came directly from the browser. Perform a redirect, rewriting the querystring appropriately
  45.                 string finalPath = redirectThrough + e.VirtualPath + PathUtils.BuildQueryString(e.QueryString, true);
  46.  
  47.                 //Redirect according to setting
  48.                 context.Response.Redirect(finalPath, !redirectPermanent);
  49.                 if (redirectPermanent) {
  50.                     context.Response.StatusCode = 301;
  51.                     context.Response.End();
  52.                 }
  53.             }
  54.  
  55.         }
  56.  
  57.         public bool Uninstall(Configuration.Config c) {
  58.             c.Plugins.remove_plugin(this);
  59.             c.Pipeline.RewriteDefaults -= Pipeline_RewriteDefaults;
  60.             c.Pipeline.PostAuthorizeRequestStart -= Pipeline_PostAuthorizeRequestStart;
  61.             return true;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement