2 // System.Web.HttpContext.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Configuration;
34 using System.Globalization;
35 using System.Runtime.Remoting.Messaging;
36 using System.Security.Permissions;
37 using System.Security.Principal;
38 using System.Threading;
39 using System.Web.Caching;
40 using System.Web.Configuration;
41 using System.Web.SessionState;
43 using System.Web.Util;
45 using System.Web.Profile;
46 using CustomErrorMode = System.Web.Configuration.CustomErrorsMode;
49 namespace System.Web {
51 // CAS - no InheritanceDemand here as the class is sealed
52 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
53 public sealed class HttpContext : IServiceProvider {
54 internal HttpWorkerRequest WorkerRequest;
55 HttpApplication app_instance;
57 HttpResponse response;
58 HttpSessionState session_state;
59 HttpServerUtility server;
60 TraceContext trace_context;
63 bool skip_authorization = false;
67 object config_timeout;
69 DateTime time_stamp = DateTime.UtcNow;
70 #if TARGET_JVM // No remoting support (CallContext) yet in Grasshopper
71 static LocalDataStoreSlot _ContextSlot = Thread.GetNamedDataSlot ("Context");
74 public HttpContext (HttpWorkerRequest wr)
77 request = new HttpRequest (WorkerRequest, this);
78 response = new HttpResponse (WorkerRequest, this);
81 public HttpContext (HttpRequest request, HttpResponse response)
83 this.request = request;
84 this.response = response;
88 public Exception [] AllErrors {
93 if (errors is Exception){
94 Exception [] all = new Exception [1];
95 all [0] = (Exception) errors;
98 return (Exception []) (((ArrayList) errors).ToArray (typeof (Exception)));
102 public HttpApplicationState Application {
104 return HttpApplicationFactory.ApplicationState;
108 public HttpApplication ApplicationInstance {
114 app_instance = value;
121 return HttpRuntime.Cache;
126 // The "Current" property is set just after we have constructed it with
127 // the 'HttpContext (HttpWorkerRequest)' constructor.
129 #if TARGET_JVM // No remoting support (CallContext) yet in Grasshopper
130 [MonoTODO("Context - Use System.Remoting.Messaging.CallContext instead of Thread storage")]
131 public static HttpContext Current
133 get { return (HttpContext) Thread.GetData (_ContextSlot); }
134 set { Thread.SetData (_ContextSlot, value); }
137 public static HttpContext Current {
139 return (HttpContext) CallContext.GetData ("c");
143 CallContext.SetData ("c", value);
148 public Exception Error {
150 if (errors == null || (errors is Exception))
151 return (Exception) errors;
152 return (Exception) (((ArrayList) errors) [0]);
156 public IHttpHandler Handler {
166 public bool IsCustomErrorEnabled {
169 CustomErrorsSection cfg = (CustomErrorsSection) WebConfigurationManager.GetSection ("system.web/customErrors");
171 CustomErrorsConfig cfg = null;
173 cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
181 if (cfg.Mode == CustomErrorMode.On)
184 return (cfg.Mode == CustomErrorMode.RemoteOnly) &&
185 (Request.WorkerRequest.GetLocalAddress () != Request.UserHostAddress);
189 public bool IsDebuggingEnabled { get { return false; } }
191 public bool IsDebuggingEnabled {
194 CompilationSection section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
195 return section.Debug;
198 return CompilationConfiguration.GetInstance (this).Debug;
206 public IDictionary Items {
209 items = new Hashtable ();
214 public HttpRequest Request {
220 public HttpResponse Response {
226 public HttpServerUtility Server {
229 server = new HttpServerUtility (this);
234 public HttpSessionState Session {
236 return session_state;
240 public bool SkipAuthorization {
242 return skip_authorization;
245 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
247 skip_authorization = value;
251 public DateTime Timestamp {
253 return time_stamp.ToLocalTime ();
257 public TraceContext Trace {
259 if (trace_context == null)
260 trace_context = new TraceContext (this);
261 return trace_context;
265 public IPrincipal User {
270 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
278 public IHttpHandler CurrentHandler {
279 get { throw new NotImplementedException (); }
283 public IHttpHandler PreviousHandler {
284 get { throw new NotImplementedException (); }
288 public ProfileBase Profile {
289 get { throw new NotImplementedException (); }
293 public void AddError (Exception errorInfo)
300 if (errors is Exception){
301 l = new ArrayList ();
305 l = (ArrayList) errors;
309 internal void ClearError (Exception e)
315 public void ClearError ()
321 [Obsolete ("use WebConfigurationManager.GetWebApplicationSection")]
323 public static object GetAppConfig (string name)
325 object o = ConfigurationSettings.GetConfig (name);
331 [Obsolete ("see GetSection")]
333 public object GetConfig (string name)
336 return GetSection (name);
338 return WebConfigurationSettings.GetConfig (name, this);
344 public static object GetGlobalResourceObject (string classKey, string resourceKey)
346 throw new NotImplementedException ();
350 public static object GetGlobalResourceObject (string classKey, string resourceKey, CultureInfo culture)
352 throw new NotImplementedException ();
356 public static object GetLocalResourceObject (string virtualPath, string resourceKey)
358 throw new NotImplementedException ();
362 public static object GetLocalResourceObject (string virtualPath, string resourceKey, CultureInfo culture)
364 throw new NotImplementedException ();
368 public object GetSection (string name)
371 return WebConfigurationManager.GetSection (name);
373 throw new NotImplementedException ();
377 object IServiceProvider.GetService (Type service)
379 if (service == typeof (HttpWorkerRequest))
380 return WorkerRequest;
383 // We return everything out of properties in case
384 // they are dynamically computed in some form in the future.
386 if (service == typeof (HttpApplication))
387 return ApplicationInstance;
389 if (service == typeof (HttpRequest))
392 if (service == typeof (HttpResponse))
395 if (service == typeof (HttpSessionState))
398 if (service == typeof (HttpApplicationState))
401 if (service == typeof (IPrincipal))
404 if (service == typeof (Cache))
407 if (service == typeof (HttpContext))
410 if (service == typeof (IHttpHandler))
413 if (service == typeof (HttpServerUtility))
416 if (service == typeof (TraceContext))
422 public void RewritePath (string path)
424 int qmark = path.IndexOf ('?');
426 RewritePath (path.Substring (0, qmark), "", path.Substring (qmark + 1));
428 RewritePath (path, null, null);
431 public void RewritePath (string filePath, string pathInfo, string queryString)
433 filePath = UrlUtils.Combine (Request.BaseVirtualDir, filePath);
434 if (!filePath.StartsWith (HttpRuntime.AppDomainAppVirtualPath))
435 throw new HttpException (404, "The virtual path '" + filePath +
436 "' maps to another application.");
438 Request.SetCurrentExePath (filePath);
439 // A null pathInfo or queryString is ignored and previous values remain untouched
440 if (pathInfo != null)
441 Request.SetPathInfo (pathInfo);
443 if (queryString != null)
444 Request.QueryStringRaw = queryString;
449 public void RewritePath (string path, bool rebaseClientPath)
451 throw new NotImplementedException ();
457 internal void SetSession (HttpSessionState state)
459 session_state = state;
462 // URL of a page used for error redirection.
463 internal string ErrorPage {
473 internal TimeSpan ConfigTimeout {
475 if (config_timeout == null) {
477 HttpRuntimeSection section = (HttpRuntimeSection)WebConfigurationManager.GetSection ("system.web/httpRuntime");
478 config_timeout = section.ExecutionTimeout;
480 HttpRuntimeConfig config = (HttpRuntimeConfig)
481 GetConfig ("system.web/httpRuntime");
482 config_timeout = new TimeSpan (0, 0, config.ExecutionTimeout);
486 return (TimeSpan) config_timeout;
490 config_timeout = value;
494 internal bool CheckIfTimeout (DateTime t)
496 if (Interlocked.CompareExchange (ref timeout_possible, 0, 0) == 0)
499 TimeSpan ts = t - time_stamp;
500 return (ts > ConfigTimeout);
503 internal bool TimeoutPossible {
504 get { return (Interlocked.CompareExchange (ref timeout_possible, 1, 1) == 1); }
507 internal void BeginTimeoutPossible ()
509 timeout_possible = 1;
512 internal void EndTimeoutPossible ()
514 Interlocked.CompareExchange (ref timeout_possible, 0, 1);
521 internal Page LastPage {