| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- //
- // System.Web.HttpContext
- //
- // Authors:
- // Patrik Torstensson ([email protected])
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (c) 2003 Novell, Inc. (http://www.novell.com)
- //
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Security.Principal;
- using System.Web.Caching;
- using System.Web.Configuration;
- using System.Web.Util;
- using System.Web.SessionState;
- using System.Threading;
- namespace System.Web
- {
- public sealed class HttpContext : IServiceProvider
- {
- private ArrayList _arrExceptions;
- private HttpResponse _oResponse;
- private HttpRequest _oRequest;
- private HttpServerUtility _Server;
- private HttpApplication _oApplication;
- private HttpSessionState _oSession;
- private HttpWorkerRequest _oWorkerRequest;
- private TraceContext _oTrace;
- private IHttpHandler _Handler;
- private IHttpAsyncHandler _AsyncHandler;
- private IPrincipal _User;
- private bool _skipauth;
- private Hashtable _oItems;
- private DateTime _oTimestamp;
- int timeoutPossible;
- long timeoutBegin;
- object configTimeout;
- string errorPage;
- public HttpContext (HttpRequest Request, HttpResponse Response)
- {
- Context = this;
- _arrExceptions = null;
- _oItems = null;
- _oTimestamp = DateTime.Now;
- _oRequest = Request;
- _oResponse = Response;
- _oTrace = new TraceContext (this);
- }
- public HttpContext (HttpWorkerRequest WorkerRequest)
- {
- Context = this;
- _arrExceptions = null;
- _oItems = null;
- _oTimestamp = DateTime.Now;
- _oRequest = new HttpRequest (WorkerRequest, this);
- _oResponse = new HttpResponse (WorkerRequest, this);
- _oWorkerRequest = WorkerRequest;
- _oTrace = new TraceContext (this);
- }
- internal HttpWorkerRequest WorkerRequest
- {
- get {
- return _oWorkerRequest;
- }
- }
- [MonoTODO("Context - Use System.Remoting.Messaging.CallContext instead of Thread storage")]
- internal static HttpContext Context
- {
- get {
- return (HttpContext) Thread.GetData (Thread.GetNamedDataSlot ("Context"));
- }
- set {
- Thread.SetData (Thread.GetNamedDataSlot ("Context"), value);
- }
- }
- public Exception [] AllErrors
- {
- get {
- if (_arrExceptions == null || _arrExceptions.Count == 0)
- return null;
- return (Exception []) _arrExceptions.ToArray (typeof (Exception));;
- }
- }
- public HttpApplicationState Application
- {
- get {
- return HttpApplicationFactory.ApplicationState;
- }
- }
- public HttpApplication ApplicationInstance
- {
- get {
- return _oApplication;
- }
- set {
- _oApplication = value;
- }
- }
- public Cache Cache
- {
- get {
- return HttpRuntime.Cache;
- }
- }
- public static HttpContext Current
- {
- get {
- return Context;
- }
- }
- public Exception Error
- {
- get {
- if (_arrExceptions == null || _arrExceptions.Count == 0)
- return null;
- return (Exception) _arrExceptions [0];
- }
- }
- public IHttpHandler Handler
- {
- get {
- return _Handler;
- }
- set {
- _Handler = value;
- }
- }
- internal IHttpAsyncHandler AsyncHandler
- {
- get {
- return _AsyncHandler;
- }
- set {
- _AsyncHandler = value;
- }
- }
- public bool IsCustomErrorEnabled {
- get {
- CustomErrorsConfig cfg;
- try {
- cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
- } catch (Exception e) {
- return false;
- }
-
- if (cfg == null)
- return false;
- CustomErrorMode mode = cfg.Mode;
- if (mode == CustomErrorMode.On)
- return true;
- return (mode == CustomErrorMode.RemoteOnly &&
- _oRequest.ServerVariables ["LOCAL_ADDR"] == _oRequest.UserHostAddress);
- }
- }
- public bool IsDebuggingEnabled
- {
- get {
- return CompilationConfiguration.GetInstance (this).Debug;
- }
- }
- public IDictionary Items
- {
- get {
- if (_oItems == null)
- _oItems = new Hashtable ();
- return _oItems;
- }
- }
- public HttpRequest Request
- {
- get {
- return _oRequest;
- }
- }
- public HttpResponse Response
- {
- get {
- return _oResponse;
- }
- }
- public HttpServerUtility Server
- {
- get {
- if (null == _Server)
- _Server = new HttpServerUtility (this);
- return _Server;
- }
- }
- public HttpSessionState Session
- {
- get {
- return (HttpSessionState) _oSession;
- }
- }
- public bool SkipAuthorization
- {
- get {
- return _skipauth;
- }
- set {
- _skipauth = value;
- }
- }
- public DateTime Timestamp
- {
- get {
- return _oTimestamp;
- }
- }
- public TraceContext Trace
- {
- get {
- return _oTrace;
- }
- }
- public IPrincipal User
- {
- get {
- return _User;
- }
- set {
- _User = value;
- }
- }
- internal bool TimeoutPossible {
- get { return (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1); }
- }
-
- internal void BeginTimeoutPossible ()
- {
- timeoutPossible = 1;
- timeoutBegin = DateTime.Now.Ticks;
- }
- internal void EndTimeoutPossible ()
- {
- Interlocked.CompareExchange (ref timeoutPossible, 0, 1);
- }
-
- internal void TryWaitForTimeout ()
- {
- while (Interlocked.CompareExchange (ref timeoutPossible, 1, 1) == 1) {
- Thread.Sleep (500);
- }
- }
- internal bool CheckIfTimeout (DateTime dt)
- {
- TimeSpan ts = new TimeSpan (dt.Ticks - timeoutBegin);
- return (ts > ConfigTimeout);
- }
- internal TimeSpan ConfigTimeout {
- get {
- if (configTimeout == null) {
- HttpRuntimeConfig config = (HttpRuntimeConfig)
- GetConfig ("system.web/httpRuntime");
- configTimeout = new TimeSpan (0, 0, config.ExecutionTimeout);
- }
- return (TimeSpan) configTimeout;
- }
- }
-
- internal string ErrorPage {
- get { return errorPage; }
- set { errorPage = value; }
- }
-
- internal void SetSession (HttpSessionState session)
- {
- _oSession = session;
- }
-
- public void AddError (Exception errorInfo)
- {
- if (_arrExceptions == null)
- _arrExceptions = new ArrayList ();
- _arrExceptions.Add (errorInfo);
- }
- public void ClearError ()
- {
- _arrExceptions = null;
- }
- public object GetConfig (string name)
- {
- return WebConfigurationSettings.GetConfig (name, this);
- }
- public static object GetAppConfig (string name)
- {
- return WebConfigurationSettings.GetConfig (name);
- }
- object IServiceProvider.GetService (Type service)
- {
- if (service == typeof (HttpWorkerRequest))
- return _oWorkerRequest;
- if (service == typeof (HttpRequest))
- return Request;
- if (service == typeof (HttpResponse))
- return Response;
- if (service == typeof (HttpApplication))
- return ApplicationInstance;
- if (service == typeof (HttpApplicationState))
- return Application;
- if (service == typeof (HttpSessionState))
- return Session;
- if (service == typeof (HttpServerUtility))
- return Server;
- return null;
- }
- public void RewritePath (string path)
- {
- //LAMESPEC: they say that they throw a ArgumentNullException, however,
- // i get a NullReferenceException...
- if (path == null)
- throw new ArgumentNullException ("path");
- string query;
- int qmark = path.IndexOf ('?');
- if (qmark == -1 || qmark + 1 >= path.Length) {
- query = null;
- } else {
- query = path.Substring (qmark + 1);
- path = path.Substring (0, qmark);
- }
- path = UrlUtils.Combine (Request.BaseVirtualDir, path);
- if (!path.StartsWith (HttpRuntime.AppDomainAppVirtualPath))
- throw new HttpException (404, "The virtual path '" + path +
- "' maps to another application.");
- Request.SetFilePath (path);
- Request.QueryStringRaw = query;
- }
- }
- }
|