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

📄 isapi20.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/ISAPI/ISAPI20.cpp,v $
 * $Date: 2003/05/13 18:42:05 $
 *
 Description:
	Execute an ISAPI plugin.

\*____________________________________________________________________________*/
//$SourceTop:$

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

#if WIN32
#include <HTTPExt.h>
#endif


#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "PIStrStr.h"
#include "DeQuote.h"
#include "PiAPI.h"
#include "StrToken.h"

#if WIN32

/*
** #define D { cerr << __FILE__ << ": " << __LINE__ << endl; }
*/
#define D

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_EXTRAHDRPREFIX					"ExtraHeadersPrefix"
#define KEY_CONF_EXTRAHEADERSIGNORE				"ExtraHeadersIgnore"
#define KEY_CONF_EXTRAHEADERS					"ExtraHeaders"
#define VALUE_NO								"No"
#define VALUE_YES								"Yes"
#define KEY_CONF_VARIABLE						"Variable"

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	ISAPI20

Description:
	Load and execute an ISAPI (Internet Server API) extension. You can find more
	informations about ISAPI filters in the <A HREF="http://msdn.microsoft.com/library/psdk/iisref/isgu0q0n.htm">
	MSDN library</A>.

Options:

<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>Variable
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>A variable definition
<TD>Variable "GATEWAY_INTERFACE=CGI/1.1"

<TR>
<TD>ExtraHeaders
<TD>Yes
<TD>Yes|No
<TD>Indicates if extra headers are considered
<TD>ExtraHeaders Yes

<TR>
<TD>ExtraHeadersPrefix
<TD>-
<TD>&lt;A string&gt;
<TD>Used as prefix of each extra header
<TD>ExtraHeadersPrefix &quot;HTTP_&quot;

<TR>
<TD>ExtraHeadersIgnore
<TD>-
<TD>&lt;Space delimited Strings&gt;
<TD>List of unconsidered extra headers
<TD>ExtraHeadersIgnore &quot;Content-Type Content-Length&quot;

</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>Variable</H5>
Specifies a server variable expression to be set into the buffer
of ISAPI extension calls to GetServerVariable. Could be set multiple
times in one configuration object.

<H5>ExtraHeaders</H5>
This configuration key defines, if extra headers are considered by
the processing of this handler.

<H5>ExtraHeadersPrefix</H5>
This configuration value will be the trailing string of each extra
request header.

<H5>ExtraHeadersIgnore</H5>
The headers in this list are not treated as extra headers.

Phase:
	HANDLE

Returns:
	PIAPI_COMPLETED, PIAPI_CONTINUE, PIAPI_ERROR, or INT_REDIRECT according
	to the status returned by the extension.

Note:
<H5>Parameters</H5>
The Pi3Expressions in the configuration variables may contain besides
the standard shortcuts used in Pi3Expressions the following, context
specific parameters.

<CENTER>
<TABLE BORDER=1>
<TH>Parameter<TH>Evaluates to<TR>
<TD>%a<TD>All extra headers, as required for header ALL_HTTP<TR>
<TD>%l<TD>Content-Length as DWORD, as required for ISAPI<TR>
</TABLE>
</CENTER>

Example:
	<PRE>
	&lt;Object&gt;
		Name ISAPI20
		Class ISAPI20Class
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		Handle Condition="&cmp(&dblookup(response,string,ObjectMap,ISAPI),ISAPI)" \
			ISAPI20
		...
	&lt;/Object&gt;

	</PRE>

/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
 *
 Class:
 Description:
	Prototypes for callback functions
	Class for ISAPI context
\*____________________________________________________________________________*/
BOOL WINAPI fnGetServerVariable( HCONN	   hConn,
					LPSTR	   lpszVariableName,
					LPVOID	  lpvBuffer,
					LPDWORD	 lpdwSize );

BOOL WINAPI fnWriteClient( HCONN	  ConnID,
					LPVOID	 Buffer,
					LPDWORD	lpdwBytes,
					DWORD	  dwReserved );

BOOL WINAPI fnReadClient( HCONN	  hConn,
					LPVOID	 lpvBuffer,
					LPDWORD	lpdwSize );

BOOL WINAPI fnServerSupportFunction( HCONN	  hConn,
					DWORD	  dwHSERRequest,
					LPVOID	 lpvBuffer,
					LPDWORD	lpdwSize,
					LPDWORD	lpdwDataType );


/*____________________________________________________________________________*\
 *
 Class:
 Description:
	Class for ISAPI context
\*____________________________________________________________________________*/
class ISAPI20;
class ISAPIContext : public _EXTENSION_CONTROL_BLOCK
{
public:
	ISAPI20 *pISAPI20;
	PIHTTP *pPIHTTP;
	int iRc;
	inline int Finish()
		{
		return PISync_unlock( sema );
		};
	inline int isFinished()
		{
		return PISync_lock( sema );
		};

private:
	PISync *sema;

	inline char *DBL( PIDB *pDB, int iType, const char *pKey )
		{
		char *pTmp = (char *)PIDB_lookup( pDB, iType, pKey, 0 );
		return pTmp ? pTmp : "";
		};

public:
	ISAPIContext( ISAPI20 *pTheISAPI20, PIHTTP *pThePIHTTP )
	:	pISAPI20( pTheISAPI20 ), pPIHTTP( pThePIHTTP ), iRc( PIAPI_COMPLETED ), sema( PIPlatform_allocLocalSemaphore(0,1) )
		{

		/* --- locals --- */
		PIDB *pQ = pPIHTTP->pRequestDB;
		PIDB *pR = pPIHTTP->pResponseDB;

		/* --- initialize member data --- */
		cbSize = sizeof( _EXTENSION_CONTROL_BLOCK );
		dwVersion = MAKELONG( HSE_VERSION_MINOR, HSE_VERSION_MAJOR );
		ConnID = (HCONN)this;
		dwHttpStatusCode = 0;
		*lpszLogData = '\0';

		/* --- input variables --- */
		lpszMethod = DBL( pQ, PIDBTYPE_STRING, KEY_HTTP_METHOD );
		lpszQueryString = DBL( pQ, PIDBTYPE_STRING, KEY_HTTP_QUERYSTRING );
		lpszPathInfo = DBL( pR, PIDBTYPE_STRING, KEY_INT_PATHINFO );
		lpszPathTranslated = DBL(pR, PIDBTYPE_STRING, KEY_INT_PATHTRANSLATED);
		lpszContentType = DBL( pQ, PIDBTYPE_RFC822, KEY_HTTP_CONTENTTYPE );

		/* --- check for stdin --- */
		cbAvailable = 0;
		lpbData = NULL;
		const char *pContentLength = DBL( pQ, PIDBTYPE_RFC822,
			KEY_HTTP_CONTENTLENGTH );
		cbTotalBytes = pContentLength ? atol( pContentLength ) : 0;

		/* --- callback functions --- */
		GetServerVariable = fnGetServerVariable;
		WriteClient = fnWriteClient;
		ReadClient = fnReadClient;
		ServerSupportFunction = fnServerSupportFunction;
		};

	virtual ~ISAPIContext()	{
		PISync_unlock( sema );
		PISync_delete( sema );
	};

};


/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class ISAPI20 : public HandlerBaseISAPI
{
private:
	/* ---
	Configuration data
	--- */
	PIDB *pTypes;					/* DB for variable types */
   	PIString sExtraHeadersPrefix;	/* Extra Header for environment values, */
									/* by default "HTTP_" */
	int iExtraHeaders;				/* whether or not to send extra headers */
	DblList lIgnoreHeaders;			/* extra headers to ignore */
	FnPi3Write aFunctions[256];		/* callback functions */
	DWORD dwPlatform;
	DWORD initVersion()
		{
		//
		//  Find out whether we are on NT or Win95/98
		//
		OSVERSIONINFO VerInfo;
		VerInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
		return GetVersionEx( &VerInfo ) ? VerInfo.dwPlatformId : 0;
		};

	inline int checkIgnoreList(const char *pKey)
		{
		/* --- exclude the variables in lIgnoreHeaders --- */
		const char *pExclude;
		for( DblListIterator l( lIgnoreHeaders ); !l.BadIndex(); l++ )
			{
			pExclude = (const char *)l.Current();
			if (!PIUtil_stricmp(pKey, pExclude))
				{
				return 1;
				}
			};
		return 0;
		}

protected:
	/*___ +++++++++++++++++++++++++++++++++++++++++++++++++ ___ *

				Load an expression or condition

	 *___ +++++++++++++++++++++++++++++++++++++++++++++++++ ___ */
	int LoadExpression( const char *pName, Pi3Expression **ppExpression,
		FnPi3Write *pFunctions, const char *pValue, PIOStrStream &os )	
		{
		assert( ppExpression );
		Pi3Expression *pExpression = *ppExpression;
		if ( pExpression )
			{
			os << "'" << pName << "' may only be specified once." << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};
		Pi3String *pError = Pi3String_new( 0 );
		pExpression = Pi3Expression_new( pValue, pFunctions, pError );
		if ( !pExpression )
			{
			os << "Error parsing expression: " <<
				Pi3String_getPtr( pError ) << ends;
			CONFIG_ERR( Object(), os.str() );
			Pi3Expression_delete( pExpression );
			Pi3String_delete( pError );
			return 0;
			};
		Pi3String_delete( pError );
		*ppExpression = pExpression;
		return 1;
		};

	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "ISAPI: ";
		if ( !PIUtil_stricmp( KEY_CONF_EXTRAHDRPREFIX, pVariable ) )
			{
				sExtraHeadersPrefix = (const char *)DeQuote( pValue );
			}
		else if ( !PIUtil_stricmp( KEY_CONF_EXTRAHEADERSIGNORE, pVariable ) )
			{
			StringTokenizer tTokens( (const char *)DeQuote(pValue), " " );
			for( int i=0; i<tTokens.NumTokens(); i++)
				{

⌨️ 快捷键说明

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