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