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

📄 virtualh.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		...
		HostMap VirtualHostByName VirtualHostObject="host1.foo.dom" \
			HostName="host1.foo.dom"
		...
	</Object>

	</PRE>
/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class VirtualHostByName : public HandlerBaseHTTP
{
private:
	PIObject *pVirtualHost;
	PIString sHostName;
	const char *pFKHost;

protected:
	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "VirtualHostByName: ";
		if ( !PIUtil_stricmp( KEY_CONF_VIRTUALHOSTOBJECT, pVariable ) )
			{
			if ( pVirtualHost )
				{
				os << "'VirtualHost' may only be specified once." << ends;
				CONFIG_ERR( Object(), os.str() );
				return 0;
				};
			pVirtualHost = PIObject_loadFromLine(
				PIObject_getDB( Object() ),
				PIObject_getConfigurationDB( Object() ),
				pValue );
			if ( !pVirtualHost )
				{/* --- error set by PIObject_loadFromLine() --- */return 0; };
			}
		else if ( !PIUtil_stricmp( KEY_CONF_HOSTNAME, pVariable ) )
			{
			sHostName = pValue;	
			}
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	VirtualHostByName( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	HandlerBaseHTTP( pObject ),
		pVirtualHost( 0 ),
		pFKHost( PIDB_getFastKey( KEY_HTTP_HOST, PIDBTYPE_RFC822 ) )
		{
		ReadParameters( iArgc, ppArgv );
		assert( pFKHost );
		};

	~VirtualHostByName()
		{
		if ( pVirtualHost )
			{ PIObject_delete( pVirtualHost, 0, 0 ); };
		}

	int Handle( int iPhase, PIHTTP &tPIHTTP, PIIOBuffer &/* tIOBuffer */ )
		{
		if ( iPhase!=PH_HOSTMAP ) { return PIAPI_ERROR; };
		
		/* --- lookup the client supplied 'Host' header --- */
		PIDB *pQ = tPIHTTP.pRequestDB;
		const char *pHost = (const char *)PIDB_lookup( pQ, PIDBTYPE_RFC822,
			pFKHost, PIDBFLAG_FASTKEY );

		if ( !pHost || PIUtil_stricmp( sHostName, pHost ) )
			{ return PIAPI_CONTINUE; };

		/* --- set this virtual host --- */
		tPIHTTP.pHostDB = PIObject_getDB( pVirtualHost );

		return PIAPI_COMPLETED;
		};
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int VirtualHostByName_constructor( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	return HandlerBaseHTTP_constructor( pObj, PI_NEW( VirtualHostByName( pObj,
		iArgc, ppArgv ) ) );
}

#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name VirtualHostByNameClass
		Type LogicExtension
		Library HTTP
		OnClassLoad HandlerBaseHTTP_onClassLoad
		Constructor VirtualHostByName_constructor
		CopyConstructor HandlerBaseHTTP_copyConstructor
		Destructor HandlerBaseHTTP_destructor
		Execute HandlerBaseHTTP_execute
	</Class>

	<Object>
		Name VirtualHostByName
		Class VirtualHostByNameClass
	</Object>

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

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_VIRTUALHOSTOBJECT		"VirtualHostObject"
#define KEY_CONF_URLPATH				"URLPath"

#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	VirtualHostByURL

Description:
	This handler allows the virtual host to be changed by remaping the
	the begining of the URL path from the client.

Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>VirtualHostObject
<TD>+
<TD>&lt;objectname&gt;
<TD>VirtualHost object to associate
<TD>VirtualHostObject="vhost1"

<TR>
<TD>URLPath
<TD>+
<TD>A URL Path fragment
<TD>The URL path to use
<TD>URLPath="/vhost1/"

</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>
	VirtualHostObject
</H5>
Specifies the virtual host object that will be associated with the request
if the client supplied 'Host' HTTP header matches the specified hostname.

<H5>
	URLPath
</H5>
Specifies the URL path to match. If this handler matches the begining of
the URL path, correctly, the matched portion will be removed from the
URL to make the URLPath based virtual hosting transparent to other phases.

Phase:
	HOSTMAP

Example:
	<PRE>
	&lt;Object&gt;
		Name VirtualHostByURL
		Class VirtualHostByURLClass
		BindAddressVariable "LocalAddr"	
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		HostMap VirtualHostByURL VirtualHostObject="host1.foo.dom" \
			URLPath="/host1/"
		...
	&lt;/Object&gt;

	</PRE>
/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class VirtualHostByURL : public HandlerBaseHTTP
{
private:
	PIObject *pVirtualHost;
	PIString sURLPath;
	const char *pFKPath;

protected:
	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "VirtualHostByURL: ";
		if ( !PIUtil_stricmp( KEY_CONF_VIRTUALHOSTOBJECT, pVariable ) )
			{
			if ( pVirtualHost )
				{
				os << "'VirtualHost' may only be specified once." << ends;
				CONFIG_ERR( Object(), os.str() );
				return 0;
				};
			pVirtualHost = PIObject_loadFromLine(
				PIObject_getDB( Object() ),
				PIObject_getConfigurationDB( Object() ),
				pValue );
			if ( !pVirtualHost )
				{/* --- error set by PIObject_loadFromLine() --- */return 0; };
			}
		else if ( !PIUtil_stricmp( KEY_CONF_URLPATH, pVariable ) )
			{
			sURLPath = pValue;	

			/* ---
			Bad things can happen wrt to server compromise if the
			specified URL path does not begin and end with '/'
			--- */
			const char *pTmp = sURLPath;
			if ( *pTmp!=HTTP_DIRSEPERATOR &&
				 pTmp[sURLPath.Len()-1]!=HTTP_DIRSEPERATOR )
				{
				os << "'URLPath' value must begin and end with a '/'." << ends;
				CONFIG_ERR( Object(), os.str() );
				return 0;
				};
			}
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	VirtualHostByURL( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	HandlerBaseHTTP( pObject ),
		pVirtualHost( 0 ),
		pFKPath( PIDB_getFastKey( KEY_INT_PATH, PIDBTYPE_STRING ) )
		{
		ReadParameters( iArgc, ppArgv );
		assert( pFKPath );
		};

	~VirtualHostByURL()
		{
		if ( pVirtualHost )
			{ PIObject_delete( pVirtualHost, 0, 0 ); };
		}

	int Handle( int iPhase, PIHTTP &tPIHTTP, PIIOBuffer &/* tIOBuffer */ )
		{
		if ( iPhase!=PH_HOSTMAP )
			{ return PIAPI_ERROR; };
		
		/* --- lookup the URL path --- */
		PIDB *pR = tPIHTTP.pResponseDB;
		const char *pPath = (const char *)PIDB_lookup( pR, PIDBTYPE_STRING,
			pFKPath, PIDBFLAG_FASTKEY );

		/* ---
		Attempt to match URL up to the length of sPath
		--- */
		if ( !pPath || strncmp( sURLPath, pPath, sURLPath.Len() ) )
			{ return PIAPI_CONTINUE; };

		/* --- reset the path with the URLPath part removed --- */
		PIString sNewPath( &( pPath[sURLPath.Len()-1] ) );
		PIDB_replace( pR, PIDBTYPE_STRING, pFKPath,
			(void *)(const char *)sNewPath, PIDBFLAG_FASTKEY );

		/* --- set this virtual host --- */
		tPIHTTP.pHostDB = PIObject_getDB( pVirtualHost );

		return PIAPI_COMPLETED;
		};
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int VirtualHostByURL_constructor( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	return HandlerBaseHTTP_constructor( pObj, PI_NEW( VirtualHostByURL( pObj,
		iArgc, ppArgv ) ) );
}

#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name VirtualHostByURLClass
		Type LogicExtension
		Library HTTP
		OnClassLoad HandlerBaseHTTP_onClassLoad
		Constructor VirtualHostByURL_constructor
		CopyConstructor HandlerBaseHTTP_copyConstructor
		Destructor HandlerBaseHTTP_destructor
		Execute HandlerBaseHTTP_execute
	</Class>

	<Object>
		Name VirtualHostByURL
		Class VirtualHostByURLClass
	</Object>

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

⌨️ 快捷键说明

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