Apollo 11.0
自动驾驶开放平台
apollo::dreamview::ProtoHandler类 参考

The ProtoHandler, built on top of CivetHandler, transfer the proto file by http for frontend to parse message. 更多...

#include <proto_handler.h>

类 apollo::dreamview::ProtoHandler 继承关系图:
apollo::dreamview::ProtoHandler 的协作图:

Public 成员函数

 ProtoHandler ()
 
bool handleGet (CivetServer *server, struct mg_connection *conn)
 
std::string GenerateETag (const std::string &content)
 

详细描述

The ProtoHandler, built on top of CivetHandler, transfer the proto file by http for frontend to parse message.

在文件 proto_handler.h 第 42 行定义.

构造及析构函数说明

◆ ProtoHandler()

apollo::dreamview::ProtoHandler::ProtoHandler ( )
inline

在文件 proto_handler.h 第 44 行定义.

44{}

成员函数说明

◆ GenerateETag()

std::string apollo::dreamview::ProtoHandler::GenerateETag ( const std::string &  content)

在文件 proto_handler.cc 第 93 行定义.

93 {
94 // 使用 std::hash 生成基于内容的哈希值
95 std::hash<std::string> hasher;
96 size_t hash = hasher(content);
97
98 // 将哈希值转换为十六进制字符串
99 std::stringstream ss;
100 ss << std::hex << hash;
101
102 return ss.str();
103}

◆ handleGet()

bool apollo::dreamview::ProtoHandler::handleGet ( CivetServer *  server,
struct mg_connection *  conn 
)

在文件 proto_handler.cc 第 26 行定义.

26 {
27 const struct mg_request_info *req_info = mg_get_request_info(conn);
28
29 // parse request rui
30 std::string request_uri = req_info->local_uri;
31
32 // replace /proto to actual file root path prefix,remove /proto
33 // todo(@lijin):adapt to package version,change this to a variable
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;
37
38 {
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];
42 }
43 }
44
45 if (content.empty()) {
46 std::string file_abs_path;
47 if (FindProtoPath(file_relative_path, &file_abs_path) &&
48 apollo::cyber::common::GetContent(file_abs_path, &content)) {
49 std::lock_guard<std::mutex> lock(cache_mutex_);
50 content_cache_[file_relative_path] = content;
51 } else {
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());
55 return true;
56 }
57 }
58
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"); // 缓存 24 小时
62 // mg_printf(conn, "ETag: \"%s\"\r\n",
63 // GenerateETag(content).c_str()); // 生成并发送ETag
64 mg_write(conn, content.data(), content.size());
65
66 return true;
67}
bool GetContent(const std::string &file_name, std::string *content)
Get file content as string.
Definition file.cc:167

该类的文档由以下文件生成: