From fab90b99aa7b5f0caad6b24a04be51a62bba2d34 Mon Sep 17 00:00:00 2001 From: Jackarain Date: Sat, 8 Jun 2024 01:06:27 +0800 Subject: [PATCH] Supported web server index.html/index.htm --- proxy/include/proxy/proxy_server.hpp | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/proxy/include/proxy/proxy_server.hpp b/proxy/include/proxy/proxy_server.hpp index a57f6603d9..ff4c9a3d7f 100644 --- a/proxy/include/proxy/proxy_server.hpp +++ b/proxy/include/proxy/proxy_server.hpp @@ -3756,6 +3756,48 @@ R"x*x*x( boost::system::error_code ec; auto& request = hctx.request_; + // 查找目录下是否存在 index.html 或 index.htm 文件, 如果存在则返回该文件. + // 否则返回目录下的文件列表. + auto index_html = fs::path(hctx.target_path_) / "index.html"; + fs::exists(index_html, ec) ? index_html = index_html : + index_html = fs::path(hctx.target_path_) / "index.htm"; + + if (fs::exists(index_html, ec)) + { + std::ifstream file(index_html.string(), std::ios::binary); + if (file) + { + std::string content( + (std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + + string_response res{ http::status::ok, request.version() }; + res.set(http::field::server, version_string); + res.set(http::field::date, server_date_string()); + auto ext = to_lower(index_html.extension().string()); + if (global_mimes.count(ext)) + res.set(http::field::content_type, global_mimes[ext]); + else + res.set(http::field::content_type, "text/plain"); + res.keep_alive(request.keep_alive()); + res.body() = content; + res.prepare_payload(); + + http::serializer sr(res); + co_await http::async_write( + m_local_socket, + sr, + net_awaitable[ec]); + if (ec) + XLOG_WARN << "connection id: " + << m_connection_id + << ", http dir write index err: " + << ec.message(); + + co_return; + } + } + auto path_list = format_path_list(hctx.target_path_, ec); if (ec) {