Update of /cvsroot/yafdotnet/yafsrc/URLRewriter.NET/Actions In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv25871/yafsrc/URLRewriter.NET/Actions Added Files: Tag: v1_0_2_NETv2 AddHeaderAction.cs ConditionalAction.cs ForbiddenAction.cs GoneAction.cs IRewriteAction.cs MethodNotAllowedAction.cs NotFoundAction.cs NotImplementedAction.cs RedirectAction.cs RewriteAction.cs SetCookieAction.cs SetLocationAction.cs SetPropertyAction.cs SetStatusAction.cs Log Message: URL Rewriter class --- NEW FILE: IRewriteAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; namespace Intelligencia.UrlRewriter { /// <summary> /// Interface for executable actions. /// </summary> public interface IRewriteAction { /// <summary> /// Executes the action. /// </summary> /// <remarks> /// Note that it is important to set the correct properties on the context /// (e.g., StatusCode, Location), rather than directly implementing the action /// (e.g., RewritePath). This allows for the correct pipeline processing of /// all the specified rules. /// </remarks> /// <param name="context">The context to execute the action on.</param> void Execute(RewriteContext context); /// <summary> /// The Processing directive determines how the rewriter should continue /// processing after this action has executed. /// </summary> RewriteProcessing Processing { get; } } } --- NEW FILE: ForbiddenAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Returns a 403 Forbidden HTTP status code. /// </summary> public sealed class ForbiddenAction : SetStatusAction { /// <summary> /// Default constructor. /// </summary> public ForbiddenAction() : base(HttpStatusCode.Forbidden) { } } } --- NEW FILE: GoneAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Returns a 410 Gone HTTP status code. /// </summary> public sealed class GoneAction : SetStatusAction { /// <summary> /// Default constructor. /// </summary> public GoneAction() : base(HttpStatusCode.Gone) { } } } --- NEW FILE: AddHeaderAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Action that adds a given header. /// </summary> public class AddHeaderAction : IRewriteAction { /// <summary> /// Default constructor. /// </summary> /// <param name="header">The header name.</param> /// <param name="value">The header value.</param> public AddHeaderAction(string header, string value) { _header = header; _value = value; } /// <summary> /// The header name. /// </summary> public string Header { get { return _header; } set { _header = value; } } /// <summary> /// The header value. /// </summary> public string Value { get { return _value; } set { _value = value; } } /// <summary> /// Executes the action. /// </summary> /// <param name="context">The rewrite context.</param> public void Execute(RewriteContext context) { context.Headers.Add(Header, Value); } /// <summary> /// The Processing directive. /// </summary> public RewriteProcessing Processing { get { return RewriteProcessing.ContinueProcessing; } } private string _header; private string _value; } } --- NEW FILE: MethodNotAllowedAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Returns a 405 Method Not Allowed HTTP status code. /// </summary> public sealed class MethodNotAllowedAction : SetStatusAction { /// <summary> /// Default constructor. /// </summary> public MethodNotAllowedAction() : base(HttpStatusCode.MethodNotAllowed) { } } } --- NEW FILE: NotImplementedAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Returns a 501 Not Implemented HTTP status code. /// </summary> public sealed class NotImplementedAction : SetStatusAction { /// <summary> /// Default constructor. /// </summary> public NotImplementedAction() : base(HttpStatusCode.NotImplemented) { } } } --- NEW FILE: NotFoundAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Returns a 404 Not Found HTTP status code. /// </summary> public sealed class NotFoundAction : SetStatusAction { /// <summary> /// Default constructor. /// </summary> public NotFoundAction() : base(HttpStatusCode.NotFound) { } } } --- NEW FILE: SetPropertyAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Action that sets properties in the context. /// </summary> public class SetPropertyAction : IRewriteAction { /// <summary> /// Default constructor. /// </summary> /// <param name="name">The name of the variable.</param> /// <param name="value">The name of the value.</param> public SetPropertyAction(string name, string value) { _name = name; _value = value; } /// <summary> /// The name of the variable. /// </summary> public string Name { get { return _name; } set { _name = value; } } /// <summary> /// The value of the variable. /// </summary> public string Value { get { return _value; } set { _value = value; } } /// <summary> /// Executes the action. /// </summary> /// <param name="context">The rewrite context.</param> public void Execute(RewriteContext context) { context.Properties.Set(Name, Value); } /// <summary> /// The Processing directive. /// </summary> public RewriteProcessing Processing { get { return RewriteProcessing.ContinueProcessing; } } private string _name; private string _value; } } --- NEW FILE: SetCookieAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Web; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Action that sets a cookie. /// </summary> public class SetCookieAction : IRewriteAction { /// <summary> /// Default constructor. /// </summary> /// <param name="cookieName">The cookie name.</param> /// <param name="cookieValue">The cookie value.</param> public SetCookieAction(string cookieName, string cookieValue) { _name = cookieName; _value = cookieValue; } /// <summary> /// The name of the variable. /// </summary> public string Name { get { return _name; } set { _name = value; } } /// <summary> /// The value of the variable. /// </summary> public string Value { get { return _value; } set { _value = value; } } /// <summary> /// Executes the action. /// </summary> /// <param name="context">The rewrite context.</param> public void Execute(RewriteContext context) { HttpCookie cookie = new HttpCookie(Name, Value); context.Cookies.Add(cookie); } /// <summary> /// The Processing directive. /// </summary> public RewriteProcessing Processing { get { return RewriteProcessing.ContinueProcessing; } } private string _name; private string _value; } } --- NEW FILE: RedirectAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; using System.Web; using System.Collections; using Intelligencia.UrlRewriter.Conditions; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Redirect using 302 temporary redirect. /// </summary> public sealed class RedirectAction : SetLocationAction, IRewriteCondition { /// <summary> /// Default constructor. /// </summary> /// <param name="location">The location to set.</param> /// <param name="permanent">Whether the redirection is permanent.</param> public RedirectAction(string location, bool permanent) : base(location) { _permanent = permanent; } /// <summary> /// Executes the action. /// </summary> /// <param name="context">The rewriting context.</param> public override void Execute(RewriteContext context) { base.Execute(context); if (_permanent) { context.StatusCode = HttpStatusCode.Moved; } else { context.StatusCode = HttpStatusCode.Found; } } /// <summary> /// The Processing directive. /// </summary> public override RewriteProcessing Processing { get { return RewriteProcessing.StopProcessing; } } /// <summary> /// Determines if the rewrite rule matches. /// </summary> /// <param name="context"></param> /// <returns></returns> public bool IsMatch(RewriteContext context) { // Ensure the conditions are met. foreach (IRewriteCondition condition in Conditions) { if (!condition.IsMatch(context)) { return false; } } return true; } /// <summary> /// Conditions that must hold for the rule to fire. /// </summary> public IList Conditions { get { return _conditions; } } private ArrayList _conditions = new ArrayList(); private bool _permanent; } } --- NEW FILE: SetLocationAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Sets the Location. /// </summary> public abstract class SetLocationAction : IRewriteAction { /// <summary> /// Default constructor. /// </summary> /// <param name="location">The location (pattern) to set.</param> public SetLocationAction(string location) { _location = location; } /// <summary> /// The location to set. This can include replacements referencing the matched pattern, /// for example $1, $2, ... $n and ${group} as well as ${ServerVariable} and mapping, e.g., /// ${MapName:$1}. /// </summary> public string Location { get { return _location; } set { _location = value; } } /// <summary> /// Executes the action. /// </summary> /// <param name="context">The rewriting context.</param> public virtual void Execute(RewriteContext context) { context.Location = context.ResolveLocation(context.Expand(Location)); } /// <summary> /// The Processing directive. /// </summary> public virtual RewriteProcessing Processing { get { return RewriteProcessing.ContinueProcessing; } } private string _location; } } --- NEW FILE: SetStatusAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; using System.Web; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Sets the StatusCode. /// </summary> public class SetStatusAction : IRewriteAction { /// <summary> /// Default constructor. /// </summary> /// <param name="statusCode">The status code to set.</param> public SetStatusAction(HttpStatusCode statusCode) { _statusCode = statusCode; } /// <summary> /// The status code. /// </summary> public HttpStatusCode StatusCode { get { return _statusCode; } set { _statusCode = value; } } /// <summary> /// Executes the action. /// </summary> /// <param name="context">The rewriting context.</param> public virtual void Execute(RewriteContext context) { context.StatusCode = StatusCode; } /// <summary> /// The Processing directive. /// </summary> public RewriteProcessing Processing { get { if ((int)StatusCode >= 300) { return RewriteProcessing.StopProcessing; } else { return RewriteProcessing.ContinueProcessing; } } } private HttpStatusCode _statusCode; } } --- NEW FILE: ConditionalAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Collections; using Intelligencia.UrlRewriter.Conditions; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// A Conditional Action /// </summary> public class ConditionalAction : IRewriteAction, IRewriteCondition { /// <summary> /// Default constructor. /// </summary> public ConditionalAction() { } /// <summary> /// Conditions that must hold for the rule to fire. /// </summary> public IList Conditions { get { return _conditions; } } /// <summary> /// Child rules. /// </summary> public IList Actions { get { return _actions; } } /// <summary> /// Determines if the action matches the current context. /// </summary> /// <param name="context">The context to match on.</param> /// <returns>True if the condition matches.</returns> public virtual bool IsMatch(RewriteContext context) { // Ensure the conditions are met. foreach (IRewriteCondition condition in Conditions) { if (!condition.IsMatch(context)) { return false; } } return true; } /// <summary> /// Executes the rule. /// </summary> /// <param name="context"></param> public virtual void Execute(RewriteContext context) { // Execute the actions. for (int i = 0; i < Actions.Count; i++) { IRewriteCondition condition = Actions[i] as IRewriteCondition; if (condition == null || condition.IsMatch(context)) { IRewriteAction action = Actions[i] as IRewriteAction; action.Execute(context); if (action.Processing != RewriteProcessing.ContinueProcessing) { _processing = action.Processing; return; } } } } /// <summary> /// Processing directive. /// </summary> public RewriteProcessing Processing { get { return _processing; } } private RewriteProcessing _processing = RewriteProcessing.ContinueProcessing; private ArrayList _actions = new ArrayList(); private ArrayList _conditions = new ArrayList(); } } --- NEW FILE: RewriteAction.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Web; using System.Collections; using Intelligencia.UrlRewriter.Conditions; namespace Intelligencia.UrlRewriter.Actions { /// <summary> /// Rewrites in-place. /// </summary> public sealed class RewriteAction : SetLocationAction, IRewriteCondition { /// <summary> /// Default constructor. /// </summary> /// <param name="location">The location to set.</param> /// <param name="processing">The processing directive.</param> public RewriteAction(string location, RewriteProcessing processing) : base(location) { _processing = processing; } /// <summary> /// Executes the action. /// </summary> /// <param name="context">The rewrite context.</param> public override void Execute(RewriteContext context) { base.Execute(context); } /// <summary> /// The Processing directive. /// </summary> public override RewriteProcessing Processing { get { return _processing; } } /// <summary> /// Determines if the rewrite rule matches. /// </summary> /// <param name="context"></param> /// <returns></returns> public bool IsMatch(RewriteContext context) { // Ensure the conditions are met. foreach (IRewriteCondition condition in Conditions) { if (!condition.IsMatch(context)) { return false; } } return true; } /// <summary> /// Conditions that must hold for the rule to fire. /// </summary> public IList Conditions { get { return _conditions; } } private ArrayList _conditions = new ArrayList(); private RewriteProcessing _processing; } } |