void RewritePath(string filePath, string pathInfo, string queryString, bool setClientFilePath) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (!VirtualPathUtility.IsValidVirtualPath(filePath)) { throw new HttpException("'" + HttpUtility.HtmlEncode(filePath) + "' is not a valid virtual path."); } bool pathRelative = VirtualPathUtility.IsAppRelative(filePath); bool pathAbsolute = pathRelative ? false : VirtualPathUtility.IsAbsolute(filePath); HttpRequest req = Request; if (req == null) { return; } if (pathRelative || pathAbsolute) { if (pathRelative) { filePath = VirtualPathUtility.ToAbsolute(filePath); } else { filePath = filePath; } } else { filePath = VirtualPathUtility.AppendTrailingSlash(req.BaseVirtualDir) + filePath; } if (!StrUtils.StartsWith(filePath, HttpRuntime.AppDomainAppVirtualPath)) { throw new HttpException(404, "The virtual path '" + HttpUtility.HtmlEncode(filePath) + "' maps to another application.", filePath); } req.SetCurrentExePath(filePath); req.SetFilePath(filePath); if (setClientFilePath) { req.ClientFilePath = filePath; } // A null pathInfo or queryString is ignored and previous values remain untouched if (pathInfo != null) { req.SetPathInfo(pathInfo); } if (queryString != null) { req.QueryStringRaw = queryString; } }
internal void Execute(IHttpHandler handler, TextWriter writer, bool preserveForm, string exePath, string queryString, bool isTransfer, bool isInclude) { #if !TARGET_J2EE // If the target handler is not Page, the transfer must not occur. // InTransit == true means we're being called from Transfer bool is_static = (handler is StaticFileHandler); if (isTransfer && !(handler is Page) && !is_static) { throw new HttpException("Transfer is only allowed to .aspx and static files"); } #endif HttpRequest request = context.Request; string oldQuery = request.QueryStringRaw; if (queryString != null) { request.QueryStringRaw = queryString; } else if (!preserveForm) { request.QueryStringRaw = String.Empty; } HttpResponse response = context.Response; WebROCollection oldForm = request.Form as WebROCollection; if (!preserveForm) { request.SetForm(new WebROCollection()); } TextWriter output = writer; if (output == null) { output = response.Output; } TextWriter previous = response.SetTextWriter(output); string oldExePath = request.CurrentExecutionFilePath; bool oldIsInclude = context.IsProcessingInclude; try { context.PushHandler(handler); if (is_static) // Not sure if this should apply to Page too { request.SetFilePath(exePath); } request.SetCurrentExePath(exePath); context.IsProcessingInclude = isInclude; if (!(handler is IHttpAsyncHandler)) { handler.ProcessRequest(context); } else { IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler)handler; IAsyncResult ar = asyncHandler.BeginProcessRequest(context, null, null); WaitHandle asyncWaitHandle = ar != null ? ar.AsyncWaitHandle : null; if (asyncWaitHandle != null) { asyncWaitHandle.WaitOne(); } asyncHandler.EndProcessRequest(ar); } } finally { if (oldQuery != request.QueryStringRaw) { if (oldQuery != null && oldQuery.Length > 0) { oldQuery = oldQuery.Substring(1); // Ignore initial '?' request.QueryStringRaw = oldQuery; // which is added here. } else { request.QueryStringRaw = String.Empty; } } response.SetTextWriter(previous); if (!preserveForm) { request.SetForm(oldForm); } context.PopHandler(); request.SetCurrentExePath(oldExePath); context.IsProcessingInclude = oldIsInclude; } }