55using System . IO ;
66using System . Linq ;
77using System . Reflection ;
8+ using System . Runtime . InteropServices ;
89using System . Runtime . Remoting . Messaging ;
910using System . Threading ;
1011using System . Web ;
@@ -37,12 +38,12 @@ internal class Dispatcher {
3738 } ;
3839
3940 //protected static ReaderWriterLockSlim dispatchersLock = new ReaderWriterLockSlim();
40- protected static object dispatchersLock = new object { } ;
41- protected static volatile Dictionary < string , Dispatcher > dispatchers = new Dictionary < string , Dispatcher > ( ) ;
41+ internal static object dispatchersLock = new object { } ;
42+ internal static volatile Dictionary < string , Dispatcher > dispatchers = new Dictionary < string , Dispatcher > ( ) ;
4243
4344 protected static string callContextKey = typeof ( Dispatcher ) . FullName ;
4445 protected static Dictionary < string , Type > webBarRegisteredPanels = new Dictionary < string , Type > ( ) ;
45-
46+
4647 internal Exception LastError = null ;
4748 internal int DumperSequence = 0 ;
4849 internal string CurrentlyRendererView = "" ;
@@ -63,66 +64,88 @@ internal class Dispatcher {
6364 protected List < string > webExceptions = null ;
6465
6566 static Dispatcher ( ) {
66- Dispatcher . StaticInitLock . EnterUpgradeableReadLock ( ) ;
67- if ( Dispatcher . StaticInitialized ) {
67+ //try {
68+ Dispatcher . StaticInitLock . EnterUpgradeableReadLock ( ) ;
69+ if ( Dispatcher . StaticInitialized ) {
70+ Dispatcher . StaticInitLock . ExitUpgradeableReadLock ( ) ;
71+ return ;
72+ }
73+ Dispatcher . StaticInitLock . EnterWriteLock ( ) ;
6874 Dispatcher . StaticInitLock . ExitUpgradeableReadLock ( ) ;
69- return ;
70- }
71- Dispatcher . StaticInitLock . EnterWriteLock ( ) ;
72- Dispatcher . StaticInitLock . ExitUpgradeableReadLock ( ) ;
73- int cfgDepth = Config . GetDepth ( ) ;
74- if ( cfgDepth > 0 ) Dispatcher . DumpDepth = cfgDepth ;
75- int cfgMaxLength = Config . GetMaxLength ( ) ;
76- if ( cfgMaxLength > 0 ) Dispatcher . DumpMaxLength = cfgMaxLength ;
77- Dispatcher . Levels = Config . GetLevels ( ) ;
78- Dispatcher . LogWriteMilisecond = Config . GetLogWriteMilisecond ( ) ;
79- Dispatcher . VirtualPathProvider = HostingEnvironment . VirtualPathProvider ;
80- if ( HttpRuntime . AppDomainAppId != null && HostingEnvironment . IsHosted ) {
81- Dispatcher . EnvType = EnvType . Web ;
82- Dispatcher . AppRoot = HttpContext . Current . Server . MapPath ( "~" ) . Replace ( '\\ ' , '/' ) . TrimEnd ( '/' ) ;
83- Dispatcher . WebDebugIps = Config . GetDebugIps ( ) ;
84- Dispatcher . staticInitWebRegisterPanels ( typeof ( Panels . Exceptions ) , typeof ( Panels . Dumps ) ) ;
85- Dispatcher . staticInitWebRegisterPanels ( Config . GetDebugPanels ( ) ) ;
86- Dispatcher . staticInitWebErrorPage ( Config . GetErrorPage ( ) ) ;
87- } else {
88- Dispatcher . EnvType = EnvType . Windows ;
89- Dispatcher . AppRoot = System . IO . Path . GetDirectoryName (
90- System . Diagnostics . Process . GetCurrentProcess ( ) . MainModule . FileName
91- ) . Replace ( '\\ ' , '/' ) . TrimEnd ( '/' ) ;
92- AppDomain . CurrentDomain . UnhandledException += delegate ( object o , UnhandledExceptionEventArgs e ) {
93- Debug . Log ( e . ExceptionObject as Exception ) ;
94- if ( e . IsTerminating ) Dispatcher . Disposed ( ) ;
95- } ;
96- if ( Dispatcher . LogWriteMilisecond > 0 ) {
97- bool exited = false ;
98- AppDomain . CurrentDomain . ProcessExit += delegate ( object o , EventArgs e ) {
99- Dispatcher . Disposed ( ) ;
100- exited = true ;
101- } ;
102- AppExitWatcher . SetConsoleCtrlHandler ( new HandlerRoutine ( ( type ) => {
103- if ( exited ) return true ;
104- Dispatcher . Disposed ( ) ;
105- return true ;
106- } ) , true ) ;
107- }
108- }
109- if ( Tools . IsWindows ( ) ) Dispatcher . AppRoot = Dispatcher . AppRoot . Substring ( 0 , 1 ) . ToUpper ( ) + Dispatcher . AppRoot . Substring ( 1 ) ;
110- if (
111- Tools . IsWindows ( ) && (
112- Dispatcher . AppRoot . IndexOf ( "/bin/Debug" ) == Dispatcher . AppRoot . Length - 10 ||
113- Dispatcher . AppRoot . IndexOf ( "/bin/Release" ) == Dispatcher . AppRoot . Length - 12
114- )
115- ) {
116- Dispatcher . SourcesRoot = System . IO . Path . GetFullPath ( Dispatcher . AppRoot + "/../.." ) . Replace ( '\\ ' , '/' ) ;
117- } else {
118- Dispatcher . SourcesRoot = "" ;
119- }
120- Dispatcher . staticInitEnabledGlobal ( ) ;
121- Dispatcher . staticInitOutputGlobal ( ) ;
122- Dispatcher . staticInitDumpCompillerGenerated ( ) ;
123- Dispatcher . staticInitDirectory ( Config . GetDirectory ( ) ) ;
124- FileLog . StaticInit ( ) ;
125- Dispatcher . StaticInitLock . ExitWriteLock ( ) ;
75+ int cfgDepth = Config . GetDepth ( ) ;
76+ if ( cfgDepth > 0 ) Dispatcher . DumpDepth = cfgDepth ;
77+ int cfgMaxLength = Config . GetMaxLength ( ) ;
78+ if ( cfgMaxLength > 0 ) Dispatcher . DumpMaxLength = cfgMaxLength ;
79+ Dispatcher . Levels = Config . GetLevels ( ) ;
80+ Dispatcher . LogWriteMilisecond = Config . GetLogWriteMilisecond ( ) ;
81+ Dispatcher . VirtualPathProvider = HostingEnvironment . VirtualPathProvider ;
82+ bool appRootInitialized = false ;
83+ if ( HttpRuntime . AppDomainAppId != null && HostingEnvironment . IsHosted ) {
84+ Dispatcher . EnvType = EnvType . Web ;
85+ if ( HttpContext . Current != null ) {
86+ try {
87+ Dispatcher . AppRoot = HttpContext . Current . Server
88+ . MapPath ( "~" ) . Replace ( '\\ ' , '/' ) . TrimEnd ( '/' ) ;
89+ appRootInitialized = true ;
90+ } catch ( Exception e4 ) {
91+ }
92+ }
93+ Dispatcher . WebDebugIps = Config . GetDebugIps ( ) ;
94+ Dispatcher . staticInitWebRegisterPanels ( typeof ( Panels . Exceptions ) , typeof ( Panels . Dumps ) ) ;
95+ Dispatcher . staticInitWebRegisterPanels ( Config . GetDebugPanels ( ) ) ;
96+ Dispatcher . staticInitWebErrorPage ( Config . GetErrorPage ( ) ) ;
97+ } else {
98+ Dispatcher . EnvType = EnvType . Windows ;
99+ try {
100+ Dispatcher . AppRoot = System . IO . Path . GetDirectoryName (
101+ System . Diagnostics . Process . GetCurrentProcess ( ) . MainModule . FileName
102+ ) . Replace ( '\\ ' , '/' ) . TrimEnd ( '/' ) ;
103+ appRootInitialized = true ;
104+ } catch ( Exception e5 ) {
105+ }
106+ AppDomain . CurrentDomain . UnhandledException += delegate ( object o , UnhandledExceptionEventArgs e1 ) {
107+ Debug . Log ( e1 . ExceptionObject as Exception ) ;
108+ if ( e1 . IsTerminating ) Dispatcher . Disposed ( ) ;
109+ } ;
110+ if ( Dispatcher . LogWriteMilisecond > 0 ) {
111+ bool exited = false ;
112+ AppDomain . CurrentDomain . ProcessExit += delegate ( object o , EventArgs e2 ) {
113+ Dispatcher . Disposed ( ) ;
114+ exited = true ;
115+ } ;
116+ AppExitWatcher . SetConsoleCtrlHandler ( new HandlerRoutine ( ( type ) => {
117+ if ( exited ) return true ;
118+ Dispatcher . Disposed ( ) ;
119+ return true ;
120+ } ) , true ) ;
121+ }
122+ }
123+ bool isWindows = Tools . IsWindows ( ) ;
124+ if ( appRootInitialized && isWindows )
125+ Dispatcher . AppRoot = Dispatcher . AppRoot . Substring ( 0 , 1 ) . ToUpper ( )
126+ + Dispatcher . AppRoot . Substring ( 1 ) ;
127+ if (
128+ appRootInitialized && isWindows && (
129+ Dispatcher . AppRoot . IndexOf ( "/bin/Debug" ) == Dispatcher . AppRoot . Length - 10 ||
130+ Dispatcher . AppRoot . IndexOf ( "/bin/Release" ) == Dispatcher . AppRoot . Length - 12
131+ )
132+ ) {
133+ Dispatcher . SourcesRoot = System . IO . Path . GetFullPath (
134+ Dispatcher . AppRoot + "/../.."
135+ ) . Replace ( '\\ ' , '/' ) ;
136+ } else {
137+ Dispatcher . SourcesRoot = "" ;
138+ }
139+ Dispatcher . staticInitEnabledGlobal ( ) ;
140+ Dispatcher . staticInitOutputGlobal ( ) ;
141+ Dispatcher . staticInitDumpCompillerGenerated ( ) ;
142+ if ( appRootInitialized )
143+ Dispatcher . staticInitDirectory ( Config . GetDirectory ( ) ) ;
144+ FileLog . StaticInit ( ) ;
145+ Dispatcher . StaticInitLock . ExitWriteLock ( ) ;
146+ /*} catch (Exception e3) {
147+ Debug.InitErrors.Add(e3);
148+ }*/
126149 }
127150 internal static Dispatcher GetCurrent ( bool createIfNecessary = true ) {
128151 string dispatchedKey = Dispatcher . EnvType == EnvType . Web
@@ -378,6 +401,7 @@ internal FireDump GetFireDump () {
378401 return this . FireDump ;
379402 }
380403 internal void Configure ( DebugConfig cfg ) {
404+ return ;
381405 if ( cfg . EnvType != EnvType . Auto ) Dispatcher . EnvType = cfg . EnvType ;
382406 if ( cfg . Enabled . HasValue ) this . Enabled = cfg . Enabled . Value ;
383407 if ( cfg . LogFormat != LogFormat . Auto ) this . Output = cfg . LogFormat ;
@@ -437,7 +461,8 @@ internal void Stop () {
437461 if ( this . WebRequestState == 0 ) this . WebRequestBegin ( ) ;
438462 if ( this . WebRequestState == 1 ) this . WebRequestSessionBegin ( ) ;
439463 if ( this . WebRequestState == 2 ) this . WebRequestSessionEnd ( ) ;
440- this . WebRequestEnd ( ) ;
464+ this . WebRequestPreSendHeaders ( ) ;
465+ this . WebRequestPreSendBody ( ) ;
441466 Dispatcher . Remove ( ) ;
442467 HttpContext . Current . Response . End ( ) ;
443468 } else {
@@ -485,7 +510,8 @@ internal void WebRequestSessionEnd () {
485510 }
486511 this . WebRequestState = 3 ;
487512 }
488- internal void WebRequestEnd ( ) {
513+ internal void WebRequestPreSendHeaders ( ) {
514+ this . GetFireDump ( ) . CloseHeaders ( ) ;
489515 if ( this . Enabled == true ) {
490516 if ( ! this . webRedirect . HasValue ) this . webRedirect = Dispatcher . webCheckIfResponseIsRedirect ( ) ;
491517 // add possible rendered exceptions and debug bar if necessary
@@ -501,6 +527,15 @@ internal void WebRequestEnd () {
501527 }
502528 // manage Content-Security-Policy http header
503529 this . webManageContentSecurityPolicyHeader ( ) ;
530+ }
531+ } else {
532+ if ( this . webTransmitErrorPage && Dispatcher . WebStaticErrorPage . Length > 0 )
533+ HtmlResponse . TransmitStaticErrorPagePrepareHeaders ( ) ;
534+ }
535+ }
536+ internal void WebRequestPreSendBody ( ) {
537+ if ( this . Enabled == true ) {
538+ if ( this . webRenderDesharpBar == 1 && this . webRedirect != true ) {
504539 // render debug bar for current request with any previous redirect records from session
505540 List < List < RenderedPanel > > renderedPanels = this . webReqEndSession
506541 ?? Dispatcher . webGetSessionStorrage ( ) ;
@@ -512,7 +547,7 @@ internal void WebRequestEnd () {
512547 }
513548 } else {
514549 if ( this . webTransmitErrorPage && Dispatcher . WebStaticErrorPage . Length > 0 )
515- HtmlResponse . TransmitStaticErrorPage ( ) ;
550+ HtmlResponse . TransmitStaticErrorPageSendContent ( ) ;
516551 }
517552 }
518553 protected void webManageContentSecurityPolicyHeader ( ) {
0 commit comments