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

📄 fastcgi.cpp

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

 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/Fcgi/FastCGI.cpp,v $
 * $Date: 2003/05/20 05:14:57 $
 *
 Description:
	Simple Fast CGI as per Open Market specifications.

\*____________________________________________________________________________*/
//$SourceTop:$

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

#include "DblList.h"
#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "PIStrStr.h"

#define D { cerr << __FILE__ << ": " << __LINE__ << endl; };
// #define D

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	FastCGI

Description:
	Perform simple fastcgi request forwarding.

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

<TR>
<TD>IOObject
<TD>+
<TD>&lt;A Pi3 Object&gt;
<TD>Prototype IO object for fast cgi application server
<TD>IOObject "FastCGIIO"

<TR>
<TD>File
<TD>-
<TD>&lt;a filename&gt;
<TD>Filename to match
<TD>File "App1.fcgi"

<TR>
<TD>FCGIHost
<TD>localhost
<TD>&lt;A hostname or IP Address&gt;
<TD>Host to connect to application server
<TD>FCGIHost "localhost"

<TR>
<TD>FCGIPort
<TD>3333
<TD>&lt;A TCP port&gt;
<TD>Port to connect to application server
<TD>FCGIPort 2323

<TR>
<TD>Variable
<TD>-
<TD>&lt;Pi3Expression&gt;
<TD>A variable,value parameter definition
<TD>Variable "REMOTE_ADDR=$A"

</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>IOObject</H5>
Specifies an IOObject which will be the prototype of IO objects used to
communicate with the fastcgi application server process.
This IOObject may directly implement an IPC mechanism (shared memory,
TCP/IP or named pipes) or its may be a filter (SSL, monitor) applied
to one of the IPC mechanisms.

<H5>File</H5>
Specifies a filename to match against to activate this handler with
the specified host and port. If this field is not present the will
attempt to get the host and port pair from the resource associated
with the request (the file should contain the hostname followed by
whitespace followed by the port number).

<H5>FCGIHost</H5>
Specifies the hostname that will be passed as argument 1 to the prototype
IO object when creating the new IO object using PIIObject_copy(), for 
TCPIP IO objects this will be used as the hostname on which to connect 
to the fast cgi application server.

<H5>FCGIPort</H5>
Specifies the port that will be passed as argument 2 to the prototype
IO object when creating the new IO object using PIIObject_copy(), for 
TCPIP IO objects this will be used as the port on which to connect 
to the fast cgi application server.

<H5>Variable</H5>
Specifies a parameter expression that will be evaluated and sent to the
fast cgi application server as part of the parameter definition. This
is equivalent to specifying a CGI environment variable.

Phase:
	HANDLE

Returns:
	PIAPI_COMPLETED on success.
	PIAPI_CONTINUE is this handler ignored the request.
	If this handler was invoked for a phase other than HANDLE then PIAPI_ERROR
	is returned.

Note:

Example:
	<PRE>
	&lt;Object&gt;
		Name FastCGI
		Class FastCGIClass
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		Handle Condition="&cmp($c,internal/x-fastcgi)" FastCGI \
			File="App1.fcgi" IOObject="FcgiIO" FCGIHost="localhost" \
			FCGIPort=3333
		...
	&lt;/Object&gt;

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

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_IOOBJECT						"IOObject"
#define KEY_CONF_FILE							"File"
#define KEY_CONF_FCGIHOST						"FCGIHost"
#define KEY_CONF_FCGIPORT						"FCGIPort"
#define KEY_CONF_VARIABLE						"Variable"

#if 1
/*
 * Listening socket file number
 */
#define FCGI_LISTENSOCK_FILENO 0

typedef struct {
    unsigned char version;
    unsigned char type;
    unsigned char requestIdB1;
    unsigned char requestIdB0;
    unsigned char contentLengthB1;
    unsigned char contentLengthB0;
    unsigned char paddingLength;
    unsigned char reserved;
} FCGI_Header;

/*
 * Number of bytes in a FCGI_Header.  Future versions of the protocol
 * will not reduce this number.
 */
#define FCGI_HEADER_LEN  8

/*
 * Value for version component of FCGI_Header
 */
#define FCGI_VERSION_1           1

/*
 * Values for type component of FCGI_Header
 */
#define FCGI_BEGIN_REQUEST       1
#define FCGI_ABORT_REQUEST       2
#define FCGI_END_REQUEST         3
#define FCGI_PARAMS              4
#define FCGI_STDIN               5
#define FCGI_STDOUT              6
#define FCGI_STDERR              7
#define FCGI_DATA                8
#define FCGI_GET_VALUES          9
#define FCGI_GET_VALUES_RESULT  10
#define FCGI_UNKNOWN_TYPE       11
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)

/*
 * Value for requestId component of FCGI_Header
 */
#define FCGI_NULL_REQUEST_ID     0

typedef struct {
    unsigned char roleB1;
    unsigned char roleB0;
    unsigned char flags;
    unsigned char reserved[5];
} FCGI_BeginRequestBody;

typedef struct {
    FCGI_Header header;
    FCGI_BeginRequestBody body;
} FCGI_BeginRequestRecord;

/*
 * Mask for flags component of FCGI_BeginRequestBody
 */
#define FCGI_KEEP_CONN  1

/*
 * Values for role component of FCGI_BeginRequestBody
 */
#define FCGI_RESPONDER  1
#define FCGI_AUTHORIZER 2
#define FCGI_FILTER     3

typedef struct {
    unsigned char appStatusB3;
    unsigned char appStatusB2;
    unsigned char appStatusB1;
    unsigned char appStatusB0;
    unsigned char protocolStatus;
    unsigned char reserved[3];
} FCGI_EndRequestBody;

typedef struct {
    FCGI_Header header;
    FCGI_EndRequestBody body;
} FCGI_EndRequestRecord;

/*
 * Values for protocolStatus component of FCGI_EndRequestBody
 */
#define FCGI_REQUEST_COMPLETE 0
#define FCGI_CANT_MPX_CONN    1
#define FCGI_OVERLOADED       2
#define FCGI_UNKNOWN_ROLE     3

/*
 * Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT records
 */
#define FCGI_MAX_CONNS  "FCGI_MAX_CONNS"
#define FCGI_MAX_REQS   "FCGI_MAX_REQS"
#define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"

typedef struct {
    unsigned char type;    
    unsigned char reserved[7];
} FCGI_UnknownTypeBody;

typedef struct {
    FCGI_Header header;
    FCGI_UnknownTypeBody body;
} FCGI_UnknownTypeRecord;

#endif

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class FastCGI : public HandlerBaseFcgi
{
private:
	FastCGI( const FastCGI &t )
	:	HandlerBaseFcgi( t )
		{ assert( 0 ); };

	/* ---
	Configuration data
	--- */
	PIObject *pIO;						/* prototype IO object */
	PIString sFile;						/* file to match */
	PIString sHost;						/* host of fcgi application server */
	int iPort;							/* port of fcgi application server */
	enum { PARAM_BLOCK_SIZE=4096 };		/* buffer size for parameters */
	DblList lVariables;					/* expression for parameters */

protected:
	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "FastCGI: ";
		if ( !PIUtil_stricmp( KEY_CONF_FILE, pVariable ) )
			{
			sFile = pValue;
			}
		else if ( !PIUtil_stricmp( KEY_CONF_FCGIHOST, pVariable ) )
			{
			sHost = pValue;
			}
		else if ( !PIUtil_stricmp( KEY_CONF_FCGIPORT, pVariable ) )
			{
			iPort = pValue ? atoi( pValue ) : 0;
			}
		else if ( !PIUtil_stricmp( KEY_CONF_IOOBJECT, pVariable ) )
			{
			if ( pIO )
				{
				os << "'IOObject' may only be specified once." << ends;
				CONFIG_ERR( Object(), os.str() );
				return 0;
				};

			PIObject *pTmpIO = PIObject_loadFromLine( 
				PIObject_getDB( Object() ),
				PIObject_getConfigurationDB( Object() ),
				pValue );

			if ( !pTmpIO )
				{ return 0; };

			pIO = pTmpIO;
			}
		else if ( !PIUtil_stricmp( KEY_CONF_VARIABLE, pVariable ) )
			{
			Pi3String *pError = Pi3String_new( 0 );
			Pi3Expression *pExpr = Pi3Expression_new( pValue, 0,
				pError );
			if ( !pExpr )
				{
				os << Pi3String_getPtr( pError ) << ends;
				Pi3String_delete( pError );
				CONFIG_ERR( Object(), os.str() ); 
				return 0;
				};
			Pi3String_delete( pError );
			lVariables.Append( (DblList::type)pExpr );
			}
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	FastCGI( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	HandlerBaseFcgi( pObject ),
		pIO( 0 ),
		sHost( "localhost" ),
		iPort( 3333 )
		{
		ReadParameters( iArgc, ppArgv );
		if ( !IsOK() )
			{ return; };

		if ( !pIO )
			{
			CONFIG_ERR( Object(), "FastCGI: Missing definition for \
'IOObject'." );
			SetOK( 0 );
			return;
			};

#if 0
		/* ---
		Make an identification string for error messages
		--- */
		PIOStrStream os;
		os << "file<" << sFile << ">_host<" << sHost << ">_port<" 
			<< iPort << ">" << ends;
		strncpy( szIdent, os.str(), IDENT_LEN );
		szIdent[IDENT_LEN]='\0';

⌨️ 快捷键说明

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