Apollo 10.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.h42 行定义.

构造及析构函数说明

◆ ProtoHandler()

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

在文件 proto_handler.h44 行定义.

44{}

成员函数说明

◆ GenerateETag()

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

在文件 proto_handler.cc118 行定义.

118 {
119 // 使用 std::hash 生成基于内容的哈希值
120 std::hash<std::string> hasher;
121 size_t hash = hasher(content);
122
123 // 将哈希值转换为十六进制字符串
124 std::stringstream ss;
125 ss << std::hex << hash;
126
127 return ss.str();
128}

◆ handleGet()

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

在文件 proto_handler.cc27 行定义.

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

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