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

📄 wincgi.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*____________________________________________________________________________*\
 *

 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/WinCGI.cpp,v $
 * $Date: 2003/06/09 13:29:54 $
 *
 Description:
	WinCGI Handler

    Rewritten so this module handles windows WinCGI only. CGI is now in
    CGI.cpp

\*____________________________________________________________________________*/
//$SourceTop:$

#include <ctype.h>
#include <stdio.h>

#include "PIStrStr.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "HTTPUtil.h"
#include "PIHTTP.h"
#include "Pi2API.h"
#include "StrToken.h"
#include "DeQuote.h"
#include "HandBase.h"
#include "Pi3Expr.h"

#if WIN32
#	include <windows.h>
#	include <io.h>
	typedef HANDLE FILEDESC;
	typedef HANDLE PROCDESC;
#	define INVALID_DESC	(INVALID_HANDLE_VALUE)
	typedef void * ENVIRONMENT;
	inline bool CloseFileDesc( FILEDESC &tFileDesc )
				{
#	if !defined(NDEBUG)
				int iRet = ::CloseHandle( tFileDesc )==0 ? false : true;
				tFileDesc = INVALID_DESC;
				return iRet;
#	else
				return ::CloseHandle( tFileDesc )==0 ? false : true;
#	endif
				};
#elif POSIX
#	include <unistd.h>
#	include <errno.h>
#	include <signal.h>
	typedef int PROCDESC;
	typedef int FILEDESC;
#	define INVALID_DESC	(-1)
	typedef void **ENVIRONMENT;
	inline bool CloseFileDesc( FILEDESC &tFileDesc )
				{
#	if !defined(NDEBUG)
				int iRet = ::close( tFileDesc )==-1 ? false : true;
				tFileDesc = INVALID_DESC;
				return iRet;
#	else
				return ::close( tFileDesc )==-1 ? false : true;
#	endif
				};
#	define _MAX_PATH	PATH_MAX
#else
#	error Unsupported operating system
#endif

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_FILEIOOBJECT					"FileIOObject"
#define KEY_CONF_DEFAULTCOMMANDLINE				"DefaultCommandLine"
#define KEY_CONF_COMMANDLINEBYEXT				"CommandLineByExt"
#define KEY_CONF_EXTRAHEADERS					"ExtraHeaders"
#define VALUE_NO								"No"
#define VALUE_YES								"Yes"
#define KEY_CONF_EXTRAHEADERSPREFIX				"ExtraHeadersPrefix"
#define KEY_CONF_EXTRAHEADERSIGNORE				"ExtraHeadersIgnore"
#define KEY_CONF_SENDCRLF						"SendCRLF"
#define KEY_CONF_EXTERNALPATH					"ExternalPath"
#define KEY_CONF_DATAFILE						"DataFile"
#define KEY_CONF_STDINFILE						"StdinFile"
#define KEY_CONF_STDOUTFILE						"StdoutFile"
#define KEY_CONF_DATABLOCK						"DataBlock"
#define KEY_CONF_FLAG							"Flag"
#define KEY_CONF_KILLAFTER						"KillAfter"
#define VALUE_16BIT								"16-Bit"

/* --- really verbose development-time debugging --- */
#define VERBOSE_DEBUG	0

#if VERBOSE_DEBUG
#	define D { cerr << __FILE__ << ", " << dec << __LINE__ << ": " << endl; }
#else
#	define D
#endif

/*
** Flags 
*/
#define FLG_16BIT								0x0001

/*____________________________________________________________________________*\
 *
 Description:
	Documentation.
\*____________________________________________________________________________*/
#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name WinCGIClass
		Type LogicExtension
		Library HTTP
		OnClassLoad HandlerBaseHTTP_onClassLoad
		Constructor WinCGI_constructor
		CopyConstructor HandlerBaseHTTP_copyConstructor
		Destructor HandlerBaseHTTP_destructor
		Execute HandlerBaseHTTP_execute
	</Class>

	<Object>
		Name WinCGI
		Class WinCGIClass
	</Object>

/*___+++CNF_END+++___*/
#endif
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	WinCGI

Description:
	This HTTP Handler handles WinCGI requests.

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

<TR>
<TD>FileIOObject
<TD>+
<TD>&lt;objectname&gt;
<TD>A Pi3 IO object
<TD>FileIOObject "CGIFileIO"

<TR>
<TD>CommandLineByExt
<TD>-
<TD>&lt;key&gt;=&quot;&lt;Pi3Expression&gt;&quot;
<TD>A key-value pair with an extension and a Pi3Expression
<TD>CommandLineByExt .cgi=&quot;perl %p%q&quot;

<TR>
<TD>DefaultCommandLine
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>A Pi3Expression, which expands to a command line
<TD>DefaultCommandLine &quot;%p %d&quot;

<TR>
<TD>ExtraHeaders
<TD>Yes
<TD>Yes|No
<TD>Indicates if extra headers are considered
<TD>ExtraHeaders Yes

<TR>
<TD>ExtraHeadersPrefix
<TD>-
<TD>&lt;A string&gt;
<TD>Used as prefix of each extra header
<TD>ExtraHeadersPrefix &quot;HTTP_&quot;

<TR>
<TD>ExtraHeadersIgnore
<TD>-
<TD>&lt;Space delimited strings&gt;
<TD>List of unconsidered extra headers
<TD>ExtraHeadersIgnore &quot;Content-Type Content-Length&quot;

<TR>
<TD>ExternalPath
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>Path to external CGI form data files
<TD>ExternalPath &quot;./CGITemp/&quot;

<TR>
<TD>DataFile
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>Path to CGI data file
<TD>DataFile &quot;./CGITemp/%u.ini&quot;

<TR>
<TD>StdinFile
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>Path to CGI input file
<TD>StdinFile &quot;./CGITemp/%u.in&quot;

<TR>
<TD>StdoutFile
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>Path to CGI output file
<TD>StdoutFile &quot;./CGITemp/%u.out&quot;

<TR>
<TD>DataBlock
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>Block of text written to datafile
<TD>DataBlock &quot;[CGI]\nRequest Protocol...&quot;

<TR>
<TD>KillAfter
<TD>-1
<TD>&lt;A number&gt;
<TD>Time to wait before killing the process.
<TD>KillAfter &quot;-1&quot;

<TR>
<TD>SendCRLF
<TD>No
<TD>Yes|No
<TD>Adds additional "CRLF" to the CGI input.
<TD>SendCRLF Yes

<TR>
<TD>Flag
<TD>-
<TD>"16-Bit", etc.
<TD>A flag to effect CGI
<TD>Flag &quot;16-Bit&quot;

</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>FileIOObject</H5>
Specifies the IO object to be used to communicate with CGI programs.
A prototype object is created on initialization and a copy is generated
using PIObject_copy() for each CGI program.

<H5>CommandLineByExt</H5>
This configuration key could be given multiple times to declare command
line mappings for file extensions of CGI programs. The value is treated
as Pi3Expression.

<H5>DefaultCommandLine</H5>
This configuration key is used to declare the default command line,
which is used, if no mapping matches. The value is treated as
Pi3Expression.

<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.

<H5>ExternalPath</H5>
A valid path name, which is used to form the full path to temporary
files used to pass external form variables to the CGI program.

<H5>DataFile</H5>
A Pi3Expression which is evaluated to form the full path to a file used to
pass variable-value arguments to the CGI program.
<P>
This mechanism is typically used to support CGI variants other than
standard CGI, namely WinCGI and DOSCGI.
<P>
Specification of 'DataFile', 'StdinFile' or 'StdoutFile' cause the
standard CGI streams environment to be disabled.

<H5>StdinFile</H5>
A Pi3Expression which is evaluated to form the full path to a file used to
pass input from the browser to the CGI program.
<P>
This mechanism is typically used to support CGI variants other than
standard CGI, namely WinCGI and DOSCGI.
<P>
Specification of 'DataFile', 'StdinFile' or 'StdoutFile' cause the
standard CGI streams environment to be disabled.

<H5>StdoutFile</H5>
A Pi3Expression which is evaluated to form the full path to a file used to
pass the standard output from the CGI program. Some forms of CGI, notably
WinCGI use files to pass information between the CGI program and HTTP server.
The parameter %u used in the expression will evaluate to a sequence of numbers
and letters unique to this HTTP process and request.
<P>
This mechanism is typically used to support CGI variants other than
standard CGI, namely WinCGI and DOSCGI.
<P>
Specification of 'DataFile', 'StdinFile' or 'StdoutFile' cause the
standard CGI streams environment to be disabled.

<H5>DataBlock</H5>
A Pi3Expression which is evaluated to form the contents of the data file
(.INI file) passed to the CGI program.
<P>
This mechanism is typically used to support CGI variants other than
standard CGI, namely WinCGI and DOSCGI.

<H5>KillAfter</H5>
Specifies the amount of time the server will wait for a CGI response
before forceably killing the process. -1 specifies an infinite timeout.
This value is measured in seconds.

<H5>SendCRLF</H5>
If this option is set to &quot;Yes&quot;, an additional linefeed (CRLF),
which is not contained in the Content-Length, is added to the CGI input
by the handler. This is required by some CGI programs.

<H5>Flag</H5>
Specifies a flag which changes CGI behaviour. This directive can be
repeated multiple times to add different flags.<P>
Flags are:-<BR>
<I>16-Bit</I> <B>Windows NT only</B>, expect CGI programs to be 16-Bit
Windows or MS-DOS images, change semantics appropriately.

Parameters:
The Pi3Expressions in the configuration variables DataFile, StdinFile,
StdoutFile and DataBlock 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>%d<TD>Full pathname to the data file<TR>
<TD>%i<TD>Full pathname to the content file<TR>
<TD>%o<TD>Full pathname of the output file<TR>
<TD>%p<TD>Full pathname to WinCGI program<TR>
<TD>%u<TD>Unique ID in the context of the current process (PID)<TR>
<TD>%q<TD>Value of an &quot;isIndex&quot; query<TR>
<TD>%h<TD>List of extra request headers in Windows ini-format<TR>
<TD>%a<TD>List of accepts headers in Windows ini-format<TR>
<TD>%l<TD>List of decoded form literals in Windows ini-format<TR>
<TD>%e<TD>List of decoded external form literals in Windows ini-format<TR>
<TD>%g<TD>List of encoded huge form data in Windows ini-format<TR>
<TD>%f<TD>List of uploaded multipart/form-data files in Windows ini-format<TR>
</TABLE>
</CENTER>

Example:
	<PRE>

	&lt;Object&gt;
		Name MyWinCGI
		Class WinCGIClass
		FileIOObject "CGIFileIO"
		CommandLineByExt .cgi="perl %p %d"
		DefaultCommandLine "%p %d"
		ExtraHeaders Yes
		ExtraHeadersPrefix "HTTP_"
		ExtraHeadersIgnore "Content-Type Content-Length"
		ExternalPath "cgi-temp/"
		DataFile "cgi-temp/$P_%u.dat"
		StdinFile "cgi-temp/$P_%u.in"
		StdoutFile "cgi-temp/$P_%u.out"
		DataBlock "\[CGI]$M\
	Request Protocol=$H$M\
	Request Method=$m$M\
	$M\
	[Accept]$M\
	%a$M\
	[System]$M\
	Output File=%o$M\
	Content File=%i$M\
	$M\
	[Extra Headers]$M\
	%h$M\
	[Form Literal]$M\
	%l$M\
	[Form External]$M\
	%e$M\
	[Form File]$M\
	%f$M\
	[Form Huge]$M\
	%g$M"
	&lt;/Object&gt;
	</PRE>
/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
inline void CloseIfValidDesc( FILEDESC &tFD )
{
	if (tFD!=INVALID_DESC)
		{ ::CloseFileDesc( tFD ); };
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
inline void CloseProcessDesc( PROCDESC &tProcess )
{
#if WIN32
	CloseIfValidDesc( tProcess );
#else
	(void)tProcess;
#endif
}

/*____________________________________________________________________________*\
 *
 Class:

⌨️ 快捷键说明

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