📄 isapiflt.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1999 Holger Zimmermann, John Roy. 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/ISAPIFLT/ISAPIFLT.cpp,v $
* $Date: 2004/03/14 09:44:55 $
*
Description:
Load and execute an ISAPI filter.
\*____________________________________________________________________________*/
//$SourceTop:$
#include <iostream.h>
#include <stdio.h>
#if WIN32
//#include <HTTPExt.h>
#include <windows.h>
#include <HTTPFilt.h>
#endif
#include "PIString.h"
#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPDefs.h"
#include "HTTPUtil.h"
#include "MiscUtil.h"
#include "PIStrStr.h"
#include "PIUtil.h"
#include "DeQuote.h"
#include "StrToken.h"
#include "Base64.h"
#if WIN32
/*
** #define D { cerr << __FILE__ << ": " << __LINE__ << endl; }
*/
#define D
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#if 0
/*
** HTML documentation for this handler
*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
ISAPIFLT
Description:
Load and execute an ISAPI (Internet Server API) filter DLL. You can find more
informations about ISAPI filters in the <A HREF="http://msdn.microsoft.com/library/psdk/iisref/isgu0q0n.htm">
MSDN library</A>. This handler will load the configured ISAPI filter DLL when
the server is started. If you want to load more than one ISAPI filter you must
use one ISAPIFLT handler for each filter DLL. During a HTTP request the handler
will dispatch the registered ISAPI filter notifications to the filter DLL.
Options:
<H4>Overview</H4>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)
<TR>
<TD>Condition
<TD>-
<TD>A Pi3Expression
<TD>If condition is TRUE, filter is responsible for the request
<TD>Condition "®exp('*/admin/*',$z)"
<TR>
<TD>FilterDLLPath
<TD>+
<TD>A valid file path
<TD>The path to an ISAPI filter DLL
<TD>FilterDLLPath "C:\Pi3Web\Isapi\daf.dll"
<TR>
<TD>Terminators
<TD>-
<TD>Init, Headers, Hostmap, Mapping, CheckPath, CheckAuth,
CheckAccess, CheckType, Handle, Log, Destroy
<TD>Phases, that are terminated by this handler
<TD>Terminators Headers | Mapping | Log
<TR>
<TD>SystemLogon
<TD>Yes
<TD>Yes|No
<TD>Indicates if the server should attempt a system logon
<TD>Terminators Headers | Mapping | Log
<TR>
<TD>Variable
<TD>-
<TD><Pi3Expression>
<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><A string>
<TD>Used as prefix of each extra header
<TD>ExtraHeadersPrefix "HTTP_"
<TR>
<TD>ExtraHeadersIgnore
<TD>-
<TD><Space delimited Strings>
<TD>List of unconsidered extra headers
<TD>ExtraHeadersIgnore "Content-Type Content-Length"
</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>Condition</H5>
If the condition given by the Pi3Expression is TRUE, the filter controlled
by this handler is responsible for the request. The filter will get all
notifications registered with GetFilterVersion during startup. This gives
the ISAPI filter handlers more flexibility and a superior control mechanism
as replacement for the IIS filter priority chain.
<H5>FilterDLLPath</H5>
The DLL given by this option will be loaded during server startup and
controlled by the handler during HTTP requests. Only ISAPI filter DLLs
are allowed here. If the DLL is not an ISAPI filter or the filter version
is not compliant or the filter uses an unsupported notification type the
DLL will not be loaded and the server start will be aborted with an
error message.
<H5>Terminators</H5>
Pi3Web server needs exactly one handler to complete one HTTP request phase.
For efficiency the following handlers will not be called if a phase is
completed. From this it follows sometimes we have to change the default
termination of a phase (i.e. Mapping ) to give ISAPI filters a chance to
be called by the main dispatcher. Sometimes we have to complete a phase by
an ISAPI filter (i.e. a Log filter). If the filter DLL returns
"SF_STATUS_REQ_HANDLED_NOTIFICATION", PIAPI_COMPLETE will be returned
automatically by the handler. Otherwise we can use this option to explicite
complete a phase if the filter is called. All phase names are allowed here
separated by "|".
<H5>SystemLogon</H5>
If this parameter is switched off and the returned status by the filter
is SF_STATUS_REQ_HANDLED_NOTIFICATION, this is treated as successful
authentication.
If this parameter is switched on and the returned status by the filter
is SF_STATUS_REQ_HANDLED_NOTIFICATION, the Pi3Web will attempt a system
logon.
<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:
All Phases
Returns:
PIAPI_COMPLETED, PIAPI_CONTINUE, PIAPI_ERROR, or INT_REDIRECT according
to the status returned by the filter.
Notes:
<H5>General</H5>
The ISAPI filter handler is an experimental plug in. Since the IIS and
Pi3Web has different architectures it is difficult to be 100% compliant.
<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>
<H5>Filter version</H5>
The version of the filter DLL is checked against the server ISAPI version
(2.1). If the version isn't compliant the filter isn't loaded and the server
start will abort with an error message.
<H5>Filter notifications</H5>
All filter notification events are supported except of SF_NOTIFY_READ_RAW_DATA
and SF_NOTIFY_SEND_RAW_DATA events. If the filter try to register these events,
the filter will not be loaded and the server start will abort with an error message.
<H5>Notification order</H5>
Filter priority is not supported with Pi3Web. If you chain ISAPI filters with
other handlers the order is fixed in Config.Pi3 in the "Handlers" section of the
HTTPLogicObject. To much filters with lots of notifications will noticeable slow
down the server.
<H5>Phase completion</H5>
An architecture sign of Pi3Web is, that one handler must finish a connection
phase by returning PIAPI_COMPLETED. If the filter DLL will return
"SF_STATUS_REQ_HANDLED_NOTIFICATION" after handling a notification, the handler
will return PIAPI_COMPLETED and the phase is finished. If the filter does not,
then you can force this with the handler option "Terminators".
<H5>ServerSupportFunction</H5>
The following support functions are not supported:
<P>SF_REQ_SET_NEXT_READ_SIZE
<P>SF_REQ_GET_CONNID
<H5>Server variables</H5>
A pre-defined set of IIS server variables are supported. "ALL_HTTP" will
hold all request headers except of the values stored in other server variables.
This is very flexible by adding, changing or removing "Variable" parameters
from the configuration file.
<H5>Authentication filters</H5>
If an authentication filter returns SF_STATUS_REQ_HANDLED_NOTIFICATION on
notification SF_NOTIFY_AUTHENTICATION, the server will attempt to logon and
impersonate the user at the system dependent on setting "SystemLogon".
<P>This works only on Windows NT/2000/XP and requires permissions to
"act as part of the operating system" for the user running the Pi3Web
(except XP). Otherwise the HTTP statuscode 401 and appropriate response
headers to force a basic authentication are set by the Pi3Web.
Example:
<PRE>
<Object>
Name DAFFLT
Class ISAPIFLTClass
Condition "®exp('*/admin/*',$z)"
FilterDLLPath "C:\Pi3Web\Isapi\daf.dll"
Terminators Headers | Mapping
</Object>
</PRE>
/*___+++HTMLDOC_END+++___*/
#endif
/*____________________________________________________________________________*\
*
Class:
Description:
Prototypes for callback functions
Class for ISAPI context
\*____________________________________________________________________________*/
BOOL WINAPI fnGetServerVariable( PHTTP_FILTER_CONTEXT pfc,
LPSTR lpszVariableName,
LPVOID lpvBuffer,
LPDWORD lpdwSize );
BOOL WINAPI fnWriteClient( PHTTP_FILTER_CONTEXT pfc,
LPVOID Buffer,
LPDWORD lpdwBytes,
DWORD dwReserved );
VOID * WINAPI fnAllocMem ( PHTTP_FILTER_CONTEXT pfc,
DWORD cbSize,
DWORD dwReserved );
BOOL WINAPI fnServerSupportFunction ( PHTTP_FILTER_CONTEXT pfc,
enum SF_REQ_TYPE sfReq,
PVOID pData,
DWORD ul1,
DWORD ul2 );
BOOL WINAPI fnAddResponseHeaders ( PHTTP_FILTER_CONTEXT pfc,
LPSTR lpszHeaders,
DWORD dwReserved );
BOOL WINAPI fnGetHeader ( PHTTP_FILTER_CONTEXT pfc,
LPSTR lpszName,
LPVOID lpvBuffer,
LPDWORD lpdwSize );
BOOL WINAPI fnSetHeader ( PHTTP_FILTER_CONTEXT pfc,
LPSTR lpszName,
LPSTR lpszValue );
BOOL WINAPI fnAddHeader ( PHTTP_FILTER_CONTEXT pfc,
LPSTR lpszName,
LPSTR lpszValue );
typedef BOOL (WINAPI * PFN_GETFILTERVERSION)( HTTP_FILTER_VERSION *pVer );
typedef DWORD (WINAPI * PFN_HTTPFILTERPROC )( PHTTP_FILTER_CONTEXT pfc,
DWORD NotificationType,
VOID *pvNotification );
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#define KEY_INT_ISAPICONTEXT "ISAPI_CONTEXT"
#define KEY_CONF_CONDITION "Condition"
#define KEY_CONF_FLTDLLPATH "FilterDLLPath"
#define KEY_CONF_TERMINATORS "Terminators"
#define KEY_CONF_EXTRAHDRPREFIX "ExtraHeadersPrefix"
#define KEY_CONF_EXTRAHEADERSIGNORE "ExtraHeadersIgnore"
#define KEY_CONF_EXTRAHEADERS "ExtraHeaders"
#define KEY_CONF_SYSTEMLOGON "SystemLogon"
#define VALUE_NO "No"
#define VALUE_YES "Yes"
#define KEY_CONF_VARIABLE "Variable"
/*____________________________________________________________________________*\
*
Flags and Options
\*____________________________________________________________________________*/
#define FLG_NONE 0x00000000
#define FLG_INIT 0x00000001
#define FLG_HEADERS 0x00000002
#define FLG_HOSTMAP 0x00000004
#define FLG_MAPPING 0x00000008
#define FLG_CHECKPATH 0x00000010
#define FLG_CHECKAUTH 0x00000020
#define FLG_CHECKACCESS 0x00000040
#define FLG_CHECKTYPE 0x00000080
#define FLG_HANDLE 0x00000100
#define FLG_LOG 0x00000200
#define FLG_DESTROY 0x00000400
/*___ +++++++++++++++++++++++++++++++++++++++++++++++++ ___ *
Map these flags to names
*___ +++++++++++++++++++++++++++++++++++++++++++++++++ ___ */
struct {
const char *pName;
int iFlag;
} aFlagMap[] =
{
{ "Init", FLG_INIT },
{ "Headers", FLG_HEADERS },
{ "Hostmap", FLG_HOSTMAP },
{ "Mapping", FLG_MAPPING },
{ "CheckPath", FLG_CHECKPATH },
{ "CheckAuth", FLG_CHECKAUTH },
{ "CheckAccess", FLG_CHECKACCESS },
{ "CheckType", FLG_CHECKTYPE },
{ "Handle", FLG_HANDLE },
{ "Log", FLG_LOG },
{ "Destroy", FLG_DESTROY }, /* ---
/* --- leave this last always --- */
{ 0, 0 }
};
/*___ +++++++++++++++++++++++++++++++++++++++++++++++++ ___ *
Map these phase flags to phases by index
*___ +++++++++++++++++++++++++++++++++++++++++++++++++ ___ */
int aPhaseMap[] =
{
// { FLG_NONE },
{ FLG_INIT },
{ FLG_HEADERS },
{ FLG_HOSTMAP },
{ FLG_MAPPING },
{ FLG_CHECKPATH },
{ FLG_CHECKAUTH },
{ FLG_CHECKACCESS },
{ FLG_CHECKTYPE },
{ FLG_HANDLE },
{ FLG_LOG },
{ FLG_DESTROY }, /* ---
/* --- leave this last always --- */
{ 0 }
};
/*____________________________________________________________________________*\
*
Class:
Description:
Class for ISAPI context
\*____________________________________________________________________________*/
class ISAPIFLT;
class ISAPIContext : public _HTTP_FILTER_CONTEXT
{
public:
ISAPIFLT *pISAPIFLT;
PIHTTP *pPIHTTP;
HTTP_FILTER_VERSION tVer;
const char *pszHeaders;
int iRc;
HANDLE hToken;
DWORD cbTotalBytes;
ISAPIContext( ISAPIFLT *pTheISAPIFLT, PIHTTP *pThePIHTTP,
HTTP_FILTER_VERSION tTheVer )
: pISAPIFLT( pTheISAPIFLT ),
pPIHTTP( pThePIHTTP ),
tVer( tTheVer ),
pszHeaders( NULL),
iRc( PIAPI_COMPLETED ),
hToken( 0 )
{
/* --- initialize member data --- */
cbSize = sizeof( _HTTP_FILTER_CONTEXT );
Revision = HTTP_FILTER_REVISION;
ServerContext = (PVOID)this;
/* --- a filter function may initialize this --- */
pFilterContext = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -