⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 admin.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    ** Read the response
    */
    iRet = HTTPCore_readHeaders( pBuffer, pR, RH_RESPONSE );
    if ( iRet )
        {
        sError = "Error reading response headers from remote server '";
        sError.Concatenate( pHost );
        sError.Concatenate( "'." );
        return iRet;
        };
    
    /* ---
    Check out status line from remote server
    --- */
    if ( ( pCLF = (const char *)
        PIDB_lookup( pR, PIDBTYPE_STRING, KEY_HTTP_CLF, 0 ) ) )
        {
        /* ++++ __________________________ +++ *
            Got status line
         * ++++ __________________________ +++ */
        /* --- grap the status code sent by the server --- */
        int i=0;
        for(; pCLF[i] && !(isspace(pCLF[i])); i++); /* advance to space */
        for(; pCLF[i] && (isspace(pCLF[i])); i++); /* advance past space */
        iRet = atoi( &(pCLF[i]) );

        /*
        ** Check out the status
        */
        if ( iRet>=200 && iRet<299 )
            {
            /* --- 2xx assume success under HTTP/1.0 --- */ 
            iRet = 0;
            };

        /*
	    ** Authentication handling
		*/
		if (( iRet == 401 )&&( *iAuthType == ID_NO_AUTH ))
			{
			sAuthenticate = (const char *)PIDB_lookup(
				pR, PIDBTYPE_RFC822, KEY_HTTP_WWW_AUTHENTICATE, 0 );
			*iAuthType = Internal_GetAuthType( pR );
			}

        if ( iRet )
            {
            /*
            ** Set the error message
            */
            sError = "Received error response from server:\n\n";
            sError.Concatenate( &(pCLF[i]) );
            sError.Concatenate( "\n\n" );

            /*
            ** Read the rest of the response
            */
            int iLen;
            const char *pErrorBuffer = PIIOBuffer_read( pBuffer, &iLen );
            while( pErrorBuffer )
                {
                PIString sTmp( pErrorBuffer, iLen );
                sError.Concatenate( sTmp );
                pErrorBuffer = PIIOBuffer_read( pBuffer, &iLen );
                };
            };
        };

    if ( !pCLF )
        {
        sError = "Error reading response from remote server '";
        sError.Concatenate( pHost );
        sError.Concatenate( "'." );
        iRet = 400;
        };

    if ( !iRet )
        {
        /* --- expect to get data? --- */
        if ( fnGetData!=0 )
            {
            iRet = (fnGetData)( pGetDataParam );
			if (iRet)
				{
				sError = "Response from remote server seems not to ";
				sError.Concatenate( "contain a valid Pi3 config file." );
				iRet = 400;
				};
            };
        };

	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
    All about getting a configuration file from the server.
\*____________________________________________________________________________*/
/*
** Structure for passing information to get and put callback functions
*/
struct DataParam
{
    PIIOBuffer *pBuffer;
    QConfig *pConfig;
    int iLength;
    void *pData;
    PIString tMessage;
};
static int Internal_fnGetDataFunction( void *pParam )
{
    DataParam &tParam = *( (DataParam *)pParam );
    
    /* --- make PIIStream and load the configuration file --- */
    PIIStream is( tParam.pBuffer );
    return Pi3File_readConfig( tParam.pConfig, is );
}
static int Internal_loadConfigFromServer(
    ConnectionPool *pPool,
    QConfig *pConfig,
    const char *pHost,
    int iPort,
    const char *pUsername,
    const char *pPassword,
	PIString &sRealm,
	cbAuthData *pGetAuthData,
	void *pUserData,
    PIString &sError )
{
    const char *pCLF = 0;
	int iRetry = 2;
	int isInteractive = !(strlen(pUsername) && strlen(pPassword));
	PIString sAuthenticate;
	int iAuthType = ID_NO_AUTH;

again:

    /*
    ** Get a connnection to the remote host from the connection pool
    */
    PIIOBuffer *pBuffer = ConnectionPool_grapConnection( pPool,
        pHost, iPort );

    if ( !pBuffer )
        { 
        sError = "Could not open connection to server '";
        sError.Concatenate( pHost );
        sError.Concatenate( "' for configuration download." );		
        return -1;
        };

    DataParam tParam;
    tParam.pBuffer = pBuffer;
    tParam.pConfig = pConfig;
    int iRet = Internal_makeHTTPRequest(
        pBuffer,
        MD_GET,
        KEY_ADMIN_GETCONFIG,
        pHost,
        iPort,
        ADMIN_CONTENTTYPE,
        "0",
        0, 0,        /* no data to put */
        Internal_fnGetDataFunction, &tParam,
		pUsername,
		pPassword,
		sAuthenticate,
		&iAuthType,
        sError
        );

    /*
    ** Done with connection
    */
    ConnectionPool_releaseConnection( pPool, pBuffer, true );

	if (iRet == 401)
		{
		/*
		** Interactive login
		*/
		if ( isInteractive )
			{
			sRealm = Internal_GetRealm( sAuthenticate );
			if ( !pGetAuthData( pUserData )) { return iRet; };
			}
		else 
			{
			iRetry--;
			}

		if (iRetry) { goto again; };
		}

	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
    All about putting a configuration file onto the server.
\*____________________________________________________________________________*/
static int Internal_fnPutDataFunction( void *pParam )
{
    DataParam &tParam = *( (DataParam *)pParam );
    
    /* --- make an PIOStream and write the configuration file --- */
    return !PIIOBuffer_write( tParam.pBuffer, (const char *)tParam.pData,
        tParam.iLength, PIIOBUF_NOBUFFER );
}
static int Internal_writeConfigToServer(
    ConnectionPool *pPool,
    QConfig *pConfig,
    const char *pHost,
    int iPort,
    const char *pUsername,
    const char *pPassword,
	PIString &sRealm,
	cbAuthData *pGetAuthData,
	void *pUserData,
    PIString &sError )
{
    const char *pCLF = 0;
	PIString sAuthenticate;
	int iAuthType = ID_NO_AUTH;
    int iRet = 0;
	int iRetry = 2;
	int isInteractive = !(strlen(pUsername) && strlen(pPassword));
	enum { BUF_SIZE=63 };
    char szFileSize[BUF_SIZE+1];
    PIFInfo *pFInfo = 0;
    int iLen;
    PIFMap *pFileMap = 0;
    const char *pMap = 0;
    DataParam tParam;
    ofstream ofs;
    int iTempFileWritten = 0;

again:

    /*
    ** Get a connnection to the remote host from the connection pool
    */
    PIIOBuffer *pBuffer = ConnectionPool_grapConnection( pPool,
        pHost, iPort );

    if ( !pBuffer )
        {
        sError = "Could not open connection to server '";
        sError.Concatenate( pHost );
        sError.Concatenate( "' for configuration upload." );
        return -1;
        };

    /*
    ** Open a temporary file and write the configuration into it
    */
    char szTmpFile[L_tmpnam];
    if ( !tmpnam( szTmpFile ) )
        {
        sError = "Could not create local temporary file for configuration \
upload.";
        iRet = -1;
        goto done;
        };
    ofs.open( szTmpFile );
    if ( !ofs.good() )
        {
        sError = "Could not open local temporary file '";
        sError.Concatenate( szTmpFile );
        sError.Concatenate( "' for configuration upload." );
        iRet = -1;
        goto done;
        };

    iTempFileWritten = 1;
    iRet = Pi3File_writeConfig( pConfig, ofs );
    ofs.close();
    if ( iRet )
        {
        sError = "Could write to local temporary file '";
        sError.Concatenate( szTmpFile );
        sError.Concatenate( "' for configuration upload." );
        goto done;
        };

    pFInfo = PIFInfo_new( szTmpFile );
    if ( !pFInfo )
        {
        sError = "Internal_writeConfigToServer: internal error.";
        iRet = -1;
        goto done;
        };

    sprintf( szFileSize, "%lu", PIFInfo_getSize( pFInfo ) );

    if ( PIFInfo_getSize( pFInfo )>0 )
        { pFileMap = PIFMap_new( pFInfo ); };
    if ( pFileMap )
        {
        pMap = (const char *)PIFMap_lock( pFileMap, &iLen );
        if ( pMap && iLen!=PIFInfo_getSize( pFInfo ) )
            {
            pMap = 0;
            };
        };

    /*
    ** If we got to this point without pMap, an error occurred someplace
    */
    if ( !pMap )
        {
        sError = "Could not memory map local temporary file '";
        sError.Concatenate( szTmpFile );
        sError.Concatenate( "' for configuration upload." );
        iRet = -1;
        goto done;
        };

    tParam.pBuffer = pBuffer;
    tParam.pConfig = pConfig;
    tParam.iLength = iLen;
    tParam.pData = (void *)pMap;
    iRet = Internal_makeHTTPRequest(
        pBuffer,
        MD_PUT,
        KEY_ADMIN_SAVECONFIG,
        pHost,
        iPort,
        ADMIN_CONTENTTYPE,
        szFileSize,
        Internal_fnPutDataFunction, &tParam,
        0, 0,        /* no data to get */
	    pUsername,
		pPassword,
		sAuthenticate,
		&iAuthType,
        sError
        );

    /*
    ** Done with connection
    */
done:
    if ( pFileMap ) { PIFMap_delete( pFileMap ); };
    if ( pFInfo ) { PIFInfo_delete( pFInfo ); };
    if ( iTempFileWritten )
        {
        ::remove( szTmpFile );
        };

    ConnectionPool_releaseConnection( pPool, pBuffer, true );

	if (iRet == 401)
		{
		/*
		** Interactive login
		*/
		if ( isInteractive )
			{
			sRealm = Internal_GetRealm( sAuthenticate );
			if ( !pGetAuthData( pUserData )) { return iRet; };
			}
		else 
			{
			iRetry--;
			}

		if (iRetry) { goto again; };
		}

	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
Admin *Admin_newLocal( const char *pFileName )
{
    return PI_NEW( _Admin( pFileName ) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
Admin *Admin_newRemote(
    PIObject *pParent,
    const char *pHost,
    int iPort,
    const char *pUsername,
    const char *pPassword,
	cbAuthData *pGetAuthData,
	void *pUserData
	)
{
    return PI_NEW( _Admin( pParent, pHost, iPort,
		pUsername, pPassword, pGetAuthData, pUserData ) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void Admin_delete( Admin *pAdmin )
{
    PI_DELETE( pAdmin );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Admin_loadConfig( Admin *pAdmin, QConfig *pConfig, PIString &sError )
{
    if ( !pAdmin )
        { return -1; };

    sError = PIString::Empty();

    if ( pAdmin->IsRemote() )
        {
        return Internal_loadConfigFromServer(
            pAdmin->pPool,
            pConfig,
            pAdmin->sHost,
            pAdmin->iPort,
			pAdmin->sUsername,
 			pAdmin->sPassword,
 			pAdmin->sRealm,
			pAdmin->cbGetAuthData,
			pAdmin->pUserData,
			sError );
        }
    else
        {
        return Internal_loadConfigFromFile( pConfig, pAdmin->sFileName );
        };
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Admin_saveConfig( Admin *pAdmin, QConfig *pConfig, PIString &sError )
{
    if ( !pAdmin )
        { return -1; };

    if ( pAdmin->IsRemote() )
        {
        return Internal_writeConfigToServer(
            pAdmin->pPool,
            pConfig,
            pAdmin->sHost,
            pAdmin->iPort,
			pAdmin->sUsername,
 			pAdmin->sPassword,
 			pAdmin->sRealm,
			pAdmin->cbGetAuthData,
			pAdmin->pUserData,
            sError );
        }
    else
        {
        return Internal_writeConfigToFile( pConfig, pAdmin->sFileName );
        };
}

const char *Admin_GetRealm(Admin *pAdmin)
{
	return pAdmin->sRealm;
}

const char *Admin_GetUsername(Admin *pAdmin)
{
	return pAdmin->sUsername;
}

void Admin_SetUsername(Admin *pAdmin, const char *pUsername)
{
	pAdmin->sUsername = pUsername;
}

void Admin_SetPassword(Admin *pAdmin, const char *pPassword)
{
	pAdmin->sPassword = pPassword;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -