📄 maperror.cpp
字号:
/*____________________________________________________________________________*\
*
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/MapError.cpp,v $
* $Date: 2003/05/13 18:42:03 $
*
Description:
Set an appropriate error document in the path when an error status
code exists. Other actions may conditionally be taken.
\*____________________________________________________________________________*/
//$SourceTop:$
#include <ctype.h>
#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "PIStrStr.h"
#include "DblList.h"
#include "StrToken.h"
/*____________________________________________________________________________*\
*
Class:
Description:
Mapping from an error status code to the virtual path for that error.
\*____________________________________________________________________________*/
class ErrorMap
{
public:
int iStatus;
PIString sVirtualPath;
ErrorMap( int iTheStatus, const char *pVirtualPath )
: iStatus( iTheStatus ), sVirtualPath( pVirtualPath )
{};
};
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#define KEY_CONF_IGNORESTATUS "IgnoreStatus"
#define KEY_CONF_DEFAULTMESSAGE "DefaultMessage"
#define KEY_CONF_ACTION "Action"
#if 0
/*
** HTML documentation for this handler
*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
MapToErrorDocument
Description:
This handler sets an error document virtual path according to the
HTTP error status code.
Subsequent mapping handlers map this virtual path to a physical path.
If a virtual path is mapped a number of the original client RFC822
request headers are modified or removed, most notably, the Method
is set to 'GET' and any 'If-Modified-Since' headers are removed. This
avoids confusion where subsequent handlers interpret the error resource
in the context of meta information of the original resource.
Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)
<TR>
<TD>IgnoreStatus
<TD>-
<TD>0, 200, etc.
<TD>HTTP response codes
<TD>IgnoreStatus="0 200"
<TR>
<TD><nnn>
<TD>-
<TD>/errors/404.html, /msgs/403.shtml, etc.
<TD>Response specific virtual error paths
<TD>404="/errors/404.html"; 403="/errors/403.shtml"
<TR>
<TD>DefaultMessage
<TD>+
<TD>/errors/Unknown.html, /msgs/unknown.shtml, etc.
<TD>Default error message virtual path
<TD>DefaultMessage="/errors/Unknown.html"
<TR>
<TD>Action
<TD>-
<TD>A Pi3Expression
<TD>Evaluated if the error code is mapped
<TD>Action="&dbreplace(response,sting,ObjectMap,...)"
</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>
IgnoreStatus
</H5>
HTTP response codes which should not be considered errors. This field
takes the form of a sequence of 3 digit decimal codes seperated by
spaces.
<STRONG>NOTE:</STRONG> Typically the values 0 and 200 will always be
included.
<H5>
<nnn>
</H5>
This directive is used to specify the error message virtual path to be set
for a specific status code.
<H5>
DefaultMessage
</H5>
The default error message. This is used for status codes which have
not been specified as ignorable, but which have no error virtual path
explicitly defined.
<H5>
Action
</H5>
A Pi3Expression that will be evaulated if the error status code is mapped.
Phase:
MAPPING
Returns:
PIAPI_CONTINUE in all non-error cases, whether or not an error
virtual path was set.
PIAPI_ERROR and PIAPI_ABORT respectively for generic and severe
error conditions.
Example:
<PRE>
<Object>
Name MapToErrorDocument
Class MapToErrorDocumentClass
IgnoreStatus "0 200"
</Object>
<Object>
...
Mapping MapToErrorDocument DefaultMessage="/errors/Unknown.html" 403="/errors/Forbidden.html"
...
</Object>
</PRE>
/*___+++HTMLDOC_END+++___*/
#endif
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class MapToErrorDocument : public HandlerBaseHTTP
{
private:
/* --- forbid copy constructor --- */
MapToErrorDocument( const MapToErrorDocument &t )
: HandlerBaseHTTP( t )
{ assert( 0 ); };
/* ---
Configuration data
--- */
DblList lIgnoreCodes;
DblList lMapToErrorVirtualPaths;
PIString sDefaultVirtualPath;
Pi3Expression *pExpr;
protected:
int Parameter( const char *pVariable, const char *pValue,
const char *pWhere )
{
assert( pVariable && pValue );
PIOStrStream os;
os << pWhere << "MapToErrorDocument: ";
if ( !PIUtil_stricmp( KEY_CONF_IGNORESTATUS, pVariable ) )
{
StringTokenizer tTokens( pValue, " " );
for( int i=0; i<tTokens.NumTokens(); i++)
{
lIgnoreCodes.Append( (DblList::type)
atoi( tTokens.GetToken( i ) ) );
};
}
else if ( !PIUtil_stricmp( KEY_CONF_DEFAULTMESSAGE, pVariable ) )
{
sDefaultVirtualPath = pValue;
}
else if ( !PIUtil_stricmp( KEY_CONF_ACTION, pVariable ) )
{
if ( pExpr )
{
os << "'Action' make only be specified once." << endl;
CONFIG_ERR( Object(), os.str() );
return 0;
};
Pi3String *pError = Pi3String_new( 0 );
Pi3Expression *pTmp = Pi3Expression_new( pValue, 0, pError );
if ( !pTmp )
{
os << "Error in action expression:" <<
Pi3String_getPtr( pError ) << endl;
CONFIG_ERR( Object(), os.str() );
Pi3String_delete( pError );
return 0;
};
Pi3String_delete( pError );
pExpr = pTmp;
}
else if ( isdigit( *pVariable ) )
{
int iTmp = atoi( pVariable );
lMapToErrorVirtualPaths.Append( (DblList::type)
PI_NEW( ErrorMap( iTmp, pValue ) ) );
}
else
{
os << "Unknown directive '" << pVariable <<
"'" << ends;
CONFIG_ERR( Object(), os.str() );
return 0;
};
return 1;
};
public:
MapToErrorDocument( PIObject *pObject, int iArgc, const char *ppArgv[] )
: HandlerBaseHTTP( pObject ),
pExpr( 0 )
{
ReadParameters( iArgc, ppArgv );
/* ---
Make sure there's a default
--- */
if ( sDefaultVirtualPath==PIString::Empty() )
{
CONFIG_ERR( Object(), "MapToErrorDocument: 'Default' not defined" );
SetOK( 0 );
return;
};
};
virtual ~MapToErrorDocument()
{
for( DblListIterator i( lMapToErrorVirtualPaths ); !i.BadIndex(); i++)
{
PI_DELETE( (ErrorMap *)i.Current() );
};
if ( pExpr )
{ Pi3Expression_delete( pExpr ); };
};
/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
Handle the request
\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
int Handle( int iPhase, PIHTTP &tPIHTTP, PIIOBuffer &/* tB */ )
{
if ( iPhase!=PH_MAPPING )
{
HTTPCore_logError( &tPIHTTP, "MapToErrorDocument: \
Configuration error, \
'MapToErrorDocument' handler may only be used for 'MAPPING' phase, not '%s' \
phase.",
HTTPUtil_phaseNumberToName( iPhase ) );
return PIAPI_ERROR;
};
PIDB *pR = tPIHTTP.pResponseDB;
PIDB *pQ = tPIHTTP.pRequestDB;
int iStatus = tPIHTTP.iStatus;
/* ---
See is this a status code that we should ignore
--- */
for( DblListIterator i( lIgnoreCodes ); !i.BadIndex(); i++ )
{
if ( iStatus==(int)i.Current() )
{ return PIAPI_CONTINUE; };
};
/* ---
Find the mapping with the appropriate status
--- */
const char *pErrorPath = 0;
for( DblListIterator j( lMapToErrorVirtualPaths ); !j.BadIndex(); j++ )
{
ErrorMap *pMap = (ErrorMap *)j.Current();
assert( pMap );
if ( iStatus==pMap->iStatus )
{
pErrorPath = pMap->sVirtualPath;
break;
};
};
/* ---
Assign default if necessary
--- */
if ( !pErrorPath )
{
pErrorPath = sDefaultVirtualPath;
};
/* ---
Save old path
--- */
void *pPath = (void *)PIDB_lookup( pR, PIDBTYPE_STRING,
KEY_INT_PATH, 0);
PIDB_add( pR, PIDBTYPE_STRING, KEY_INT_OLDPATH, pPath, 0);
/* ---
Change the path, and modify other headers
--- */
PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_PATH, (void *)pErrorPath, 0);
PIDB_replace( pQ, PIDBTYPE_RFC822, KEY_HTTP_IFMODIFIEDSINCE, 0, 0);
PIDB_replace( pQ, PIDBTYPE_OPAQUE, KEY_HTTP_METHOD, (void *)MD_GET, 0);
/* ---
Perform action if specified
--- */
if ( pExpr )
{ Pi3Expression_write( pExpr, &tPIHTTP, 0, 0, 0 ); };
/* --- done --- */
return PIAPI_CONTINUE;
};
};
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int MapToErrorDocument_constructor( PIObject *pObj,
int iArgc, const char *ppArgv[] )
{
return HandlerBaseHTTP_constructor( pObj, PI_NEW( MapToErrorDocument( pObj,
iArgc, ppArgv ) ) );
}
#if 0
/*___+++CNF_BEGIN+++___*/
<Class>
Name MapToErrorDocumentClass
Type LogicExtension
Library HTTP
OnClassLoad HandlerBaseHTTP_onClassLoad
Constructor MapToErrorDocument_constructor
CopyConstructor HandlerBaseHTTP_copyConstructor
Destructor HandlerBaseHTTP_destructor
Execute HandlerBaseHTTP_execute
</Class>
<Object>
Name MapToErrorDocument
Class MapToErrorDocumentClass
</Object>
/*___+++CNF_END+++___*/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -