Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

STL WebServer

, 8 May 2000
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

Share

About the Author

David Hubbard

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 9 May 2000
Article Copyright 2000 by David Hubbard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid