C
C
calculator2122021-09-29 10:28:58
C++ / C#
calculator212, 2021-09-29 10:28:58

Why can't I get the whole post request body in c++ asio?

I'm trying to figure out the code of a small web server on asio link to the source . I had such a problem that I send a post request to this server with a small file in the body via postman, but only a small part of the request is accepted, I wanted to know why it is not possible to accept the entire request?

the part of the request i get

spoiler
POST / HTTP/1.1
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Postman-Token: 83e831df-1e8a-47e8-94b3-2403e4efff8b
Host: 127.0.0.1:1234
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------087172824010993708739017
Content-Length: 12397

----------------------------087172824010993708739017
Content-Disposition: form-data; name="image"; filename="clion.png"
Content-Type: image/png

�PNG


code for receiving and processing requests
spoiler
void HttpAcceptor::AcceptConnection()
{
  std::shared_ptr<asio::ip::tcp::socket> sock(new asio::ip::tcp::socket(m_IOService));

  m_Acceptor.async_accept(*sock.get(),
      [this, sock](const boost::system::error_code& ec)
      {
        if(ec.value() == 0){
          (new HttpService(sock))->HttpHandleRequest();
        }else{
          std::cout<<"Error occured, Error code = "<<ec.value()
                   <<" Message: "<<ec.message();
        }

        // accept next request if not stopped yet
        if(!m_IsStopped.load()){
          AcceptConnection();
        }else{
          //stop accepting incoming connection requests
          m_Acceptor.close();
        }
      });
}


//код обработки запроса
void HttpService::HttpHandleRequest()
{
  //read the request from client
  asio::async_read_until(*m_Socket.get(),
        m_Request, '\n',
        [this](const boost::system::error_code& ec, 
               std::size_t bytes_transfered)
         {
           //get string from read stream
           std::string request_line;
           std::istream request_stream(&m_Request);
           std::getline(request_stream, request_line, '\0');

           //parse the string and get HTTP request
           HttpRequestParser parser(request_line);
           std::shared_ptr<HttpRequest> http_request = parser.GetHttpRequest();
           std::cout<<"Request: {"<<std::endl;
           std::cout<<http_request->request;
           std::cout<<"}"<<std::endl;
           SendResponse();
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-09-29
@Rsa97

We look at the PNG format , the file header.
137 80 78 71 13 10 26 10
13 10 is nothing but \r\n.
And your code reads the request just before \n.
According to your mind, you need to read the headers until an empty string (\r\n\r\n) is encountered, then look for the Content-Length header and read the number of bytes specified in it.
Then, taking into account the Content-Type, restore the request from the read data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question