ASIO_HTTP is a free, cross-platform HTTP server framework that uses Boost::asio to handle network services. Its API is similar to the Python Django framework.
boost::asio boost::json
1、准备工作
git clone xxx
cd asio_http
./mkwebapp.sh
2、在webapp/urls.cpp中添加,"/uploda"是路由,savefile是views.cpp中的函数。
std::map<std::string, std::function<void (request&)>> urlpatterns = {
{"/upload", savefile},
};
3、views.cpp中的函数例子
void savefile(h_context &c) {
utils::print_req(c.request_);
std::string filename;
for(auto it:c.request_.body_vhs){
if(std::string::npos!=it.find("filename=")){
filename = extract_filename(it);
}
}
save_file(c.request_.content, filename);
json::object response_json;
response_json["h"] = "Ture";
std::string json_str = json::serialize(response_json);
c.reply_.josnstr(json_str);
}
request结构为
struct request
{
// request();
std::string method;
std::string uri;
int http_version_major;
int http_version_minor;
std::vector<header> headers;
std::string body;
int content_length;
std::string boundary;
std::string content;
std::vector<header> body_headers;
std::vector<std::string> body_vhs;
std::string body_hstr;
std::string get_boundary() ;
std::string extract_content();
};
4、run "Usage: http_server
<doc_root> cd build
cmake ..
./asio_http 0.0.0.0 8090 doc_root