奇妙的beast htt异步发送
  • 板块学术版
  • 楼主坎巴拉
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/18 11:23
  • 上次更新2025/1/18 14:52:43
查看原帖
奇妙的beast htt异步发送
358070
坎巴拉楼主2025/1/18 11:23
 void send_file(const fs::path& file_path) {
     // 构造响应
     http::response<http::file_body> response{ http::status::ok, request_.version() };
     response.set(http::field::server, "Boost.Beast HTTPS Server");
     response.set(http::field::access_control_allow_origin, "*");

     // 查找文件扩展名
     std::string ext = file_path.extension().string();
     //std::cout << ext << "\n";
     if (mime_map.count(ext) > 0) {
         response.set(http::field::content_type, mime_map[ext]);
     }
     else {
         response.set(http::field::content_type, "application/octet-stream");
     }


     response.body().open(file_path.string().c_str(), beast::file_mode::read, ec);
     if (ec) {
         send_not_found();
         return;
     }
     // 发送响应
     auto self(shared_from_this());
     http::async_write(ssl_stream_, response,
         [self](beast::error_code ec, std::size_t bytes_transferred) {
             boost::ignore_unused(bytes_transferred);
             std::cerr << ec.message() << std::endl;
             self->do_close();
         });//为甚都进来了你只发4k?
 }
2025/1/18 11:23
加载中...