Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using ImageResizer.Configuration;
- using System.Text.RegularExpressions;
- using System.Collections.Specialized;
- using System.Web;
- using ImageResizer.Util;
- namespace ImageResizer.Plugins.AzureCDN {
- /// <summary>
- /// Allows querystrings to be expressed with '/' or ';' instead of '?', allow the querystring to survive the AzureCDN guillotine.
- /// Since IIS can't stand '&' symbols in the path, you have to replace both '?' and '&' with ';'
- /// Later I hope to include control adapters to automate the process.
- /// </summary>
- public class AzureCDNPlugin : IPlugin {
- public AzureCDNPlugin() { }
- Config c;
- protected string redirectThrough = null;
- protected bool redirectPermanent = false;
- public IPlugin Install(Configuration.Config c) {
- this.c = c;
- c.Plugins.add_plugin(this);
- c.Pipeline.PostAuthorizeRequestStart += Pipeline_PostAuthorizeRequestStart;
- c.Pipeline.RewriteDefaults += Pipeline_RewriteDefaults;
- redirectThrough = c.get("azurecdn.redirectthrough", null);
- redirectPermanent = c.get("azurecdn.permanentRedirect", false);
- return this;
- }
- void Pipeline_PostAuthorizeRequestStart(System.Web.IHttpModule sender, System.Web.HttpContext context) {
- if (redirectThrough != null && c.Pipeline.ModifiedQueryString.Count > 0) {
- //It wasn't a AzureCDN URL - it had a normal querystring
- context.Items[c.Pipeline.ModifiedPathKey + ".hadquery"] = true;
- }
- }
- void Pipeline_RewriteDefaults(System.Web.IHttpModule sender, System.Web.HttpContext context, IUrlEventArgs e) {
- ///Handle redirectThrough behavior
- if (redirectThrough != null && context.Items[c.Pipeline.ModifiedPathKey + ".hadquery"] != null) {
- //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
- string finalPath = redirectThrough + e.VirtualPath + PathUtils.BuildQueryString(e.QueryString, true);
- //Redirect according to setting
- context.Response.Redirect(finalPath, !redirectPermanent);
- if (redirectPermanent) {
- context.Response.StatusCode = 301;
- context.Response.End();
- }
- }
- }
- public bool Uninstall(Configuration.Config c) {
- c.Plugins.remove_plugin(this);
- c.Pipeline.RewriteDefaults -= Pipeline_RewriteDefaults;
- c.Pipeline.PostAuthorizeRequestStart -= Pipeline_PostAuthorizeRequestStart;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement