2 // System.Web.HttpContext
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2003 Novell, Inc. (http://www.novell.com)
11 using System.Collections;
12 using System.Configuration;
13 using System.Security.Principal;
14 using System.Web.Caching;
15 using System.Web.Configuration;
16 using System.Web.Util;
17 using System.Web.SessionState;
18 using System.Threading;
22 public sealed class HttpContext : IServiceProvider
24 private ArrayList _arrExceptions;
26 private HttpResponse _oResponse;
27 private HttpRequest _oRequest;
28 private HttpServerUtility _Server;
29 private HttpApplication _oApplication;
30 private HttpSessionState _oSession;
31 private HttpWorkerRequest _oWorkerRequest;
32 private TraceContext _oTrace;
33 private IHttpHandler _Handler;
34 private IHttpAsyncHandler _AsyncHandler;
35 private IPrincipal _User;
37 private bool _skipauth;
38 private Hashtable _oItems;
39 private DateTime _oTimestamp;
45 public HttpContext (HttpRequest Request, HttpResponse Response)
49 _arrExceptions = null;
51 _oTimestamp = DateTime.Now;
53 _oResponse = Response;
54 _oTrace = new TraceContext (this);
57 public HttpContext (HttpWorkerRequest WorkerRequest)
61 _arrExceptions = null;
63 _oTimestamp = DateTime.Now;
64 _oRequest = new HttpRequest (WorkerRequest, this);
65 _oResponse = new HttpResponse (WorkerRequest, this);
66 _oWorkerRequest = WorkerRequest;
67 _oTrace = new TraceContext (this);
70 internal HttpWorkerRequest WorkerRequest
73 return _oWorkerRequest;
77 [MonoTODO("Context - Use System.Remoting.Messaging.CallContext instead of Thread storage")]
78 internal static HttpContext Context
81 return (HttpContext) Thread.GetData (Thread.GetNamedDataSlot ("Context"));
85 Thread.SetData (Thread.GetNamedDataSlot ("Context"), value);
89 public Exception [] AllErrors
92 if (_arrExceptions == null || _arrExceptions.Count == 0)
95 return (Exception []) _arrExceptions.ToArray (typeof (Exception));
99 public HttpApplicationState Application
102 return HttpApplicationFactory.ApplicationState;
106 public HttpApplication ApplicationInstance
109 return _oApplication;
112 _oApplication = value;
119 return HttpRuntime.Cache;
123 public static HttpContext Current {
124 get { return Context; }
126 set { Context = value; }
130 public Exception Error
133 if (_arrExceptions == null || _arrExceptions.Count == 0)
136 return (Exception) _arrExceptions [0];
140 public IHttpHandler Handler
151 internal IHttpAsyncHandler AsyncHandler
154 return _AsyncHandler;
158 _AsyncHandler = value;
162 public bool IsCustomErrorEnabled {
164 CustomErrorsConfig cfg;
166 cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
167 } catch (Exception) {
174 CustomErrorMode mode = cfg.Mode;
175 if (mode == CustomErrorMode.On)
178 return (mode == CustomErrorMode.RemoteOnly &&
179 _oRequest.ServerVariables ["LOCAL_ADDR"] == _oRequest.UserHostAddress);
183 public bool IsDebuggingEnabled
186 return CompilationConfiguration.GetInstance (this).Debug;
190 public IDictionary Items
194 _oItems = new Hashtable ();
200 public HttpRequest Request
207 public HttpResponse Response
214 public HttpServerUtility Server
218 _Server = new HttpServerUtility (this);
224 public HttpSessionState Session
227 return (HttpSessionState) _oSession;
231 public bool SkipAuthorization
242 public DateTime Timestamp
249 public TraceContext Trace
256 public IPrincipal User
266 internal bool TimeoutPossible {
267 get { return (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1); }
270 internal void BeginTimeoutPossible ()
273 timeoutBegin = DateTime.Now.Ticks;
276 internal void EndTimeoutPossible ()
278 Interlocked.CompareExchange (ref timeoutPossible, 0, 1);
281 internal void TryWaitForTimeout ()
283 while (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1) {
288 internal bool CheckIfTimeout (DateTime dt)
290 TimeSpan ts = new TimeSpan (dt.Ticks - timeoutBegin);
291 return (ts > ConfigTimeout);
294 internal TimeSpan ConfigTimeout {
296 if (configTimeout == null) {
297 HttpRuntimeConfig config = (HttpRuntimeConfig)
298 GetConfig ("system.web/httpRuntime");
299 configTimeout = new TimeSpan (0, 0, config.ExecutionTimeout);
302 return (TimeSpan) configTimeout;
305 configTimeout = value;
309 internal string ErrorPage {
310 get { return errorPage; }
311 set { errorPage = value; }
314 internal void SetSession (HttpSessionState session)
319 public void AddError (Exception errorInfo)
321 if (_arrExceptions == null)
322 _arrExceptions = new ArrayList ();
324 _arrExceptions.Add (errorInfo);
327 public void ClearError ()
329 _arrExceptions = null;
332 public object GetConfig (string name)
334 return WebConfigurationSettings.GetConfig (name, this);
337 public static object GetAppConfig (string name)
339 return WebConfigurationSettings.GetConfig (name);
342 object IServiceProvider.GetService (Type service)
344 if (service == typeof (HttpWorkerRequest))
345 return _oWorkerRequest;
347 if (service == typeof (HttpRequest))
350 if (service == typeof (HttpResponse))
353 if (service == typeof (HttpApplication))
354 return ApplicationInstance;
356 if (service == typeof (HttpApplicationState))
359 if (service == typeof (HttpSessionState))
362 if (service == typeof (HttpServerUtility))
368 public void RewritePath (string path)
370 //LAMESPEC: they say that they throw a ArgumentNullException, however,
371 // i get a NullReferenceException...
373 throw new ArgumentNullException ("path");
376 int qmark = path.IndexOf ('?');
377 if (qmark == -1 || qmark + 1 >= path.Length) {
380 query = path.Substring (qmark + 1);
381 path = path.Substring (0, qmark);
384 path = UrlUtils.Combine (Request.BaseVirtualDir, path);
385 if (!path.StartsWith (HttpRuntime.AppDomainAppVirtualPath))
386 throw new HttpException (404, "The virtual path '" + path +
387 "' maps to another application.");
389 Request.SetFilePath (path);
390 Request.QueryStringRaw = query;
394 public void RewritePath (string filePath, string pathInfo, string queryString)
396 RewritePath (filePath + "?" + queryString);
397 Request.SetPathInfo (pathInfo);