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.Runtime.Remoting.Messaging;
35 using System.Security.Permissions;
36 using System.Security.Principal;
37 using System.Threading;
38 using System.Web.Caching;
39 using System.Web.Configuration;
40 using System.Web.SessionState;
42 using System.Web.Util;
44 namespace System.Web {
46 public sealed class HttpContext : IServiceProvider {
47 internal HttpWorkerRequest WorkerRequest;
48 HttpApplication app_instance;
50 HttpResponse response;
51 HttpSessionState session_state;
52 HttpServerUtility server;
53 TraceContext trace_context;
56 bool skip_authorization = false;
60 object config_timeout;
62 DateTime time_stamp = DateTime.UtcNow;
64 public HttpContext (HttpWorkerRequest wr)
67 request = new HttpRequest (WorkerRequest, this);
68 response = new HttpResponse (WorkerRequest, this);
71 public HttpContext (HttpRequest request, HttpResponse response)
73 this.request = request;
74 this.response = response;
78 public Exception [] AllErrors {
83 if (errors is Exception){
84 Exception [] all = new Exception [1];
85 all [0] = (Exception) errors;
88 return (Exception []) (((ArrayList) errors).ToArray (typeof (Exception)));
92 public HttpApplicationState Application {
94 return HttpApplicationFactory.ApplicationState;
98 public HttpApplication ApplicationInstance {
104 app_instance = value;
111 return HttpRuntime.Cache;
116 // The "Current" property is set just after we have constructed it with
117 // the 'HttpContext (HttpWorkerRequest)' constructor.
119 public static HttpContext Current {
121 return (HttpContext) CallContext.GetData ("c");
125 CallContext.SetData ("c", value);
129 public Exception Error {
131 if (errors == null || (errors is Exception))
132 return (Exception) errors;
133 return (Exception) (((ArrayList) errors) [0]);
137 public IHttpHandler Handler {
147 public bool IsCustomErrorEnabled {
149 CustomErrorsConfig cfg = null;
151 cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
157 if (cfg.Mode == CustomErrorMode.On)
160 return (cfg.Mode == CustomErrorMode.RemoteOnly) &&
161 (Request.WorkerRequest.GetLocalAddress () != Request.UserHostAddress);
165 public bool IsDebuggingEnabled {
167 return CompilationConfiguration.GetInstance (this).Debug;
171 public IDictionary Items {
174 items = new Hashtable ();
179 public HttpRequest Request {
185 public HttpResponse Response {
191 public HttpServerUtility Server {
194 server = new HttpServerUtility (this);
199 public HttpSessionState Session {
201 return session_state;
205 public bool SkipAuthorization {
207 return skip_authorization;
210 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
212 skip_authorization = value;
216 public DateTime Timestamp {
218 return time_stamp.ToLocalTime ();
222 public TraceContext Trace {
224 if (trace_context == null)
225 trace_context = new TraceContext (this);
226 return trace_context;
230 public IPrincipal User {
235 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
241 public void AddError (Exception errorInfo)
248 if (errors is Exception){
249 l = new ArrayList ();
253 l = (ArrayList) errors;
257 public void ClearError ()
262 public static object GetAppConfig (string name)
264 object o = ConfigurationSettings.GetConfig (name);
269 public object GetConfig (string name)
271 return WebConfigurationSettings.GetConfig (name, this);
274 object IServiceProvider.GetService (Type service)
276 if (service == typeof (HttpWorkerRequest))
277 return WorkerRequest;
280 // We return everything out of properties in case
281 // they are dynamically computed in some form in the future.
283 if (service == typeof (HttpApplication))
284 return ApplicationInstance;
286 if (service == typeof (HttpRequest))
289 if (service == typeof (HttpResponse))
292 if (service == typeof (HttpSessionState))
295 if (service == typeof (HttpApplicationState))
298 if (service == typeof (IPrincipal))
301 if (service == typeof (Cache))
304 if (service == typeof (HttpContext))
307 if (service == typeof (IHttpHandler))
310 if (service == typeof (HttpServerUtility))
313 if (service == typeof (TraceContext))
319 public void RewritePath (string path)
321 int qmark = path.IndexOf ('?');
323 RewritePath (path.Substring (0, qmark), "", path.Substring (qmark + 1));
325 RewritePath (path, null, null);
328 public void RewritePath (string filePath, string pathInfo, string queryString)
330 filePath = UrlUtils.Combine (Request.BaseVirtualDir, filePath);
331 if (!filePath.StartsWith (HttpRuntime.AppDomainAppVirtualPath))
332 throw new HttpException (404, "The virtual path '" + filePath +
333 "' maps to another application.");
335 Request.SetCurrentExePath (filePath);
336 // A null pathInfo or queryString is ignored and previous values remain untouched
337 if (pathInfo != null)
338 Request.SetPathInfo (pathInfo);
340 if (queryString != null)
341 Request.QueryStringRaw = queryString;
346 internal void SetSession (HttpSessionState state)
348 session_state = state;
351 // URL of a page used for error redirection.
352 internal string ErrorPage {
362 internal TimeSpan ConfigTimeout {
364 if (config_timeout == null) {
365 HttpRuntimeConfig config = (HttpRuntimeConfig)
366 GetConfig ("system.web/httpRuntime");
367 config_timeout = new TimeSpan (0, 0, config.ExecutionTimeout);
370 return (TimeSpan) config_timeout;
374 config_timeout = value;
378 internal bool CheckIfTimeout (DateTime t)
380 if (Interlocked.CompareExchange (ref timeout_possible, 0, 0) == 0)
383 TimeSpan ts = t - time_stamp;
384 return (ts > ConfigTimeout);
387 internal bool TimeoutPossible {
388 get { return (Interlocked.CompareExchange (ref timeout_possible, 1, 1) == 1); }
391 internal void BeginTimeoutPossible ()
393 timeout_possible = 1;
396 internal void EndTimeoutPossible ()
398 Interlocked.CompareExchange (ref timeout_possible, 0, 1);
405 internal Page LastPage {