This class is a small scale http web server capable of handling GET and POST requests.
When a request has been received an application defined callback is called with the request info, so that the user can provide the content in the response.
Various response status functions have been added to make life easier. Such as SendNotAuthorized() which will send the not authorized response to the client, thus making them log on using basic authentication.
A couple of my projects that use this class can be found here:-
Netgear SysLog
MusicService
CWebServer Methods:
::InitWinsock()
Calls WSAStartUp().
::CleanupWinsock()
Calls WSACleanup().
::Start()
Starts the web server.
::Stop()
Stops the web server.
::GetConnectionCount()
Returns the number of current client connections.
Class CWebHandler
This class is used to handle the accepted connection. The request is received and parsed here. And this class is used to send the response to the client.
You dont need to create one of these classes because they are created by CWebServer and passed to the program via the callback. From where you can access the methods/properties.
<hr>CWebHandler Methods
::SendResponse()
Send content to the client.
::SendNotModified()
::SendNotAuthorized()
::SendNotFound()
::SendForbidden()
::SendServerError()
::SendBadMethod()
These functions send miscellanious status responses to the client.
CWebHandler Properties
::m_req
Contains information about the http client request, such as common headers post data etc.
::m_sa
Is the sockadd_in of the client.
<hr>Sample Usage:
Code:
#include "WebServer.h"
#include <stdio.h>
#include <conio.h>
//
// Request Handler
//
BOOL RequestHandler(CWebHandler *pHandler) {
char *lpzContent = "<b>This is the content!</b>";
return pHandler->SendResponse(CONTENT_HTML,lpzContent,-1,0,0);
}
//
// Program Entry Point
//
void main( void ) {
CWebServer svr;
//Init winsock
if (!svr.InitWinsock()) return;
//start the server
if (!svr.Start(80,RequestHandler,50,0)) goto fin;
//press any key to stop the server
while (_kbhit()) Sleep(1);
getch();
//stop the server
svr.Stop();
fin:
svr.CleanupWinsock();
}
#include <stdio.h>
#include <conio.h>
//
// Request Handler
//
BOOL RequestHandler(CWebHandler *pHandler) {
char *lpzContent = "<b>This is the content!</b>";
return pHandler->SendResponse(CONTENT_HTML,lpzContent,-1,0,0);
}
//
// Program Entry Point
//
void main( void ) {
CWebServer svr;
//Init winsock
if (!svr.InitWinsock()) return;
//start the server
if (!svr.Start(80,RequestHandler,50,0)) goto fin;
//press any key to stop the server
while (_kbhit()) Sleep(1);
getch();
//stop the server
svr.Stop();
fin:
svr.CleanupWinsock();
}












