27 const struct mg_request_info *req_info = mg_get_request_info(conn);
30 std::string request_uri = req_info->local_uri;
34 std::string file_relative_path = request_uri.substr(6);
35 std::string mime_type = mg_get_builtin_mime_type(request_uri.c_str());
36 std::string content, response_header;
39 std::lock_guard<std::mutex> lock(cache_mutex_);
40 if (content_cache_.find(file_relative_path) != content_cache_.end()) {
41 content = content_cache_[file_relative_path];
45 if (content.empty()) {
46 std::string file_abs_path;
47 if (FindProtoPath(file_relative_path, &file_abs_path) &&
49 std::lock_guard<std::mutex> lock(cache_mutex_);
50 content_cache_[file_relative_path] = content;
52 response_header =
"HTTP/1.1 404 Not Found\r\nContent-Type: " + mime_type +
53 "\r\n\r\nFile not found";
54 mg_printf(conn, response_header.c_str());
59 response_header =
"HTTP/1.1 200 OK\r\nContent-Type: " + mime_type +
"\r\n";
60 mg_printf(conn, response_header.c_str());
61 mg_printf(conn,
"Cache-Control: max-age=86400\r\n\r\n");
64 mg_write(conn, content.data(), content.size());
69bool ProtoHandler::FindProtoPath(
const std::string file_relative_path,