Got rid of those Internal(1) warnings
[mono.git] / mcs / class / System.Web / System.Web / HttpContext.cs
1 //
2 // System.Web.HttpContext
3 //
4 // Authors:
5 //      Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2003 Novell, Inc. (http://www.novell.com)
9 //
10 using System;
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;
19
20 namespace System.Web
21 {
22         public sealed class HttpContext : IServiceProvider
23         {
24                 private ArrayList _arrExceptions;
25
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;
36
37                 private bool _skipauth;
38                 private Hashtable               _oItems;
39                 private DateTime                _oTimestamp;
40                 int timeoutPossible;
41                 long timeoutBegin;
42                 object configTimeout;
43                 string errorPage;
44
45                 public HttpContext (HttpRequest Request, HttpResponse Response)
46                 {
47                         Context = this;
48
49                         _arrExceptions = null;
50                         _oItems = null;
51                         _oTimestamp = DateTime.Now;
52                         _oRequest = Request;
53                         _oResponse = Response;
54                         _oTrace = new TraceContext (this);
55                 }
56
57                 public HttpContext (HttpWorkerRequest WorkerRequest)
58                 {
59                         Context = this;
60
61                         _arrExceptions = null;
62                         _oItems = 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);
68                 }
69
70                 internal HttpWorkerRequest WorkerRequest
71                 {
72                         get {
73                                 return _oWorkerRequest;
74                         }
75                 }
76
77                 [MonoTODO("Context - Use System.Remoting.Messaging.CallContext instead of Thread storage")]
78                 internal static HttpContext Context
79                 {
80                         get {
81                                 return (HttpContext) Thread.GetData (Thread.GetNamedDataSlot ("Context"));
82                         }
83
84                         set {
85                                 Thread.SetData (Thread.GetNamedDataSlot ("Context"), value);
86                         }
87                 }
88
89                 public Exception [] AllErrors
90                 {
91                         get {
92                                 if (_arrExceptions == null || _arrExceptions.Count == 0)
93                                         return null;
94
95                                 return (Exception []) _arrExceptions.ToArray (typeof (Exception));
96                         }
97                 }
98
99                 public HttpApplicationState Application
100                 {
101                         get {
102                                 return HttpApplicationFactory.ApplicationState;
103                         }
104                 }
105
106                 public HttpApplication ApplicationInstance
107                 {
108                         get {
109                                 return _oApplication;
110                         }
111                         set {
112                                 _oApplication = value;
113                         }
114                 }
115
116                 public Cache Cache
117                 {
118                         get {
119                                 return HttpRuntime.Cache;
120                         }
121                 }
122
123                 public static HttpContext Current {
124                         get { return Context; }
125 #if NET_1_1
126                         set { Context = value; }
127 #endif
128                 }
129
130                 public Exception Error
131                 {
132                         get {
133                                 if (_arrExceptions == null || _arrExceptions.Count == 0)
134                                         return null;
135
136                                 return (Exception) _arrExceptions [0];
137                         }
138                 }
139
140                 public IHttpHandler Handler
141                 {
142                         get {    
143                                 return _Handler;
144                         }
145
146                         set {
147                                 _Handler = value;
148                         }
149                 }
150
151                 internal IHttpAsyncHandler AsyncHandler
152                 {
153                         get {    
154                                 return _AsyncHandler;
155                         }
156
157                         set {
158                                 _AsyncHandler = value;
159                         }
160                 }
161
162                 public bool IsCustomErrorEnabled {
163                         get {
164                                 CustomErrorsConfig cfg;
165                                 try {
166                                         cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
167                                 } catch (Exception) {
168                                         return false;
169                                 }
170                                 
171                                 if (cfg == null)
172                                         return false;
173
174                                 CustomErrorMode mode = cfg.Mode;
175                                 if (mode == CustomErrorMode.On)
176                                         return true;
177
178                                 return (mode == CustomErrorMode.RemoteOnly &&
179                                         _oRequest.ServerVariables ["LOCAL_ADDR"] == _oRequest.UserHostAddress);
180                         }
181                 }
182
183                 public bool IsDebuggingEnabled
184                 {
185                         get {
186                                 return CompilationConfiguration.GetInstance (this).Debug;
187                         }
188                 }
189
190                 public IDictionary Items
191                 {
192                         get {
193                                 if (_oItems == null)
194                                         _oItems = new Hashtable ();
195
196                                 return _oItems;
197                         }
198                 }
199
200                 public HttpRequest Request
201                 {
202                         get {
203                                 return _oRequest;
204                         }
205                 }
206
207                 public HttpResponse Response
208                 {
209                         get {
210                                 return _oResponse;
211                         }
212                 }
213
214                 public HttpServerUtility Server
215                 {
216                         get {
217                                 if (null == _Server)
218                                         _Server = new HttpServerUtility (this);
219
220                                 return _Server; 
221                         }
222                 }
223
224                 public HttpSessionState Session
225                 {
226                         get {
227                                 return (HttpSessionState) _oSession;
228                         }
229                 }
230
231                 public bool SkipAuthorization
232                 {
233                         get {
234                                 return _skipauth;
235                         }
236
237                         set {
238                                 _skipauth = value;
239                         }
240                 }
241
242                 public DateTime Timestamp
243                 {
244                         get {
245                                 return _oTimestamp;
246                         }
247                 }
248
249                 public TraceContext Trace
250                 {
251                         get {
252                                 return _oTrace;
253                         }
254                 }
255
256                 public IPrincipal User
257                 {
258                         get {
259                                 return _User;
260                         }
261                         set {
262                                 _User = value;
263                         }
264                 }
265
266                 internal bool TimeoutPossible {
267                         get { return (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1); }
268                 }
269                 
270                 internal void BeginTimeoutPossible ()
271                 {
272                         timeoutPossible = 1;
273                         timeoutBegin = DateTime.Now.Ticks;
274                 }
275
276                 internal void EndTimeoutPossible ()
277                 {
278                         Interlocked.CompareExchange (ref timeoutPossible, 0, 1);
279                 }
280                 
281                 internal void TryWaitForTimeout () 
282                 {
283                         while (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1) {
284                                 Thread.Sleep (500);
285                         }
286                 }
287
288                 internal bool CheckIfTimeout (DateTime dt)
289                 {
290                         TimeSpan ts = new TimeSpan (dt.Ticks - timeoutBegin);
291                         return (ts > ConfigTimeout);
292                 }
293
294                 internal TimeSpan ConfigTimeout {
295                         get {
296                                 if (configTimeout == null) {
297                                         HttpRuntimeConfig config = (HttpRuntimeConfig)
298                                                                 GetConfig ("system.web/httpRuntime");
299                                         configTimeout = new TimeSpan (0, 0, config.ExecutionTimeout);
300                                 }
301
302                                 return (TimeSpan) configTimeout;
303                         }
304                         set {
305                                 configTimeout = value;
306                         }
307                 }
308                 
309                 internal string ErrorPage {
310                         get { return errorPage; }
311                         set { errorPage = value; }
312                 }
313                 
314                 internal void SetSession (HttpSessionState session)
315                 {
316                         _oSession = session;
317                 }
318                 
319                 public void AddError (Exception errorInfo)
320                 {
321                         if (_arrExceptions == null)
322                                 _arrExceptions = new ArrayList ();
323
324                         _arrExceptions.Add (errorInfo);
325                 }
326
327                 public void ClearError ()
328                 {
329                         _arrExceptions = null;
330                 }
331
332                 public object GetConfig (string name)
333                 {
334                         return WebConfigurationSettings.GetConfig (name, this);
335                 }
336
337                 public static object GetAppConfig (string name)
338                 {
339                         return WebConfigurationSettings.GetConfig (name);
340                 }
341
342                 object IServiceProvider.GetService (Type service)
343                 {
344                         if (service == typeof (HttpWorkerRequest)) 
345                                 return _oWorkerRequest;
346
347                         if (service == typeof (HttpRequest))
348                                 return Request;
349
350                         if (service == typeof (HttpResponse))
351                                 return Response;
352
353                         if (service == typeof (HttpApplication))
354                                 return ApplicationInstance;
355
356                         if (service == typeof (HttpApplicationState))
357                                 return Application;
358
359                         if (service == typeof (HttpSessionState))
360                                 return Session;
361
362                         if (service == typeof (HttpServerUtility))
363                                 return Server;
364
365                         return null;
366                 }
367
368                 public void RewritePath (string path)
369                 {
370                         //LAMESPEC: they say that they throw a ArgumentNullException, however,
371                         // i get a NullReferenceException...
372                         if (path == null)
373                                 throw new ArgumentNullException ("path");
374
375                         string query;
376                         int qmark = path.IndexOf ('?');
377                         if (qmark == -1 || qmark + 1 >= path.Length) {
378                                 query = null;
379                         } else {
380                                 query = path.Substring (qmark + 1);
381                                 path = path.Substring (0, qmark);
382                         }
383
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.");
388
389                         Request.SetFilePath (path);
390                         Request.QueryStringRaw = query;
391                 }
392
393 #if NET_1_1
394                 public void RewritePath (string filePath, string pathInfo, string queryString)
395                 {
396                         RewritePath (filePath + "?" + queryString);
397                         Request.SetPathInfo (pathInfo);
398                 }
399 #endif
400         }
401 }
402