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

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

\*____________________________________________________________________________*/
//$SourceTop:$

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#define PITHREAD_H_
#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "PIStrStr.h"
#include "DeQuote.h"
#include "PiAPI.h"
#include "FlexLexer.h"
#include "psp_scanner.h"
#include <EXTERN.h>
#include <perl.h>

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

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_DESTRUCTLEVEL		"DestructLevel"

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

Description:
	Load and execute an PSP (Perl Server Pages) extension.

Options:

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

<TR>
<TD>DestructLevel
<TD>-
<TD>0, 1
<TD>define destruction level for perl interpreters
<TD>DestructLevel 1

</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>DestructLevel</H5>
A value of 1 will force the explicite destruction of used perl interpreter
instances during server shutdown. This may currently cause server crashes.
A value of 0 (default, recommended) does nothing on shutdown.

Phase:
	HANDLE

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

Note:
	For the PSP language specification refer to the respective Pi3-HOWTO page.

Example:
	<PRE>
	&lt;Object&gt;
		Name PSP
		Class FlexibleHandlerClass
		Condition "&cmp(&dblookup(response,string,ObjectMap),PSP)"
		CheckPath ReturnCode ReturnCode=COMPLETED
		CheckType ReturnCode ReturnCode=COMPLETED
		CheckAccess AccessByFile RequirePermissions="X"
		Handle PerlSrvPages
	&lt;/Object&gt;
	</PRE>

A PSP script to perform Http redirects:
	<PRE>
	&lt;%
	sub pspSetResponse {
		$pspResponse{&quot;Location&quot;} = &quot;http://localhost&quot;.$pspRequest{&quot;PathInfo&quot;};
		$pspResponseStatus = 302;
		return 0;
	}
	%&gt;
	</PRE>

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


EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);

EXTERN_C void xs_init(pTHX);

class thePSPParser : public PspFlexLexer
{
private:
	PerlInterpreter *my_perl;
	PIHTTP *pHttp;
	PIIOBuffer *pBuf;
	int iFlags;
	int iContentLen;

	virtual int doShebang(const char *txt) {
		/* --- don't send the shebang line to the client! --- */
		return 0;
	};
	virtual int doPlain(const char *txt) {
		if (get_pass()) {
			PIIOBuffer_write( pBuf, txt, strlen(txt), iFlags );
		} else {
			iContentLen += strlen(txt);
		}
		return 0;
	};
	virtual int doScriptlet(const char *txt) {
		PERL_SET_CONTEXT(my_perl);
		eval_pv(txt, TRUE);
		return 0;
	};
	virtual int doExpression(const char *txt) {
		STRLEN n_a;
		PERL_SET_CONTEXT(my_perl);		
		SV *pSV = eval_pv(txt, FALSE);
		const char *pRet = SvPV(pSV, n_a);
		if (get_pass()) {
			PIIOBuffer_write( pBuf, pRet, strlen(pRet), iFlags );
		} else {
			iContentLen += strlen(pRet);
		}
		return 0;
	};
	virtual int doError(const char *msg) {
		HTTPCore_logError(pHttp, msg);
		return 0;
	};
	virtual const char *makeAbsolute(const char *path) {
		Pi3String *s = Pi3String_new("");
		PIDB *pDB = pHttp->pHostDB;
		HTTPCore_relativeToAbsolutePath( pDB, path, s);
		return Pi3String_getPtr( s );
	};

public:
	thePSPParser( const char *ifname, PerlInterpreter *pPerl,
		PIHTTP &tPIHTTP, PIIOBuffer &tBuffer, int theFlags ) :
		PspFlexLexer( ifname ),
		pHttp( &tPIHTTP ),
		pBuf( &tBuffer ),
		my_perl( pPerl ),
		iFlags( theFlags ),
		iContentLen( 0 )
	{
	};

	int getContentLen() { return iContentLen; };
};

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class PspHandler : public HandlerBasePi3Perl
{
private:
	/* ---
	Configuration data
	--- */
	PIDB *pContextDB;
	const char *pFKPath;
	const char *pFKContentType;
	const char *pFKContentLength;
	const char *pFKMethod;
	const char *pFKQueryString;
	int iDestructLevel;
	PISync *pMutex;

	PerlInterpreter *getPerl( const char *pPath, int *iNew ) {
		*iNew = 0;
		PerlInterpreter *my_perl = (PerlInterpreter *)PIDB_lookup( pContextDB,
			PIDBTYPE_OPAQUE, pPath, 0 );
		if (!my_perl) {
			my_perl = perl_alloc();
			if ( my_perl ) {
				PERL_SET_CONTEXT(my_perl);
				perl_construct(my_perl);
				char *embedding[] = { "", "-e", "0" };
				perl_parse(my_perl, xs_init, 3, embedding, NULL);
				perl_run(my_perl);

				PIDB_add( pContextDB, PIDBTYPE_OPAQUE, pPath, (void *)my_perl, 0 );
				*iNew = 1;
			};
		};
		return my_perl;
	};

	int SetResponse( PerlInterpreter *my_perl, PIHTTP *pPIHTTP, PIIOBuffer *pBuffer ) {

#define SPLIT_HASH(HASH) "foreach $pair (split(/&/, $pspPlain)) {	\
	($name, $value) = split(/=/, $pair);							\
	$name =~ tr/+/ /;												\
	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;	\
	$name =~ s/~!/ ~!/g;											\
	$value =~ tr/+/ /;												\
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;	\
	$value =~ s/~!/ ~!/g;											\
	$"HASH"{$name} = $value;										\
};"
		PIDB *pQ = pPIHTTP->pRequestDB;
		PIDB *pR = pPIHTTP->pResponseDB;
		const char *pPath = (const char *)PIDB_lookup( pR, PIDBTYPE_STRING,
			pFKPath, PIDBFLAG_FASTKEY );
		const char *pContentType = (const char *)PIDB_lookup( pQ, PIDBTYPE_RFC822,
			pFKContentType, PIDBFLAG_FASTKEY );
		const char *pMethod = (const char *)PIDB_lookup( pQ, PIDBTYPE_STRING,
			pFKMethod, PIDBFLAG_FASTKEY );

		/* --- Set default Content-Type to 'text/html' --- */
		PIDB_replace( pR, PIDBTYPE_RFC822, pFKContentType, (void *)"text/html", PIDBFLAG_FASTKEY );

		PIDBIterator *pIter;
		const char *pKey;
		const char *pValue;

		PERL_SET_CONTEXT(my_perl);

		/* --- Populate %pspRequest --- */
		HV* reqHash = get_hv("pspRequest", FALSE);
		PIString sTmp;
		if (reqHash) {
			pIter = PIDB_getIterator( pQ, PIDBTYPE_RFC822, 0, 0 );
			if ( pIter ) {
				for( ; PIDBIterator_atValidElement( pIter ); PIDBIterator_next( pIter )) {

					pValue = (char *)PIDBIterator_current( pIter, &pKey );
					hv_store(reqHash, pKey, strlen(pKey), newSVpv(pValue, 0), 0);
					HTTPCore_logDebug( DBG_LOW,
						"PSP: Variable Request|RFC822|%s|%s added to request hash.",
						pKey, pValue );

				};
				PIDBIterator_delete( pIter );
			};

			pIter = PIDB_getIterator( pQ, PIDBTYPE_STRING, 0, 0 );
			if ( pIter ) {
				for( ; PIDBIterator_atValidElement( pIter ); PIDBIterator_next( pIter )) {

					pValue = (char *)PIDBIterator_current( pIter, &pKey );
					hv_store(reqHash, pKey, strlen(pKey), newSVpv(pValue, 0), 0);
					HTTPCore_logDebug( DBG_LOW,
						"PSP: Variable Request|STRING|%s|%s added to request hash.",
						pKey, pValue );
				};
				PIDBIterator_delete( pIter );
			};

			pIter = PIDB_getIterator( pR, PIDBTYPE_STRING, 0, 0 );
			if ( pIter ) {
				for( ; PIDBIterator_atValidElement( pIter ); PIDBIterator_next( pIter )) {

					pValue = (char *)PIDBIterator_current( pIter, &pKey );
					hv_store(reqHash, pKey, strlen(pKey), newSVpv(pValue, 0), 0);
					HTTPCore_logDebug( DBG_LOW,
						"PSP: Variable Response|STRING|%s|%s added to request hash.",
						pKey, pValue );
				};
				PIDBIterator_delete( pIter );
			};
		};

		/* --- Populate %pspResponse --- */
		reqHash = get_hv("pspResponse", FALSE);
		if (reqHash) {
			pIter = PIDB_getIterator( pR, PIDBTYPE_RFC822, 0, 0 );
			if ( pIter ) {
				for( ; PIDBIterator_atValidElement( pIter ); PIDBIterator_next( pIter )) {

					pValue = (char *)PIDBIterator_current( pIter, &pKey );
					hv_store(reqHash, pKey, strlen(pKey), newSVpv(pValue, 0), 0);
					HTTPCore_logDebug( DBG_LOW,
						"PSP: Variable Response|RFC822|%s|%s added to response hash.",
						pKey, pValue );
				}
				PIDBIterator_delete( pIter );
			}
		};

		/* --- Populate %pspForm --- */
		if ( pMethod && !PIUtil_stricmp( pMethod, "POST" )) {
			HV* frmHash = get_hv("pspForm", FALSE);
			if ( frmHash ) {

				/* --- reset the temporary --- */
//				SV *tmp = sv_2mortal(get_sv("tmp", TRUE));
				SV *tmp = get_sv("pspPlain", TRUE);
//				sv_clear(tmp);				

				char *pData = (char *)PIDB_lookup( pQ, PIDBTYPE_RFC822,
					pFKContentLength, PIDBFLAG_FASTKEY );
				if (!pData) {
					HTTPCore_logError( pPIHTTP, "PSP: \
HTTP client didn't provide Content-Length in POST request." );
					return HTTPUtil_doHTTPError( pPIHTTP, ST_INTERNALERROR );			
				};
				int iContentLength = atoi(pData);
				int iRead = 0;
				

⌨️ 快捷键说明

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