📄 siteroot.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/SiteRoot/SiteRoot.cpp,v $
* $Date: 2003/05/13 18:42:22 $
*
Description:
\*____________________________________________________________________________*/
//$SourceTop:$
#include <stdio.h>
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "HandBase.h"
#include "PIStrStr.h"
#include "Admin.h"
/*
** #define D { cerr << __FILE__ << ": " << __LINE__ << endl; }
*/
#define D
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#define KEY_CONF_CONFIGFILE "ConfigFile"
#define KEY_CONF_LOGICOBJECT "LogicObject"
#define KEY_CONF_ROOTDIRECTORY "RootDirectory"
#define KEY_CONF_REALM "Realm"
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#if 0
/*
** HTML documentation for this handler
*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
SiteRoot
Description:
Dispatches between web and remote administration requests and maintains
a local configuration file by processing remote administration requests.
Options:
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)
<TR>
<TD>ConfigFile
<TD>+
<TD><file name>
<TD>The remotely administered configuration file
<TD>ConfigFile "./Config.pi3"
<TR>
<TD>LogicObject
<TD>+
<TD><object name>
<TD>The top logic handler object to handle web requests
<TD>LogicObject "TopSiteLogic"
<TR>
<TD>Realm
<TD>-
<TD><string>
<TD>The authentication realm for remote administration
<TD>Realm "RemoteAdmin"
<TR>
<TD>RootDirectory
<TD>-
<TD><path name>
<TD>A root directory for this object
<TD>RootDirectory "../"
</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>
<H4>Description of Options</H4>
<H5>ConfigFile</H5>
The configuration file, which is maintained by this object using remote
administration. It can be the current configuration file as well as another
file containing the remotely administered sections. The path to the file can
be absolute or is relative to the server root.
<H5>LogicObject</H5>
This object is the top of the handler logic, which is called by the SiteRoot
object for non-administrative request.
<H5>Realm</H5>
The authentication realm for remote administration. And authentication object
for that realm must exist elsewhere in the configuration. For remote administration
it is strongly recommended to use SSL for channel encryption or at least Digest
Access Authentication for secure remote authentication.
<H5>RootDirectory</H5>
This sets the root directory for everything under this object, i.e. the path
root for the host object, which processes remote administration requests.
Example:
<PRE>
<Object>
Name SiteRoot
Class SiteRootClass
LogicObject "TopSiteLogic"
Realm "Administration"
ConfigFile "./Conf/Config.pi3"
</Object>
</PRE>
/*___+++HTMLDOC_END+++___*/
#endif
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class SiteRoot : public HandlerBaseSR
{
private:
/* ---
Fastkeys
--- */
const char *pFKAdminCommand;
/* ---
Configuration data
--- */
PIString sConfigFile; /* the configuration file name for site */
PIString sConfigFileTmp; /* temp. config file for replace ops. */
PISync *pMutex; /* mutex to synchronize access to data */
PIObject *pLogicObject; /* the logic object being used */
int iInSiteCount; /* current number of threads using site */
PIObject *pNewLogicObject; /* new site ready to be swapped in */
PIDB *pDB; /* DB for object */
PIString sLogicObjectName; /* Name of logic object */
PIString sRootDirectory; /* The root directory */
PIString sRealm; /* The authentication realm */
protected:
int Parameter( const char *pVariable, const char *pValue,
const char *pWhere )
{
assert( pVariable && pValue );
PIOStrStream os;
os << pWhere << "SiteRoot: ";
if ( 0 )
{
}
else if ( !PIUtil_stricmp( KEY_CONF_LOGICOBJECT, pVariable ) )
{
sLogicObjectName = pValue;
}
else if ( !PIUtil_stricmp( KEY_CONF_ROOTDIRECTORY, pVariable ) )
{
Pi3String *pTmp = Pi3String_new( pValue );
HTTPCore_relativeToAbsolutePath( PIObject_getDB( Object() ),
pValue, pTmp );
sRootDirectory = Pi3String_getPtr( pTmp );
Pi3String_delete( pTmp );
/*
** Set this as the root directory for everything under this
** object
*/
PIDB_replace( PIObject_getDB( Object() ), PIDBTYPE_STRING,
KEY_INT_HOSTROOT, (void *)(const char *)sRootDirectory, 0 );
}
else if ( !PIUtil_stricmp( KEY_CONF_CONFIGFILE, pVariable ) )
{
sConfigFile = pValue;
}
else if ( !PIUtil_stricmp( KEY_CONF_REALM, pVariable ) )
{
sRealm = pValue;
}
else
{
os << "Unknown directive '" << pVariable <<
"'" << ends;
CONFIG_ERR( Object(), os.str() );
return 0;
};
return 1;
};
public:
SiteRoot( PIObject *pObject, int iArgc, const char *ppArgv[] )
: HandlerBaseSR( pObject ),
pFKAdminCommand( PIDB_getFastKey( KEY_HTTP_X_ADMINCOMMAND,
PIDBTYPE_RFC822 ) ),
pMutex( PIPlatform_allocLocalMutex() ),
pLogicObject( 0 ),
iInSiteCount( 0 ),
pNewLogicObject( 0 ),
pDB( 0 )
{
/*
** Reset the DB for future object loads/unloads
*/
pDB = PIDB_new( PIObject_getDB(pObject), "SiteRoot" );
PIObject_setDB( pObject, pDB );
ReadParameters( iArgc, ppArgv );
if ( !IsOK() )
{ return; };
if ( sConfigFile.Len()==0 )
{
CONFIG_ERR( Object(), "SiteRoot: 'ConfigFile' not defined" );
SetOK( 0 );
return;
};
/*
** Convert configuration file to absoluate format.
*/
Pi3String *pTmp = Pi3String_new( sConfigFile );
HTTPCore_relativeToAbsolutePath( PIObject_getDB( Object() ),
sConfigFile, pTmp );
sConfigFile = Pi3String_getPtr( pTmp );
Pi3String_delete( pTmp );
sConfigFileTmp = sConfigFile;
sConfigFileTmp.Concatenate( ".new" );
pLogicObject = LoadConfigurationFile( sConfigFile, 0 );
if ( !pLogicObject )
{
SetOK( 0 );
return;
};
};
~SiteRoot()
{
assert( pNewLogicObject==0 );
assert( iInSiteCount==0 );
PISync_delete( pMutex );
PIObject_delete( pLogicObject, 0, 0 );
if ( pDB )
{
PIObject_setDB( Object(), PIDB_getParent( pDB ) );
PIDB_delete( pDB );
pDB = 0;
};
};
/*
** Load up the logic object for this site from the specified
** configuration file
*/
PIObject *LoadConfigurationFile( const char *pFile, int iNewConfig )
{
PIDB *pDB = PIObject_getDB( Object() );
PIConfig *pConfigDB = 0;
PIObject *pObject = 0;
if ( PIConfig_loadConfigurationFile( pDB, pFile, &pConfigDB ) )
{
/*
** Write the error message into the same file that the
** configuration was written into, after deleting it if this
** is a new configuration.
*/
if ( iNewConfig )
{
::remove( pFile );
PILog_writeDiagnostics( pDB, PILOG_ALL, pFile );
};
return 0;
};
pObject = PIObject_load( pDB, pConfigDB, sLogicObjectName, 0, 0 );
if ( !pObject )
{
/*
** Write the error message into the same file that the
** configuration was written into, after deleting it if
** this is a new configuration
*/
if ( iNewConfig )
{
::remove( pFile );
PILog_writeDiagnostics( pDB, PILOG_ALL, pFile );
};
};
/*
** Delete the configuration DB
*/
PIDB_delete( (PIDB *)pConfigDB );
/* ---
Delete the configuration DB
--- */
PIDBIterator *pIter = PIDB_getIterator( pDB, PIDBTYPE_TREE,
PIDBKEY_CONFIGURATION, 0 );
if ( pIter )
{
for( ;
PIDBIterator_atValidElement( pIter );
PIDBIterator_next( pIter )
)
{
PIDBIterator_removeCurrent( pIter );
};
PIDBIterator_delete( pIter );
};
/*
** The new object has been loaded
*/
return pObject;
};
/*
** Send the configuration file to the client
*/
int SendConfigFile( PIHTTP &tPIHTTP )
{
enum { BUF_SIZE=63 };
char szBuf[BUF_SIZE+1];
unsigned long ulLen;
PIDB *pR = tPIHTTP.pResponseDB;
PIDB *pQ = tPIHTTP.pRequestDB;
int iRet = PIAPI_ABORT;
PIFInfo *pFInfo = PIFInfo_new( sConfigFile );
if ( !pFInfo || !PIFInfo_exists( pFInfo ) ||
!PIFInfo_isRegular( pFInfo ) )
{
HTTPCore_logError( &tPIHTTP, "SiteRoot: Configuration file \
'%s' does not exist or is not a regular file (internal error).",
(const char *)sConfigFile );
iRet = HTTPUtil_doHTTPError( &tPIHTTP, ST_INTERNALERROR );
goto done;
};
/*
** Set the content type and content length
*/
/* --- content type --- */
PIDB_add( pR, PIDBTYPE_RFC822, KEY_HTTP_CONTENTTYPE, (void *)ADMIN_CONTENTTYPE,
0 );
/* --- content-length --- */
ulLen = PIFInfo_getSize( pFInfo );
sprintf( szBuf, "%lu", ulLen );
PIDB_add( pR, PIDBTYPE_RFC822, KEY_HTTP_CONTENTLENGTH, szBuf, 0 );
/* --- status --- */
tPIHTTP.iStatus = ST_OK;
/* --- start response --- */
if ( HTTPCore_sendGeneralHeaders( &tPIHTTP ) ||
HTTPCore_sendEntityHeaders( &tPIHTTP, pR ) )
{
HTTPCore_logError( &tPIHTTP, "%s", "SiteRoot: Failed to send \
response headers for admin command." );
iRet = HTTPUtil_doHTTPError( &tPIHTTP, ST_INTERNALERROR );
goto done;
};
/* --- send the file --- */
iRet = HTTPUtil_sendFile( tPIHTTP.pBuffer, pFInfo, PIIOBUF_NONE, 0 );
done:
if ( pFInfo ) { PIFInfo_delete( pFInfo ); };
return iRet;
};
/*
** Swallow up extranious input from client, typically in the
** event of an error.
*/
void SwallowInput( PIIOBuffer *pBuffer, int iLen )
{
int iRead;
for( ; iLen>0 && PIIOBuffer_read( pBuffer, &iRead ); iLen-=iRead );
assert( iLen==0 );
};
/*
** Get the configuration file from the remote client
*/
int GetConfigFile( PIHTTP &tPIHTTP )
{
enum { FILE_BUF_SIZE=4096 };
char szFileBuf[FILE_BUF_SIZE];
int iRet = PIAPI_ABORT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -