From ceb4368daf1fab6629889ec838383e2dd4a8ed14 Mon Sep 17 00:00:00 2001 From: a158 Date: Fri, 12 Dec 2025 19:15:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SSL=E5=92=8C=E6=99=AE?= =?UTF-8?q?=E9=80=9A=E5=8F=8C=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/net/http_define.h | 1 + src/base/environment.cpp | 2 +- src/net/http_center.cpp | 14 +++++++++++++- src/net/http_response.cpp | 31 +++++++++++++++---------------- src/net/http_website.cpp | 20 ++++++++++++++++++-- src/util/file.cpp | 2 ++ 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/include/net/http_define.h b/include/net/http_define.h index c533c59..4723008 100644 --- a/include/net/http_define.h +++ b/include/net/http_define.h @@ -76,6 +76,7 @@ namespace ylib std::string domain; // 端口 ushort port = 0; + ushort port_ssl = 0; // 开启SSL bool ssl = false; }; diff --git a/src/base/environment.cpp b/src/base/environment.cpp index ce9700a..496c7d6 100644 --- a/src/base/environment.cpp +++ b/src/base/environment.cpp @@ -20,5 +20,5 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web namespace ylib { extern ylib::environment *env = new ylib::environment(); - extern ylib::log4* log = nullptr; + extern ylib::log4* log = new ylib::log4(""); } diff --git a/src/net/http_center.cpp b/src/net/http_center.cpp index 2aa4861..8966d16 100644 --- a/src/net/http_center.cpp +++ b/src/net/http_center.cpp @@ -171,6 +171,18 @@ std::vector ylib::network::http::center::listen_ports() } if(find == false) ports.push_back(m_config.website[i].host[x].port); + + find = false; + for_iter(iter, ports) + { + if (*iter == m_config.website[i].host[x].port_ssl) + { + find = true; + break; + } + } + if (find == false) + ports.push_back(m_config.website[i].host[x].port_ssl); } } return ports; @@ -182,7 +194,7 @@ bool ylib::network::http::center::port_have_ssl(ushort port) { for (size_t x = 0; x < m_config.website[i].host.size(); x++) { - if (m_config.website[i].host[x].port == port) + if (m_config.website[i].host[x].port_ssl == port) { return m_config.website[i].host[x].ssl; } diff --git a/src/net/http_response.cpp b/src/net/http_response.cpp index 7110f47..4d20fa0 100644 --- a/src/net/http_response.cpp +++ b/src/net/http_response.cpp @@ -154,24 +154,23 @@ bool ylib::network::http::response::send_file(const std::string& filepath, int32 //ylib::log->info(filepath2,"response"); //取文件信息 - { - struct stat statbuf; - if (stat(filepath2.c_str(), &statbuf) != 0) - return false; - filesize = statbuf.st_size; - last_modify_time = statbuf.st_mtime; - } //{ - // auto filesize = std::filesystem::file_size(filepath2); - // auto ftime = std::filesystem::last_write_time(filepath2); - // // 把 file_time_type 转为 system_clock::time_point - // auto sctp = std::chrono::time_point_cast( - // ftime - decltype(ftime)::clock::now() + std::chrono::system_clock::now() - // ); - - // // 转为 time_t(UNIX 时间戳) - // last_modify_time = std::chrono::system_clock::to_time_t(sctp); + // struct stat statbuf; + // if (stat(filepath2.c_str(), &statbuf) != 0) + // return false; + // filesize = statbuf.st_size; + // last_modify_time = statbuf.st_mtime; //} + { + filesize = std::filesystem::file_size(filepath2); + auto ftime = std::filesystem::last_write_time(filepath2); + // 把 file_time_type 转为 system_clock::time_point + auto sctp = std::chrono::time_point_cast( + ftime - decltype(ftime)::clock::now() + std::chrono::system_clock::now() + ); + // 转为 time_t(UNIX 时间戳) + last_modify_time = std::chrono::system_clock::to_time_t(sctp); + } // 设置为已发送 m_response = true; //if (filesize != 0) diff --git a/src/net/http_website.cpp b/src/net/http_website.cpp index f651bf8..c53b5f9 100644 --- a/src/net/http_website.cpp +++ b/src/net/http_website.cpp @@ -71,6 +71,7 @@ bool ylib::network::http::website::start(const website_config& config) for (size_t i = 0; i < m_config.host.size(); i++) { auto hostpoint = new network::http::host; + auto hostpoint_ssl = new network::http::host; network::http::ssl* ssl = nullptr; @@ -83,7 +84,7 @@ bool ylib::network::http::website::start(const website_config& config) ylib::log->warn("The certificate is not open or does not exist, Host:"+ m_config.host[i].domain); else { - ssl = new network::http::ssl(center()->server(m_config.host[i].port),ssl_config); + ssl = new network::http::ssl(center()->server(m_config.host[i].port_ssl),ssl_config); /*注册SSL*/ if (ssl->regist() == false) { @@ -102,10 +103,25 @@ bool ylib::network::http::website::start(const website_config& config) } } } + } + else + { - hostpoint->init(m_config.host[i].domain, m_config.host[i].port,ssl); + } + if (m_config.host[i].ssl) + { + hostpoint_ssl->init(m_config.host[i].domain, m_config.host[i].port_ssl, ssl); + m_hosts.push_back(hostpoint_ssl); + } + hostpoint->init(m_config.host[i].domain, m_config.host[i].port,nullptr); m_hosts.push_back(hostpoint); + + + + + + } if(m_cache->start(m_config.cache) == false) { diff --git a/src/util/file.cpp b/src/util/file.cpp index e632c26..387045b 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -216,6 +216,8 @@ std::streamsize ylib::file_io::size() bool file_io::is_open() { + if (m_stream == nullptr) + return false; return m_stream->is_open(); }