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

📄 bscproxy.cpp

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

 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/BscProxy.cpp,v $
 * $Date: 2003/05/13 18:42:01 $
 *
 Description:
	Simple HTTP proxying.

    The caching function is still under development.

\*____________________________________________________________________________*/
//$SourceTop:$

#include <iostream.h>
#include <stdio.h>
#include <ctype.h>

#include "DblList.h"
#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "PIStrStr.h"

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name BasicProxyClass
		Type LogicExtension
		Library HTTP
		OnClassLoad HandlerBaseHTTP_onClassLoad
		Constructor BasicProxy_constructor
		CopyConstructor HandlerBaseHTTP_copyConstructor
		Destructor HandlerBaseHTTP_destructor
		Execute HandlerBaseHTTP_execute
	</Class>

	<Object>
		Name BasicProxy
		Class BasicProxyClass
	</Object>

/*___+++CNF_END+++___*/
#endif
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	BasicProxy

Description:
	Perform simple proxy requests. This handler allows some filtering of
	forward client headers, but no caching. Proxy requests use HTTP/1.0

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

<TR>
<TD>IOObject
<TD>-
<TD>&lt;A Pi3 Object&gt;
<TD>Prototype IO object for proxy forwarding
<TD>IOObject "TCPIPClient"

<TR>
<TD>CacheRoot
<TD>-
<TD>&lt;directory path&gt;
<TD>Relative or Absolute path to cache root
<TD>CacheRoot "Cache"

<!--

AllowHosts is not documented for now because its still under development

<TR>
<TD>AllowHosts
<TD>-
<TD>&lt;directory path&gt;
<TD>Relative or Absolute path to cache root
<TD>CacheRoot "Cache"

-->

</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>IOObject</H5>
Specifies the protocol IO object used to connect to remote HTTP servers. This
object can be a protocol IO implementor (such as TCP/IP or named pipes) or an
IO filter which manipulates the data en route to the server.

<H5>CacheRoot</H5>
Specifies the path to the directory where cached data will be kept for
proxy caching.
If this value is not specified proxy caching is disabled.

Phase:
	HANDLE

Returns:
	PIAPI_COMPLETED on success.
	If this handler was invoked for a phase other than HANDLE then PIAPI_ERROR
	is returned.

Note:
Example:
	<PRE>
	&lt;Object&gt;
		Name BasicProxy
		Class BasicProxyClass
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		Handle BasicProxy
		...
	&lt;/Object&gt;

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

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_IOOBJECT	"IOObject"
#define KEY_CONF_CACHEROOT	"CacheRoot"
#define KEY_CONF_ALLOWHOSTS	"AllowHosts"

#define FIELD_LENGTH 		(127)
#define CACHE_PATH_LENGTH 	(511)

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class CacheRecord
{
public:
	int iVersion;
	char szContentType[FIELD_LENGTH+1];
	char szContentEncoding[FIELD_LENGTH+1];
	char szFileName[CACHE_PATH_LENGTH+1];
	time_t tLastModified;

	CacheRecord()
	:	iVersion(1)
		{
		memset( szContentType, 0, FIELD_LENGTH );
		memset( szContentEncoding, 0, FIELD_LENGTH );
		};
};

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class BasicProxy : public HandlerBaseHTTP
{
private:
	/* ---
	Configuration data
	--- */
	PIObject *pIO;		/* --- prototype IO object for proxy connections --- */
	PIString sCacheRoot;
	DblList lAllowHosts;

	/* --- forbid copy constructor --- */
	BasicProxy( const BasicProxy &t )
	: HandlerBaseHTTP( t )
		{ assert( 0 ); };

protected:
	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "BasicProxy: ";
		if ( !PIUtil_stricmp( KEY_CONF_ALLOWHOSTS, pVariable ) )
			{
			lAllowHosts.Append( (DblList::type)
				PI_NEW( PIString( pValue ) ) );
			}
		else if ( !PIUtil_stricmp( KEY_CONF_CACHEROOT, pVariable ) )
			{
			Pi3String *pTmp = Pi3String_new( pValue );
			HTTPCore_relativeToAbsolutePath( PIObject_getDB( Object() ),
                pValue, pTmp );
			sCacheRoot = Pi3String_getPtr( pTmp );
			Pi3String_delete( pTmp );
			int i=sCacheRoot.Len();
			if ( i )
				{
				const char *pTmp = sCacheRoot;
				char cLast = pTmp[i-1];
				if ( cLast!='/' && cLast!='\\' )
					{ sCacheRoot.Concatenate( HTTP_DIRSEPERATOR ); };
				};
			}
		else if ( !PIUtil_stricmp( KEY_CONF_IOOBJECT, pVariable ) )
			{
			if ( pIO )
				{
				os << "'IOObject' may only be specified once." << ends;
				CONFIG_ERR( Object(), os.str() );
				return 0;
				};

			PIObject *pTmpIO = PIObject_loadFromLine( 
				PIObject_getDB( Object() ),
				PIObject_getConfigurationDB( Object() ),
				pValue );

			if ( !pTmpIO )
				{ return 0; };

			pIO = pTmpIO;
			}
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	BasicProxy( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	HandlerBaseHTTP( pObject ), pIO( 0 )
		{
		ReadParameters( iArgc, ppArgv );
		if ( !IsOK() )
			{ return; };

		if ( !pIO )
			{
			CONFIG_ERR( Object(), "BasicProxy: Missing definition for \
'IOObject'." );
			SetOK( 0 );
			return;
			};
		
		assert( pIO );
		};

	~BasicProxy()
		{
		for( DblListIterator i(lAllowHosts); !i.BadIndex(); i++ )
			{ PI_DELETE( (PIString *)i.Current() ); };
		PIObject_delete( pIO, 0, 0 );
		};

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

							Utilities

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
	/* ---
	Filesystem canonical name given a name with (possibly) filesystem
	excluded characters
	--- */
	void Canonical( const PIString &sRawString, PIString &sResult )
		{
		Pi3String *pRawString = Pi3String_new( sRawString );
		Pi3String *pTmp = Pi3String_new( 0 );
		HTTPUtil_urlEncode( pRawString, pTmp );
		sResult = Pi3String_getPtr( pTmp );
		Pi3String_delete( pTmp );
		};

	/* ---
	Get the full path to a cached file
	--- */
	void CacheFileName( const char *pHost, const char *pPort, 
		const char *pURI, const char *pExtn, PIString &sFileName )
		{
		assert( pHost );
		assert( pURI );
		assert( pExtn );
		
		sFileName = sCacheRoot;
		PIString sTmp( pHost );
		if ( pPort )
			{
			sTmp.Concatenate( ':' );
			sTmp.Concatenate( pPort );
			};
		Pi3String *pTmp = Pi3String_new( sTmp );
		Pi3String *pTmp2 = Pi3String_new( 0 );
		HTTPUtil_urlEncode( pTmp, pTmp2 );
		sFileName.Concatenate( Pi3String_getPtr( pTmp2 ) );
		sFileName.Concatenate( '_' );
		Pi3String *pTmp3 = Pi3String_new( pURI );
		HTTPUtil_urlEncode( pTmp3, pTmp2 );
		sFileName.Concatenate( Pi3String_getPtr( pTmp2 ) );
		sFileName.Concatenate( '.' );
		sFileName.Concatenate( pExtn );
		Pi3String_delete( pTmp );
		Pi3String_delete( pTmp2 );
		Pi3String_delete( pTmp3 );
		};

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

								Guts

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
	/* ---
	GetProxyConnection(), get a connection to the proxy server, either
	by creating a new one or by reusing an existing connection, kept
	open from a previous request
	--- */
	PIObject *GetProxyConnection( PIHTTP &tPIHTTP, const char *pProtocol,
		const char *pHost, int iPort )
		{

⌨️ 快捷键说明

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