📄 sendssi.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/SendSSI.cpp,v $
* $Date: 2004/07/04 19:28:42 $
*
Description:
Server-side include handling.
\*____________________________________________________________________________*/
//$SourceTop:$
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "PIStrStr.h"
#include "Pi3Expr.h"
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#define KEY_CONF_EXEC "Exec"
#define KEY_VALUE_YES "Yes"
#define KEY_CONF_HEADERPATTERN "HeaderPattern"
#define KEY_CONF_FOOTERPATTERN "FooterPattern"
#define KEY_CONF_EXECFILEFIXUP "ExecFileFixup"
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#if 0
/*
** HTML documentation for this handler
*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
SendSSI
Description:
Parse and send server-side includes.
Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)
<TR>
<TD>Exec
<TD>No
<TD>'Yes' or 'No'
<TD>Specific whether CGI scripts may be executed
<TD>Exec="Yes"
<TR>
<TD>HeaderPattern
<TD>-
<TD>A Pi3Expression
<TD>Output first before the page
<TD>HeaderPattern="<HTML><TITLE>The Title</TITLE>"
<TR>
<TD>FooterPattern
<TD>-
<TD>A Pi3Expression
<TD>Output last
<TD>FooterPattern="</HTML>"
<TR>
<TD>ExecFileFixup
<TD>-
<TD>A Pi3Expression
<TD>Evaluated to fixup CGI markers
<TD>ExecFileFixup="&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>
Exec
</H5>
Specify whether or not CGI scripts may be executed as part of SSI processing.
Comparision with 'Yes' or 'No' is not case sensitive.
<H5>
HeaderPattern
</H5>
Specifies a pattern of data that will be pre-appended to the file.
<H5>
FooterPattern
</H5>
Specifies a pattern of data that will be appended to the file.
<H5>
ExecFileFixup
</H5>
For <I>exec</I> ssi directives with the file specified via
'file=' this pattern is evaluated to allow a user defined
Pi3Expression to cause markers to be set. This is because
virtual path processing would normally set markers to indicate
which CGI handler to use, but 'file=' bypasses the virtual
path mapping phase.
Note:
Phase:
HANDLE
Returns:
PIAPI_COMPLETED if the file was sent.
If this handler was invoked for a phase other than HANDLE then PIAPI_ERROR
is returned.
Example:
<PRE>
<Object>
Name SendSSI
Class SendSSIClass
</Object>
<Object>
...
Handle SendSSI Exec="Yes"
...
</Object>
</PRE>
/*___+++HTMLDOC_END+++___*/
#endif
/*____________________________________________________________________________*\
*
Class:
Description:
class for storing SSI configuration values
\*____________________________________________________________________________*/
class SSIConfig
{
public:
PIString sTimeFmt;
PIString sSizeFmt;
PIString sErrMsg;
SSIConfig()
:
sTimeFmt( "%a, %d %b %Y %T" ),
sSizeFmt( "%d bytes" ),
sErrMsg( "** SSI Error **" )
{};
SSIConfig( const SSIConfig &tConfig )
:
sTimeFmt( tConfig.sTimeFmt ),
sSizeFmt( tConfig.sSizeFmt ),
sErrMsg( tConfig.sErrMsg )
{};
};
/*____________________________________________________________________________*\
*
Class:
Description:
simple class to pass parameters to expression callback functions
\*____________________________________________________________________________*/
class SSIParam
{
public:
PIFInfo *pFInfo;
SSIConfig &tConfig;
Pi3Expression *pExecFileFixup;
SSIParam( PIFInfo *pTheFInfo, SSIConfig &tTheConfig, Pi3Expression *pExpr )
: pFInfo( pTheFInfo ), tConfig( tTheConfig ), pExecFileFixup( pExpr )
{};
};
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static int Internal_GMTTime( PIHTTP * /* pPIHTTP */, SSIConfig &tConfig,
time_t tT, char *pszBuffer, int iLength )
{
enum { BUF_SIZE=255 };
char szBuf[BUF_SIZE+1];
PIPlatform_beforeUnsafeBlock();
struct tm *pTms = gmtime( &tT );
int iRet = strftime( szBuf, BUF_SIZE, tConfig.sTimeFmt, pTms );
PIPlatform_afterUnsafeBlock();
szBuf[iRet] = '\0';
if ( iLength )
{ strncpy( pszBuffer, szBuf, iLength ); };
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Date GMT
\*____________________________________________________________________________*/
static int f_a( PIHTTP *pPIHTTP, void *pData, char *pszBuffer, int iLength )
{
(void)*pPIHTTP;
assert( pData );
SSIParam *pParam = (SSIParam *)pData;
time_t tT;
time( &tT );
return Internal_GMTTime( pPIHTTP, pParam->tConfig, tT, pszBuffer,
iLength );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Date local
\*____________________________________________________________________________*/
static int f_b( PIHTTP *pPIHTTP, void *pData, char *pszBuffer, int iLength )
{
(void)pPIHTTP;
assert( pData );
SSIParam *pParam = (SSIParam *)pData;
time_t tT;
time( &tT );
enum { BUF_SIZE=255 };
char szBuf[BUF_SIZE+1];
PIPlatform_beforeUnsafeBlock();
struct tm *pTms = localtime( &tT );
int iRet = strftime( szBuf, BUF_SIZE, pParam->tConfig.sTimeFmt, pTms );
PIPlatform_afterUnsafeBlock();
szBuf[iRet] = '\0';
if ( iLength )
{ strncpy( pszBuffer, szBuf, iLength ); };
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Last modified
\*____________________________________________________________________________*/
static int f_c( PIHTTP *pPIHTTP, void *pData, char *pszBuffer, int iLength )
{
(void)pPIHTTP;
assert( pData );
SSIParam *pParam = (SSIParam *)pData;
time_t tT = PIFInfo_getLastModified( pParam->pFInfo );
return Internal_GMTTime( pPIHTTP, pParam->tConfig, tT, pszBuffer,
iLength );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Last modified
\*____________________________________________________________________________*/
static int f_d( PIHTTP *pPIHTTP, void *pData, char *pszBuffer, int iLength )
{
(void)pPIHTTP;
(void)pData;
#define TMP "## Not Implemented ##"
if ( iLength>0 )
{
assert( pszBuffer );
strncpy( pszBuffer, TMP, iLength );
};
return sizeof(TMP)-1;
}
/*____________________________________________________________________________*\
*
Description:
Create a map of FnPi3Write functions for parameters to expressions
\*____________________________________________________________________________*/
static FnPi3Write aFunctions[256] =
{
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
*/
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 .. 15 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 .. 31 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32 .. 47 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 48 .. 63 */
/* A B C D E F G H I J K L M N O */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 64 .. 79 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 .. 95 */
/* a b c d e f g h i j k l m n o */
0,f_a,f_b,f_c,f_d, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 96 .. 111 */
/* q r s t u v w x y z */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 112 .. 127 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* .. 255 */
};
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class SendSSI : public HandlerBaseHTTP
{
private:
/* --- function and DB of functions --- */
typedef int (* SSIFunc)( SendSSI &, PIHTTP &, const char *, int,
const char *, int, SSIParam & );
PIDB *pFunctions;
PIDB *pVariables;
/* ---
Configuration data
--- */
SSIConfig tConfig;
int iAllowExec;
Pi3Expression *pHeaderPattern; /* pattern for top */
Pi3Expression *pFooterPattern; /* pattern for footer */
Pi3Expression *pExecFileFixup; /* pattern exec file fix up */
/* ---
Internal methods
--- */
/* ---
Dispatch a subrequest to get information on a file or return it to
the client.
Returns non-zero on success, 0 on failure.
--- */
int GetSSIFile( PIHTTP &tPIHTTP, const char *pSSIFile,
int iSSIFileLen, int iVirtual, SSIParam &tParam, int iExec,
int iDoHandle, PIFInfo **ppFInfo )
{
/* --- make sub request context --- */
PIHTTP *pChildHTTP = PIHTTP_newChild( &tPIHTTP );
/*
** Find the '?' in the path (for Query String)
*/
int i=0;
for( ; i<iSSIFileLen && pSSIFile[i]!='?'; i++ );
/* --- set path --- */
PIString sTmp( pSSIFile, i );
/* --- set query string --- */
PIString sQS;
if ( i<iSSIFileLen )
{
i++;
PIString sTmpQS( &(pSSIFile[i]), iSSIFileLen-i );
sQS = sTmpQS;
};
if ( !iVirtual )
{
PIString sFile( PIFInfo_getPathRoot( tParam.pFInfo ) );
sFile.Concatenate( HTTP_DIRSEPERATOR );
sFile.Concatenate( sTmp );
sTmp = sFile;
};
PIDB_replace( pChildHTTP->pResponseDB, PIDBTYPE_STRING,
KEY_INT_PATH, (void *)(const char *)sTmp, 0 );
PIDB_replace( pChildHTTP->pRequestDB, PIDBTYPE_STRING,
KEY_HTTP_QUERYSTRING, (void *)(const char *)sQS, 0 );
/* --- dispatch the sub request across a number of phases --- */
int iRet = PIAPI_ERROR;
iRet = HTTPCore_dispatch(
pChildHTTP,
iVirtual ? PH_MAPPING : PH_CHECKPATH,
PH_CHECKTYPE );
/* --- check for error --- */
if ( iRet!=PIAPI_COMPLETED ||
((pChildHTTP->iStatus!=0) && (pChildHTTP->iStatus!=ST_OK)) )
{
PIHTTP_delete( pChildHTTP );
return 0;
};
/* ---
If this is exec and not virtual and we have a pExecFileFixup pattern
execute it now to setup variables so the physical path is
processed by the correct CGI handler.
This is necessary because virtual paths usually sets the
appropriate markers as part of path mapping
--- */
if ( iExec && !iVirtual && tParam.pExecFileFixup )
{
Pi3Expression_write( tParam.pExecFileFixup, pChildHTTP, 0, 0, 0 );
};
/* ---
check this is not an executable file, just check Content-Type is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -