/// <summary>
        /// Obtains a handler by mapping <paramref name="rawUrl"/> to the list of patterns in <paramref name="handlerMappings"/>.
        /// </summary>
        /// <param name="appContext">the application context corresponding to the current request</param>
        /// <param name="context">The <see cref="HttpContext"/> instance for this request.</param>
        /// <param name="requestType">The HTTP data transfer method (GET, POST, ...)</param>
        /// <param name="rawUrl">The requested <see cref="HttpRequest.RawUrl"/>.</param>
        /// <param name="physicalPath">The physical path of the requested resource.</param>
        /// <param name="handlerMappings"></param>
        /// <param name="handlerWithFactoryTable"></param>
        /// <returns>A handler instance for processing the current request.</returns>
        protected IHttpHandler MapHandlerInstance(IConfigurableApplicationContext appContext, HttpContext context, string requestType, string rawUrl, string physicalPath, HandlerMap handlerMappings, IDictionary handlerWithFactoryTable)
        {
            // resolve handler instance by mapping the url to the list of patterns
            HandlerMapEntry handlerMapEntry = handlerMappings.MapPath(rawUrl);

            if (handlerMapEntry == null)
            {
                throw new HttpException(404, HttpStatusCode.NotFound.ToString());
            }
            object handlerObject = appContext.GetObject(handlerMapEntry.HandlerObjectName);

            if (handlerObject is IHttpHandler)
            {
                return((IHttpHandler)handlerObject);
            }
            else if (handlerObject is IHttpHandlerFactory)
            {
                // keep a reference to the issuing factory for later ReleaseHandler call
                IHttpHandlerFactory factory = (IHttpHandlerFactory)handlerObject;
                IHttpHandler        handler = factory.GetHandler(context, requestType, rawUrl, physicalPath);
                lock (handlerWithFactoryTable.SyncRoot)
                {
                    handlerWithFactoryTable.Add(handler, factory);
                }
                return(handler);
            }

            throw new HttpException((int)HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString());
        }
Exemple #2
0
        /// <summary>
        /// Maps the <paramref name="virtualPath"/> to a handler object name by matching against all registered patterns.
        /// </summary>
        /// <param name="virtualPath">the virtual path</param>
        /// <returns>the object name</returns>
        public HandlerMapEntry MapPath(string virtualPath)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug(string.Format("looking up mapping for url '{0}'", virtualPath));
            }
            for (int i = 0; i < this._internalTable.Count; i++)
            {
                HandlerMapEntry handlerMapEntry = (HandlerMapEntry)this._internalTable[i];
                if (handlerMapEntry.UrlPattern.IsMatch(virtualPath))
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug(string.Format("found mapping '{0}' for url '{1}'", handlerMapEntry, virtualPath));
                    }
                    return(handlerMapEntry);
                }
            }

            if (Log.IsDebugEnabled)
            {
                Log.Debug(string.Format("no mapping found for url '{0}'", virtualPath));
            }
            return(null);
        }
        public void CanMatch_LeadingLiteralsPattern()
        {
            HandlerMap map = new HandlerMap();

            map.Add("/test1/*", "theHandlerIWant");

            HandlerMapEntry handler = map.MapPath("/test1/test2/default.aspx");

            Assert.NotNull(handler);
            Assert.AreEqual("theHandlerIWant", handler.HandlerObjectName);
        }