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

📄 virtualh.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________*\
 *

 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/HTTP/VirtualH.cpp,v $
 * $Date: 2003/05/13 18:42:04 $
 *
 Description:
	Handlers to support virtual hosting.

\*____________________________________________________________________________*/
//$SourceTop:$

#include "HandBase.h"
#include "HTTPCore.h"
#include "PIString.h"			/* from Base library */
#include "PIStrStr.h"			/* from Base library */

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_VIRTUALHOSTOBJECT		"VirtualHostObject"
#define KEY_CONF_BINDADDRESS			"BindAddress"
#define KEY_CONF_BINDADDRESSVARIABLE	"BindAddressVariable"
#define KEY_DEFAULT_BINDADDRESSVARIABLE	"LocalAddr"

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

Description:
	Some operating systems allow multiple IP address to be used by the
	same computer. On such systems it is possible to have multiple HTTP
	servers simulated by the same server by customizing the servers behaviour
	depending on the local IP address to which the client is bound.
	This handler allows the requests virtual host object to be changed 
	depending on the local IP address of the request.

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>BindAddress
<TD>+
<TD>IP address of form www.xxx.yyy.zzz
<TD>Server bound IP address
<TD>BindAddress="172.23.23.37"

<TR>
<TD>BindAddressVariable
<TD>"LocalAddr"
<TD>Variable name
<TD>IO object variable name
<TD>BindAddressVariable="LocalAddr"

</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 local IP address matches the IP address given by the 'BindAddress'
parameter.

<H5>
	BindAddress
</H5>
Specifies the IP address to match against the connection&qts local IP 
address.

<H5>
	BindAddressVariable
</H5>
Give the variable name which stores the connections local bind address in 
the connection IO object, this would typically be 'LocalAddr'.

Phase:
	HOSTMAP	

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

	&lt;Object&gt;
		...
		HostMap VirtualHostByAddress VirtualHostObject="host1.foo.dom" \
			BindAddress=120.0.0.1
		...
	&lt;/Object&gt;

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

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class VirtualHostByAddress : public HandlerBaseHTTP
{
private:
	PIObject *pVirtualHost;
	PIString sBindAddress;
	const char *pFKBindAddressVariable;

protected:
	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "VirtualHostByAddress: ";
		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_BINDADDRESS, pVariable ) )
			{
			sBindAddress = pValue;	
			}
		else if ( !PIUtil_stricmp( KEY_CONF_BINDADDRESSVARIABLE, pVariable ) )
			{
			pFKBindAddressVariable = PIDB_getFastKey( pValue, PIDBTYPE_STRING );
			}
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	VirtualHostByAddress( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	HandlerBaseHTTP( pObject ),
		pVirtualHost( 0 ),
		pFKBindAddressVariable( 0 )
		{
		ReadParameters( iArgc, ppArgv );
		if ( !pFKBindAddressVariable )
			{
			pFKBindAddressVariable = PIDB_getFastKey(
				KEY_DEFAULT_BINDADDRESSVARIABLE, PIDBTYPE_STRING );
			};
		assert( pFKBindAddressVariable );
		};

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

	int Handle( int iPhase, PIHTTP &tPIHTTP, PIIOBuffer &/* tIOBuffer */ )
		{
		if ( iPhase!=PH_HOSTMAP )
			{ return PIAPI_ERROR; };

		/* --- get the local IP address of this connection --- */
		PIDB *pC = tPIHTTP.pConnectionDB;
		const char *pIP = (const char *)PIDB_lookup( pC, PIDBTYPE_STRING,
			pFKBindAddressVariable, PIDBFLAG_FASTKEY );

		if ( !pIP || strcmp( sBindAddress, pIP ) )
			{ return PIAPI_CONTINUE; };

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

		return PIAPI_COMPLETED;
		};
};

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

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

	<Object>
		Name VirtualHostByAddress
		Class VirtualHostByAddressClass
		BindAddressVariable	"LocalAddr"
	</Object>

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

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_VIRTUALHOSTOBJECT		"VirtualHostObject"
#define KEY_CONF_HOSTNAME				"HostName"

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

Description:
	This handler allows the virtual host to be changed by filtering the
	'Host' rfc822 header field sent by the browser.

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>HostName
<TD>+
<TD>A hostname 
<TD>Hostname of virtual host.
<TD>Hostname="foo.name.dom"

</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>
	Hostname
</H5>
Specifies a hostname for the virtual host.

Phase:
	HOSTMAP

Example:
	<PRE>
	&lt;Object&gt;
		Name VirtualHostByName
		Class VirtualHostByNameClass
	&lt;/Object&gt;

	&lt;Object&gt;

⌨️ 快捷键说明

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