Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2012
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     public class AzureCDNPlugin : IPlugin {
  12.  
  13.         public AzureCDNPlugin() { }
  14.  
  15.         Config c;
  16.  
  17.         protected string redirectThrough = null;
  18.         protected bool redirectPermanent = false;
  19.         public IPlugin Install(Configuration.Config c) {
  20.             this.c = c;
  21.             c.Plugins.add_plugin(this);
  22.             c.Pipeline.PostAuthorizeRequestStart += Pipeline_PostAuthorizeRequestStart;
  23.             c.Pipeline.RewriteDefaults += Pipeline_RewriteDefaults;
  24.             redirectThrough = c.get("azurecdn.redirectthrough", null);
  25.             redirectPermanent = c.get("azurecdn.permanentRedirect", false);
  26.             return this;
  27.         }
  28.  
  29.         void Pipeline_PostAuthorizeRequestStart(System.Web.IHttpModule sender, System.Web.HttpContext context) {
  30.             if (redirectThrough != null && c.Pipeline.ModifiedQueryString.Count > 0) {
  31.                 //It wasn't a AzureCDN URL - it had a normal querystring
  32.                 context.Items[c.Pipeline.ModifiedPathKey + ".hadquery"] = true;
  33.             }
  34.             //Transform semicolon querystrings into the normal collection
  35.             TransformAzureCDNUrl(context);
  36.         }
  37.  
  38.         bool TransformAzureCDNUrl(System.Web.HttpContext context) {
  39.             string s = c.Pipeline.PreRewritePath;
  40.             int semi = s.IndexOf(';');
  41.             if (semi < 0) return false; //No querystring here.
  42.             int question = s.IndexOf('?');
  43.             if (question > -1) throw new ImageProcessingException("ASP.NET failed to parse querystring, question mark remains in path segment: " + s);
  44.  
  45.  
  46.  
  47.             string path = s.Substring(0, semi);
  48.  
  49.             //Why do we care what image type it is? That gets checked later
  50.             //if (!c.Pipeline.IsAcceptedImageType(path)) return false; //Must be valid image type
  51.             c.Pipeline.PreRewritePath = path; //Fix the path.
  52.  
  53.             //Parse the fake query, merge it with the real one, and we are done.
  54.             string query = s.Substring(semi);
  55.             NameValueCollection q = Util.PathUtils.ParseQueryStringFriendlyAllowSemicolons(query);
  56.  
  57.             //Merge the querystring with everything found in PathInfo. THe querystring wins on conflicts
  58.             foreach (string key in c.Pipeline.ModifiedQueryString.Keys)
  59.                 q[key] = c.Pipeline.ModifiedQueryString[key];
  60.  
  61.             c.Pipeline.ModifiedQueryString = q;
  62.  
  63.             return true;
  64.         }
  65.  
  66.         void Pipeline_RewriteDefaults(System.Web.IHttpModule sender, System.Web.HttpContext context, IUrlEventArgs e) {
  67.             ///Handle redirectThrough behavior
  68.             if (redirectThrough != null && context.Items[c.Pipeline.ModifiedPathKey + ".hadquery"] != null) {
  69.                 //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
  70.                 string finalPath = redirectThrough + e.VirtualPath + PathUtils.BuildSemicolonQueryString(e.QueryString, true);
  71.  
  72.                 //Redirect according to setting
  73.                 context.Response.Redirect(finalPath, !redirectPermanent);
  74.                 if (redirectPermanent) {
  75.                     context.Response.StatusCode = 301;
  76.                     context.Response.End();
  77.                 }
  78.             }
  79.  
  80.         }
  81.  
  82.         public bool Uninstall(Configuration.Config c) {
  83.             c.Plugins.remove_plugin(this);
  84.             c.Pipeline.RewriteDefaults -= Pipeline_RewriteDefaults;
  85.             c.Pipeline.PostAuthorizeRequestStart -= Pipeline_PostAuthorizeRequestStart;
  86.             return true;
  87.         }
  88.     }
  89. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement