浏览器将随机HTTP消息正文发送到我的boost.asio服务器。我可以改变这个吗?

时间:2019-06-14 15:39:58

标签: c++ http boost-asio

最终编辑:回答的问题,让人难以理解。但是您仍然可能会发现这很有趣。

编辑:问题可能已经稍微改变。发表后澄清

我正在使用我构建的boost.asio示例中的示例服务器。当浏览器(Firefox)请求图像或事后用ajax发布内容时,似乎是在发送先前请求/ ajax发布的图像的随机标头,切词/标头或“摘要”。一个具体的例子是,不仅发送了我想通过ajax发送到服务器的字符串,而且还发送了一些JavaScript代码。

在处理每个新请求之前,将缓冲区设置为“ \ 0”,所以我不认为这是问题所在。另外,如上所述,这不仅是在使用ajax的情况下,因此如果出现JavaScript错误,则可能不相关。我对HTTP协议的经验很少-也许有一些我不知道的解决方案?

    POST /post HTTP/1.1
    headers

    THIS IS WHAT WAS POSTEDlick = function() {
    ...
    }

    GET /index HTTP/1.1
    headers

    che-Control: max-age=0

这是c ++代码,几乎是从boost.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;
    }

如果可能,我不希望GET的邮件正文中收到任何内容 除了我想在POST中发布的内容。

编辑:这实际上可能与我如何处理缓冲区中接收的内容有关。请参见下面的示例:

    #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

但是奇怪的是它没有显示“ END”。

在一个更复杂的示例中,我得到的字符串甚至在文件名的末尾都有一部分。在此示例中为“ le.txt”。

1 个答案:

答案 0 :(得分:-1)

答案令人震惊:user207421有了解决方案。

Content-Length标头值是获取真实消息正文所需要的。

感谢所有花时间研究此问题的人。

PS:我帖子中的第二个问题仍然很奇怪,但与问题无关。