html
- File
- microserver.tar.gz
- Required
- Download the last version code (0.9.48), compile and install
- Compile
- g++ server.cpp -o server -lmicrohttpd
- Execute
- ./server
- Access
- localhost:4242
Server Header
c
class EmbeddedServer {
private:
struct MHD_Daemon * daemon;
std::string root;
public:
EmbeddedServer(int port=4242);
~EmbeddedServer();
static int ahc_echo(
void * cls,
struct MHD_Connection* connection,
const char* url,
const char* method,
const char* version,
const char* upload_data,
size_t * upload_data_size,
void ** ptr
);
};
Context Header
c
class Context {
public:
struct MHD_PostProcessor* pp;
std::string get;
std::string post;
};
Connection Header
c
class Connection {
public:
std::string url;
struct MHD_Connection* server_conn;
struct MHD_Response* response;
int response_code;
clock_t time_ini;
Connection(struct MHD_Connection* server_conn, std::string url);
void sendFile(std::string _url);
void sendText(std::string text);
void createCookie();
void redirect(std::string url);
void error(int num, std::string msg);
int finish();
};
Server Code
c
EmbeddedServer::EmbeddedServer(int port){
this->daemon = MHD_start_daemon(
MHD_USE_THREAD_PER_CONNECTION,
port,
NULL,
NULL,
&EmbeddedServer::ahc_echo,
this,
MHD_OPTION_END
);
printf("Server work in localhost:%d
",port);
}
EmbeddedServer::~EmbeddedServer(){
if (this->daemon)
MHD_stop_daemon(this->daemon);
}
int EmbeddedServer::ahc_echo(
void* _server,
struct MHD_Connection* connection,
const char* url,
const char* method,
const char* version,
const char* upload_data,
size_t* _upload_data_size,
void** con_cls
) {
Context* context = (Context*) *con_cls;
size_t upload_data_size = *_upload_data_size;
if (context == NULL){
context = new Context();
if ( strcmp(method,"POST") == 0 )
context->pp = MHD_create_post_processor(connection, 1024, web_config_post, (void*)&context->get );
*con_cls = context;
return MHD_YES;
}
if (upload_data_size > 0 ){
MHD_post_process(context->pp, upload_data, upload_data_size);
*_upload_data_size = 0;
return MHD_YES;
} else {
MHD_destroy_post_processor(context->pp);
}
/*
MHD_RESPONSE_HEADER_KIND Response header
MHD_HEADER_KIND HTTP header.
MHD_COOKIE_KIND Cookies. Note that the original HTTP header containing the cookie(s) will still be available and intact.
MHD_POSTDATA_KIND POST data. This is available only if a content encoding supported by MHD is used (currently only URL encoding), and only if the posted content fits within the available memory pool. Note that in that case, the upload data given to the MHD_AccessHandlerCallback will be empty (since it has already been processed).
MHD_GET_ARGUMENT_KIND GET (URI) arguments.
MHD_FOOTER_KIND */
MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, web_config_get, (void*)&context->get);
EmbeddedServer& server = *((EmbeddedServer*)_server);
Connection conn(connection, url);
string text;
text = "<html><body>Ok!<br>";
text += context->get;
text += context->post;
text += "</body></html>";
conn.sendText( text );
delete context;
return conn.finish();
}