From c05fcbd7cdd4d6d808e6f0f5ea3caae844e5873c Mon Sep 17 00:00:00 2001 From: Francesco Saverio Giudice Date: Mon, 30 Mar 2009 19:07:18 +0000 Subject: [PATCH] 2009-03-30 21:05 UTC+0200 Francesco Saverio Giudice (info/at/fsgiudice.com) * harbour/contrib/examples/uhttpd/uhttpd.prg * error string formatting using hb_GTInfo() functions. Tested only in windows. * harbour/contrib/examples/uhttpd/session.prg + added retrying in case of error in writing / deleting session files. * harbour/contrib/examples/uhttpd/cgifunc.prg * changed FCreate() to hb_FCreate() --- harbour/ChangeLog | 9 ++++ harbour/contrib/examples/uhttpd/cgifunc.prg | 9 +--- harbour/contrib/examples/uhttpd/session.prg | 46 +++++++++++++++------ harbour/contrib/examples/uhttpd/uhttpd.prg | 22 +++++++--- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 75d59341cb..dd4a360b6b 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,15 @@ 2009-12-31 13:59 UTC+0100 Foo Bar (foo.bar foobar.org) */ +2009-03-30 21:05 UTC+0200 Francesco Saverio Giudice (info/at/fsgiudice.com) + * harbour/contrib/examples/uhttpd/uhttpd.prg + * error string formatting + using hb_GTInfo() functions. Tested only in windows. + * harbour/contrib/examples/uhttpd/session.prg + + added retrying in case of error in writing / deleting session files. + * harbour/contrib/examples/uhttpd/cgifunc.prg + * changed FCreate() to hb_FCreate() + 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 diff --git a/harbour/contrib/examples/uhttpd/cgifunc.prg b/harbour/contrib/examples/uhttpd/cgifunc.prg index 654b61ec57..857b089f87 100644 --- a/harbour/contrib/examples/uhttpd/cgifunc.prg +++ b/harbour/contrib/examples/uhttpd/cgifunc.prg @@ -714,14 +714,9 @@ PROCEDURE uhttpd_WriteToLogFile( cString, cLog, lCreate ) IF cLog <> NIL IF !lCreate .AND. FILE( cLog ) - nHandle := FOpen( cLog, FO_READWRITE + FO_SHARED) + nHandle := FOpen( cLog, FO_READWRITE + FO_SHARED ) ELSE - nHandle := FCreate( cLog ) - // After creation, I close and reopen it shared - IF Ferror() == 0 .AND. nHandle > 0 - FClose( nHandle ) - nHandle := FOpen( cLog, FO_READWRITE + FO_SHARED) - ENDIF + nHandle := hb_FCreate( cLog, FC_NORMAL, FO_READWRITE + FO_SHARED ) //__OutDebug( "Create ", nHandle ) ENDIF diff --git a/harbour/contrib/examples/uhttpd/session.prg b/harbour/contrib/examples/uhttpd/session.prg index 39b5bb91f6..cd065fd62f 100644 --- a/harbour/contrib/examples/uhttpd/session.prg +++ b/harbour/contrib/examples/uhttpd/session.prg @@ -582,7 +582,7 @@ METHOD SessionRead( cID ) CLASS uhttpd_Session LOCAL nFileSize LOCAL cBuffer LOCAL nRetry := 0 - LOCAL nFError := 0 + DEFAULT cID TO ::cSID cFile := ::cSavePath + HB_OSPathSeparator() + ::cName + "_" + cID //TraceLog( "SessionRead: cFile", cFile ) @@ -620,6 +620,7 @@ METHOD SessionWrite( cID, cData ) CLASS uhttpd_Session LOCAL cFile LOCAL nFileSize LOCAL lOk := FALSE + LOCAL nRetry := 0 //TraceLog( "SessionWrite() - cID, cData", cID, cData ) DEFAULT cID TO ::cSID @@ -630,16 +631,21 @@ METHOD SessionWrite( cID, cData ) CLASS uhttpd_Session cFile := ::cSavePath + HB_OSPathSeparator() + ::cName + "_" + cID //TraceLog( "SessionWrite() - cFile", cFile ) IF nFileSize > 0 - IF ( nH := FCreate( cFile, FC_NORMAL ) ) <> -1 - IF ( FWrite( nH, @cData, nFileSize ) ) <> nFileSize - uhttpd_Die( "ERROR: On writing session file : " + cFile + ", File error : " + hb_cStr( FError() ) ) + DO WHILE nRetry++ <= ::nFileRetry + IF ( nH := hb_FCreate( cFile, FC_NORMAL, FO_READWRITE + FO_DENYWRITE ) ) <> -1 + IF ( FWrite( nH, @cData, nFileSize ) ) <> nFileSize + uhttpd_Die( "ERROR: On writing session file : " + cFile + ", File error : " + hb_cStr( FError() ) ) + ELSE + lOk := TRUE + ENDIF + FClose( nH ) ELSE - lOk := TRUE + //uhttpd_Die( "ERROR: On WRITING session file. I can not create session file : " + cFile + ", File error : " + hb_cStr( FError() ) ) + hb_idleSleep( ::nFileWait / 1000 ) + LOOP ENDIF - FClose( nH ) - ELSE - uhttpd_Die( "ERROR: On WRITING session file. I can not create session file : " + cFile + ", File error : " + hb_cStr( FError() ) ) - ENDIF + EXIT + ENDDO ELSE // If session data is empty, I will delete the file if exist //IF File( cFile ) @@ -653,6 +659,8 @@ RETURN lOk METHOD SessionDestroy( cID ) CLASS uhttpd_Session LOCAL cFile LOCAL lOk + LOCAL nRetry := 0 + //TraceLog( "SessionDestroy() - cID", cID ) DEFAULT cID TO ::cSID @@ -661,9 +669,22 @@ METHOD SessionDestroy( cID ) CLASS uhttpd_Session //TraceLog( "SessionDestroy() - cID, oCGI:h_Session", cID, DumpValue( oCGI:h_Session ) ) cFile := ::cSavePath + HB_OSPathSeparator() + ::cName + "_" + cID - IF !( lOk := ( FErase( cFile ) == 0 ) ) - uhttpd_Die( "ERROR: On deleting session file : " + cFile + ", File error : " + hb_cStr( FError() ) ) - ELSE + + lOk := FALSE + DO WHILE nRetry++ <= ::nFileRetry + IF ( lOk := ( FErase( cFile ) == 0 ) ) + EXIT + ELSE + hb_idleSleep( ::nFileWait / 1000 ) + LOOP + ENDIF + ENDDO + + //IF !( lOk := ( FErase( cFile ) == 0 ) ) + // uhttpd_Die( "ERROR: On deleting session file : " + cFile + ", File error : " + hb_cStr( FError() ) ) + //ELSE + + IF lOk //TraceLog( "SessionDestroy() - Sessione Eliminata - File " + cFile ) // Genero un nuovo SID ::RegenerateID() @@ -684,6 +705,7 @@ METHOD SessionGC( nMaxLifeTime ) CLASS uhttpd_Session //TraceLog( "GC: aFile[ F_NAME ], aFile[ F_DATE ], Date(), aFile[ F_TIME ], Time(), nSecs, nMaxLifeTime", ; // aFile[ F_NAME ], aFile[ F_DATE ], Date(), aFile[ F_TIME ], Time(), nSecs, nMaxLifeTime ) IF nSecs > nMaxLifeTime + // No error checking here, because if I cannot delete file now I will find it again on next loop FErase( ::cSavePath + HB_OSPathSeparator() + aFile[ F_NAME ] ) ENDIF NEXT diff --git a/harbour/contrib/examples/uhttpd/uhttpd.prg b/harbour/contrib/examples/uhttpd/uhttpd.prg index 9cf89a2cf0..8fa7801a5e 100644 --- a/harbour/contrib/examples/uhttpd/uhttpd.prg +++ b/harbour/contrib/examples/uhttpd/uhttpd.prg @@ -2321,6 +2321,8 @@ STATIC FUNCTION uhttpd_DefError( oError ) LOCAL nChoice LOCAL n + LOCAL cDateTime, cString + LOCAL cNewLine := hb_OSNewLine() // By default, division by zero results in zero IF oError:genCode == EG_ZERODIV .AND. ; @@ -2360,7 +2362,7 @@ STATIC FUNCTION uhttpd_DefError( oError ) cCallstack := "" n := 1 DO WHILE ! Empty( ProcName( ++n ) ) - cCallstack += "Called from " + ProcName( n ) + "(" + hb_NToS( ProcLine( n ) ) + ") " + cCallstack += "Called from " + ProcName( n ) + "(" + hb_NToS( ProcLine( n ) ) + ") " + cNewLine ENDDO // Build buttons @@ -2387,9 +2389,9 @@ STATIC FUNCTION uhttpd_DefError( oError ) DO WHILE nChoice == 0 IF cDOSError == NIL - nChoice := Alert( cMessage + " " + cCallstack, aOptions ) + nChoice := Alert( cMessage + ";" + cCallstack, aOptions ) ELSE - nChoice := Alert( cMessage + ";" + cDOSError + " " + cCallstack, aOptions ) + nChoice := Alert( cMessage + " " + cDOSError + ";" + cCallstack, aOptions ) ENDIF ENDDO @@ -2411,13 +2413,21 @@ STATIC FUNCTION uhttpd_DefError( oError ) cMessage += " " + cDOSError ENDIF - OutErr( hb_OSNewLine() ) + OutErr( cNewLine ) OutErr( cMessage ) - OutErr( hb_OSNewLine() ) + OutErr( cNewLine ) OutErr( cCallstack ) // Write to errorlog - uhttpd_WriteToLogFile( cMessage + HB_OsPathSeparator() + cCallstack, Exe_Path() + "\error.log" ) + cDateTime := HB_TTOC( hb_DateTime() ) + cString := Replicate( "*", 70 ) + cNewLine + ; + cDateTime + cNewLine + ; + Replicate( "*", 70 ) + cNewLine + ; + cMessage + cNewLine + ; + cCallstack + cNewLine + ; + Replicate( "*", 70 ) + cNewLine + + uhttpd_WriteToLogFile( cString, Exe_Path() + "\error.log" ) ErrorLevel( 1 ) QUIT