From: Jaben C. <ja...@us...> - 2007-02-08 00:49:44
|
Update of /cvsroot/yafdotnet/yafsrc/URLRewriter.NET/Conditions In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv25871/yafsrc/URLRewriter.NET/Conditions Added Files: Tag: v1_0_2_NETv2 AddressCondition.cs ExistsCondition.cs IRewriteCondition.cs MatchCondition.cs MethodCondition.cs PropertyMatchCondition.cs UrlMatchCondition.cs Log Message: URL Rewriter class --- NEW FILE: ExistsCondition.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.IO; using System.Net; using System.Web; using System.Text.RegularExpressions; using Intelligencia.UrlRewriter.Utilities; namespace Intelligencia.UrlRewriter.Conditions { /// <summary> /// Condition that tests the existence of a file. /// </summary> public class ExistsCondition : IRewriteCondition { /// <summary> /// Constructor. /// </summary> /// <param name="location"></param> public ExistsCondition(string location) { _location = location; } /// <summary> /// Determines if the condition is matched. /// </summary> /// <param name="context">The rewriting context.</param> /// <returns>True if the condition is met.</returns> public bool IsMatch(RewriteContext context) { string filename = context.MapPath(context.Expand(_location)); return File.Exists(filename) || Directory.Exists(filename); } private string _location; } } --- NEW FILE: UrlMatchCondition.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Web; using System.Text.RegularExpressions; namespace Intelligencia.UrlRewriter.Conditions { /// <summary> /// Matches on the current URL. /// </summary> public sealed class UrlMatchCondition : IRewriteCondition { /// <summary> /// Default constructor. /// </summary> /// <param name="pattern"></param> public UrlMatchCondition(string pattern) { _pattern = pattern; } /// <summary> /// Determines if the condition is matched. /// </summary> /// <param name="context">The rewriting context.</param> /// <returns>True if the condition is met.</returns> public bool IsMatch(RewriteContext context) { if (_regex == null) { _regex = new Regex(context.ResolveLocation(Pattern), RegexOptions.IgnoreCase); } Match match = _regex.Match(context.Location); if (match.Success) { context.LastMatch = match; return true; } else { return false; } } /// <summary> /// The pattern to match. /// </summary> public string Pattern { get { return _pattern; } set { _pattern = value; } } private Regex _regex; private string _pattern; } } --- NEW FILE: IRewriteCondition.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 conditions. /// </summary> public interface IRewriteCondition { /// <summary> /// Determines if the condition matches. /// </summary> /// <param name="context">The rewrite context.</param> /// <returns>True if the condition is met.</returns> bool IsMatch(RewriteContext context); } } --- NEW FILE: PropertyMatchCondition.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Text.RegularExpressions; namespace Intelligencia.UrlRewriter.Conditions { /// <summary> /// Performs a property match. /// </summary> public sealed class PropertyMatchCondition : MatchCondition { /// <summary> /// Default constructor. /// </summary> /// <param name="propertyName"></param> /// <param name="pattern"></param> public PropertyMatchCondition(string propertyName, string pattern) : base(pattern) { _propertyName = propertyName; } /// <summary> /// The property name. /// </summary> public string PropertyName { get { return _propertyName; } set { _propertyName = value; } } /// <summary> /// Determines if the condition is matched. /// </summary> /// <param name="context">The rewriting context.</param> /// <returns>True if the condition is met.</returns> public override bool IsMatch(RewriteContext context) { string property = context.Properties[PropertyName]; if (property != null) { Match match = Pattern.Match(property); if (match.Success) { context.LastMatch = match; return true; } else { return false; } } else { return false; } } private string _propertyName = String.Empty; } } --- NEW FILE: MethodCondition.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Text.RegularExpressions; namespace Intelligencia.UrlRewriter.Conditions { /// <summary> /// Matches on the current method. /// </summary> public sealed class MethodCondition : MatchCondition { /// <summary> /// Default constructor. /// </summary> /// <param name="pattern"></param> public MethodCondition(string pattern) : base(GetMethodPattern(pattern)) { } /// <summary> /// Determines if the condition is matched. /// </summary> /// <param name="context">The rewriting context.</param> /// <returns>True if the condition is met.</returns> public override bool IsMatch(RewriteContext context) { return Pattern.IsMatch(context.Method); } private static string GetMethodPattern(string method) { // Convert the GET, POST, * to ^GET|POST|.+$ return String.Format("^{0}$", Regex.Replace(method, @"[^a-zA-Z,\*]+", "").Replace(",", "|").Replace("*", ".+")); } } } --- NEW FILE: AddressCondition.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Net; using System.Text.RegularExpressions; using Intelligencia.UrlRewriter.Utilities; namespace Intelligencia.UrlRewriter.Conditions { /// <summary> /// Matches on the current remote IP address. /// </summary> public sealed class AddressCondition : IRewriteCondition { /// <summary> /// Default constructor. /// </summary> /// <param name="pattern"></param> public AddressCondition(string pattern) { _range = IPRange.Parse(pattern); } /// <summary> /// Determines if the condition is matched. /// </summary> /// <param name="context">The rewriting context.</param> /// <returns>True if the condition is met.</returns> public bool IsMatch(RewriteContext context) { string ipAddress = context.Properties[Constants.RemoteAddressHeader]; if (ipAddress != null) { return _range.InRange(IPAddress.Parse(ipAddress)); } else { return false; } } private IPRange _range; } } --- NEW FILE: MatchCondition.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Text.RegularExpressions; namespace Intelligencia.UrlRewriter.Conditions { /// <summary> /// Base class for MatchConditions. /// </summary> public abstract class MatchCondition : IRewriteCondition { /// <summary> /// Default constructor. /// </summary> /// <param name="pattern">Pattern to match.</param> public MatchCondition(string pattern) { _pattern = new Regex(pattern, RegexOptions.IgnoreCase); } /// <summary> /// Whether the MatchCondition is to be negated. /// </summary> public bool Negative { get { return _negative; } set { _negative = value; } } /// <summary> /// The pattern to match. /// </summary> public Regex Pattern { get { return _pattern; } set { _pattern = value; } } /// <summary> /// Determines if the condition is matched. /// </summary> /// <param name="context">The rewriting context.</param> /// <returns>True if the condition is met.</returns> public abstract bool IsMatch(RewriteContext context); private bool _negative = false; private Regex _pattern; } } |