Skip to content

Commit fab90b9

Browse files
committed
Supported web server index.html/index.htm
1 parent e35c938 commit fab90b9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

proxy/include/proxy/proxy_server.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,6 +3756,48 @@ R"x*x*x(<html>
37563756
boost::system::error_code ec;
37573757
auto& request = hctx.request_;
37583758

3759+
// 查找目录下是否存在 index.html 或 index.htm 文件, 如果存在则返回该文件.
3760+
// 否则返回目录下的文件列表.
3761+
auto index_html = fs::path(hctx.target_path_) / "index.html";
3762+
fs::exists(index_html, ec) ? index_html = index_html :
3763+
index_html = fs::path(hctx.target_path_) / "index.htm";
3764+
3765+
if (fs::exists(index_html, ec))
3766+
{
3767+
std::ifstream file(index_html.string(), std::ios::binary);
3768+
if (file)
3769+
{
3770+
std::string content(
3771+
(std::istreambuf_iterator<char>(file)),
3772+
std::istreambuf_iterator<char>());
3773+
3774+
string_response res{ http::status::ok, request.version() };
3775+
res.set(http::field::server, version_string);
3776+
res.set(http::field::date, server_date_string());
3777+
auto ext = to_lower(index_html.extension().string());
3778+
if (global_mimes.count(ext))
3779+
res.set(http::field::content_type, global_mimes[ext]);
3780+
else
3781+
res.set(http::field::content_type, "text/plain");
3782+
res.keep_alive(request.keep_alive());
3783+
res.body() = content;
3784+
res.prepare_payload();
3785+
3786+
http::serializer<false, string_body, http::fields> sr(res);
3787+
co_await http::async_write(
3788+
m_local_socket,
3789+
sr,
3790+
net_awaitable[ec]);
3791+
if (ec)
3792+
XLOG_WARN << "connection id: "
3793+
<< m_connection_id
3794+
<< ", http dir write index err: "
3795+
<< ec.message();
3796+
3797+
co_return;
3798+
}
3799+
}
3800+
37593801
auto path_list = format_path_list(hctx.target_path_, ec);
37603802
if (ec)
37613803
{

0 commit comments

Comments
 (0)