HTTP Server Two-time SDK Development
1. Overview
The HTTP protocol can effectively fuse the visible protocol with the binary protocol, and its structure is good, the two parts of the domain and the entity are identical, and the structure of the head domain is consistent and can be expanded arbitrarily. We can simulate the structure of MFC message based on the structure of head domain , and design an efficient and extensible parsing architecture.
This program is designed to simulate the message mechanism of MFC to implement the HTTP header domain parsing, and enable the implementation of the private protocol based on the structure of the resolution, the implementation of http The basic framework of the Protocol makes it possible to develop quickly, based on this framework, two times. the use of C + + Development Web site, the full implementation of interface and data separation, so that C + + programs can quickly develop web-based programs.
If you need to implement two development of the HTTP protocol based on this architecture , you first need to take a time zone to understand the basic structure of the HTTP protocol.
This series of articles will continue to be published, so stay tuned.
2. Operating Environment
The currently available libraries run under the Windows platform and are compiled using the vs2008 environment. Subsequent implementations of the Linux library will be implemented .
3. How to start the main program 3.1. A simple program:
#include ".. /httpserver/servermain.h "
int _tmain (int argc, _tchar* argv[])
{
Starthttpserver (8082,NULL, 1);
return 0;
}
The program launches an HTTP service on the 8082 port so that it can be accessed directly by placing index.html or other files on the appropriate working directory .
Enter in the browser:http://127.0.0.1:8082/ will appear in the index.html display interface.
or enter http://127.0.0.1:8082/ to specify the file name .
3.2. Implement your Ownhttp
If you need to implement different information that is returned according to the user's request parameters, you need to add your own logic processing.
Our logical processing is implemented in one object so that multiple requests can be saved on the same processing site. So we first need to implement the Create processing object.
#include ".. /httpserver/servermain.h "
#include "./client/mfhhttpclienttest.h"
int _tmain (int argc, _tchar* argv[])
{
Starthttpserver (8082,createmfhhttpclienttest, 1);
return 0;
}
This code adds a callback function, createmfhhttpclienttest, to create a client class that connects to the customer . Creating an object in the callback function is primarily to prepare for taking over the creation of other objects.
The code is as follows:
mfhhttpclient* createmfhhttpclienttest(SOCKET clientsocket,struct sockaddr_in *lpclientaddr)
{
mfhhttpclienttest* Client = new mfhhttpclienttest(clientsocket, Lpclientaddr);
return client;
}
mfhhttpclienttest The main work of the class is to create different processing objects based on the user's request path and extension, (through which you can implement your own head domain resolution, you can completely ignore http, please refer to the following article for details ). The main implementation functions are as follows:
/**
* creates an HTTP object that implements filtering in a derived class
* @path the requested path
* @ext the extension of the request path
* @version the requested version number
* @return successfully returns an object based on the chttpbase class derivation
*/
Virtual chttpbase* createhttpobj(char* path,char* ext,char * version);
Virtual void Freehttpobj (chttpbase*);
A simple creation function is as follows:
chttpbase* mfhhttpclienttest::createhttpobj(char* path,Char * ext,Char* version)
{
If (ext && strcmp(ext,"MFH") = = 0)
{
return (new myhttp());
}
return mfhhttpclient::createhttpobj(path,ext,version);
}
This will enter the HTTP://127.0.0.1:8082/WJH_2010.MFH on the Web page into the myhttp processing.
myhttp needs to be addressed in 3 virtual functions:
header domain parsing is done to call the function, in the function needs to be based on user-submitted parameters to do special processing
Virtual int ONrequest ();
tells the underlying how the header domain is returned to the client after processing is complete
Virtual int Onresponseheader (Char* resopnse,int maxlen);
finally send your own data to the client
Virtual int Read (Char* data,int maxlen);
For example:
Preparing data in onrequest based on client requests
in the Onresponseheader tell the client to prepare the data size, the encoding format is what information;
finally in Read populate data into data , which is returned to the client.
Based on the chttpbase class, of course, you can implement the private header domain resolution, in the onresponseheader to increase the private header field (see the following chapters in detail ).
the implementation of Myhttp is as follows:
/**
* Responding to clients
* @return successful return to HTTP_RESPIONE_OK
* failed to return other code
* @note
*/
int myhttp::onrequest()
{
add your own processing logic here.
return HTTP_RESPIONE_OK;
}
Const Char* g_mytest = ";
/**
* Back to client
* @return returns the length of content that needs to be written to the client side
*/
int myhttp::onresponseheader(char* resopnse,int maxlen)
{
Basic variables need to be set after processing is complete
M_contentlen = strlen(g_mytest);
int len = chttpbase::onresponseheader(resopnse,maxlen);
continue to increase the private header field
return Len;
}
/**
* reads the data returned by the client to the server side
*/
int myhttp::Read(char* data,int maxlen)
{
strncpy (data,g_mytest,maxlen);
return strlen (g_mytest);
}
This article introduces the basic use of the architecture, followed by the introduction of how the session is introduced.
The code is completely original and can be compiled on any C + + compiler. In good faith with you technical Daniel door discussion, if necessary, please send an email to [email protected].
HTTP Server two development SDK