HttpContext.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // System.Web.HttpContext
  3. //
  4. // Authors:
  5. // Patrik Torstensson ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  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. namespace System.Web
  20. {
  21. public sealed class HttpContext : IServiceProvider
  22. {
  23. private ArrayList _arrExceptions;
  24. private HttpResponse _oResponse;
  25. private HttpRequest _oRequest;
  26. private HttpServerUtility _Server;
  27. private HttpApplication _oApplication;
  28. private HttpSessionState _oSession;
  29. private HttpWorkerRequest _oWorkerRequest;
  30. private TraceContext _oTrace;
  31. private IHttpHandler _Handler;
  32. private IHttpAsyncHandler _AsyncHandler;
  33. private IPrincipal _User;
  34. private bool _skipauth;
  35. private Hashtable _oItems;
  36. private DateTime _oTimestamp;
  37. int timeoutPossible;
  38. long timeoutBegin;
  39. object configTimeout;
  40. string errorPage;
  41. public HttpContext (HttpRequest Request, HttpResponse Response)
  42. {
  43. Context = this;
  44. _arrExceptions = null;
  45. _oItems = null;
  46. _oTimestamp = DateTime.Now;
  47. _oRequest = Request;
  48. _oResponse = Response;
  49. _oTrace = new TraceContext (this);
  50. }
  51. public HttpContext (HttpWorkerRequest WorkerRequest)
  52. {
  53. Context = this;
  54. _arrExceptions = null;
  55. _oItems = null;
  56. _oTimestamp = DateTime.Now;
  57. _oRequest = new HttpRequest (WorkerRequest, this);
  58. _oResponse = new HttpResponse (WorkerRequest, this);
  59. _oWorkerRequest = WorkerRequest;
  60. _oTrace = new TraceContext (this);
  61. }
  62. internal HttpWorkerRequest WorkerRequest
  63. {
  64. get {
  65. return _oWorkerRequest;
  66. }
  67. }
  68. [MonoTODO("Context - Use System.Remoting.Messaging.CallContext instead of Thread storage")]
  69. internal static HttpContext Context
  70. {
  71. get {
  72. return (HttpContext) Thread.GetData (Thread.GetNamedDataSlot ("Context"));
  73. }
  74. set {
  75. Thread.SetData (Thread.GetNamedDataSlot ("Context"), value);
  76. }
  77. }
  78. public Exception [] AllErrors
  79. {
  80. get {
  81. if (_arrExceptions == null || _arrExceptions.Count == 0)
  82. return null;
  83. return (Exception []) _arrExceptions.ToArray (typeof (Exception));;
  84. }
  85. }
  86. public HttpApplicationState Application
  87. {
  88. get {
  89. return HttpApplicationFactory.ApplicationState;
  90. }
  91. }
  92. public HttpApplication ApplicationInstance
  93. {
  94. get {
  95. return _oApplication;
  96. }
  97. set {
  98. _oApplication = value;
  99. }
  100. }
  101. public Cache Cache
  102. {
  103. get {
  104. return HttpRuntime.Cache;
  105. }
  106. }
  107. public static HttpContext Current
  108. {
  109. get {
  110. return Context;
  111. }
  112. }
  113. public Exception Error
  114. {
  115. get {
  116. if (_arrExceptions == null || _arrExceptions.Count == 0)
  117. return null;
  118. return (Exception) _arrExceptions [0];
  119. }
  120. }
  121. public IHttpHandler Handler
  122. {
  123. get {
  124. return _Handler;
  125. }
  126. set {
  127. _Handler = value;
  128. }
  129. }
  130. internal IHttpAsyncHandler AsyncHandler
  131. {
  132. get {
  133. return _AsyncHandler;
  134. }
  135. set {
  136. _AsyncHandler = value;
  137. }
  138. }
  139. public bool IsCustomErrorEnabled {
  140. get {
  141. CustomErrorsConfig cfg;
  142. try {
  143. cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
  144. } catch (Exception e) {
  145. return false;
  146. }
  147. if (cfg == null)
  148. return false;
  149. CustomErrorMode mode = cfg.Mode;
  150. if (mode == CustomErrorMode.On)
  151. return true;
  152. return (mode == CustomErrorMode.RemoteOnly &&
  153. _oRequest.ServerVariables ["LOCAL_ADDR"] == _oRequest.UserHostAddress);
  154. }
  155. }
  156. public bool IsDebuggingEnabled
  157. {
  158. get {
  159. return CompilationConfiguration.GetInstance (this).Debug;
  160. }
  161. }
  162. public IDictionary Items
  163. {
  164. get {
  165. if (_oItems == null)
  166. _oItems = new Hashtable ();
  167. return _oItems;
  168. }
  169. }
  170. public HttpRequest Request
  171. {
  172. get {
  173. return _oRequest;
  174. }
  175. }
  176. public HttpResponse Response
  177. {
  178. get {
  179. return _oResponse;
  180. }
  181. }
  182. public HttpServerUtility Server
  183. {
  184. get {
  185. if (null == _Server)
  186. _Server = new HttpServerUtility (this);
  187. return _Server;
  188. }
  189. }
  190. public HttpSessionState Session
  191. {
  192. get {
  193. return (HttpSessionState) _oSession;
  194. }
  195. }
  196. public bool SkipAuthorization
  197. {
  198. get {
  199. return _skipauth;
  200. }
  201. set {
  202. _skipauth = value;
  203. }
  204. }
  205. public DateTime Timestamp
  206. {
  207. get {
  208. return _oTimestamp;
  209. }
  210. }
  211. public TraceContext Trace
  212. {
  213. get {
  214. return _oTrace;
  215. }
  216. }
  217. public IPrincipal User
  218. {
  219. get {
  220. return _User;
  221. }
  222. set {
  223. _User = value;
  224. }
  225. }
  226. internal bool TimeoutPossible {
  227. get { return (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1); }
  228. }
  229. internal void BeginTimeoutPossible ()
  230. {
  231. timeoutPossible = 1;
  232. timeoutBegin = DateTime.Now.Ticks;
  233. }
  234. internal void EndTimeoutPossible ()
  235. {
  236. Interlocked.CompareExchange (ref timeoutPossible, 0, 1);
  237. }
  238. internal void TryWaitForTimeout ()
  239. {
  240. while (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1) {
  241. Thread.Sleep (500);
  242. }
  243. }
  244. internal bool CheckIfTimeout (DateTime dt)
  245. {
  246. TimeSpan ts = new TimeSpan (dt.Ticks - timeoutBegin);
  247. return (ts > ConfigTimeout);
  248. }
  249. internal TimeSpan ConfigTimeout {
  250. get {
  251. if (configTimeout == null) {
  252. HttpRuntimeConfig config = (HttpRuntimeConfig)
  253. GetConfig ("system.web/httpRuntime");
  254. configTimeout = new TimeSpan (0, 0, config.ExecutionTimeout);
  255. }
  256. return (TimeSpan) configTimeout;
  257. }
  258. }
  259. internal string ErrorPage {
  260. get { return errorPage; }
  261. set { errorPage = value; }
  262. }
  263. internal void SetSession (HttpSessionState session)
  264. {
  265. _oSession = session;
  266. }
  267. public void AddError (Exception errorInfo)
  268. {
  269. if (_arrExceptions == null)
  270. _arrExceptions = new ArrayList ();
  271. _arrExceptions.Add (errorInfo);
  272. }
  273. public void ClearError ()
  274. {
  275. _arrExceptions = null;
  276. }
  277. public object GetConfig (string name)
  278. {
  279. return WebConfigurationSettings.GetConfig (name, this);
  280. }
  281. public static object GetAppConfig (string name)
  282. {
  283. return WebConfigurationSettings.GetConfig (name);
  284. }
  285. object IServiceProvider.GetService (Type service)
  286. {
  287. if (service == typeof (HttpWorkerRequest))
  288. return _oWorkerRequest;
  289. if (service == typeof (HttpRequest))
  290. return Request;
  291. if (service == typeof (HttpResponse))
  292. return Response;
  293. if (service == typeof (HttpApplication))
  294. return ApplicationInstance;
  295. if (service == typeof (HttpApplicationState))
  296. return Application;
  297. if (service == typeof (HttpSessionState))
  298. return Session;
  299. if (service == typeof (HttpServerUtility))
  300. return Server;
  301. return null;
  302. }
  303. public void RewritePath (string path)
  304. {
  305. //LAMESPEC: they say that they throw a ArgumentNullException, however,
  306. // i get a NullReferenceException...
  307. if (path == null)
  308. throw new ArgumentNullException ("path");
  309. string query;
  310. int qmark = path.IndexOf ('?');
  311. if (qmark == -1 || qmark + 1 >= path.Length) {
  312. query = null;
  313. } else {
  314. query = path.Substring (qmark + 1);
  315. path = path.Substring (0, qmark);
  316. }
  317. path = UrlUtils.Combine (Request.BaseVirtualDir, path);
  318. if (!path.StartsWith (HttpRuntime.AppDomainAppVirtualPath))
  319. throw new HttpException (404, "The virtual path '" + path +
  320. "' maps to another application.");
  321. Request.SetFilePath (path);
  322. Request.QueryStringRaw = query;
  323. }
  324. }
  325. }