我从以下位置构建了 server3 示例:
http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/examples.html
我做的唯一一件事——修改了request_handler.cpp
for:
// Decode url to path.
std::string request_path;
if (!url_decode(req.uri, request_path))
{
rep = reply::stock_reply(reply::bad_request);
return;
}
// Request path must be absolute and not contain "..".
if (request_path.empty() || request_path[0] != '/'
|| request_path.find("..") != std::string::npos)
{
rep = reply::stock_reply(reply::bad_request);
return;
}
// Fill out the reply to be sent to the client.
rep.status = reply::ok;
std::string filename = "/tmp/test.mp4";
std::ifstream file (filename.c_str(), std::ios::in|std::ios::binary);
char buf[1024000]; // 1MB Buffer read
while (file.read(buf, sizeof(buf)).gcount() > 0)
rep.content.append(buf, file.gcount());
rep.headers.resize(9);
rep.headers[0].name = "Content-Length";
rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
rep.headers[1].name = "Content-Type";
rep.headers[1].value = "video/mp4";
当我打开chrome并点击服务器时,我可以得到视频,没问题。同时,我打开另一个选项卡并点击服务器,没有任何反应。看起来它等到第一个选项卡完成。
目标是拥有一个处理多个连接并发送多个文件的服务器..