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

📄 condget.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 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/CondGet.cpp,v $
 * $Date: 2004/07/04 19:19:32 $
 *
 Description:

\*____________________________________________________________________________*/
//$SourceTop:$

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

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

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define KEY_CONF_FLAG		"Flag"
#define VALUE_CONF_REFRESH	"RefreshOlderThanServer"

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

Description:
	Set the HTTP response status code to '304 Not Modified' if all the
	following criteria are met:-
<UL>
<LI>The HTTP request method is 'GET'.
<LI>The HTTP request contains an 'If-Modified-Since' client header.
<LI>The resource is older than the date specified by the 'If-Modified-Since'
header.
</UL>

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

<TR>
<TD>Flag
<TD>-
<TD>RefreshOlderThanServer
<TD>Force refresh on server start
<TD>Flag="RefreshOlderThanServer"

</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>
	Flag
</H5>
Specify a flag which effects the behaviour of this handler.<P>
<B>RefreshOlderThanServer</B> forces the handler not to return 304 
(not modified) status for a file modified before the server was last
restarted (this handler was loaded).

Phase:
	Any phase, usually CHECKPATH.

Returns:
	PIAPI_COMPLETED if the status was set to '304 Not Modified', otherwise
	PIAPI_CONTINUE.

Example:
	<PRE>
	&lt;Object&gt;
		Name ConditionalGet
		Class ConditionalGetClass
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		CheckType ConditionalGet
		...
	&lt;/Object&gt;

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

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class ConditionalGet : public HandlerBaseHTTP
{
private:
	/* ---
	Configuration data
	--- */
	time_t tLastRestart;
	int iLastRestart;

	/* ---
	Fast DB lookup keys
	--- */
	const char *pFKMethod;
	const char *pFKLastModified;
	const char *pFKRange;
	const char *pFKPath;

protected:
	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );
		PIOStrStream os;
		os << pWhere << "ConditionalGet: ";
		if ( !PIUtil_stricmp( KEY_CONF_FLAG, pVariable ) )
			{
			if ( !PIUtil_stricmp( VALUE_CONF_REFRESH, pValue ) )
				{
				time( &tLastRestart );
				iLastRestart = 1;
				}
			else
				{
				os << "Unknown flag '" << pValue <<
					"'" << ends;
				CONFIG_ERR( Object(), os.str() );
				return 0;
				};
			}
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	ConditionalGet( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	HandlerBaseHTTP( pObject ),
		tLastRestart( 0 ),
		iLastRestart( 0 ),
		pFKMethod( PIDB_getFastKey( KEY_HTTP_METHOD, PIDBTYPE_OPAQUE )),
		pFKLastModified( PIDB_getFastKey( KEY_HTTP_IFMODIFIEDSINCE, PIDBTYPE_RFC822 )),
		pFKRange( PIDB_getFastKey( KEY_HTTP_RANGE, PIDBTYPE_RFC822 )),
		pFKPath( PIDB_getFastKey( KEY_INT_PATH, PIDBTYPE_STRING ))
		{
		ReadParameters( iArgc, ppArgv );
		};

	int Handle( int /* iPhase */, PIHTTP &tPIHTTP, PIIOBuffer &/* tIOBuffer */ )
		{
		PIDB *pQ = tPIHTTP.pRequestDB;
		PIDB *pR = tPIHTTP.pResponseDB;

		/* ---
		check if-modified-since header
		--- */
		const char *pIMS = (const char *)PIDB_lookup( pQ, PIDBTYPE_RFC822,
			pFKLastModified, PIDBFLAG_FASTKEY );
		if ( !pIMS )
			{ return PIAPI_CONTINUE; };

		int iMethod = (int)PIDB_lookup( pQ, PIDBTYPE_OPAQUE, pFKMethod, PIDBFLAG_FASTKEY);
		if (iMethod != MD_GET && iMethod != MD_HEAD)
			{ return PIAPI_CONTINUE; };

		/* ---
		'Partial GET', If-Range header cannot be handled here
		--- */
		if ( PIDB_lookup( pQ, PIDBTYPE_RFC822, pFKRange, PIDBFLAG_FASTKEY ) )
			{ return PIAPI_CONTINUE; };

		int iIMSLength = -1;
#if 0
/*
** NOTE:
** I didn't see this use of length in any HTTP specification
** and it interferes with caching of directory indexes so I'm removing it
*/
		/* --- seperate date from length --- */
		int i=0;
		for( ; pIMS[i] && pIMS[i]!=';'; i++);
		if ( pIMS[i] )
			{
			i++;
			for( ; pIMS[i] && (isspace(pIMS[i])); i++);
			if ( !PIUtil_strncmpi( &(pIMS[i]), "length=", 7 ) )
				{
				iIMSLength = atoi( &(pIMS[i+7]) );
				};
			};
#endif

		/* ---
		Get file object
		--- */
		const char *pPath = (const char *)PIDB_lookup( pR, PIDBTYPE_STRING,
			pFKPath, PIDBFLAG_FASTKEY );
		PIFInfo *pFInfo = HTTPCore_getCachedFile( pPath ); 

		/* --- always serve up directories fresh --- */
		if ( PIFInfo_isDirectory( pFInfo ) )
			{
			HTTPCore_releaseCachedFile( pFInfo );	
			return PIAPI_CONTINUE;
			};

		/* --- compare file lengths --- */
		if ( iIMSLength>=0 )
			{
			int iFileLen = PIFInfo_getSize( pFInfo );
			if ( iIMSLength!=iFileLen )
				{
				/* --- file has been modified --- */
				HTTPCore_releaseCachedFile( pFInfo );	
				return PIAPI_CONTINUE;
				};
			};

		/* --- read file Last-Modified --- */
		time_t tTmp = PIFInfo_getLastModified( pFInfo );
		HTTPCore_releaseCachedFile( pFInfo );	

		/*
		** Adjust time to be no older than server start time
		** if appropriate.
		*/
		if ( iLastRestart && ( tTmp <= tLastRestart ) )
			{
			/* --- file is older than last server restart --- */
			return PIAPI_CONTINUE;
			};

		PIPlatform_beforeUnsafeBlock();
		struct tm *pLM = gmtime( &tTmp );
		if ( !pLM )
			{
			PIPlatform_afterUnsafeBlock();
			return PIAPI_ERROR;
			};
		struct tm tLM;
		memcpy( &tLM, pLM, sizeof( struct tm ) );
		PIPlatform_afterUnsafeBlock();

		/* --- read If-Modified-Since time --- */
		struct tm tIMS;
		if ( HTTPUtil_readTime( pIMS, &tIMS ) )
			{ return PIAPI_CONTINUE; };
#if 0
cerr << "sec: " << tIMS.tm_sec << endl;
cerr << "min: " << tIMS.tm_min << endl;
cerr << "hour: " << tIMS.tm_hour << endl;
cerr << "mday: " << tIMS.tm_mday << endl;
cerr << "mon: " << tIMS.tm_mon << endl;
cerr << "year: " << tIMS.tm_year << endl;
#endif

#if 0
		int tm_sec;     /* seconds after the minute - [0,59] */
		int tm_min;     /* minutes after the hour - [0,59] */
		int tm_hour;    /* hours since midnight - [0,23] */
		int tm_mday;    /* day of the month - [1,31] */
		int tm_mon;     /* months since January - [0,11] */
		int tm_year;    /* years since 1900 */
		int tm_wday;    /* days since Sunday - [0,6] */
		int tm_yday;  	/* NOT USED BY PI3 */
		int tm_isdst;	/* NOT USED BY PI3 */
#endif
		/* ---
		Compare modification dates
		--- */
		if (	tLM.tm_sec != tIMS.tm_sec ||
				tLM.tm_min != tIMS.tm_min ||
				tLM.tm_hour != tIMS.tm_hour ||
				tLM.tm_mday != tIMS.tm_mday ||
				tLM.tm_mon != tIMS.tm_mon ||
				tLM.tm_year != tIMS.tm_year
			)
			{
			/* --- files have been modified --- */
			return PIAPI_CONTINUE;
			};

		/* ---
		OK, no modification, issue an internal redirect with status 304
		--- */
		return HTTPUtil_doHTTPError( &tPIHTTP, ST_NOTMODIFIED );
		};
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int ConditionalGet_constructor( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	return HandlerBaseHTTP_constructor( pObj, PI_NEW( ConditionalGet( pObj,
		iArgc, ppArgv ) ) );
}

#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name ConditionalGetClass
		Type LogicExtension
		Library HTTP
		OnClassLoad HandlerBaseHTTP_onClassLoad
		Constructor ConditionalGet_constructor
		CopyConstructor HandlerBaseHTTP_copyConstructor
		Destructor HandlerBaseHTTP_destructor
		Execute HandlerBaseHTTP_execute
	</Class>

	<Object>
		Name ConditionalGet
		Class ConditionalGetClass
	</Object>

/*___+++CNF_END+++___*/
#endif

⌨️ 快捷键说明

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