You can write and extend your own multi-platform http server with the lasote/httpserver block.
It allows you to implement just a subclass of httpserver::HttpMiddleware to attend browsers or other http client requests.
A method call
from your subclass object will be called with a Request* object and a Response* object.
Just modify the body and/or headers of Response object in your method and the server will do all the work.
$ bii find
$ cd bin
$ #run server executable
These are the files you will need in your block to have your HTTP Server up and running:
main_server.cpp
This file just instanciates the server and runs it with simple configuration parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include "lasote/httpserver/http_server.h"
#include "my_http_middle_ware.h"
using namespace httpserver;
using namespace gip;
int main(){
MyHttpMiddleware my_mmiddleware;
HttpServerConf conf(9000, 300, 60, 5);
HttpServer http_server;
http_server.run(&my_mmiddleware, &conf);
return 0;
}
|
my_http_middle_ware.h
Defines your HttpMiddleware subclass.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #pragma once
#include "lasote/httpserver/http_middleware.h"
using namespace httpserver;
class MyHttpMiddleware : public httpserver::HttpMiddleware {
public:
MyHttpMiddleware() : HttpMiddleware(NULL){}
MyHttpMiddleware(HttpMiddleware* other_middleware) : HttpMiddleware(other_middleware){
}
virtual ~MyHttpMiddleware();
virtual void call(Request&, Response&);
};
|
my_http_middle_ware.cpp
Implements HttpMiddleware subclass. You should implement thecall
method, reading the header variables from the request, and modifying the response to return the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include "my_http_middle_ware.h"
#include "sstream"
#include "iostream"
MyHttpMiddleware::~MyHttpMiddleware(){
}
void MyHttpMiddleware::call(Request& request, Response& response){
ostringstream html;
html << "<!DOCTYPE html>\n<html>\n<body>\n";
if(request.get("name") != ""){
html << "Hello " << request.get("name") << "<br><br>";
}
//Build the html form
string form;
form = "\
<form name='form' action='/' method='POST'>\n\
Name: <br>\n\
<input type='text' name='name'> <br>\n\
<input type='submit''/>\n\
</form>\n\
";
html << form << "</body>\n</html>\n";
// Set content type we are printing
response.content_type("text/html");
// Set the body
response.body = html.str();
}
|
Download: httpserver.zip
The previous code has been tested on:
You can find more information in the readme.txt file of lasote/httpserver