大多数嵌入式设备由于没有屏幕输出,只能通过Web页面来配置。这里利用CPPHttpLib来实现HttpServer。
HttpServer是利用CPPHttpLib开源库实现的Http服务器CppHttpLib是基于C++11的HTTP开源库,开源协议是MIT.
CppHttpLib下载地址
#ifndef HTTP_H
#define HTTP_H
#include "httplib.h"
namespace http = httplib;
#endif
该文件引用C++ HTTPLIB库头文件httplib.h,将命名空间httplib重命名为http,简化后续引用。
#include "http.h"
#include
struct HttpServer : private http::Server
{
HttpServer(bool is_log = false);
bool set_root_path(std::string const& url, std::string const& dir);
HttpServer &get(const std::string &pattern, http::Server::Handler handler);
HttpServer &post(const std::string &pattern, http::Server::Handler handler);
HttpServer &put(const std::string &pattern, http::Server::Handler handler);
bool run(const char *host, int port);
private:
std::string log(const http::Request &req, const http::Response &res);
std::string dump_headers(const http::Headers &headers);
private:
bool is_log_;
};
类型说明:
HttpServer::HttpServer(bool is_log)
: is_log_(is_log)
{
if(is_log_)
set_logger([this](const http::Request &req, const http::Response &res)
{
printf("%s", this->log(req, res).c_str());
});
}
函数说明:
bool HttpServer::set_root_path(std::string const& url, std::string const& dir)
{
return set_mount_point(url, dir);
}
函数说明:
HttpServer &HttpServer::get(const std::string &pattern, http::Server::Handler handler)
{
Get(pattern, handler);
return *this;
}
HttpServer &HttpServer::post(const std::string &pattern, http::Server::Handler handler)
{
Post(pattern, handler);
return *this;
}
HttpServer &HttpServer::put(const std::string &pattern, http::Server::Handler handler)
{
Put(pattern, handler);
return *this;
}
函数说明:
bool HttpServer::run(const char *host, int port)
{
return listen(host, port);
}
函数说明:
std::string HttpServer::log(const http::Request &req, const http::Response &res)
{
std::string s;
char buf[BUFSIZ];
s += "================================\n";
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
req.version.c_str(), req.path.c_str());
s += buf;
std::string query;
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%c%s=%s",
(it == req.params.begin()) ? '?' : '&', x.first.c_str(),
x.second.c_str());
query += buf;
}
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
s += buf;
s += dump_headers(req.headers);
s += "--------------------------------\n";
snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
s += buf;
s += dump_headers(res.headers);
s += "\n";
if (!req.body.empty()) { s += req.body; }
s += "\n";
return s;
}
std::string HttpServer::dump_headers(const http::Headers &headers) {
std::string s;
char buf[BUFSIZ];
for (auto it = headers.begin(); it != headers.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
s += buf;
}
return s;
}
函数说明:
#include "httpserver.h"
int main(int argc, char* argv[])
{
bool isLog = true;
HttpServer server(isLog);
if(argc > 1)
server.set_root_path("/", argv[1]);
else
server.set_root_path("/", "dist/www");
server.run("0.0.0.0", 80);
return 0;
}
说明:
利用CppHttpLib库实现一个HttpServer还是很简单的。