📄 httpcore.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/Pi3API/HTTPCore.cpp,v $
* $Date: 2003/06/01 12:10:14 $
*
Description:
HTTP core facilities.
TODO:
i) The status and headers request line parsing doesn't allow for
line continuation with tabs.
ii) Some of error response codes should be changed. There a little
overuse of HTTP BAD REQUEST. Some HTTP/1.1 gateway error
response codes should be used instead.
\*____________________________________________________________________________*/
//$SourceTop:$
#define TEMP 1
#if !defined(NDEBUG)
# include <iostream.h>
#endif
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <fstream.h>
#include <stdio.h>
#include <stdarg.h>
#include "PIString.h"
#include "HTTPDefs.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#include "PICompat.h"
#include "MiscUtil.h"
#include "DeQuote.h"
#include "PIStrStr.h"
#include "StrToken.h"
#include "Pi3Expr.h"
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#define KEY_CONF_HANDLERS "Handlers"
#define KEY_CONF_SERVERROOT "ServerRoot"
#define KEY_CONF_SERVERSTAMP "ServerStamp"
#define KEY_CONF_CLIENTSTAMP "ClientStamp"
#define KEY_CONF_ERRORLOGFILE "ErrorLogFile"
#define KEY_CONF_DEBUGLOGFILE "DebugLogFile"
#define KEY_CONF_DEBUGBEFOREHANDLER "DebugBeforeHandler"
#define KEY_CONF_DEBUGAFTERHANDLER "DebugAfterHandler"
#define KEY_CONF_DEFAULTMIMETYPE "DefaultMIMEType"
/*
#define D { cerr << __FILE__ << ", " << __LINE__ << ": " << endl; }
*/
#define D
enum { LOCAL_BUFFER_LEN=4095 }; /* --- used for read RFC822, URI etc --- */
/* --- maximum number of internal redirects --- */
#define MAX_REDIRECTS 10
#define CONFIG_ERR(this, msg) \
{ PILog_addMessage( PIObject_getDB(this), \
PIObject_getConfigurationDB(this), PILOG_ERROR, (msg) ); }
/*____________________________________________________________________________*\
*
Description:
Global fast keys.
\*____________________________________________________________________________*/
static bool bFastKeysInitialized = false;
static const char *pFKCLF = 0;
static const char *pFKMethod = 0;
static const char *pFKURI = 0;
static const char *pFKPath = 0;
static const char *pFKQueryString = 0;
static const char *pFKProtocol = 0;
static const char *pFKMethodNumber = 0;
static const char *pFKProtocolNumber = 0;
static const char *pFKProxyProtocol = 0;
static const char *pFKProxyHost = 0;
static const char *pFKProxyPort = 0;
static const char *pFKKeepOpenO = 0;
static const char *pFKKeepOpenH = 0;
static const char *pFKBytesSent = 0;
static const char *pFKHostRoot = 0;
static const char *pFKStatusCode = 0;
static const char *pFKTransferEnc = 0;
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Internal_initFastKeys()
{
if ( bFastKeysInitialized )
{
return;
};
pFKCLF = PIDB_getFastKey( KEY_HTTP_CLF, PIDBTYPE_STRING );
pFKMethod = PIDB_getFastKey( KEY_HTTP_METHOD, PIDBTYPE_STRING );
pFKURI = PIDB_getFastKey( KEY_HTTP_URI, PIDBTYPE_STRING );
pFKPath = PIDB_getFastKey( KEY_INT_PATH, PIDBTYPE_STRING );
pFKQueryString = PIDB_getFastKey( KEY_HTTP_QUERYSTRING, PIDBTYPE_STRING );
pFKProtocol = PIDB_getFastKey( KEY_HTTP_PROTOCOL, PIDBTYPE_STRING );
pFKMethodNumber = PIDB_getFastKey( KEY_HTTP_METHOD, PIDBTYPE_OPAQUE );
pFKProtocolNumber = PIDB_getFastKey( KEY_HTTP_PROTOCOL, PIDBTYPE_OPAQUE );
pFKProxyProtocol = PIDB_getFastKey( KEY_HTTP_PROXYPROTOCOL,
PIDBTYPE_STRING );
pFKProxyHost = PIDB_getFastKey( KEY_HTTP_PROXYHOST, PIDBTYPE_STRING );
pFKProxyPort = PIDB_getFastKey( KEY_HTTP_PROXYPORT, PIDBTYPE_STRING );
pFKKeepOpenO = PIDB_getFastKey( KEY_INT_KEEPOPEN, PIDBTYPE_OPAQUE );
pFKKeepOpenH = PIDB_getFastKey( KEY_INT_KEEPOPEN, PIDBTYPE_RFC822 );
pFKBytesSent = PIDB_getFastKey( KEY_INT_BYTESSENT, PIDBTYPE_OPAQUE );
pFKHostRoot = PIDB_getFastKey( KEY_INT_HOSTROOT, PIDBTYPE_STRING );
pFKStatusCode = PIDB_getFastKey( KEY_HTTP_STATUSCODE, PIDBTYPE_OPAQUE );
pFKTransferEnc = PIDB_getFastKey( KEY_HTTP_TRANSFERENCODING, PIDBTYPE_RFC822 );
bFastKeysInitialized = true;
assert( pFKMethod && pFKURI && pFKPath && pFKQueryString &&
pFKProtocol &&
pFKMethodNumber && pFKProtocolNumber &&
pFKProxyProtocol && pFKProxyHost && pFKProxyPort &&
pFKKeepOpenO && pFKKeepOpenH && pFKBytesSent &&
pFKHostRoot && pFKStatusCode
);
}
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class ServerGlobals
{
private:
/* --- forbid copy constructor --- */
ServerGlobals( const ServerGlobals & )
{ assert( 0 ); };
public:
PIString sServerRoot;
PIString sServerStamp;
PIDB *pMIMEDB;
PIString sDefaultMIMEType;
#if TEMP
PIPLATFORM_FD tErrorLog;
#else
FILE *pErrorLog;
#endif
FILE *pDebugLog;
Pi3Expression *pBeforeHandler;
Pi3Expression *pAfterHandler;
DblList lHandlers;
ServerGlobals()
: pMIMEDB( 0 ),
#if TEMP
tErrorLog( PIPLATFORM_FD_INVALID ),
#else
pErrorLog( 0 ),
#endif
pDebugLog( 0 ),
pBeforeHandler( 0 ),
pAfterHandler( 0 )
{};
~ServerGlobals()
{
for( DblListIterator i( lHandlers ); !i.BadIndex(); i++ )
{ PIObject_delete( (PIObject *)i.Current(), 0, 0 ); };
#if TEMP
if ( tErrorLog!=PIPLATFORM_FD_INVALID )
{
PIFile_close( tErrorLog );
tErrorLog = PIPLATFORM_FD_INVALID;
};
#else
if ( pErrorLog )
{
::fclose( pErrorLog );
pErrorLog = 0;
};
#endif
if ( pDebugLog )
{
::fclose( pDebugLog );
pDebugLog = 0;
};
Pi3Expression_delete( pBeforeHandler );
Pi3Expression_delete( pAfterHandler );
PIDB_delete( pMIMEDB );
};
};
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class ClientGlobals
{
private:
/* --- forbid copy constructor --- */
ClientGlobals( const ClientGlobals & )
{ assert( 0 ); };
public:
PIString sClientStamp;
ClientGlobals()
{};
~ClientGlobals()
{
};
};
/*____________________________________________________________________________*\
*
Description:
Globals
\*____________________________________________________________________________*/
static ServerGlobals *pSG = 0;
static ClientGlobals *pCG = 0;
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
0 if not loaded, -1 on error, 1 if loaded.
\*____________________________________________________________________________*/
int Internal_LoadExpression( Pi3Expression **ppExpression, const char *pName,
PIObject *pObject )
{
const char *pValue = (const char *)PIConfig_lookupValue(
PIObject_getConfigurationDB( pObject ), pName, 0, 0 );
if ( !pValue )
{ return 0; };
DeQuote tValue( pValue );
assert( ppExpression );
PIString sError;
Pi3Expression *pExpression = 0;
pExpression = Pi3Expression_new( tValue, 0, &sError );
if ( !pExpression )
{
PILog_addMessage(
PIObject_getDB( pObject ),
PIObject_getConfigurationDB( pObject ),
PILOG_ERROR,
"HTTPCore_init: Error parsing expression: %s",
(const char *)sError );
return -1;
};
*ppExpression = pExpression;
return 1;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int Internal_addMIMEEntry( PIDB *pMIMEDB, const char *pLine, PIString &sMsg )
{
assert( pLine );
int i;
/*
** Scan past initial space
*/
i = 0;
for( ; pLine[i] && isspace( pLine[i] ); i++ );
if ( !pLine[i] )
{
sMsg = "HTTP1_0: Syntax error in MIME entry.";
return 0;
};
pLine = &( pLine[i] );
/*
** Scan to first whitespace
*/
for( i=0; pLine[i] && !isspace( pLine[i] ); i++ );
PIString sExtn( pLine, i );
#if WIN32
/*
** Silently convert extension to upper case
*/
sExtn.ConvertToUpperCase();
#endif
const char *pExtn = sExtn;
/*
** Skip over all space
*/
for( ; pLine[i] && isspace( pLine[i] ); i++ );
if ( !pLine[i] )
{
sMsg = "HTTP1_0: Syntax error in MIME entry.";
return 0;
};
pLine = &( pLine[i] );
for( i=0; pLine[i] && !isspace( pLine[i] ); i++ );
/*
** New extension is in sExtn, MIME type starts at pLine and has
** length i
*/
PIString sMIMEType( pLine, i );
const char *pMIMEType = sMIMEType;
const char *pPreviousMIMEType = (const char *)PIDB_lookup(
pMIMEDB, PIDBTYPE_STRING, pExtn, 0 );
if ( pPreviousMIMEType )
{
sMsg = "HTTP1_0: Extension '";
sMsg.Concatenate( pExtn );
sMsg.Concatenate( "' is already mapped to MIME type '" );
sMsg.Concatenate( pPreviousMIMEType );
sMsg.Concatenate( "'." );
return 0;
};
if (PIDB_add( pMIMEDB, PIDBTYPE_STRING, pExtn,
(void *)pMIMEType, 0 ))
{
sMsg = "HTTP1_0: internal error adding MIME type.";
return 0;
};
return 1;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int Internal_loadAllMIMETypes( PIDB *pMIMEDB, PIObject *pObject, PIDB *pConf )
{
PIDBIterator *pIter = PIDB_getIterator( pConf, PIDBTYPE_USER,
KEY_CONF_ADDMIMEENTRY, 0 );
if ( !pIter ) { return 0; };
for( ; PIDBIterator_atValidElement( pIter );
PIDBIterator_next( pIter ) )
{
const char *pVariable = 0;
PIConfigString *pString = (PIConfigString *)PIUserType_getValue(
(PIUserType *)PIDBIterator_current( pIter, &pVariable ) );
const char *pValue = PIConfigString_getString( pString );
/* --- generate location string --- */
PIOStrStream os;
#if 0
/* NOTE: This error information is really terse */
long lLine = pString->GetY();
/* --- if line position is 0, this is the previous line --- */
if ( !pString->GetX() && lLine>0 )
{ lLine--; };
os << "[" << lLine << "] ";
#endif
/* --- no variable --- */
if ( !pVariable || !*pVariable )
{
os << "HTTP1_0: Internal error adding MIME type '" <<
pVariable << "'" << ends;
CONFIG_ERR( pObject, os.str() );
PIDBIterator_delete( pIter );
return 0;
};
/* --- no value --- */
if ( !pValue || !*pValue )
{
os << "HTTP1_0: No value for variable '" <<
pVariable << "'" << ends;
CONFIG_ERR( pObject, os.str() );
PIDBIterator_delete( pIter );
return 0;
};
/* --- ignore 'Name' or 'Class' parameters --- */
if ( !PIUtil_stricmp( PIDBKEY_CONF_NAME, pVariable ) ||
!PIUtil_stricmp( PIDBKEY_CONF_CLASS, pVariable ) )
{ continue; };
DeQuote tUnQuoted( pValue );
PIString sMsg;
if ( !Internal_addMIMEEntry( pMIMEDB, tUnQuoted.GetString(), sMsg ) )
{
os << sMsg << ends;
CONFIG_ERR( pObject, os.str() );
PIDBIterator_delete( pIter );
return 0;
};
};
PIDBIterator_delete( pIter );
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -