2 // System.Web.HttpContext
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2003, 2004 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.
33 using System
.Collections
;
34 using System
.Configuration
;
35 using System
.Security
.Principal
;
36 using System
.Web
.Caching
;
37 using System
.Web
.Configuration
;
38 using System
.Web
.Util
;
39 using System
.Web
.SessionState
;
40 using System
.Threading
;
41 using System
.Runtime
.Remoting
.Messaging
;
45 public sealed class HttpContext
: IServiceProvider
47 private ArrayList _arrExceptions
;
49 private HttpResponse _oResponse
;
50 private HttpRequest _oRequest
;
51 private HttpServerUtility _Server
;
52 private HttpApplication _oApplication
;
53 private HttpSessionState _oSession
;
54 private HttpWorkerRequest _oWorkerRequest
;
55 private TraceContext _oTrace
;
56 private IHttpHandler _Handler
;
57 private IHttpAsyncHandler _AsyncHandler
;
59 private bool _skipauth
;
60 private Hashtable _oItems
;
61 private DateTime _oTimestamp
;
68 public HttpContext (HttpRequest Request
, HttpResponse Response
)
72 _arrExceptions
= null;
74 _oTimestamp
= DateTime
.Now
;
76 _oResponse
= Response
;
77 _oTrace
= new TraceContext (this);
80 public HttpContext (HttpWorkerRequest WorkerRequest
)
84 _arrExceptions
= null;
86 _oTimestamp
= DateTime
.Now
;
87 _oRequest
= new HttpRequest (WorkerRequest
, this);
88 _oResponse
= new HttpResponse (WorkerRequest
, this);
89 _oWorkerRequest
= WorkerRequest
;
90 _oTrace
= new TraceContext (this);
93 internal HttpWorkerRequest WorkerRequest
96 return _oWorkerRequest
;
100 internal static HttpContext Context
103 return (HttpContext
) CallContext
.GetData ("Context");
107 CallContext
.SetData ("Context", value);
111 public Exception
[] AllErrors
114 if (_arrExceptions
== null || _arrExceptions
.Count
== 0)
117 return (Exception
[]) _arrExceptions
.ToArray (typeof (Exception
));
121 public HttpApplicationState Application
124 return HttpApplicationFactory
.ApplicationState
;
128 public HttpApplication ApplicationInstance
131 return _oApplication
;
134 _oApplication
= value;
141 return HttpRuntime
.Cache
;
145 public static HttpContext Current
{
146 get { return Context; }
148 set { Context = value; }
152 public Exception Error
155 if (_arrExceptions
== null || _arrExceptions
.Count
== 0)
158 return (Exception
) _arrExceptions
[0];
162 public IHttpHandler Handler
173 internal IHttpAsyncHandler AsyncHandler
176 return _AsyncHandler
;
180 _AsyncHandler
= value;
184 public bool IsCustomErrorEnabled
{
186 CustomErrorsConfig cfg
;
188 cfg
= (CustomErrorsConfig
) GetConfig ("system.web/customErrors");
189 } catch (Exception
) {
196 CustomErrorMode mode
= cfg
.Mode
;
197 if (mode
== CustomErrorMode
.On
)
200 return (mode
== CustomErrorMode
.RemoteOnly
&&
201 _oRequest
.ServerVariables
["LOCAL_ADDR"] == _oRequest
.UserHostAddress
);
205 public bool IsDebuggingEnabled
208 return CompilationConfiguration
.GetInstance (this).Debug
;
212 public IDictionary Items
216 _oItems
= new Hashtable ();
222 public HttpRequest Request
229 public HttpResponse Response
236 public HttpServerUtility Server
240 _Server
= new HttpServerUtility (this);
246 public HttpSessionState Session
249 return (HttpSessionState
) _oSession
;
253 public bool SkipAuthorization
264 public DateTime Timestamp
271 public TraceContext Trace
278 public IPrincipal User
{
280 set { user = value; }
283 internal bool TimeoutPossible
{
284 get { return (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1); }
287 internal void BeginTimeoutPossible ()
290 timeoutBegin
= DateTime
.Now
.Ticks
;
293 internal void EndTimeoutPossible ()
295 Interlocked
.CompareExchange (ref timeoutPossible
, 0, 1);
298 internal void TryWaitForTimeout ()
300 while (Interlocked
.CompareExchange (ref timeoutPossible
, 1, 1) == 1) {
305 internal bool CheckIfTimeout (DateTime dt
)
307 TimeSpan ts
= new TimeSpan (dt
.Ticks
- timeoutBegin
);
308 return (ts
> ConfigTimeout
);
311 internal TimeSpan ConfigTimeout
{
313 if (configTimeout
== null) {
314 HttpRuntimeConfig config
= (HttpRuntimeConfig
)
315 GetConfig ("system.web/httpRuntime");
316 configTimeout
= new TimeSpan (0, 0, config
.ExecutionTimeout
);
319 return (TimeSpan
) configTimeout
;
322 configTimeout
= value;
326 internal string ErrorPage
{
327 get { return errorPage; }
328 set { errorPage = value; }
331 internal void SetSession (HttpSessionState session
)
336 public void AddError (Exception errorInfo
)
338 if (_arrExceptions
== null)
339 _arrExceptions
= new ArrayList ();
341 _arrExceptions
.Add (errorInfo
);
344 public void ClearError ()
346 _arrExceptions
= null;
349 public object GetConfig (string name
)
351 return WebConfigurationSettings
.GetConfig (name
, this);
354 public static object GetAppConfig (string name
)
356 return WebConfigurationSettings
.GetConfig (name
);
359 object IServiceProvider
.GetService (Type service
)
361 if (service
== typeof (HttpWorkerRequest
))
362 return _oWorkerRequest
;
364 if (service
== typeof (HttpRequest
))
367 if (service
== typeof (HttpResponse
))
370 if (service
== typeof (HttpApplication
))
371 return ApplicationInstance
;
373 if (service
== typeof (HttpApplicationState
))
376 if (service
== typeof (HttpSessionState
))
379 if (service
== typeof (HttpServerUtility
))
385 public void RewritePath (string path
)
387 //LAMESPEC: they say that they throw a ArgumentNullException, however,
388 // i get a NullReferenceException...
390 throw new ArgumentNullException ("path");
393 int qmark
= path
.IndexOf ('?');
394 if (qmark
== -1 || qmark
+ 1 >= path
.Length
) {
397 query
= path
.Substring (qmark
+ 1);
398 path
= path
.Substring (0, qmark
);
401 path
= UrlUtils
.Combine (Request
.BaseVirtualDir
, path
);
402 if (!path
.StartsWith (HttpRuntime
.AppDomainAppVirtualPath
))
403 throw new HttpException (404, "The virtual path '" + path
+
404 "' maps to another application.");
406 Request
.SetFilePath (path
);
407 Request
.QueryStringRaw
= query
;
411 public void RewritePath (string filePath
, string pathInfo
, string queryString
)
413 RewritePath (filePath
+ "?" + queryString
);
414 Request
.SetPathInfo (pathInfo
);