Apollo 11.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
17#include <fstream>
18
19#include "cyber/common/file.h"
20#include "cyber/common/log.h"
22
23namespace apollo {
24namespace dreamview {
25
26bool ProtoHandler::handleGet(CivetServer *server, struct mg_connection *conn) {
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}
68
69bool ProtoHandler::FindProtoPath(const std::string file_relative_path,
70 std::string* file_abs_path) {
71 std::string tmp_file_path;
72 // source code
73 tmp_file_path = "/apollo" + file_relative_path;
74 if (apollo::cyber::common::PathExists(tmp_file_path)) {
75 *file_abs_path = tmp_file_path;
76 return true;
77 }
78 // package -source code
79 tmp_file_path = "/apollo_workspace" + file_relative_path;
80 if (apollo::cyber::common::PathExists(tmp_file_path)) {
81 *file_abs_path = tmp_file_path;
82 return true;
83 }
84 // package -source code
85 tmp_file_path = "/opt/apollo/neo/src" + file_relative_path;
86 if (apollo::cyber::common::PathExists(tmp_file_path)) {
87 *file_abs_path = tmp_file_path;
88 return true;
89 }
90 return false;
91}
92
93std::string ProtoHandler::GenerateETag(const std::string &content) {
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}
104
105} // namespace dreamview
106} // namespace apollo
bool handleGet(CivetServer *server, struct mg_connection *conn)
std::string GenerateETag(const std::string &content)
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