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

📄 httputil.cpp

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

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Internal_readRFC1123Time(
		int iWeekday,
		const char *pLine,
		struct tm &tT )
{
	if ( iWeekday<0 )
		{ return -1; };

#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;	/* days since January 1 - [0,365] */
	int tm_isdst;   /* daylight savings time flag */
#endif

	/*
	 * RFC1123:
	 *	
	 * Sun, 06 Nov 1994 08:49:37 GMT	; RFC 822, updated by RFC 1123
	 *		|| | | |  | || || || | |
	 *		|| | | |  11111111112222
	 *		012345678901234567890123
	 *
	 */
	tT.tm_sec = atoi( &(pLine[18]) );
	tT.tm_min = atoi( &(pLine[15]) );
	tT.tm_hour = atoi( &(pLine[12]) );
	tT.tm_mday = atoi( &(pLine[0]) );
	tT.tm_mon = Internal_monthNumber( &(pLine[3]) );
	tT.tm_year = atoi( &(pLine[7]) )-1900;
	tT.tm_wday = atoi( &(pLine[iWeekday]) );
/*
**	tT.tm_yday = atoi( &(pLine[]) );
**	tT.tm_isdst = atoi( &(pLine[]) );
*/
	if ( strncmp( &(pLine[21]), "GMT", 3 ) )
		{ return -1; }; 

	return 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Internal_readRFC850Time(
		int iWeekday,
		const char *pLine,
		struct tm &tT )
{
	if ( iWeekday<0 )
		{ return -1; };

#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;	/* days since January 1 - [0,365] */
	int tm_isdst;   /* daylight savings time flag */
#endif

	/*
	 * RFC850:
	 *	
	 * Sunday, 06-Nov-94 08:49:37 GMT   ; RFC 850, obsoleted by RFC 1036
	 *		   || | | || || || || | |
	 *		   || | | || 111111111122
	 *		   0123456789012345678901
	 *
	 */
	tT.tm_sec = atoi( &(pLine[16]) );
	tT.tm_min = atoi( &(pLine[13]) );
	tT.tm_hour = atoi( &(pLine[10]) );
	tT.tm_mday = atoi( &(pLine[0]) );
	tT.tm_mon = Internal_monthNumber( &(pLine[3]) );
	tT.tm_year = atoi( &(pLine[7]) );
	tT.tm_wday = atoi( &(pLine[iWeekday]) );
/*
**	tT.tm_yday = atoi( &(pLine[]) );
**	tT.tm_isdst = atoi( &(pLine[]) );
*/
	if ( strncmp( &(pLine[19]), "GMT", 3 ) )
		{ return -1; }; 

	return 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Internal_readANSICTime(
		int iWeekday,
		const char *pLine,
		struct tm &tT )
{
	if ( iWeekday<0 )
		{ return -1; };

#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;	/* days since January 1 - [0,365] */
	int tm_isdst;   /* daylight savings time flag */
#endif

	/*
	 * ANSI C's asctime() format:
	 *	
	 * Sun Nov  6 08:49:37 1994		 ; ANSI C's asctime() format
	 *	   | | |  || || || |  |
	 *	   | | |  || 1111111111
	 *	 01234567890123456789
	 *
	 */
	tT.tm_sec = atoi( &(pLine[13]) );
	tT.tm_min = atoi( &(pLine[10]) );
	tT.tm_hour = atoi( &(pLine[7]) );
	tT.tm_mday = atoi( &(pLine[4]) );
	tT.tm_mon = Internal_monthNumber( &(pLine[0]) );
	tT.tm_year = atoi( &(pLine[16]) )-1900;
	tT.tm_wday = atoi( &(pLine[iWeekday]) );
/*
**	tT.tm_yday = atoi( &(pLine[]) );
**	tT.tm_isdst = atoi( &(pLine[]) );
*/

	return 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Return zero on success.
	This is not very tolerant right now.
	Tolerant of garbage or other date after the date in the same string.
\*____________________________________________________________________________*/
int HTTPUtil_readTime( const char *pLine, struct tm *pT )
{
	assert( pLine );
	assert( pT );

	if ( !pLine || !pT )
		{ return -1; };

	struct tm &tT = *pT;

	/* --- read line to first ' ' or ',' --- */
	int i=0;
	for( ; pLine[i] && pLine[i]!=' ' && pLine[i]!=','; i++);
	switch( pLine[i] )
		{
		case ' ':
			/* --- asctime() --- */
			if ( i!=3 ) { return -1; };
			return Internal_readANSICTime( 
				Internal_weekdayNumber( pLine ), &(pLine[4]), tT );

		case ',':
			/* --- asctime() --- */
			if ( i==3 )
				{ return Internal_readRFC1123Time( 
					Internal_weekdayNumber( pLine ), &(pLine[5]), tT ); }
			else
				{ return Internal_readRFC850Time( 
					Internal_weekdayNumber( pLine ), &(pLine[i+2]), tT ); };

		default:;
			return -1;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns, 0 match failed, 1 match succeeded.
\*____________________________________________________________________________*/
int Internal_regexMatch(
	const char *pRegex,
	int iRegexLen,
	const char *pString,
	int iStringLen )
{
	int i=0;
	int j=0;
	for( ; i<iRegexLen; i++, j++ )
		{
		if ( pRegex[i]=='*' )
			{
			i++;
			for(; i<iRegexLen && j<iStringLen; j++)
				{
				if ( Internal_regexMatch(
					&(pRegex[i]),
					iRegexLen-i,
					&(pString[j]),
					iStringLen-j ) )
					{ return 1; };
				};
			if ( i==iRegexLen )
				{ return 1; }
			else
				{ return 0; };
			};

		if ( i==iStringLen || pString[i]!=pRegex[i] )
			{ return 0; };
		};
	return iRegexLen==iStringLen ? 1 : 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns non-zero on success.
\*____________________________________________________________________________*/
int HTTPUtil_regexMatchPi3String( const Pi3String *pRegexPattern,
	const Pi3String *pString )
{
	assert( pRegexPattern );
	assert( pString );
	int iMatch = Internal_regexMatch( *pRegexPattern, pRegexPattern->Len(),
		*pString, pString->Len() );
	return iMatch;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns non-zero on success.
\*____________________________________________________________________________*/
int HTTPUtil_regexMatch( const char *pRegex, int iRegexLen,
	const char *pString, int iStringLen )
{
	assert( pRegex && pString );
	if ( !pRegex || !iRegexLen || !pString || !iStringLen )
		{
		if ( !iRegexLen && !iStringLen )
			{ return 1; };
		return 0;
		};
	return Internal_regexMatch( pRegex, iRegexLen, pString, iStringLen );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void HTTPUtil_urlEncode( const Pi3String *pToEncode, Pi3String *pResult )
{
	assert( pToEncode );
	assert( pResult );
	if ( !pToEncode || !pResult )
		{ return; };
	const PIString &sToEncode = *pToEncode;
	PIString &sResult = *pResult;

	PIOStrStream os;
	int iLen = sToEncode.Len();
	const char *pS = sToEncode;
	PIString sAscii;
	for( int i=0; i<iLen; i++)
		{
		char ch=pS[i];
		if ( ch==' ' )
			{ os << '+'; }
		else if ( !(isalnum(ch)) && (ch!='_') )
			{
			if ( pi_ltoa( ch, 16, sAscii ) )
				{ os << '%' << sAscii; }
			}
		else
			{ os << ch; };
		};

	os << ends;
	sResult = os.str();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void HTTPUtil_urlDecode( const Pi3String *pToDecode, Pi3String *pResult )
{
	assert( pToDecode );
	assert( pResult );
	if ( !pToDecode || !pResult )
		{ return; };
	const PIString &sToDecode = *pToDecode;
	PIString &sResult = *pResult;

	int iLen = sToDecode.Len();
	char *pTmp = new char[iLen+1];
	strcpy( pTmp, sToDecode );	
	int i=0, j=0;
	for(; i<iLen; i++, j++)
		{
		if ( pTmp[i]=='+' )
			{ pTmp[j] = ' '; }
		else if ( pTmp[i]=='%' )
			{
			if ( ((i+1)<iLen) && pTmp[i+1]=='%' )
				{ pTmp[j]=pTmp[i]; i++; }
			else if ( (i+2)<iLen )
				{
				char szBuf[3];
				szBuf[2]='\0';
				strncpy( szBuf, &(pTmp[i+1]), 2 );
				pTmp[j] = (char)strtol( szBuf, (char **)0, 16 ); 
				i+=2;
				};
			}
		else
			{ pTmp[j]=pTmp[i]; };
		};
	pTmp[j] = '\0';
	sResult = pTmp;
	delete [] pTmp;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int HTTPUtil_doHTTPError( PIHTTP *pPIHTTP, int iErrorCode )
{
	assert( pPIHTTP );
	assert( iErrorCode );
	
	/* --- set the error status code --- */
	pPIHTTP->iStatus = iErrorCode;

	PIDB *pR = pPIHTTP->GetResponseDB();
	PIDB *pQ = pPIHTTP->GetRequestDB();

	/* ---
	clear any content-type, content-length, objectmap,
	authentication realm, authentication type.
	--- */
	if ( PIDB_lookup( pR, PIDBTYPE_RFC822, KEY_HTTP_CONTENTTYPE, 0 ) )
		PIDB_replace( pR, PIDBTYPE_RFC822, KEY_HTTP_CONTENTTYPE, 0, 0 );
	if ( PIDB_lookup( pR, PIDBTYPE_RFC822, KEY_HTTP_CONTENTLENGTH, 0 ) )
		PIDB_replace( pR, PIDBTYPE_RFC822, KEY_HTTP_CONTENTLENGTH, 0, 0 );
	if ( PIDB_lookup( pR, PIDBTYPE_STRING, KEY_INT_OBJECTMAP, 0 ) )
		PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_OBJECTMAP, 0, 0 );
	if ( PIDB_lookup( pR, PIDBTYPE_STRING, KEY_INT_AUTHENTICATIONREALM, 0 ) )
		PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_AUTHENTICATIONREALM, 0, 0 );
	if ( PIDB_lookup( pR, PIDBTYPE_STRING, KEY_INT_AUTHTYPE, 0 ) )
		PIDB_replace( pR, PIDBTYPE_STRING, KEY_INT_AUTHTYPE, 0, 0 );

	/* --- set the method to 'GET' --- */
	PIDB_replace( pQ, PIDBTYPE_OPAQUE, KEY_HTTP_METHOD, (void *)MD_GET, 0 );
	PIDB_replace( pQ, PIDBTYPE_STRING, KEY_HTTP_METHOD, (void *)MD_NAME_GET, 0);

	/* --- disable keep alive --- */
	PIHTTP_disableKeepOpen( *pPIHTTP );

	/* --- return INT_REDIRECT, to cause a redirect --- */
	return INT_REDIRECT;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int HTTPUtil_sendFile( PIIOBuffer *pBuffer, PIFInfo *pFInfo,
	int iBufferingFlags, int iSendFlags )
{
	(void)iSendFlags;		/* --- not currently used --- */
	assert( pBuffer );
	assert( pFInfo );

	if ( !pBuffer || !pFInfo )
		{
        return PIAPI_ERROR;
        };

	/* --- get path --- */
	const char *pPath = PIFInfo_getPath( pFInfo );

	/* --- get the file map --- */
	const char *pMap = 0;
	int iLen = 0;

	/* --- get filemap object --- */
	if ( PIFInfo_getSize( pFInfo )>0 )
		{
		PIFMap *pFileMap = PIFMap_new( pFInfo );
		if ( pFileMap )
			{
			pMap = (const char *)PIFMap_lock( pFileMap, &iLen );
			};

⌨️ 快捷键说明

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