/// <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()); }
private void Context_PostResolveRequestCache(object sender, EventArgs e) { if (!(RpcManager.AppHost?.Config?.Service?.Paths?.Length > 0)) { return; } foreach (var item in RpcManager.AppHost?.Config?.Service?.Paths) { if (HttpContext.Current?.Request == null) { continue; } var start = HttpContext.Current?.Request.ApplicationPath?.Length > 1 ? HttpContext.Current.Request.ApplicationPath.Length + 1 : 1; if (HttpContext.Current.Request.Path.Length - start < item.Length) { continue; } var result = CultureInfo.InvariantCulture.CompareInfo.Compare( HttpContext.Current.Request.Path, start, item.Length, item, 0, item.Length, CompareOptions.OrdinalIgnoreCase); if (result == 0) { HttpContext.Current.RemapHandler(_factory.GetHandler(HttpContext.Current, null, null, null)); } } }
/// <summary> /// Same as <see cref="IHttpHandlerFactory.GetHandler"/> except the /// HTTP context is typed as <see cref="HttpContextBase"/> instead /// of <see cref="HttpContext"/>. /// </summary> public static IHttpHandler GetHandler(this IHttpHandlerFactory factory, HttpContextBase context, string requestType, string url, string pathTranslated) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } return(factory.GetHandler(context.ApplicationInstance.Context, requestType, url, pathTranslated)); }
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { IHttpHandlerFactory pageHandlerFactory = CreateFactory(); IHttpHandler pageHandler = pageHandlerFactory.GetHandler(context, requestType, url, pathTranslated); BuildUpPage(pageHandler); return(pageHandler); }
/// <summary> /// Create a handler instance for the given URL. /// </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> /// <returns>A handler instance for processing the current request.</returns> protected override IHttpHandler CreateHandlerInstance(IConfigurableApplicationContext appContext, HttpContext context, string requestType, string rawUrl, string physicalPath) { IHttpHandler handler = _innerFactory.GetHandler(context, requestType, rawUrl, physicalPath); // find a matching object definition string appRelativeVirtualPath = WebUtils.GetAppRelativePath(rawUrl); NamedObjectDefinition nod = FindWebObjectDefinition(appRelativeVirtualPath, appContext.ObjectFactory); string objectDefinitionName = (nod != null) ? nod.Name : rawUrl; handler = WebSupportModule.ConfigureHandler(context, handler, appContext, objectDefinitionName, (nod != null)); return(handler); }
//This function is invoked the first time a request is made to XAMLx file //It caches url as key and one of the //following 4 as value -> "Handler/Factory/HandlerCLRType/Exception" IHttpHandler GetHandlerFirstTime(HttpContext context, string requestType, string url, string pathTranslated) { Type httpHandlerType; ConfigurationErrorsException configException; //GetCompiledType is costly - invoke it just once. //This null check is required for "error after GetCompiledType on first attempt" cases only if (this.hostedXamlType == null) { this.hostedXamlType = GetCompiledCustomString(context.Request.AppRelativeCurrentExecutionFilePath); } if (XamlHostingConfiguration.TryGetHttpHandlerType(url, this.hostedXamlType, out httpHandlerType)) { if (TD.HttpHandlerPickedForUrlIsEnabled()) { TD.HttpHandlerPickedForUrl(url, hostedXamlType.FullName, httpHandlerType.FullName); } if (typeof(IHttpHandler).IsAssignableFrom(httpHandlerType)) { IHttpHandler handler = (IHttpHandler)CreateInstance(httpHandlerType); if (handler.IsReusable) { this.cachedResult = handler; } else { this.cachedResult = httpHandlerType; } return(handler); } else if (typeof(IHttpHandlerFactory).IsAssignableFrom(httpHandlerType)) { IHttpHandlerFactory factory = (IHttpHandlerFactory)CreateInstance(httpHandlerType); this.cachedResult = factory; IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated); return(HandlerWrapper.Create(handler, factory)); } else { configException = new ConfigurationErrorsException(SR.NotHttpHandlerType(url, this.hostedXamlType, httpHandlerType.FullName)); this.cachedResult = configException; throw FxTrace.Exception.AsError(configException); } } configException = new ConfigurationErrorsException(SR.HttpHandlerForXamlTypeNotFound(url, this.hostedXamlType, XamlHostingConfiguration.XamlHostingSection)); this.cachedResult = configException; throw FxTrace.Exception.AsError(configException); }
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { IHttpHandlerFactory pageFactory = CreatePageFactory(); IHttpHandler handler = pageFactory.GetHandler(context, requestType, url, pathTranslated); handler = Build(handler); Page page = handler as Page; if (page != null) { page.Init += new EventHandler(Page_Init); } return(page); }
public static void BCLPageHandlerFactoryBehaviorImpl() { using (TestWebContext ctx = new TestWebContext("/Test", "DoesNotExist.oaspx")) { try { IHttpHandlerFactory phf = (IHttpHandlerFactory)Activator.CreateInstance(typeof(System.Web.UI.Page).Assembly.GetType("System.Web.UI.PageHandlerFactory"), true); phf.GetHandler(HttpContext.Current, "GET", ctx.HttpWorkerRequest.GetFilePath(), ctx.HttpWorkerRequest.GetFilePathTranslated()); } catch (HttpException e) { Assert.AreEqual(404, e.GetHttpCode()); Assert.IsTrue(e.Message.IndexOf(ctx.HttpWorkerRequest.GetFilePath()) > 0); } } }
public void ProcessRequest(HttpContext context) { IHttpHandlerFactory fact = (IHttpHandlerFactory)Activator.CreateInstance(Type.GetType("System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35")); IHttpHandler handler = fact.GetHandler(context, context.Request.RequestType, context.Request.Path, context.Request.PhysicalApplicationPath); try { handler.ProcessRequest(context); context.Response.StatusCode = 200; context.ApplicationInstance.CompleteRequest(); } finally { fact.ReleaseHandler(handler); } }
private IHttpHandler GetHandlerSubSequent(HttpContext context, string requestType, string url, string pathTranslated) { if (this.cachedResult is IHttpHandler) { return((IHttpHandler)this.cachedResult); } if (this.cachedResult is IHttpHandlerFactory) { IHttpHandlerFactory cachedResult = (IHttpHandlerFactory)this.cachedResult; return(XamlHttpHandlerFactory.HandlerWrapper.Create(cachedResult.GetHandler(context, requestType, url, pathTranslated), cachedResult)); } if (!(this.cachedResult is Type)) { throw FxTrace.Exception.AsError((ConfigurationErrorsException)this.cachedResult); } return((IHttpHandler)XamlHttpHandlerFactory.CreateInstance((Type)this.cachedResult)); }
private IHttpHandler GetHandlerFirstTime(HttpContext context, string requestType, string url, string pathTranslated) { Type type; ConfigurationErrorsException exception; if (this.hostedXamlType == null) { this.hostedXamlType = this.GetCompiledCustomString(context.Request.AppRelativeCurrentExecutionFilePath); } if (XamlHostingConfiguration.TryGetHttpHandlerType(url, this.hostedXamlType, out type)) { if (TD.HttpHandlerPickedForUrlIsEnabled()) { TD.HttpHandlerPickedForUrl(url, this.hostedXamlType.FullName, type.FullName); } if (typeof(IHttpHandler).IsAssignableFrom(type)) { IHttpHandler handler = (IHttpHandler)XamlHttpHandlerFactory.CreateInstance(type); if (handler.IsReusable) { this.cachedResult = handler; return(handler); } this.cachedResult = type; return(handler); } if (typeof(IHttpHandlerFactory).IsAssignableFrom(type)) { IHttpHandlerFactory factory = (IHttpHandlerFactory)XamlHttpHandlerFactory.CreateInstance(type); this.cachedResult = factory; return(XamlHttpHandlerFactory.HandlerWrapper.Create(factory.GetHandler(context, requestType, url, pathTranslated), factory)); } exception = new ConfigurationErrorsException(System.Xaml.Hosting.SR.NotHttpHandlerType(url, this.hostedXamlType, type.FullName)); this.cachedResult = exception; throw FxTrace.Exception.AsError(exception); } exception = new ConfigurationErrorsException(System.Xaml.Hosting.SR.HttpHandlerForXamlTypeNotFound(url, this.hostedXamlType, "system.xaml.hosting/httpHandlers")); this.cachedResult = exception; throw FxTrace.Exception.AsError(exception); }
//This function retrievs the cached object and uses it to get handler or exception IHttpHandler GetHandlerSubSequent(HttpContext context, string requestType, string url, string pathTranslated) { if (this.cachedResult is IHttpHandler) { return((IHttpHandler)this.cachedResult); } else if (this.cachedResult is IHttpHandlerFactory) { IHttpHandlerFactory factory = ((IHttpHandlerFactory)this.cachedResult); IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated); return(HandlerWrapper.Create(handler, factory)); } else if (this.cachedResult is Type) { return((IHttpHandler)CreateInstance((Type)this.cachedResult)); } else { throw FxTrace.Exception.AsError((ConfigurationErrorsException)this.cachedResult); } }
// Used by HttpServerUtility.Execute internal IHttpHandler GetHandler (HttpContext context) { HttpRequest request = context.Request; string verb = request.RequestType; string url = request.FilePath; IHttpHandler handler = null; #if NET_2_0 HttpHandlersSection section = (HttpHandlersSection) WebConfigurationManager.GetSection ("system.web/httpHandlers"); object o = section.LocateHandler (verb, url); #else HandlerFactoryConfiguration factory_config = (HandlerFactoryConfiguration) HttpContext.GetAppConfig ("system.web/httpHandlers"); object o = factory_config.LocateHandler (verb, url); #endif factory = o as IHttpHandlerFactory; if (factory == null) { handler = (IHttpHandler) o; } else { handler = factory.GetHandler (context, verb, url, request.PhysicalPath); } context.Handler = handler; return handler; }