1 namespace System.Web.Mvc {
3 using System.Diagnostics.CodeAnalysis;
5 using System.Web.Mvc.Resources;
8 internal static class HttpHandlerUtil {
10 // Since Server.Execute() doesn't propagate HttpExceptions where the status code is
11 // anything other than 500, we need to wrap these exceptions ourselves.
12 [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The Dispose on Page doesn't do anything by default, and we control both of these internal types.")]
13 public static IHttpHandler WrapForServerExecute(IHttpHandler httpHandler) {
14 IHttpAsyncHandler asyncHandler = httpHandler as IHttpAsyncHandler;
15 return (asyncHandler != null) ? new ServerExecuteHttpHandlerAsyncWrapper(asyncHandler) : new ServerExecuteHttpHandlerWrapper(httpHandler);
18 // Server.Execute() requires that the provided IHttpHandler subclass Page.
19 internal class ServerExecuteHttpHandlerWrapper : Page {
20 private readonly IHttpHandler _httpHandler;
22 public ServerExecuteHttpHandlerWrapper(IHttpHandler httpHandler) {
23 _httpHandler = httpHandler;
26 internal IHttpHandler InnerHandler {
32 public override void ProcessRequest(HttpContext context) {
33 Wrap(() => _httpHandler.ProcessRequest(context));
36 protected static void Wrap(Action action) {
43 protected static TResult Wrap<TResult>(Func<TResult> func) {
47 catch (HttpException he) {
48 if (he.GetHttpCode() == 500) {
49 throw; // doesn't need to be wrapped
52 HttpException newHe = new HttpException(500, MvcResources.ViewPageHttpHandlerWrapper_ExceptionOccurred, he);
59 private sealed class ServerExecuteHttpHandlerAsyncWrapper : ServerExecuteHttpHandlerWrapper, IHttpAsyncHandler {
60 private readonly IHttpAsyncHandler _httpHandler;
62 public ServerExecuteHttpHandlerAsyncWrapper(IHttpAsyncHandler httpHandler)
64 _httpHandler = httpHandler;
67 public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) {
68 return Wrap(() => _httpHandler.BeginProcessRequest(context, cb, extraData));
71 public void EndProcessRequest(IAsyncResult result) {
72 Wrap(() => _httpHandler.EndProcessRequest(result));