[corlib] Remove unused files
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / MvcHttpHandler.cs
1 namespace System.Web.Mvc {
2     using System;
3     using System.Web;
4     using System.Web.Mvc.Async;
5     using System.Web.Routing;
6     using System.Web.SessionState;
7
8     public class MvcHttpHandler : UrlRoutingHandler, IHttpAsyncHandler, IRequiresSessionState {
9
10         private static readonly object _processRequestTag = new object();
11
12         protected virtual IAsyncResult BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, object state) {
13             HttpContextBase iHttpContext = new HttpContextWrapper(httpContext);
14             return BeginProcessRequest(iHttpContext, callback, state);
15         }
16
17         protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state) {
18             IHttpHandler httpHandler = GetHttpHandler(httpContext);
19             IHttpAsyncHandler httpAsyncHandler = httpHandler as IHttpAsyncHandler;
20
21             if (httpAsyncHandler != null) {
22                 // asynchronous handler
23                 BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
24                     return httpAsyncHandler.BeginProcessRequest(HttpContext.Current, asyncCallback, asyncState);
25                 };
26                 EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) {
27                     httpAsyncHandler.EndProcessRequest(asyncResult);
28                 };
29                 return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _processRequestTag);
30             }
31             else {
32                 // synchronous handler
33                 Action action = delegate {
34                     httpHandler.ProcessRequest(HttpContext.Current);
35                 };
36                 return AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag);
37             }
38         }
39
40         protected internal virtual void EndProcessRequest(IAsyncResult asyncResult) {
41             AsyncResultWrapper.End(asyncResult, _processRequestTag);
42         }
43
44         private static IHttpHandler GetHttpHandler(HttpContextBase httpContext) {
45             DummyHttpHandler dummyHandler = new DummyHttpHandler();
46             dummyHandler.PublicProcessRequest(httpContext);
47             return dummyHandler.HttpHandler;
48         }
49
50         // synchronous code
51         protected override void VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext) {
52             if (httpHandler == null) {
53                 throw new ArgumentNullException("httpHandler");
54             }
55
56             httpHandler.ProcessRequest(HttpContext.Current);
57         }
58
59         #region IHttpAsyncHandler Members
60         IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) {
61             return BeginProcessRequest(context, cb, extraData);
62         }
63
64         void IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) {
65             EndProcessRequest(result);
66         }
67         #endregion
68
69         // Since UrlRoutingHandler.ProcessRequest() does the heavy lifting of looking at the RouteCollection for
70         // a matching route, we need to call into it. However, that method is also responsible for kicking off
71         // the synchronous request, and we can't allow it to do that. The purpose of this dummy class is to run
72         // only the lookup portion of UrlRoutingHandler.ProcessRequest(), then intercept the handler it returns
73         // and execute it asynchronously.
74
75         private sealed class DummyHttpHandler : UrlRoutingHandler {
76             public IHttpHandler HttpHandler;
77
78             public void PublicProcessRequest(HttpContextBase httpContext) {
79                 ProcessRequest(httpContext);
80             }
81
82             protected override void VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext) {
83                 // don't process the request, just store a reference to it
84                 HttpHandler = httpHandler;
85             }
86         }
87
88     }
89 }