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

📄 pihttp.h

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 H
字号:
/*____________________________________________________________________________*\
 *

 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/PIHTTP.h,v $
 * $Date: 2003/05/28 06:14:59 $
 *
 Description:

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#ifndef PIHTTP_H_
#define PIHTTP_H_

#include "Pi2API.h"
#include "PIIOBuf.h"
#include "HTTPDefs.h"

	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
						Internal C++ Interface

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
#if defined(__cplusplus) && defined(PI3_INTERNAL)

#include <assert.h>
#include <stddef.h>

#include "DblList.h"		/* --- from Base library --- */

#define HTTP_DB				"HTTP DB"
#define HTTP_REQUEST_DB		"HTTP Request DB"
#define HTTP_RESPONSE_DB	"HTTP Response DB"

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
class PIHTTP
{
public:
	/* --- public constant values --- */
	const int ciPhase;

	/* --- public mutable values --- */	
	int iStatus;
	
	PIDB *pConnectionDB;
	PIDB *pHostDB;
	PIDB *pRequestDB;
	PIDB *pResponseDB;
	PIIOBuffer *pBuffer;
    void *pUser1;           /* --- user data --- */
    void *pUser2;           /* --- pointers  --- */

public:
	/* --- private members, don't access, even though not marked private --- */
	PIHTTP *pParent;		/* --- NULL if this is not a subrequest --- */
	PIDB *pDB;
	int iLastResultCode;
	PITimer *pTimer;
	const char *pHandlerName;
	int iDelta;				/* --- profile delta of time in handler --- */
	int iDepth;				/* --- depth of current request if nested --- */
	DblList	lMemoryRecords;	/* --- memory records --- */
		
private:
	/* --- copy constructor will be disallowed --- */
	PIHTTP( const PIHTTP &tH );

	/* --- internal methods --- */
	inline void ClearRequest( int ClearMemory );

public:
	PIHTTP( PIHTTP *pTheParentRequest );
	PIHTTP( PIObject *pTheIOObject, PIDB *pTheHostDB );
	PIHTTP( PIDB *pHost );
	~PIHTTP();

	/* --- new request --- */
	void NewRequest();

	/* ---
	High level data structures that make up a request/response
	cycle
	--- */
	inline PIDB *GetConnectionDB()			/* connection DB */
		{ return pConnectionDB; };	
	inline PIDB *GetHostDB()				/* virtual host DB */
		{ return pHostDB; };	
	inline PIDB *GetDB()					/* generic DB */
		{ return pDB; };
	inline PIDB *GetRequestDB()				/* request specific information */
		{ return pRequestDB; };
	inline PIDB *GetResponseDB()			/* response specific information */
		{ return pResponseDB; };
	inline PIIOBuffer &GetIOBuffer()		/* IO buffer object */
		{ return *pBuffer; };
	inline int IsSubRequest()				/* is this a subrequest? */
		{ return pParent!=0; };

	/* --- allow the virtual host to be changed --- */
	inline void SetHostDB( PIDB *pNewHostDB )
		{ pHostDB = pNewHostDB; };

	/* --- non-inline functions --- */
	void *AllocMem( size_t tSize );			/* allocate memory */

};

/* --- inlines --- */
inline void PIHTTP_disableKeepOpen( PIHTTP &tPIHTTP ) 
	{ 
	PIDB_replace( tPIHTTP.GetConnectionDB(), PIDBTYPE_OPAQUE, 
		KEY_INT_KEEPOPEN, (void *)0, 0 );
	};

	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
						External C Interface

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
#else
/*____________________________________________________________________________*\
 *
 Description:
	C structure version of C++ class.
\*____________________________________________________________________________*/
struct _PIHTTP
{
	/* --- public constant values --- */
	int ciPhase;

	/* --- public mutable values --- */	
	int iStatus;
	
	PIDB *pConnectionDB;
	PIDB *pHostDB;
	PIDB *pRequestDB;
	PIDB *pResponseDB;
	PIIOBuffer *pBuffer;
	void *pUser1;           /* --- user data --- */
	void *pUser2;           /* --- pointers  --- */

	/* --- other members are private --- */

};
typedef struct _PIHTTP PIHTTP;

#endif
	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
						C Functions

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
/*____________________________________________________________________________*\
 *
 Name:
	PIHTTP_new

 Synopsis:
	PIHTTP *PIHTTP_new( PIObject *pIOObject, PIDB *pHostDB )

 Description:
	This function creates a new HTTP request object, which is internally used by the
	request dispatcher. The HTTP request object is forwarded to the logic handlers
	during process the request.

 Notes:
	The created PIHTTP * is accessible as the following C structure:

	<PRE>
struct _PIHTTP
{
	// --- public constant values --- //
	int ciPhase;

	// --- public mutable values --- //
	int iStatus;
	
	PIDB *pConnectionDB;
	PIDB *pHostDB;
	PIDB *pRequestDB;
	PIDB *pResponseDB;
	PIIOBuffer *pBuffer;
	void *pUser1;           // --- user data --- //
	void *pUser2;           // --- pointers  --- //
};
typedef struct _PIHTTP PIHTTP;
	</PRE>

 Return Values:
	A new pointer to a PIHTTP structure or NULL on error.

 Errors:
 See Also:
	PIHTTP_delete
	PIHTTP_newChild
	HTTPCore_dispatch
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIHTTP *PIHTTP_new( PIObject *pIOObject, PIDB *pHostDB );

/*____________________________________________________________________________*\
 *
 Name:
	PIHTTP_newChild

 Synopsis:
	PIHTTP *PIHTTP_newChild( PIHTTP *pParent )

 Description:
	This function will create a child of the pParent HTTP object, which will
	inherit/share the following properties:

	<UL>
		<LI>connection database
		<LI>host database
		<LI>request database
		<LI>IO-buffer
		<LI>object internal database (configuration values)
	</UL>

	The child object has its own response database.

 Notes:
	This function is often used to create a child object in order to process
	a request in a speculative way. This can be used to calculate certain values,
	which are required for further processing in the parent or to forward a request.
	The following example does compute the physical mapping for an arbitrary URI
	in pPath:

	<XMP>
	// --- make sub request context --- //
	PIHTTP *pChildHTTP = PIHTTP_newChild( pPIHTTP );

	PIDB_replace( pChildHTTP->pResponseDB, PIDBTYPE_STRING,
		KEY_INT_PATH, (void *)pPath, 0 );

	// --- dispatch the sub request across the mapping phase --- //
	int iRet = HTTPCore_dispatch( pChildHTTP, PH_MAPPING, PH_MAPPING );

	// ---  retrieve the childs mapped path  --- //
	const char *pMappedPath = (const char *)PIDB_lookup( pChildHTTP->pResponseDB,
		PIDBTYPE_STRING, KEY_INT_PATH, 0 );
	</XMP>

 Return Values:
	A new pointer to a PIHTTP structure or NULL on error.

 Errors:
 See Also:
	PIHTTP_new
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIHTTP *PIHTTP_newChild( PIHTTP *pParent );

/*____________________________________________________________________________*\
 *
 Name:
	PIHTTP_delete

 Synopsis:
	int PIHTTP_delete( PIHTTP *pPIHTTP )

 Description:
	This function deletes the given HTTP object.

 Notes:
	The function will free all memory blocks allocated with PIHTTP_allocMem.

 Return Values:
	PIAPI_COMPLETED on success, otherwise PIAPI_ERROR.

 Errors:
	PIAPI_ERROR

 See Also:
	PIHTTP_new
	PIHTTP_allocMem
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIHTTP_delete( PIHTTP *pPIHTTP );

/*____________________________________________________________________________*\
 *
 Name:
	PIHTTP_allocMem

 Synopsis:
	void *PIHTTP_allocMem( PIHTTP *pPIHTTP, unsigned int iSize )

 Description:
	This function does allocate user memory in the scope of the	HTTP object
	referred by *pPIHTTP. The memory will be freed, when the HTTP object is
	deleted.

 Notes:
 Return Values:
	Pointer to the new memory block with iSize on success, otherwise
	NULL on error.

 Errors:
 See Also:
	PIHTTP_delete
\*____________________________________________________________________________*/
PUBLIC_PIAPI void *PIHTTP_allocMem( PIHTTP *pPIHTTP, unsigned int iSize );

/*____________________________________________________________________________*\
 *
 Name:
	PIHTTP_isChild

 Synopsis:
	int PIHTTP_isChild( PIHTTP *pPIHTTP )

 Description:
	Checks if a HTTP request is a child request.

 Notes:
 Return Values:
	PIAPI_TRUE if the request is a child, otherwise PIAPI_FALSE.

 Errors:
 See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIHTTP_isChild( PIHTTP *pPIHTTP );


#endif /* PIHTTP_H_ */

⌨️ 快捷键说明

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