Click here to Skip to main content
15,896,457 members
Articles / Desktop Programming / MFC

STL WebServer

Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
8 May 2000 90.3K   2.5K   47  
A set of classes written in STL that implement a web server
#if !defined(HttpSocketHandler_H)
#define HttpSocketHandler_H


#include "HttpServerInfo.h"
#include "HttpRequestProcess.h"
#include "SocketHandler.h"
#include "TransferStatus.h"


////////////////////////////////////////////////////////////////////////////////////
// HttpSocketHandler
// 
// Purpose:		handler for http socket. socket handler creates the socket and
//				the handler notifies us of socket events


class HttpSocketHandler : 
	public SocketHandler
{
	TransferStatus		_status;			// controls transfer
	HttpServerInfo &	_server;			// ref to server info
	HttpRequestProcess	_processReq;		// used to process requests
	bool				_keepAlive;			// shows if connection left open after
											// request processed

public:

	HttpSocketHandler ( HttpServerInfo & server ) : 
		
		SocketHandler(),

		_status(),
		_server( server ),
		_processReq(),
		_keepAlive(false)

	{}

	virtual ~HttpSocketHandler ()
	{
		SocketHandler::release();
	}

	// socket event handlers
	bool onClose ( int error )
	{
		// close the socket
		Socket & sock = getSocket();
		sock.close();

		// remove handler
		delete this;

		return true;
	}

	bool onSend ( int error )
	{
		// get socket
		Socket & sock = getSocket();

		// get buffer to send
		LPTSTR buffer = _processReq.data();
		long   length = _processReq.size();

		// send response
		long noSent = sock.send( buffer, length );

		// update transfer status
		_status.incNoSent( noSent );

		// if done and dont keep open close socket
		// this will delete the handler 
		if ( _status.finished() && !_keepAlive )
		{
			sock.close();
		}

		return true;
	}

	bool onReceive ( int error )
	{
		// get socket
		Socket & sock = getSocket();

		// reset transfer status
		_status.reset();

		// get no of bytes
		long length = sock.getRecieveSize();
		if ( length == 0 )
		{
			return false;
		}


		// recieve data
		char buffer[1024];

		// terminate buffer
		buffer[length] = '\0';

		// recieve 
		length = sock.recieve( buffer, 1024 );
		if ( length == 0 )
		{
			return false;
		}

		printf("%s",buffer);

		// process request
		if ( _processReq.process( buffer, length ) )
		{
			// setup transfer info
			long noToSend = _processReq.size();
			_status.setSizeToSend( noToSend );

			//string body = _processReq.getBody();

		}

		return true;
	}



	// run method
	//long run ();
};


#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions