Boost ASIO:文档不完整

Boost ASIO : Document not complete

本文关键字:文档 ASIO Boost      更新时间:2023-10-16

我正在为Boost ASIO套接字侦听器使用以下模板代码:

#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main()
{
  try
  {
    boost::asio::io_service io_service;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 88));
    for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);
      std::string message = "<!DOCTYPE html><html><body><h1>HELLO WORLD</h1></body></html>";
      boost::system::error_code ignored_error;
      boost::asio::write(socket, boost::asio::buffer(message), boost::asio::transfer_all(), ignored_error);
    }
  } catch (std::exception& e) { std::cerr << e.what() << std::endl; }
  return 0;
}

当我在浏览器中查看它时,它运行良好。

但当我试图从另一个应用程序(如Node.JS中的HTTP.request)中读取它时,它失败了,并出现解析错误。如果我通过https://validator.w3.org,上面写着"I/O错误:由于非文档错误,无法确定结果"。

请参阅HTTP规范(RFC7230)第19页第3节

HTTP消息的格式为:

 HTTP-message   = start-line
                  *( header-field CRLF )
                  CRLF
                  [ message-body ]

进一步阅读将揭示起始行的限制和要求、标题的格式以及向接收方指示消息正文长度的要求(或在分块消息的情况下检测它的方法)

上面的代码格式如下:

[ message-body ]