beast: Buffer_Body not working (message const char * "need buffer" 0x000070000939ad59)

When reporting a bug please include the following:

Version of Beast Release 1.74.0

Steps necessary to reproduce the problem


Testservice.cpp:
//Create a BoostWebserver
bool TestService::init()
{

    const char * crt_file = "/Users//Documents/testBoost/server.crt";
    const char * key_file = "/Users//Documents/testBoost/server.key";

    if (m_server)
    {
        BoostWeb::ServiceNode server_node;
        server_node.host = "127.0.0.1";
        
        server_node.port = 9000;
        //server_node.root = "f:/codes_vs2015/munu/munu/www";
        server_node.timeout = 6000;
        server_node.body_limit = 0;
        server_node.protocol = BoostWeb::support_protocol_t::protocol_all;
        if (!m_web_manager.init(this, &server_node, 1, true, crt_file, key_file, 1))
        {
            return (false);
        }
        
        return true;
    }

bool TestService::handle_request(const BoostWeb::HttpConnectionBase & connection, const BoostWeb::HttpRequestBase & request, BoostWeb::HttpResponseBase & response)
{
    const char * path = "D:\\test\\main.jpg";
    std::ifstream file(path, std::ios::binary);
    file.seekg(0, std::ios::end);
    std::streamsize size = file.tellg();
    file.seekg(0, std::ios::beg);
    
    std::string data(size, 0x0);
    file.read(&data[0], size);
    
    response.set_body(std::move(data));
    return (true);
}
//Whenever someone hits http://localhost:9000. I need to respond back with an buffer(main.jpg in this case) that I will be receiving from Testservice and to the client(browser) as buffer. main.jpg is displayed in the browser. The content is dynamic(buffer) can be any image that testservice will be receiving
In HttpUtility.h
    boost::beast::http::response<boost::beast::http::string_body, Fields> res;
       HttpRequest<Body, Fields> request(req);
            HttpResponse<boost::beast::http::string_body, Fields> response(res);
            service->handle_request(connection, request, response); // call Testservice to get the data. Response is string as mentioned above in handle_request method.
  auto f= response.get_body(); // f is the string data
   char char_array[200];
            
              // copying the contents of the
              // string to char array
   strcpy(char_array, f.c_str());
 http::response<http::buffer_body> res{http::status::ok, req.version()}; // Is this right way to define the buffer_body?
 res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
 res.set(http::field::content_type, "image/png");
 res.keep_alive(req.keep_alive());
 res.body().data = char_array; // Not working
 res.body().size = 200; //Not working
 res.prepare_payload();
 return send(std::move(res));

Output: str content: https::send error (5) {need buffer}

All relevant compiler information

Please let me know if I have missed anything.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16

Most upvoted comments

The file above is not a complete program. It would really help me if you could create the simplest possible complete program which demonstrates the issue. (often performing this exercise actually reveals the problem).