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

📄 pathmap.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				}
			else if ( !PIUtil_stricmp( "COMPLETED", pValue ))
				{
                 iReturnStatus = PIAPI_COMPLETED;
				}
			else
                {
                os << "Return Status must be either CONTINUE or COMPLETED (default)." << ends;
                CONFIG_ERR( Object(), os.str() );
                return 0;
                };
			}
		else
			{
			os << "Unknown directive '" << pVariable << "'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};	

		return 1;
		};

public:
	PathMapper( PIObject *pTheObject, int iArgc, const char *ppArgv[] )
	:	HandlerBaseHTTP( pTheObject ),
		iCaseSensitive( 1 ),
		iPathInfo( 0 ),
		iRedirect( 0 ),
		pFKPath( PIDB_getFastKey( KEY_INT_PATH, PIDBTYPE_STRING ) ),
		iReturnStatus( PIAPI_COMPLETED )
		{	
		ReadParameters( iArgc, ppArgv );

		/* ---
		Validate parameters
		--- */
		if ( sFrom==PIString::Empty() )
			{
			CONFIG_ERR( Object(), "PathMapper: 'From' not defined" );
			};
		if ( sTo==PIString::Empty() )
			{
			CONFIG_ERR( Object(), "PathMapper: 'To' not defined" );
			};
		if ( !iRedirect )
			{
			Pi3String *pTmp = Pi3String_new( sTo );
			HTTPCore_relativeToAbsolutePath( PIObject_getDB( Object() ),
				sTo, pTmp );
			sTo = Pi3String_getPtr( pTmp );
			Pi3String_delete( pTmp );
			}
		};

	virtual ~PathMapper()
		{
		for ( DblListIterator i( lActions ); !i.BadIndex(); i++ )
			{
			Pi3Expression_delete( (Pi3Expression *)i.Current() );
			};
		};

	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\

								Utilities

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
	/* ---
	Create a translated PATH_INFO
	--- */
	void CreatePathTranslated( PIHTTP &tPIHTTP )
		{
		PIDB *pR = tPIHTTP.pResponseDB;
		const char *pPathInfo = (const char*)PIDB_lookup( pR, PIDBTYPE_STRING, 
			KEY_INT_PATHINFO, 0 );
		if ( pPathInfo )
			{
			/*
			** Path translated is the PATH_INFO, mapped from virtual to
			** physical path
			*/
			/* --- make sub request context --- */
			PIHTTP *pChildHTTP = PIHTTP_newChild( &tPIHTTP );

			/* --- set path --- */
			PIDB_replace( pChildHTTP->pResponseDB, PIDBTYPE_STRING,
				KEY_INT_PATH, (void *)pPathInfo, 0 );

			/* --- dispatch the sub request across the mapping phase --- */
			int iRet = PIAPI_ERROR;
			iRet = HTTPCore_dispatch(
				pChildHTTP,
				PH_MAPPING,
				PH_MAPPING );
			
			/* --- copy the childs path to this path translated --- */
			if ( iRet==PIAPI_COMPLETED &&
				((pChildHTTP->iStatus==0) || (pChildHTTP->iStatus==ST_OK)) )
				{
				void *pPath = PIDB_lookup( pChildHTTP->pResponseDB,
					PIDBTYPE_STRING, KEY_INT_PATH, 0 );
				PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_PATHTRANSLATED, 
					pPath, 0 );
				}
			else
				{
				PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_PATHTRANSLATED, 
					0, 0 );
				};
			PIHTTP_delete( pChildHTTP );
			};
		};

	/* ---
	Create script name variable. The is the URI without the
	path info at the end.
	--- */
	void CreateScriptName( PIDB *pQ, PIDB *pR )
		{
		const char *pURI = (const char*)PIDB_lookup( pQ, PIDBTYPE_STRING, 
			KEY_HTTP_URI, 0 );
		const char *pPathInfo = (const char*)PIDB_lookup( pR, PIDBTYPE_STRING, 
			KEY_INT_PATHINFO, 0 );
		
		if ( !pURI )
			{ return; };
		if ( !pPathInfo )
			{
			PIDB_add( pR, PIDBTYPE_STRING, KEY_INT_SCRIPTNAME,
				(void *)pURI, 0 );
			return;
			};
		int iULen = strlen( pURI );
		int iPLen = strlen( pPathInfo );
		if ( iULen < iPLen )
			{ return; };
		PIString sTmp( pURI, iULen - iPLen );
		PIDB_add( pR, PIDBTYPE_STRING, KEY_INT_SCRIPTNAME,
			(void *)(const char *)sTmp, 0 );
		};

	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
								Real stuff

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
	int Handle( int /* iPhase */, PIHTTP &tPIHTTP, PIIOBuffer & )
		{
		PIDB *pR = tPIHTTP.pResponseDB;
		PIDB *pQ = tPIHTTP.pRequestDB;

		/* ---
		Check if the first component of the path matches
		--- */
		/* --- get Path --- */
		const char *pPath = (const char *)PIDB_lookup( pR, PIDBTYPE_STRING,
			pFKPath, PIDBFLAG_FASTKEY );
		if ( !pPath )
			{ return PIAPI_ERROR; };

		int iMatches = iCaseSensitive ?
			!strncmp( pPath, sFrom, sFrom.Len() ):
			!PIUtil_strncmpi( pPath, sFrom, sFrom.Len() );
		PIString sNewPath( sTo );
		if ( !iMatches )
			{
			return PIAPI_CONTINUE;
			};

		if ( iRedirect )
			{
			sNewPath.Concatenate( &( pPath[sFrom.Len()] ) );
			PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_PATH,
		        (void *)(const char *)sNewPath, 0 );

			/* ---
			Evaluate success expressions
			--- */
			for( DblListIterator i( lActions ); !i.BadIndex(); i++ )
				{
				Pi3Expression_write( (Pi3Expression *)i.Current(),
					&tPIHTTP, 0, 0, 0 );
				};
			return PIAPI_COMPLETED;
			};

		const char *pPathInfo = 0;
		if ( iPathInfo )
			{
			const char *pTmp = &( pPath[sFrom.Len()] );
			size_t i;
			for( i=0; pTmp[i]; i++ )
				{
				/* --- there need to be more then a remaining '/' --- */
//				if (strlen(pTmp) == i + 1 ) break;

				if ( pTmp[i]==HTTP_DIRSEPERATOR )
					{

					/*
					** Assumption: the string following the part of the path
					** representing a non-directory is the PathInfo
					** (i.e. ignoring executable scriptname, this will be
					** handled later)
					*/
					PIString sTmpPath(sNewPath);
					sTmpPath.Concatenate( PIString( pTmp, i ) );

					/*
					** skip directories
					*/
					PIFInfo *pFInfo = PIFInfo_new( sTmpPath ); 
					if ( PIFInfo_isDirectory( pFInfo ) )
						{
						PIFInfo_delete( pFInfo );
						continue;
						}
		
					PIFInfo_delete( pFInfo );

					/*
					** Ok, we found a valid PathInfo
					*/
					pPathInfo = &( pTmp[i] );
					break;
					};
				};

			if ( pPathInfo )
				{
				PIDB_add( pR, PIDBTYPE_STRING, KEY_INT_PATHINFO,
					(void *)pPathInfo, 0 );
				};

			sNewPath.Concatenate( PIString( pTmp, i ) );
			}
		else
			{
			sNewPath.Concatenate( &( pPath[sFrom.Len()] ) );
			};

		PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_PATH,
            (void *)(const char *)sNewPath, 0 );

		/* ---
		Setup path translated and script name
		--- */
		CreatePathTranslated( tPIHTTP );
		CreateScriptName( pQ, pR );

		/* ---
		Evaluate success expressions
		--- */
		for( DblListIterator i( lActions ); !i.BadIndex(); i++ )
			{
			Pi3Expression_write( (Pi3Expression *)i.Current(),
				&tPIHTTP, 0, 0, 0 );
			};

		return iReturnStatus;
		};
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PathMapper_constructor( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	return HandlerBaseHTTP_constructor(pObj, new PathMapper( pObj, iArgc, ppArgv));
}
#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name PathMapperClass
		Type LogicExtension
		Library HTTP
		OnClassLoad HandlerBaseHTTP_onClassLoad
		Constructor PathMapper_constructor
		CopyConstructor HandlerBaseHTTP_copyConstructor
		Destructor HandlerBaseHTTP_destructor
		Execute HandlerBaseHTTP_execute
	</Class>

	<Object>
		Name PathMapper
		Class PathMapperClass
		PathInfo "Yes"
	</Object>

/*___+++CNF_END+++___*/
#endif

⌨️ 快捷键说明

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