www.pudn.com > acdx.rar > HttpBuffer.h


#if !defined(HttpBuffer_H) 
#define HttpBuffer_H 
 
 
#include "HttpUtil.h" 
 
#include  
using namespace std; 
 
///////////////////////////////////////////////////////////////////////////////////// 
// HttpBufffer 
// 
// Purpose:	represents an buffer which stores an http formatted buffer 
 
 
//##ModelId=424BB6450249 
class HttpBuffer 
{ 
 
	//##ModelId=424BB645024B 
	string			_contents;			// stores entire http stream 
 
public: 
 
	//##ModelId=424BB6450259 
	HttpBuffer () : 
		_contents("") 
	{} 
 
	//##ModelId=424BB645025A 
	virtual ~HttpBuffer () 
	{ 
		release(); 
	} 
 
	// create/release 
	//##ModelId=424BB645025C 
	bool create () 
	{ 
		// reset contents 
		_contents = ""; 
 
		return true; 
	} 
 
 
	//##ModelId=424BB6450269 
	bool create  ( LPCTSTR buffer, long length ) 
	{ 
		if ( buffer && length > 0 ) 
			_contents = buffer; 
	} 
 
	//##ModelId=424BB645026C 
	void release () 
	{ 
		// clear contents 
		_contents.erase(); 
	} 
 
	// access methods 
	//##ModelId=424BB6450278 
	string getBody () 
	{ 
		// get pos of body in request 
		long pos = _contents.find(DOUBLE_CRLF); 
 
		// if valid return it 
		if ( pos != -1 ) 
		{ 
			// just get the body 
			pos += strlen(DOUBLE_CRLF); 
 
			return _contents.substr(pos); 
		} 
		else 
			return string(""); 
	} 
 
	// add methods 
	//##ModelId=424BB6450279 
	int addString( const string & str ) 
	{ 
		_contents += str; 
		return _contents.size(); 
	} 
 
 
	//##ModelId=424BB6450288 
	int addString( LPCTSTR id ) 
	{ 
		string str; 
		StringUtil::loadString( str, id ); 
 
		return addString( str ); 
	} 
 
	//##ModelId=424BB645028A 
	int addStatus( LPCTSTR strStatus ) 
	{ 
		string strVer = "HTTP/1.0 "; 
		addString( strVer ); 
		addString( strStatus ); 
		addString( CRLF ); 
 
		// stuff the server name 
		string strServer; 
		if ( StringUtil::loadString( strServer, IDS_SERVER_NAME ) && !strServer.empty() ) 
			addHeader( "Server", strServer ); 
 
		// stuff the date 
		string strDate; 
		return addHeader( "Date", HttpUtil::getHttpDate() ); 
	} 
 
	//##ModelId=424BB6450298 
	virtual 
	int addStatus( long status ) 
	{ 
		// get status string 
		string strStatus; 
		HttpUtil::getStatusString(status,strStatus); 
		 
		return addStatus(strStatus.c_str()); 
	} 
 
	//##ModelId=424BB645029B 
	virtual 
	int addError( long idError ) 
	{ 
		string strError; 
		HttpUtil::getStatusString( idError, strError ); 
 
		addStatus( idError ); 
 
		return addString( CRLF ); 
	} 
 
	//##ModelId=424BB64502A7 
	int addHeader( string strName, string strValue ) 
	{ 
		addString( strName ); 
		addString( ": " ); 
		addString( strValue ); 
		return addString( CRLF ); 
	} 
 
	//##ModelId=424BB64502AA 
	int addHeader( string strName, int nValue ) 
	{ 
		string strValue; 
		addString( strName ); 
		addString( ": " ); 
 
		StringUtil::formatString( strValue, "%d", nValue ); 
		addString( strValue ); 
		return addString( CRLF ); 
	} 
 
	//##ModelId=424BB64502B9 
	void addMethod ( long method, string & version ) 
	{ 
		// add command 
		addString( httpCommands[method] ); 
		addString( " / HTTP/" ); 
		addString( version ); 
		addString( CRLF ); 
		addString( CRLF ); 
	} 
 
	//##ModelId=424BB64502C7 
	void addMethod ( long method, string & version, string & file ) 
	{ 
		// add command 
		addString( httpCommands[method] ); 
		addString( " /" ); 
		if ( !file.empty() ) 
			addString( file ); 
		addString( " HTTP/" ); 
		addString( version ); 
		addString( CRLF ); 
		addString( CRLF ); 
	} 
 
 
	// operators 
	//##ModelId=424BB64502D7 
	void operator = ( const HttpBuffer & buffer ) 
	{ 
		_contents = buffer._contents; 
	} 
 
	//##ModelId=424BB64502D9 
	operator string & () 
	{ 
		return _contents; 
	} 
 
	//##ModelId=424BB64502E6 
	LPTSTR data () 
	{ 
		return (LPTSTR) _contents.c_str(); 
	} 
 
	//##ModelId=424BB64502E7 
	LPCTSTR c_str ()  
	{ 
		return _contents.c_str(); 
	} 
 
	//##ModelId=424BB64502E8 
	long size () 
	{ 
		return _contents.size(); 
	} 
	 
	//##ModelId=424BB64502E9 
	void getString ( string & set ) 
	{ 
		set = _contents; 
	} 
 
	//##ModelId=424BB64502F6 
	string & getContents () 
	{ 
		return _contents; 
	} 
}; 
 
 
 
#endif