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;
48 namespace System.Web {
50 // CAS - no InheritanceDemand here as the class is sealed
51 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
52 public sealed class HttpContext : IServiceProvider {
53 internal HttpWorkerRequest WorkerRequest;
54 HttpApplication app_instance;
56 HttpResponse response;
57 HttpSessionState session_state;
58 HttpServerUtility server;
59 TraceContext trace_context;
62 bool skip_authorization = false;
66 object config_timeout;
68 DateTime time_stamp = DateTime.UtcNow;
69 #if TARGET_JVM // No remoting support (CallContext) yet in Grasshopper
70 static LocalDataStoreSlot _ContextSlot = Thread.GetNamedDataSlot ("Context");
73 public HttpContext (HttpWorkerRequest wr)
76 request = new HttpRequest (WorkerRequest, this);
77 response = new HttpResponse (WorkerRequest, this);
80 public HttpContext (HttpRequest request, HttpResponse response)
82 this.request = request;
83 this.response = response;
87 public Exception [] AllErrors {
92 if (errors is Exception){
93 Exception [] all = new Exception [1];
94 all [0] = (Exception) errors;
97 return (Exception []) (((ArrayList) errors).ToArray (typeof (Exception)));
101 public HttpApplicationState Application {
103 return HttpApplicationFactory.ApplicationState;
107 public HttpApplication ApplicationInstance {
113 app_instance = value;
120 return HttpRuntime.Cache;
125 // The "Current" property is set just after we have constructed it with
126 // the 'HttpContext (HttpWorkerRequest)' constructor.
128 #if TARGET_JVM // No remoting support (CallContext) yet in Grasshopper
129 [MonoTODO("Context - Use System.Remoting.Messaging.CallContext instead of Thread storage")]
130 public static HttpContext Current
132 get { return (HttpContext) Thread.GetData (_ContextSlot); }
133 set { Thread.SetData (_ContextSlot, value); }
136 public static HttpContext Current {
138 return (HttpContext) CallContext.GetData ("c");
142 CallContext.SetData ("c", value);
147 public Exception Error {
149 if (errors == null || (errors is Exception))
150 return (Exception) errors;
151 return (Exception) (((ArrayList) errors) [0]);
155 public IHttpHandler Handler {
165 public bool IsCustomErrorEnabled {
167 CustomErrorsConfig cfg = null;
169 cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
175 if (cfg.Mode == CustomErrorMode.On)
178 return (cfg.Mode == CustomErrorMode.RemoteOnly) &&
179 (Request.WorkerRequest.GetLocalAddress () != Request.UserHostAddress);
183 public bool IsDebuggingEnabled { get { return false; } }
185 public bool IsDebuggingEnabled {
188 return CompilationConfiguration.GetInstance (this).Debug;
195 public IDictionary Items {
198 items = new Hashtable ();
203 public HttpRequest Request {
209 public HttpResponse Response {
215 public HttpServerUtility Server {
218 server = new HttpServerUtility (this);
223 public HttpSessionState Session {
225 return session_state;
229 public bool SkipAuthorization {
231 return skip_authorization;
234 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
236 skip_authorization = value;
240 public DateTime Timestamp {
242 return time_stamp.ToLocalTime ();
246 public TraceContext Trace {
248 if (trace_context == null)
249 trace_context = new TraceContext (this);
250 return trace_context;
254 public IPrincipal User {
259 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
267 public IHttpHandler CurrentHandler {
268 get { throw new NotImplementedException (); }
272 public IHttpHandler PreviousHandler {
273 get { throw new NotImplementedException (); }
278 public ProfileBase Profile {
279 get { throw new NotImplementedException (); }
284 public void AddError (Exception errorInfo)
291 if (errors is Exception){
292 l = new ArrayList ();
296 l = (ArrayList) errors;
300 internal void ClearError (Exception e)
306 public void ClearError ()
312 [Obsolete ("see WebConfigurationManager")]
314 public static object GetAppConfig (string name)
316 object o = ConfigurationSettings.GetConfig (name);
322 [Obsolete ("see GetSection")]
324 public object GetConfig (string name)
326 return WebConfigurationSettings.GetConfig (name, this);
331 public static object GetGlobalResourceObject (string classKey, string resourceKey)
333 throw new NotImplementedException ();
337 public static object GetGlobalResourceObject (string classKey, string resourceKey, CultureInfo culture)
339 throw new NotImplementedException ();
343 public static object GetLocalResourceObject (string virtualPath, string resourceKey)
345 throw new NotImplementedException ();
349 public static object GetLocalResourceObject (string virtualPath, string resourceKey, CultureInfo culture)
351 throw new NotImplementedException ();
355 public object GetSection (string name)
357 throw new NotImplementedException ();
360 object IServiceProvider.GetService (Type service)
362 if (service == typeof (HttpWorkerRequest))
363 return WorkerRequest;
366 // We return everything out of properties in case
367 // they are dynamically computed in some form in the future.
369 if (service == typeof (HttpApplication))
370 return ApplicationInstance;
372 if (service == typeof (HttpRequest))
375 if (service == typeof (HttpResponse))
378 if (service == typeof (HttpSessionState))
381 if (service == typeof (HttpApplicationState))
384 if (service == typeof (IPrincipal))
387 if (service == typeof (Cache))
390 if (service == typeof (HttpContext))
393 if (service == typeof (IHttpHandler))
396 if (service == typeof (HttpServerUtility))
399 if (service == typeof (TraceContext))
405 public void RewritePath (string path)
407 int qmark = path.IndexOf ('?');
409 RewritePath (path.Substring (0, qmark), "", path.Substring (qmark + 1));
411 RewritePath (path, null, null);
414 public void RewritePath (string filePath, string pathInfo, string queryString)
416 filePath = UrlUtils.Combine (Request.BaseVirtualDir, filePath);
417 if (!filePath.StartsWith (HttpRuntime.AppDomainAppVirtualPath))
418 throw new HttpException (404, "The virtual path '" + filePath +
419 "' maps to another application.");
421 Request.SetCurrentExePath (filePath);
422 // A null pathInfo or queryString is ignored and previous values remain untouched
423 if (pathInfo != null)
424 Request.SetPathInfo (pathInfo);
426 if (queryString != null)
427 Request.QueryStringRaw = queryString;
432 public void RewritePath (string path, bool rebaseClientPath)
434 throw new NotImplementedException ();
440 internal void SetSession (HttpSessionState state)
442 session_state = state;
445 // URL of a page used for error redirection.
446 internal string ErrorPage {
456 internal TimeSpan ConfigTimeout {
458 if (config_timeout == null) {
459 HttpRuntimeConfig config = (HttpRuntimeConfig)
460 GetConfig ("system.web/httpRuntime");
461 config_timeout = new TimeSpan (0, 0, config.ExecutionTimeout);
464 return (TimeSpan) config_timeout;
468 config_timeout = value;
472 internal bool CheckIfTimeout (DateTime t)
474 if (Interlocked.CompareExchange (ref timeout_possible, 0, 0) == 0)
477 TimeSpan ts = t - time_stamp;
478 return (ts > ConfigTimeout);
481 internal bool TimeoutPossible {
482 get { return (Interlocked.CompareExchange (ref timeout_possible, 1, 1) == 1); }
485 internal void BeginTimeoutPossible ()
487 timeout_possible = 1;
490 internal void EndTimeoutPossible ()
492 Interlocked.CompareExchange (ref timeout_possible, 0, 1);
499 internal Page LastPage {