Apollo 10.0
自动驾驶开放平台
proto_handler.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <fstream>
20
21#include "cyber/common/file.h"
22#include "cyber/common/log.h"
23
24namespace apollo {
25namespace dreamview {
26
27bool ProtoHandler::handleGet(CivetServer *server, struct mg_connection *conn) {
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}
69
70bool ProtoHandler::FindProtoPath(const std::string file_relative_path,
71 std::string *file_abs_path) {
72 std::string tmp_file_path;
73
74 const char *apollo_env_workroot = std::getenv("APOLLO_ROOT_DIR");
75 if (apollo_env_workroot != nullptr) {
76 tmp_file_path = std::string(apollo_env_workroot) + file_relative_path;
77 if (apollo::cyber::common::PathExists(tmp_file_path)) {
78 ADEBUG << "find proto file in APOLLO_ROOT_DIR: " << tmp_file_path;
79 *file_abs_path = tmp_file_path;
80 return true;
81 }
82 }
83
84 const char *apollo_distribution_home =
85 std::getenv("APOLLO_DISTRIBUTION_HOME");
86 if (apollo_distribution_home != nullptr) {
87 tmp_file_path =
88 std::string(apollo_distribution_home) + "/src" + file_relative_path;
89 if (apollo::cyber::common::PathExists(tmp_file_path)) {
90 ADEBUG << "find proto file in APOLLO_DISTRIBUTION_HOME: "
91 << tmp_file_path;
92 *file_abs_path = tmp_file_path;
93 return true;
94 }
95 }
96
97 // source code
98 tmp_file_path = "/apollo" + file_relative_path;
99 if (apollo::cyber::common::PathExists(tmp_file_path)) {
100 *file_abs_path = tmp_file_path;
101 return true;
102 }
103 // package -source code
104 tmp_file_path = "/apollo_workspace" + file_relative_path;
105 if (apollo::cyber::common::PathExists(tmp_file_path)) {
106 *file_abs_path = tmp_file_path;
107 return true;
108 }
109 // package -source code
110 tmp_file_path = "/opt/apollo/neo/src" + file_relative_path;
111 if (apollo::cyber::common::PathExists(tmp_file_path)) {
112 *file_abs_path = tmp_file_path;
113 return true;
114 }
115 return false;
116}
117
118std::string ProtoHandler::GenerateETag(const std::string &content) {
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}
129
130} // namespace dreamview
131} // namespace apollo
bool handleGet(CivetServer *server, struct mg_connection *conn)
std::string GenerateETag(const std::string &content)
#define ADEBUG
Definition log.h:41
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195
bool GetContent(const std::string &file_name, std::string *content)
Get file content as string.
Definition file.cc:167
class register implement
Definition arena_queue.h:37