C++ 浏览器将随机HTTP消息正文发送到my boost.asio服务器。我能换这个吗?
最终编辑:问题回答,令人印象深刻。但你可能仍然会发现这很有趣 编辑:问题可能略有变化。稍后在邮件中澄清 我使用的是boost.asio示例中的一个示例服务器,它是我构建的基础。当浏览器(Firefox)请求一个图像或随后使用ajax发布某个内容时,它似乎在发送随机的标题、单词/标题片段或先前请求的图像的“片段”/ajax post。一个具体的例子是,不仅发送了我希望通过ajax发送到服务器的字符串,而且还发送了一些javascript代码 在处理每个新请求之前,缓冲区被设置为“\0”,因此我认为这不是问题所在。此外,正如前面提到的,这不仅仅是在使用ajax时,所以如果有javascript错误,这可能是无关紧要的。我对HTTP协议几乎没有经验-也许有一些我不知道的解决方案C++ 浏览器将随机HTTP消息正文发送到my boost.asio服务器。我能换这个吗?,c++,http,boost-asio,C++,Http,Boost Asio,最终编辑:问题回答,令人印象深刻。但你可能仍然会发现这很有趣 编辑:问题可能略有变化。稍后在邮件中澄清 我使用的是boost.asio示例中的一个示例服务器,它是我构建的基础。当浏览器(Firefox)请求一个图像或随后使用ajax发布某个内容时,它似乎在发送随机的标题、单词/标题片段或先前请求的图像的“片段”/ajax post。一个具体的例子是,不仅发送了我希望通过ajax发送到服务器的字符串,而且还发送了一些javascript代码 在处理每个新请求之前,缓冲区被设置为“\0”,因此我认为
POST /post HTTP/1.1
headers
THIS IS WHAT WAS POSTEDlick = function() {
...
}
GET /index HTTP/1.1
headers
che-Control: max-age=0
这是C++代码,它从BooS.asIO例子中复制得非常好:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
class connection
: public boost::enable_shared_from_this<connection>
{
public:
typedef boost::shared_ptr<connection> pointer;
static pointer create(boost::asio::io_contenxt& io_context)
{
return pointer(new connection(io_context));
}
tcp::socket& socket() {
return socket_;
}
void start()
{
buf[0] = '\0'; //apparently not necessary?
socket_.async_read_some(boost::asio::buffer(buf,length),
boost::bind(&connection::follow_up_write, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
private:
connection(boost::asio::io_context& io_context)
: socket_(io_context)
{
}
follow_up_write(const boost::system::error_code& error, size_t by) {
if(!error) {
//this is a proposed solution, for simple cout-ing
//it works, but still problems working with buffer
//contents (part of EDIT)
std::string s;
for(unsigned int i = 0; i < (int)by; i++) {
s.push_back(buf[i]);
}
//this is for me to check, obviously:
std::cout << s << std::endl;
do_something_with_reply dswr(s);
boost::asio::async_write(socket_, boost::asio::buffer(dswr.answer),
boost::bind(&connection::follow_up, shared_from_this()));
}
else
std::cout << error.message() << std::endl;
}
void follow_up() {
//nothing here yet
}
tcp::socket socket_;
enum {length = 10000}
char buf[length];
}
//not good at naming, don't judge
class server
{
public:
server(boost::asio::io_context& io_context)
: io_context_(io_context), acceptor_(io_context, tcp::endpoint(tcp::v4(), 8080))
{
start_accept();
}
private:
void start_accept()
{
connection::pointer new_connection = connection::create(io_context_);
acceptor_.async_accept(new_connection->socket(),
boost::bind(&server::handle_accept, this,
new_connection, boost::asio::placeholders::error));
}
void handle_accept(connection::pointer new_connection, const boost::system::error_code& error)
{
if(!error) {
new_connection->start();
}
start_accept();
}
boost::asio::io_context& io_context_;
tcp::acceptor acceptor_;
}
int main()
{
try {
boost::asio::io_context io_context;
server server(io_context);
io_context.run();
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
但奇怪的是,它没有显示“结束”
在一个更复杂的例子中,我的结果字符串的末尾甚至有一部分文件名。在本例中,答案可能是“le.txt”。答案令人印象深刻:user207421拥有解决方案 内容长度头值是获得真正的消息正文所需的值 感谢所有花时间看这篇文章的人
附言:我文章中的第二个问题仍然很奇怪,但与这个问题无关。听起来更像是在缓冲区之外阅读。我对boost aiso不太熟悉,但如果浏览器真的在发送随机请求,那么不会。你不能改变这一点。您无法控制浏览器的功能。然而,这可能不是真正发生的事情,所以要谨慎对待。不要将缓冲设置为任何东西。只要访问缓冲区的字符数不要超过你收到的字符数,问题就会消失。编辑文章。不幸的是,缓冲区限制似乎不是解决方案。继续获取与您的问题无关的“che Control:max age=0”,但您在
后续\u write
中有未定义的行为。[1] async\u write
立即返回,[2]asio::buffer
不复制数据(它只是保存数据指针的包装器)。[3] 简言之,你有悬空的参考资料。有什么奇怪的?关于什么?@user207421作为一名非计算机科学家,几乎所有事情都很奇怪。
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::ifstream file("file.txt");
std::string all, txt;
if(file.is_open()) {
while(getline(file,txt)) {
all.append(txt + '\n');
}
}
std::string s;
s.append("Start");
for(unsigned int i = 0; i < all.length(); i++) {
s.push_back(all[i]);
}
s.append("END" + '\n');
std::cout << s << std::endl;
}
StartContent of file
Content of file
Content of file