From ee399fa1dc502b5b0d2ee4474692fb7b70fdad6c Mon Sep 17 00:00:00 2001 From: Francesco Saverio Giudice Date: Mon, 30 Mar 2009 18:14:42 +0000 Subject: [PATCH] 2009-03-30 20:13 UTC+0200 Francesco Saverio Giudice (info/at/fsgiudice.com) * harbour/contrib/examples/uhttpd/uhttpd.prg + added support for closing console using "X" system button using hb_GTInfo() functions. Tested only in windows. * formatting * harbour/contrib/examples/uhttpd/uhttpdc.c * added WM_CLOSE event in win_SysRefresh() * harbour/contrib/examples/uhttpd/session.prg * minor change --- harbour/ChangeLog | 10 + harbour/contrib/examples/uhttpd/session.prg | 2 +- harbour/contrib/examples/uhttpd/uhttpd.prg | 293 ++++++++++++-------- harbour/contrib/examples/uhttpd/uhttpdc.c | 9 +- 4 files changed, 195 insertions(+), 119 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 7aa920dc62..75d59341cb 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,16 @@ 2009-12-31 13:59 UTC+0100 Foo Bar (foo.bar foobar.org) */ +2009-03-30 20:13 UTC+0200 Francesco Saverio Giudice (info/at/fsgiudice.com) + * harbour/contrib/examples/uhttpd/uhttpd.prg + + added support for closing console using "X" system button + using hb_GTInfo() functions. Tested only in windows. + * formatting + * harbour/contrib/examples/uhttpd/uhttpdc.c + * added WM_CLOSE event in win_SysRefresh() + * harbour/contrib/examples/uhttpd/session.prg + * minor change + 2009-03-30 17:28 UTC+0200 Viktor Szakats (harbour.01 syenar hu) * contrib/hbfbird/Makefile * contrib/gtalleg/Makefile diff --git a/harbour/contrib/examples/uhttpd/session.prg b/harbour/contrib/examples/uhttpd/session.prg index 53eab5d790..39b5bb91f6 100644 --- a/harbour/contrib/examples/uhttpd/session.prg +++ b/harbour/contrib/examples/uhttpd/session.prg @@ -71,7 +71,7 @@ MEMVAR _COOKIE, _SESSION, _REQUEST, _SERVER -#define MY_CRCKEY "UhTTpK3y!@" +#define MY_CRCKEY "UhTTpK3y76" FUNCTION uhttpd_SessionNew( cSessionName, cSessionPath ) RETURN uhttpd_Session():New( cSessionName, cSessionPath ) diff --git a/harbour/contrib/examples/uhttpd/uhttpd.prg b/harbour/contrib/examples/uhttpd/uhttpd.prg index 5e24ba0c66..9cf89a2cf0 100644 --- a/harbour/contrib/examples/uhttpd/uhttpd.prg +++ b/harbour/contrib/examples/uhttpd/uhttpd.prg @@ -96,6 +96,7 @@ #include "inkey.ch" #include "error.ch" #include "hbmemory.ch" +#include 'hbgtinfo.ch' #include "hbextern.ch" // need this to use with HRB @@ -131,7 +132,7 @@ #endif #define APP_NAME "uhttpd" -#define APP_VER_NUM "0.4.2" +#define APP_VER_NUM "0.4.3" #define APP_VERSION APP_VER_NUM + APP_GD_SUPPORT + APP_INET_SUPPORT + APP_DT_SUPPORT #define AF_INET 2 @@ -184,6 +185,8 @@ // dynamic call for HRB support DYNAMIC HRBMAIN +STATIC s_lQuitRequest := FALSE + STATIC s_hmtxQueue, s_hmtxServiceThreads, s_hmtxRunningThreads, s_hmtxLog, s_hmtxConsole, s_hmtxBusy STATIC s_hmtxHRB @@ -272,6 +275,8 @@ FUNCTION MAIN( ... ) cGT := HB_GTVERSION() IF ( cGT == "NUL" ) lConsole := FALSE + ELSE + hb_gtInfo( HB_GTI_NOTIFIERBLOCK, {|nEvent, ...| GT_notifier( nEvent, ... ) } ) ENDIF // TOCHECK: now not force case insensitive @@ -533,8 +538,17 @@ FUNCTION MAIN( ... ) #ifdef USE_WIN_ADDONS // windows resource releasing - 1 millisecond wait - WIN_SYSREFRESH( 1 ) + IF WIN_SYSREFRESH( 1 ) != 0 + EXIT + ENDIF #endif + IF hb_mutexLock( s_hmtxBusy ) + IF s_lQuitRequest + hb_mutexUnlock( s_hmtxBusy ) + EXIT + ENDIF + hb_mutexUnlock( s_hmtxBusy ) + ENDIF IF s_lConsole @@ -639,6 +653,7 @@ STATIC FUNCTION AcceptConnections() LOCAL nServiceConnections, nServiceThreads, nMaxServiceThreads, nThreadID LOCAL pThread LOCAL lCanNotify + LOCAL lQuitRequest := FALSE ErrorBlock( { | oError | uhttpd_DefError( oError ) } ) @@ -667,14 +682,25 @@ STATIC FUNCTION AcceptConnections() #ifdef USE_WIN_ADDONS // releasing resources - WIN_SYSREFRESH( 1 ) + IF WIN_SYSREFRESH( 1 ) != 0 + lQuitRequest := TRUE + ENDIF #endif + IF hb_mutexLock( s_hmtxBusy ) + IF s_lQuitRequest + hb_mutexUnlock( s_hmtxBusy ) + lQuitRequest := TRUE + EXIT + ENDIF + hb_mutexUnlock( s_hmtxBusy ) + ENDIF + // Waiting a connection from main application loop hb_mutexSubscribe( s_hmtxQueue,, @hSocket ) // I have a QUIT request - IF hSocket == NIL + IF hSocket == NIL .OR. lQuitRequest // Requesting to Running threads to quit (using -1 value) AEVAL( s_aRunningThreads, {|| hb_mutexNotify( s_hmtxRunningThreads, -1 ) } ) @@ -799,9 +825,21 @@ STATIC FUNCTION ProcessConnection() #ifdef USE_WIN_ADDONS // releasing resources - WIN_SYSREFRESH( 1 ) + IF WIN_SYSREFRESH( 1 ) != 0 + lQuitRequest := TRUE + EXIT + ENDIF #endif + IF hb_mutexLock( s_hmtxBusy ) + IF s_lQuitRequest + hb_mutexUnlock( s_hmtxBusy ) + lQuitRequest := TRUE + EXIT + ENDIF + hb_mutexUnlock( s_hmtxBusy ) + ENDIF + // Waiting a connection from AcceptConnections() but up to defined time hb_mutexSubscribe( s_hmtxRunningThreads, THREAD_MAX_WAIT, @hSocket ) @@ -947,9 +985,21 @@ STATIC FUNCTION ServiceConnection() #ifdef USE_WIN_ADDONS // releasing resources - WIN_SYSREFRESH( 1 ) + IF WIN_SYSREFRESH( 1 ) != 0 + lQuitRequest := TRUE + EXIT + ENDIF #endif + IF hb_mutexLock( s_hmtxBusy ) + IF s_lQuitRequest + hb_mutexUnlock( s_hmtxBusy ) + lQuitRequest := TRUE + EXIT + ENDIF + hb_mutexUnlock( s_hmtxBusy ) + ENDIF + // Waiting a connection from AcceptConnections() but up to defined time hb_mutexSubscribe( s_hmtxServiceThreads, THREAD_MAX_WAIT, @hSocket ) @@ -2231,6 +2281,37 @@ STATIC FUNCTION ParseIni( cConfig ) RETURN hDefault +STATIC FUNCTION FileUnAlias( cScript ) + LOCAL cFileName, x + + // Checking if the request contains a Script Alias + IF HB_HHasKey( s_hScriptAliases, cScript ) + // in this case I have to substitute the alias with the real file name + cFileName := hb_hGet( s_hScriptAliases, cScript ) + + // substitute macros + cFileName := StrTran( cFileName, "$(DOCROOT_DIR)", _SERVER[ "DOCUMENT_ROOT" ] ) + cFileName := StrTran( cFileName, "$(APP_DIR)" , Exe_Path() ) + ENDIF + + IF cFileName == NIL + + // Checking if the request contains an alias + FOR EACH x IN s_hAliases + IF x:__enumKey() == Left( cScript, Len( x:__enumKey() ) ) + cFileName := x:__enumValue() + SubStr( cScript, Len( x:__enumKey() ) + 1 ) + + // substitute macros + cFileName := StrTran( cFileName, "$(DOCROOT_DIR)", _SERVER[ "DOCUMENT_ROOT" ] ) + cFileName := StrTran( cFileName, "$(APP_DIR)" , Exe_Path() ) + EXIT + ENDIF + NEXT + + ENDIF + +RETURN cFileName + STATIC FUNCTION uhttpd_DefError( oError ) LOCAL cMessage LOCAL cCallstack @@ -2377,86 +2458,6 @@ STATIC FUNCTION ErrorMessage( oError ) RETURN cMessage -// ------------------ FILTERS --------------------- -STATIC FUNCTION Handler_HrbScript( cFileName ) - LOCAL xResult - LOCAL cHRBBody, pHRB, oError - - TRY - // Lock HRB to avoid MT race conditions - IF !HRB_ACTIVATE_CACHE - cHRBBody := HRB_LoadFromFile( uhttpd_OSFileName( cFileName ) ) - ENDIF - IF hb_mutexLock( s_hmtxHRB ) - IF HRB_ACTIVATE_CACHE - // caching modules - IF !hb_HHasKey( s_hHRBModules, cFileName ) - hb_HSet( s_hHRBModules, cFileName, HRB_LoadFromFile( uhttpd_OSFileName( cFileName ) ) ) - ENDIF - cHRBBody := s_hHRBModules[ cFileName ] - ENDIF - IF !EMPTY( pHRB := HB_HRBLOAD( cHRBBody ) ) - - xResult := HRBMAIN() - - HB_HRBUNLOAD( pHRB ) - ELSE - uhttpd_SetStatusCode( 404 ) - t_cErrorMsg := "File does not exist: " + cFileName - ENDIF - hb_mutexUnlock( s_hmtxHRB ) - - ENDIF - - IF HB_ISSTRING( xResult ) - uhttpd_AddHeader( "Content-Type", "text/html" ) - uhttpd_Write( xResult ) - ELSE - // Application in HRB module is responsible to send HTML content - ENDIF - - CATCH oError - - WriteToConsole( "Error!" ) - - uhttpd_AddHeader( "Content-Type", "text/html" ) - uhttpd_Write( "Error" ) - uhttpd_Write( "
Description: " + hb_cStr( oError:Description ) ) - uhttpd_Write( "
Filename: " + hb_cStr( oError:filename ) ) - uhttpd_Write( "
Operation: " + hb_cStr( oError:operation ) ) - uhttpd_Write( "
OsCode: " + hb_cStr( oError:osCode ) ) - uhttpd_Write( "
GenCode: " + hb_cStr( oError:genCode ) ) - uhttpd_Write( "
SubCode: " + hb_cStr( oError:subCode ) ) - uhttpd_Write( "
SubSystem: " + hb_cStr( oError:subSystem ) ) - uhttpd_Write( "
Args: " + hb_cStr( hb_ValToExp( oError:args ) ) ) - uhttpd_Write( "
ProcName: " + hb_cStr( procname( 0 ) ) ) - uhttpd_Write( "
ProcLine: " + hb_cStr( procline( 0 ) ) ) - END - - RETURN MakeResponse() - -STATIC FUNCTION Handler_CgiScript( cFileName ) - LOCAL xResult - - IF ( CGIExec( uhttpd_OSFileName(cFileName), @xResult ) ) == 0 - - //uhttpd_AddHeader( "Content-Type", cI ) - //uhttpd_Write( xResult ) - RETURN "HTTP/1.1 200 OK " + CR_LF + xResult - - ELSE - - uhttpd_AddHeader( "Content-Type", "text/html" ) - IF !Empty( xResult ) - uhttpd_Write( xResult ) - ELSE - uhttpd_Write( "CGI Error" ) - ENDIF - - ENDIF - - RETURN MakeResponse() - // ---------------------------------------------------------------------------------- // HANDLERS // ---------------------------------------------------------------------------------- @@ -2545,6 +2546,85 @@ STATIC FUNCTION Handler_ServerStatus() RETURN MakeResponse() +STATIC FUNCTION Handler_HrbScript( cFileName ) + LOCAL xResult + LOCAL cHRBBody, pHRB, oError + + TRY + // Lock HRB to avoid MT race conditions + IF !HRB_ACTIVATE_CACHE + cHRBBody := HRB_LoadFromFile( uhttpd_OSFileName( cFileName ) ) + ENDIF + IF hb_mutexLock( s_hmtxHRB ) + IF HRB_ACTIVATE_CACHE + // caching modules + IF !hb_HHasKey( s_hHRBModules, cFileName ) + hb_HSet( s_hHRBModules, cFileName, HRB_LoadFromFile( uhttpd_OSFileName( cFileName ) ) ) + ENDIF + cHRBBody := s_hHRBModules[ cFileName ] + ENDIF + IF !EMPTY( pHRB := HB_HRBLOAD( cHRBBody ) ) + + xResult := HRBMAIN() + + HB_HRBUNLOAD( pHRB ) + ELSE + uhttpd_SetStatusCode( 404 ) + t_cErrorMsg := "File does not exist: " + cFileName + ENDIF + hb_mutexUnlock( s_hmtxHRB ) + + ENDIF + + IF HB_ISSTRING( xResult ) + uhttpd_AddHeader( "Content-Type", "text/html" ) + uhttpd_Write( xResult ) + ELSE + // Application in HRB module is responsible to send HTML content + ENDIF + + CATCH oError + + WriteToConsole( "Error!" ) + + uhttpd_AddHeader( "Content-Type", "text/html" ) + uhttpd_Write( "Error" ) + uhttpd_Write( "
Description: " + hb_cStr( oError:Description ) ) + uhttpd_Write( "
Filename: " + hb_cStr( oError:filename ) ) + uhttpd_Write( "
Operation: " + hb_cStr( oError:operation ) ) + uhttpd_Write( "
OsCode: " + hb_cStr( oError:osCode ) ) + uhttpd_Write( "
GenCode: " + hb_cStr( oError:genCode ) ) + uhttpd_Write( "
SubCode: " + hb_cStr( oError:subCode ) ) + uhttpd_Write( "
SubSystem: " + hb_cStr( oError:subSystem ) ) + uhttpd_Write( "
Args: " + hb_cStr( hb_ValToExp( oError:args ) ) ) + uhttpd_Write( "
ProcName: " + hb_cStr( procname( 0 ) ) ) + uhttpd_Write( "
ProcLine: " + hb_cStr( procline( 0 ) ) ) + END + + RETURN MakeResponse() + +STATIC FUNCTION Handler_CgiScript( cFileName ) + LOCAL xResult + + IF ( CGIExec( uhttpd_OSFileName(cFileName), @xResult ) ) == 0 + + //uhttpd_AddHeader( "Content-Type", cI ) + //uhttpd_Write( xResult ) + RETURN "HTTP/1.1 200 OK " + CR_LF + xResult + + ELSE + + uhttpd_AddHeader( "Content-Type", "text/html" ) + IF !Empty( xResult ) + uhttpd_Write( xResult ) + ELSE + uhttpd_Write( "CGI Error" ) + ENDIF + + ENDIF + + RETURN MakeResponse() + STATIC FUNCTION LoadMimeTypes() LOCAL hMimeTypes // TODO: load mime types from file @@ -2596,33 +2676,14 @@ STATIC FUNCTION LoadMimeTypes() } RETURN hMimeTypes -STATIC FUNCTION FileUnAlias( cScript ) - LOCAL cFileName, x - - // Checking if the request contains a Script Alias - IF HB_HHasKey( s_hScriptAliases, cScript ) - // in this case I have to substitute the alias with the real file name - cFileName := hb_hGet( s_hScriptAliases, cScript ) - - // substitute macros - cFileName := StrTran( cFileName, "$(DOCROOT_DIR)", _SERVER[ "DOCUMENT_ROOT" ] ) - cFileName := StrTran( cFileName, "$(APP_DIR)" , Exe_Path() ) - ENDIF - - IF cFileName == NIL - - // Checking if the request contains an alias - FOR EACH x IN s_hAliases - IF x:__enumKey() == Left( cScript, Len( x:__enumKey() ) ) - cFileName := x:__enumValue() + SubStr( cScript, Len( x:__enumKey() ) + 1 ) - - // substitute macros - cFileName := StrTran( cFileName, "$(DOCROOT_DIR)", _SERVER[ "DOCUMENT_ROOT" ] ) - cFileName := StrTran( cFileName, "$(APP_DIR)" , Exe_Path() ) - EXIT - ENDIF - NEXT - - ENDIF - -RETURN cFileName +STATIC FUNCTION GT_notifier( nEvent, xParams ) + LOCAL nReturn := 0 + DO CASE + CASE nEvent == HB_GTE_CLOSE + IF hb_mutexLock( s_hmtxBusy ) + s_lQuitRequest := TRUE + nReturn := 1 + hb_mutexUnlock( s_hmtxBusy ) + ENDIF + ENDCASE +RETURN nReturn diff --git a/harbour/contrib/examples/uhttpd/uhttpdc.c b/harbour/contrib/examples/uhttpd/uhttpdc.c index 7b4e98c1ce..349186380d 100644 --- a/harbour/contrib/examples/uhttpd/uhttpdc.c +++ b/harbour/contrib/examples/uhttpd/uhttpdc.c @@ -85,6 +85,11 @@ BOOL win_SysRefresh( int iMsec ) { switch( msg.message ) { + case WM_CLOSE: + { + iQuit = 1; + goto stopLoop; + } case WM_QUIT: { iQuit = ( int ) msg.wParam; @@ -123,14 +128,14 @@ stopLoop: HB_FUNC( WIN_SYSREFRESH ) { - hb_retl( win_SysRefresh( hb_parni( 1 ) ) ); + hb_retni( win_SysRefresh( hb_parni( 1 ) ) ); } #else HB_FUNC( WIN_SYSREFRESH ) { - hb_retl( TRUE ); + hb_retni( ( int ) FALSE ); } #endif