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

📄 pathmap.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/PathMap.cpp,v $
 * $Date: 2004/08/01 20:13:09 $
 *
 Description:
	Path mapping.

\*____________________________________________________________________________*/
//$SourceTop:$

#include <assert.h>

#include "DblList.h"		/* --- from Base library --- */

#include "HandBase.h"
#include "PIHTTP.h"
#include "PIStrStr.h"
#include "Pi3Expr.h"
#include "HTTPDefs.h"
#include "HTTPCore.h"

/*____________________________________________________________________________*\ *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_FROM			"From"
#define KEY_CONF_TO				"To"
#define KEY_CONF_CASESENSITIVE	"CaseSensitive"
#define KEY_CONF_PATHINFO		"PathInfo"
#define KEY_CONF_REDIRECT		"Redirect"
#define KEY_CONF_ACTION			"Action"
#define VALUE_NO				"No"
#define VALUE_YES				"Yes"
#define KEY_CONF_RETURNCODE		"ReturnCode"

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

Description:
	Maps a virtual path to another virtual path or a physical path.

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

<TR>
<TD>From
<TD>+
<TD>&lt;path&gt;
<TD>Search path
<TD>From="/"

<TR>
<TD>To
<TD>+
<TD>&lt;path&gt;
<TD>Replace path
<TD>To="/home/WWWDocs/"

<TR>
<TD>CaseSensitive
<TD>Yes
<TD>Yes|No
<TD>Specifies case sensitivity of "From" matching
<TD>CaseSensitive="No"

<TR>
<TD>PathInfo
<TD>No
<TD>Yes|No
<TD>Specifies if path after "To" is PATH_INFO
<TD>PathInfo="Yes"

<TR>
<TD>Redirect
<TD>No
<TD>Yes|No
<TD>Specifies if path after "To" is a URL for redirection
<TD>Redirect="Yes"

<TR>
<TD>Action
<TD>-
<TD>A Pi3 Expression
<TD>Expression to be evaluated if path is mapped
<TD>Action="&dbreplace(response,string,ObjectMap,Scripts)"

</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>
	From
</H5>
Specifies the virtual path to be replaced. The first and last characters
of this virtual path must be '/'.
<H5>
	To
</H5>
Specifies the path to replace the entire virtual path.
<H5>
	CaseSensitive
</H5>
Specifies whether or not matching the "From" pattern to the virtual
path will be case sensitive or not.
<H5>
	PathInfo
</H5>
Specifies whether or not the pattern which follows the "From" section
of the virtual path will be appended to the "To" path or placed in the
PATH_INFO variable.<P>Typically CGI mapping sets PATH_INFO whereas regular
document mapping does not.
<H5>
	Redirect
</H5>
Specifies whether or not the pattern which follows the "To" section
of the virtual path will be used as an URL for redirection, i.e. without
expansion to a physical file path.
Note, that the PathMapper itself only prepares redirection but does not
trigger it.
<H5>
	Action
</H5>
Specifies a Pi3 expression which will be evaluated if the path is mapped by
this handler. This allows a 'marker' to be set to effect the processing of 
later phases. This directive may be specified multiple times for multiple
different actions.

Phase:
	MAPPING

Returns:
	PIAPI_ERROR if this handler was invoked for an inappropriate phase, 
	otherwise, PIAPI_COMPLETED if a path was mapped and PIAPI_CONTINUE if
	no path was mapped.
		
Note:
	The 'From' pattern must begin and end with '/' character, otherwise an
	initialization error will be raised. This is to ensure that path mapping
	does not result in unintended files becoming visible.
	<P>
	Note that paths are mapped in the order listed in the configuration file,
	so a path mapping statement with From="/" would match before one with
	From="/icons/" is listed first. Path mapping should be list in reverse
	order of directory depth to ensure correct mapping (place mapper with
	From="/" AFTER the one with From="/icons/").

Example:
	<PRE>
	&lt;Object&gt;
		Name PathMapper
		Class PathMapperClass
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		CheckAccess PathMapper From="/" To="/home/Pi3Web/WebRoot/"
		...
	&lt;/Object&gt;

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

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class PathMapper : public HandlerBaseHTTP
{
private:
	PIString sFrom;
	PIString sTo;
	int iCaseSensitive;
	int iPathInfo;
	int iRedirect;
	DblList lActions;			/* allow multiple actions */
	const char *pFKPath;		/* faster access to 'Path' variable */
	int iReturnStatus;

protected:
	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "PathMapper: ";
		if ( !PIUtil_stricmp( KEY_CONF_FROM, pVariable ) )
			{
			sFrom = pValue;

            /* ---
            Bad things can happen wrt to server compromise if the
            specified URL path does not begin and end with '/'
            --- */
            const char *pTmp = sFrom;
            if ( *pTmp!=HTTP_DIRSEPERATOR &&
                 pTmp[sFrom.Len()-1]!=HTTP_DIRSEPERATOR )
                {
                os << "'From' value must begin and end with a '/'." << ends;
                CONFIG_ERR( Object(), os.str() );
                return 0;
                };
			}
		else if ( !PIUtil_stricmp( KEY_CONF_TO, pVariable ) )
			{
			sTo = pValue;
			}
		else if ( !PIUtil_stricmp( KEY_CONF_CASESENSITIVE, pVariable ) )
			{
			if ( !PIUtil_stricmp( VALUE_NO, pValue ) )
				{ iCaseSensitive = 0; }; 
			}
		else if ( !PIUtil_stricmp( KEY_CONF_PATHINFO, pVariable ) )
			{
			if ( !PIUtil_stricmp( VALUE_YES, pValue ) )
				{ iPathInfo = 1; }; 
			}
		else if ( !PIUtil_stricmp( KEY_CONF_REDIRECT, pVariable ) )
			{
			if ( !PIUtil_stricmp( VALUE_YES, pValue ) )
				{ iRedirect = 1; }; 
			}
		else if ( !PIUtil_stricmp( KEY_CONF_ACTION, pVariable ) )
			{
			Pi3Expression *pExpr = 0;
			Pi3String *pError = Pi3String_new( 0 );
			pExpr = Pi3Expression_new( pValue, 0, pError );
			if ( !pExpr )
				{
				os << "Bad 'Action' String: " << Pi3String_getPtr( pError )
					<< ends;
				Pi3String_delete( pError );
				CONFIG_ERR( Object(), os.str() );
				Pi3Expression_delete( pExpr );
				pExpr = 0;
				return 0;
				};
			Pi3String_delete( pError );
			lActions.Append( (DblList::type) pExpr );
			}
		else if ( !PIUtil_stricmp( KEY_CONF_RETURNCODE, pVariable ) )
			{
            /* ---
            only CONTINUE or COMPLETED are allowed here
            --- */
            if ( !PIUtil_stricmp( "CONTINUE", pValue ))
				{
                 iReturnStatus = PIAPI_CONTINUE;

⌨️ 快捷键说明

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