From: Jaben C. <ja...@us...> - 2007-02-08 00:49:49
|
Update of /cvsroot/yafdotnet/yafsrc/URLRewriter.NET/Utilities In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv25871/yafsrc/URLRewriter.NET/Utilities Added Files: Tag: v1_0_2_NETv2 Constants.cs HttpContextFacade.cs IContextFacade.cs IPRange.cs Message.cs MessageProvider.cs TypeHelper.cs Log Message: URL Rewriter class --- NEW FILE: Message.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; namespace Intelligencia.UrlRewriter.Utilities { /// <summary> /// Message ids /// </summary> internal enum Message { AttributeNotAllowed, ElementNotAllowed, ElementNoAttributes, ElementNoElements, MappedValuesNotAllowed, ValueOfProcessingAttribute, AttributeRequired, FullTypeNameRequiresAssemblyName, AssemblyNameRequired, TypeNameRequired, MapAlreadyDefined, InvalidTypeSpecified, InputIsNotHex, AddressesNotOfSameType, ProductName, StoppingBecauseOfRule, RestartingBecauseOfRule, ResultNotFound, CallingErrorHandler, RewritingXtoY, RedirectingXtoY, TooManyRestarts } } --- NEW FILE: IContextFacade.cs --- using System; using System.Web; using System.Collections.Specialized; namespace Intelligencia.UrlRewriter.Utilities { /// <summary> /// Interface for a facade to the context. Useful for plugging out the HttpContext object /// in unit tests. /// </summary> public interface IContextFacade { /// <summary> /// Retrieves the application path. /// </summary> /// <returns>The application path.</returns> string GetApplicationPath(); /// <summary> /// Retrieves the raw url. /// </summary> /// <returns>The raw url.</returns> string GetRawUrl(); /// <summary> /// Retrieves the current request url. /// </summary> /// <returns>The request url.</returns> Uri GetRequestUrl(); /// <summary> /// Maps the url to the local file path. /// </summary> /// <param name="url">The url to map.</param> /// <returns>The local file path.</returns> string MapPath(string url); /// <summary> /// Sets the status code for the response. /// </summary> /// <param name="code">The status code.</param> void SetStatusCode(int code); /// <summary> /// Rewrites the request to the new url. /// </summary> /// <param name="url">The new url to rewrite to.</param> void RewritePath(string url); /// <summary> /// Sets the redirection location to the given url. /// </summary> /// <param name="url">The url of the redirection location.</param> void SetRedirectLocation(string url); /// <summary> /// Appends a header to the response. /// </summary> /// <param name="name">The header name.</param> /// <param name="value">The header value.</param> void AppendHeader(string name, string value); /// <summary> /// Adds a cookie to the response. /// </summary> /// <param name="cookie">The cookie to add.</param> void AppendCookie(HttpCookie cookie); /// <summary> /// Handles an error with the error handler. /// </summary> /// <param name="handler">The error handler to use.</param> void HandleError(IRewriteErrorHandler handler); /// <summary> /// Sets a context item. /// </summary> /// <param name="item">The item key</param> /// <param name="value">The item value</param> void SetItem(object item, object value); /// <summary> /// Retrieves a context item. /// </summary> /// <param name="item">The item key.</param> /// <returns>The item value.</returns> object GetItem(object item); /// <summary> /// Retrieves the HTTP method used by the request. /// </summary> /// <returns>The HTTP method.</returns> string GetHttpMethod(); /// <summary> /// Gets a collection of server variables. /// </summary> /// <returns></returns> NameValueCollection GetServerVariables(); /// <summary> /// Gets a collection of headers. /// </summary> /// <returns></returns> NameValueCollection GetHeaders(); /// <summary> /// Gets a collection of cookies. /// </summary> /// <returns></returns> HttpCookieCollection GetCookies(); } } --- NEW FILE: HttpContextFacade.cs --- using System; using System.Web; using System.Collections.Specialized; namespace Intelligencia.UrlRewriter.Utilities { /// <summary> /// A naive pass-through implementation of the ContextFacade on the HttpContext. /// Mock implementations would want to do something more interesting like implement checks that /// the actions were called. /// </summary> public class HttpContextFacade : IContextFacade { /// <summary> /// Retrieves the application path. /// </summary> /// <returns>The application path.</returns> public string GetApplicationPath() { return HttpContext.Current.Request.ApplicationPath; } /// <summary> /// Retrieves the raw url. /// </summary> /// <returns>The raw url.</returns> public string GetRawUrl() { return HttpContext.Current.Request.RawUrl; } /// <summary> /// Retrieves the current request url. /// </summary> /// <returns>The request url.</returns> public Uri GetRequestUrl() { return HttpContext.Current.Request.Url; } /// <summary> /// Maps the url to the local file path. /// </summary> /// <param name="url">The url to map.</param> /// <returns>The local file path.</returns> public string MapPath(string url) { return HttpContext.Current.Server.MapPath(url); } /// <summary> /// Sets the status code for the response. /// </summary> /// <param name="code">The status code.</param> public void SetStatusCode(int code) { HttpContext.Current.Response.StatusCode = code; } /// <summary> /// Rewrites the request to the new url. /// </summary> /// <param name="url">The new url to rewrite to.</param> public void RewritePath(string url) { HttpContext.Current.RewritePath(url); } /// <summary> /// Sets the redirection location to the given url. /// </summary> /// <param name="url">The url of the redirection location.</param> public void SetRedirectLocation(string url) { HttpContext.Current.Response.RedirectLocation = url; } /// <summary> /// Appends a header to the response. /// </summary> /// <param name="name">The header name.</param> /// <param name="value">The header value.</param> public void AppendHeader(string name, string value) { HttpContext.Current.Response.AppendHeader(name, value); } /// <summary> /// Adds a cookie to the response. /// </summary> /// <param name="cookie">The cookie to add.</param> public void AppendCookie(HttpCookie cookie) { HttpContext.Current.Response.AppendCookie(cookie); } /// <summary> /// Handles an error with the error handler. /// </summary> /// <param name="handler">The error handler to use.</param> public void HandleError(IRewriteErrorHandler handler) { handler.HandleError(HttpContext.Current); } /// <summary> /// Sets a context item. /// </summary> /// <param name="item">The item key</param> /// <param name="value">The item value</param> public void SetItem(object item, object value) { HttpContext.Current.Items[item] = value; } /// <summary> /// Retrieves a context item. /// </summary> /// <param name="item">The item key.</param> /// <returns>The item value.</returns> public object GetItem(object item) { return HttpContext.Current.Items[item]; } /// <summary> /// Retrieves the HTTP method used by the request. /// </summary> /// <returns>The HTTP method.</returns> public string GetHttpMethod() { return HttpContext.Current.Request.HttpMethod; } /// <summary> /// Gets a collection of server variables. /// </summary> /// <returns></returns> public NameValueCollection GetServerVariables() { return HttpContext.Current.Request.ServerVariables; } /// <summary> /// Gets a collection of headers. /// </summary> /// <returns></returns> public NameValueCollection GetHeaders() { return HttpContext.Current.Request.Headers; } /// <summary> /// Gets a collection of cookies. /// </summary> /// <returns></returns> public HttpCookieCollection GetCookies() { return HttpContext.Current.Request.Cookies; } } } --- NEW FILE: TypeHelper.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; namespace Intelligencia.UrlRewriter.Utilities { /// <summary> /// Helper class for dealing with types. /// </summary> internal sealed class TypeHelper { private TypeHelper() { } /// <summary> /// Loads and activates a type /// </summary> /// <param name="fullTypeName">The full name of the type to activate "TypeName, AssemblyName"</param> /// <param name="args">Arguments to pass to the constructor</param> /// <returns>The object</returns> public static object Activate(string fullTypeName, object[] args) { string[] components = fullTypeName.Split(",".ToCharArray(), 2); if (components.Length != 2) { throw new ArgumentOutOfRangeException("fullTypeName", fullTypeName, MessageProvider.FormatString(Message.FullTypeNameRequiresAssemblyName)); } return Activate(components[1].Trim(), components[0].Trim(), args); } /// <summary> /// Loads and activates a type /// </summary> /// <param name="assemblyName">The assembly name</param> /// <param name="typeName">The type name</param> /// <param name="args">Arguments to pass to the constructor</param> /// <returns>The object</returns> public static object Activate(string assemblyName, string typeName, object[] args) { if (assemblyName.Length == 0) { throw new ArgumentOutOfRangeException("assembly", assemblyName, MessageProvider.FormatString(Message.AssemblyNameRequired)); } if (typeName.Length == 0) { throw new ArgumentOutOfRangeException("typeName", typeName, MessageProvider.FormatString(Message.TypeNameRequired)); } return AppDomain.CurrentDomain.CreateInstanceAndUnwrap(assemblyName, typeName, false, 0, null, args, null, null, null); } } } --- NEW FILE: MessageProvider.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; using System.Resources; using System.Reflection; using System.Collections; namespace Intelligencia.UrlRewriter.Utilities { /// <summary> /// Message provider. /// </summary> internal sealed class MessageProvider { private MessageProvider() { } public static string FormatString(Message message, params object[] args) { string format; if (_messageCache.ContainsKey(message)) { format = (string)_messageCache[message]; } else { ResourceManager resources = new ResourceManager(Constants.Messages, Assembly.GetExecutingAssembly()); format = resources.GetString(message.ToString()); _messageCache.Add(message, format); } return String.Format(format, args); } private static Hashtable _messageCache = new Hashtable(); } } --- NEW FILE: IPRange.cs --- using System; using System.Net; using System.Text.RegularExpressions; namespace Intelligencia.UrlRewriter.Utilities { /// <summary> /// Represents a range of IP addresses. /// </summary> public sealed class IPRange { /// <summary> /// Constructor. /// </summary> /// <param name="address">A range of 1 ip address.</param> public IPRange(IPAddress address) { _minimumAddress = address; _maximumAddress = address; } /// <summary> /// Constructor. /// </summary> /// <param name="minimumAddress">Lowest IP address.</param> /// <param name="maximumAddress">Highest IP address.</param> public IPRange(IPAddress minimumAddress, IPAddress maximumAddress) { if (IPRange.Compare(minimumAddress, maximumAddress) == -1) { _minimumAddress = minimumAddress; _maximumAddress = maximumAddress; } else { _minimumAddress = maximumAddress; _maximumAddress = minimumAddress; } } /// <summary> /// Parses an IP address range. /// </summary> /// <remarks> /// ddd.ddd.ddd.ddd - single IP address /// ddd.ddd.ddd.* - class C range /// ddd.ddd.* - class B range /// ddd.* - class A range /// ddd.ddd.ddd.ddd - ccc.ccc.ccc.ccc - specific range /// </remarks> /// <param name="pattern">The pattern</param> /// <returns>The IPRange instance.</returns> public static IPRange Parse(string pattern) { pattern = Regex.Replace(pattern, @"([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\.\*", @"$1.0-$1.255"); pattern = Regex.Replace(pattern, @"([0-9]{1,3}\.[0-9]{1,3})\.\*", @"$1.0.0-$1.255.255"); pattern = Regex.Replace(pattern, @"([0-9]{1,3})\.\*", @"$1.0.0.0-$1.255.255.255"); string[] parts = pattern.Split('-'); if (parts.Length > 1) { return new IPRange(IPAddress.Parse(parts[0].Trim()), IPAddress.Parse(parts[1].Trim())); } else { return new IPRange(IPAddress.Parse(pattern.Trim())); } } /// <summary> /// Deteremines if the given IP address is in the range. /// </summary> /// <param name="address">The IP address.</param> /// <returns>True if the address is in the range.</returns> public bool InRange(IPAddress address) { return IPRange.Compare(MinimumAddress, address) <= 0 && IPRange.Compare(address, MaximumAddress) <= 0; } /// <summary> /// Minimum address (inclusive). /// </summary> public IPAddress MinimumAddress { get { return _minimumAddress; } } /// <summary> /// Maximum address (inclusive). /// </summary> public IPAddress MaximumAddress { get { return _maximumAddress; } } /// <summary> /// Compares two IPAddresses. /// Less than zero {left} is less than {right}. /// Zero {left} equals {right}. /// Greater than zero {left} is greater than {right}. /// </summary> /// <param name="left"></param> /// <param name="right"></param> /// <returns></returns> public static int Compare(IPAddress left, IPAddress right) { byte[] leftBytes = left.GetAddressBytes(); byte[] rightBytes = right.GetAddressBytes(); if (leftBytes.Length != rightBytes.Length) { throw new ArgumentOutOfRangeException(MessageProvider.FormatString(Message.AddressesNotOfSameType)); } for (int i = 0; i < leftBytes.Length; i++) { if (leftBytes[i] < rightBytes[i]) return -1; else if (leftBytes[i] > rightBytes[i]) return 1; } return 0; } private IPAddress _minimumAddress; private IPAddress _maximumAddress; } } --- NEW FILE: Constants.cs --- // UrlRewriter - A .NET URL Rewriter module // Version 1.7 // // Copyright 2006 Intelligencia // Copyright 2006 Seth Yates // using System; namespace Intelligencia.UrlRewriter.Utilities { /// <summary> /// Constants for the parser. /// </summary> internal sealed class Constants { private Constants() { } public static readonly string Messages = "Intelligencia.UrlRewriter.Messages"; public static readonly string RewriterNode = "rewriter"; public static readonly string RemoteAddressHeader = "REMOTE_ADDR"; public static readonly string Localhost = "localhost"; public static readonly string AttributeAction = "{0}-{1}"; public static readonly string HeaderXPoweredBy = "X-Powered-By"; public static readonly string AttrID = "id"; public static readonly string AttrAction = "action"; public static readonly string AttrExists = "exists"; public static readonly string AttrFile = "file"; public static readonly string AttrAddress = "address"; public static readonly string AttrHeader = "header"; public static readonly string AttrMethod = "method"; public static readonly string AttrMatch = "match"; public static readonly string AttrValue = "value"; public static readonly string AttrProperty = "property"; public static readonly string AttrStatus = "status"; public static readonly string AttrCookie = "cookie"; public static readonly string AttrRewrite = "rewrite"; public static readonly string AttrRedirect = "redirect"; public static readonly string AttrProcessing = "processing"; public static readonly string AttrPermanent = "permanent"; public static readonly string AttrValueContinue = "continue"; public static readonly string AttrValueRestart = "restart"; public static readonly string AttrValueStop = "stop"; public static readonly string AttrFrom = "from"; public static readonly string AttrName = "name"; public static readonly string AttrTo = "to"; public static readonly string AttrType = "type"; public static readonly string AttrVerb = "verb"; public static readonly string AttrCode = "code"; public static readonly string AttrUrl = "url"; public static readonly string AttrParser = "parser"; public static readonly string AttrTransform = "transform"; public static readonly string AttrLogger = "logger"; public static readonly string ElementIf = "if"; public static readonly string ElementIfNot = "ifnot"; public static readonly string ElementAnd = "and"; public static readonly string ElementAdd = "add"; public static readonly string ElementSet = "set"; public static readonly string ElementErrorHandler = "error-handler"; public static readonly string ElementForbidden = "forbidden"; public static readonly string ElementNotImplemented = "not-implemented"; public static readonly string ElementNotAllowed = "not-allowed"; public static readonly string ElementGone = "gone"; public static readonly string ElementNotFound = "not-found"; public static readonly string ElementRewrite = "rewrite"; public static readonly string ElementRedirect = "redirect"; public static readonly string ElementMap = "map"; public static readonly string ElementMapping = "mapping"; public static readonly string ElementDomain = "domain"; public static readonly string ElementServer = "server"; public static readonly string ElementVersion = "version"; public static readonly string ElementRegister = "register"; public static readonly string ElementExpires = "expires"; public static readonly string TransformDecode = "decode"; public static readonly string TransformEncode = "encode"; public static readonly string TransformBase64 = "base64"; public static readonly string TransformBase64Decode = "base64decode"; public static readonly string TransformLower = "lower"; public static readonly string TransformUpper = "upper"; } } |