23 namespace visualization {
24 namespace webrtc_server {
26 static int LogMessage(
const struct mg_connection *conn,
const char *message) {
43 bool handle(CivetServer *server,
struct mg_connection *conn) {
45 const struct mg_request_info *req_info = mg_get_request_info(conn);
48 Json::Value in = this->getInputMessage(req_info, conn);
51 Json::Value out(func_(req_info, in));
54 std::string answer =
"";
55 if (out.isNull() ==
false) {
56 answer = Json::writeString(writer_builder_, out);
57 mg_printf(conn,
"HTTP/1.1 200 OK\r\n");
58 mg_printf(conn,
"Access-Control-Allow-Origin: *\r\n");
59 mg_printf(conn,
"Content-Type: text/plain\r\n");
60 mg_printf(conn,
"Content-Length: %zd\r\n", answer.size());
61 mg_printf(conn,
"\r\n");
62 mg_write(conn, answer.c_str(), answer.size());
67 "request_uri: {}, local_uri: {}, request_method: {}, "
68 "query_string: {}, content_length: {}, answer: {}.",
69 req_info->request_uri, req_info->local_uri,
70 req_info->request_method,
71 req_info->query_string ? req_info->query_string :
"nullptr",
72 req_info->content_length, answer);
75 bool handleGet(CivetServer *server,
struct mg_connection *conn)
override {
76 return handle(server, conn);
78 bool handlePost(CivetServer *server,
struct mg_connection *conn)
override {
79 return handle(server, conn);
84 Json::StreamWriterBuilder writer_builder_;
85 Json::CharReaderBuilder reader_builder_;
87 Json::Value getInputMessage(
const struct mg_request_info *req_info,
88 struct mg_connection *conn) {
89 Json::Value json_message;
92 long long tlen = req_info->content_length;
96 const long long bufSize = 1024;
99 long long rlen = tlen - nlen;
100 if (rlen > bufSize) {
103 rlen = mg_read(conn, buf, (
size_t)rlen);
107 body.append(buf, rlen);
113 std::unique_ptr<Json::CharReader> reader(
114 reader_builder_.newCharReader());
116 if (!reader->parse(body.c_str(), body.c_str() + body.size(),
117 &json_message, &errors)) {
127 std::map<std::string, HttpFunction> &func,
128 const std::vector<std::string> &options)
131 for (
auto it : func) {
HttpServerRequestHandler(std::map< std::string, HttpFunction > &func, const std::vector< std::string > &options)
std::function< Json::Value(const struct mg_request_info *req_info, const Json::Value &)> HttpFunction
RequestHandler(HttpServerRequestHandler::HttpFunction &func)
bool handle(CivetServer *server, struct mg_connection *conn)
bool handlePost(CivetServer *server, struct mg_connection *conn) override
bool handleGet(CivetServer *server, struct mg_connection *conn) override
static int LogMessage(const struct mg_connection *conn, const char *message)
static const struct CivetCallbacks * getCivetCallbacks()
static struct CivetCallbacks _callbacks
Generic file read and write utility for python interface.