GClasses

GClasses::GHttpServer Class Reference

This class allows you to implement a simple HTTP daemon. More...

#include <GHttp.h>

Inheritance diagram for GClasses::GHttpServer:
GClasses::GDynamicPageServer

List of all members.

Public Member Functions

 GHttpServer (int nPort)
virtual ~GHttpServer ()
bool process ()
 You should call this method constantly inside the main loop. It returns true if it did anything, and false if it didn't, so if it returns false you may want to sleep for a little while.
void setContentType (const char *szContentType)
 Specifies the content-type of the response.
void setCookie (const char *szPayload, bool bPersist)
 Specifies the set-cookie header to be sent with the response.
void setModifiedTime (time_t t)
 Sets the date (modified time) to be sent with the file so the client can cache it.
GTCPServersocket ()
 Returns a reference to the socket on which this server listens.

Static Public Member Functions

static void unescapeUrl (char *szOut, const char *szIn, size_t nInLen)
 Unescapes a URL. (i.e. replace "%20" with " ", etc.). szOut should point to a buffer at least as big as szIn (including the null terminator). This will stop when it hits the null-terminator in szIn or when nInLen characters have been parsed. So if szIn is null-terminated, you can safely pass in a huge arbitrary value for nInLen.
static bool parseFileParam (const char *pParams, size_t nParamsLen, const char **ppFilename, size_t *pFilenameLen, const unsigned char **ppFile, size_t *pFileLen)
 This is a rather hacky method that parses the parameters of a specific upload-file form.

Protected Member Functions

virtual void onProcessLine (GHttpConnection *pConn, const char *szLine)
void processPostData (GHttpConnection *pConn, const unsigned char *pData, size_t nDataSize)
void processHeaderLine (GHttpConnection *pConn, const char *szLine)
void beginRequest (GHttpConnection *pConn, int eType, const char *szIn)
void sendResponse (GHttpConnection *pConn)
void sendNotModifiedResponse (GHttpConnection *pConn)
void onReceiveFullPostRequest (GHttpConnection *pConn)
virtual void setHeaders (const char *szUrl, const char *szParams)=0
 This method should set the content type and the date headers, and any other headers deemed necessary.
virtual void doGet (const char *szUrl, const char *szParams, size_t nParamsLen, const char *szCookie, std::ostream &response)=0
 The primary purpose of this method is to push a response into pResponse. Typically this method will call SetHeaders.
virtual void doPost (const char *szUrl, unsigned char *pData, size_t nDataSize, const char *szCookie, std::ostream &response)=0
 This method takes ownership of pData. Don't forget to delete it. When the POST is caused by an HTML form, it's common for this method to just call DoGet (passing pData for szParams) and then delete pData. (For convenience, a '\0' is already appended at the end of pData.)
virtual bool hasBeenModifiedSince (const char *szUrl, const char *szDate)=0
 This is called when the client does a conditional GET. It should return true if you wish to re-send the file, and DoGet will be called.

Protected Attributes

char * m_pReceiveBuf
GHttpServerSocket * m_pSocket
std::vector< GHttpServerBuffer * > m_buffers
std::ostringstream m_stream
char m_szContentType [64]
char m_szCookie [MAX_COOKIE_SIZE]
bool m_bPersistCookie
time_t m_modifiedTime

Detailed Description

This class allows you to implement a simple HTTP daemon.


Constructor & Destructor Documentation

GClasses::GHttpServer::GHttpServer ( int  nPort)
virtual GClasses::GHttpServer::~GHttpServer ( ) [virtual]

Member Function Documentation

void GClasses::GHttpServer::beginRequest ( GHttpConnection *  pConn,
int  eType,
const char *  szIn 
) [protected]
virtual void GClasses::GHttpServer::doGet ( const char *  szUrl,
const char *  szParams,
size_t  nParamsLen,
const char *  szCookie,
std::ostream &  response 
) [protected, pure virtual]

The primary purpose of this method is to push a response into pResponse. Typically this method will call SetHeaders.

Implemented in GClasses::GDynamicPageServer.

virtual void GClasses::GHttpServer::doPost ( const char *  szUrl,
unsigned char *  pData,
size_t  nDataSize,
const char *  szCookie,
std::ostream &  response 
) [protected, pure virtual]

This method takes ownership of pData. Don't forget to delete it. When the POST is caused by an HTML form, it's common for this method to just call DoGet (passing pData for szParams) and then delete pData. (For convenience, a '\0' is already appended at the end of pData.)

Implemented in GClasses::GDynamicPageServer.

virtual bool GClasses::GHttpServer::hasBeenModifiedSince ( const char *  szUrl,
const char *  szDate 
) [protected, pure virtual]

This is called when the client does a conditional GET. It should return true if you wish to re-send the file, and DoGet will be called.

Implemented in GClasses::GDynamicPageServer.

virtual void GClasses::GHttpServer::onProcessLine ( GHttpConnection *  pConn,
const char *  szLine 
) [inline, protected, virtual]
void GClasses::GHttpServer::onReceiveFullPostRequest ( GHttpConnection *  pConn) [protected]
static bool GClasses::GHttpServer::parseFileParam ( const char *  pParams,
size_t  nParamsLen,
const char **  ppFilename,
size_t *  pFilenameLen,
const unsigned char **  ppFile,
size_t *  pFileLen 
) [static]

This is a rather hacky method that parses the parameters of a specific upload-file form.

bool GClasses::GHttpServer::process ( )

You should call this method constantly inside the main loop. It returns true if it did anything, and false if it didn't, so if it returns false you may want to sleep for a little while.

void GClasses::GHttpServer::processHeaderLine ( GHttpConnection *  pConn,
const char *  szLine 
) [protected]
void GClasses::GHttpServer::processPostData ( GHttpConnection *  pConn,
const unsigned char *  pData,
size_t  nDataSize 
) [protected]
void GClasses::GHttpServer::sendNotModifiedResponse ( GHttpConnection *  pConn) [protected]
void GClasses::GHttpServer::sendResponse ( GHttpConnection *  pConn) [protected]
void GClasses::GHttpServer::setContentType ( const char *  szContentType)

Specifies the content-type of the response.

void GClasses::GHttpServer::setCookie ( const char *  szPayload,
bool  bPersist 
)

Specifies the set-cookie header to be sent with the response.

virtual void GClasses::GHttpServer::setHeaders ( const char *  szUrl,
const char *  szParams 
) [protected, pure virtual]

This method should set the content type and the date headers, and any other headers deemed necessary.

Implemented in GClasses::GDynamicPageServer.

void GClasses::GHttpServer::setModifiedTime ( time_t  t) [inline]

Sets the date (modified time) to be sent with the file so the client can cache it.

GTCPServer* GClasses::GHttpServer::socket ( ) [inline]

Returns a reference to the socket on which this server listens.

static void GClasses::GHttpServer::unescapeUrl ( char *  szOut,
const char *  szIn,
size_t  nInLen 
) [static]

Unescapes a URL. (i.e. replace "%20" with " ", etc.). szOut should point to a buffer at least as big as szIn (including the null terminator). This will stop when it hits the null-terminator in szIn or when nInLen characters have been parsed. So if szIn is null-terminated, you can safely pass in a huge arbitrary value for nInLen.


Member Data Documentation

std::vector<GHttpServerBuffer*> GClasses::GHttpServer::m_buffers [protected]
GHttpServerSocket* GClasses::GHttpServer::m_pSocket [protected]
std::ostringstream GClasses::GHttpServer::m_stream [protected]
char GClasses::GHttpServer::m_szCookie[MAX_COOKIE_SIZE] [protected]