Erroneous implementation of boost-beast sync http server

空扰寡人 提交于 2019-12-11 10:55:53

问题


I'm trying to display an image file in the browser through a boost beast http server. Using the example from here I wrote the following:

#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <cstdlib>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/config.hpp>

using boost::asio::ip::tcp;
namespace http = boost::beast::http;

int main(){
    try{
        boost::asio::io_context io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));
        boost::system::error_code err;

        boost::beast::flat_buffer buffer;

        bool close = false;

        http::file_body::value_type body;
        std::string path = "./x.jpg";

        for (;;){
            tcp::socket socket(io_service);
            acceptor.accept(socket);

            body.open(path.c_str(), boost::beast::file_mode::scan, err);
            auto const size = body.size();

            http::request<http::string_body> req;
            http::read(socket, buffer, req, err);
            if(err)
                std::cerr << "read: " << err.message() << "\n"; 

            if(req.method() == http::verb::head){
                http::response<http::empty_body> res{http::status::ok, req.version()};
                res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
                res.set(http::field::content_type, "image/jpeg");
                res.content_length(size);
                res.keep_alive(req.keep_alive());
                http::write(socket, res, err);
            }

            http::response<http::file_body> res{std::piecewise_construct,
                std::make_tuple(std::move(body),
                std::make_tuple(http::status::ok, req.version()))};
            res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
            res.set(http::field::content_type, "image/jpeg");
            res.content_length(size);
            res.keep_alive(req.keep_alive());
            http::write(socket, res, err);
        }
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return 0;
}

I'm trying to compile it using

g++ -std=c++11 bs2.cpp -o bs2 -lpthread -lboost_system `pkg-config opencv --cflags --libs`

But I can't make sense of the following error I'm getting:

In file included from /usr/include/boost/beast/core/multi_buffer.hpp:15:0,
                 from /usr/include/boost/beast/core/buffered_read_stream.hpp:15,
                 from /usr/include/boost/beast/core.hpp:16,
                 from bs2.cpp:12:
/usr/include/boost/beast/core/detail/empty_base_optimization.hpp: In instantiation of ‘boost::beast::detail::empty_base_optimization<T, UniqueID, false>::empty_base_optimization(Arg1&&, ArgN&& ...) [with Arg1 = boost::beast::http::basic_file_body<boost::beast::file_posix>::value_type; ArgN = {std::tuple<boost::beast::http::status, unsigned int>}; T = boost::beast::http::basic_file_body<boost::beast::file_posix>::value_type; int UniqueID = 0]’:
/usr/include/boost/beast/http/message.hpp:908:51:   required from ‘boost::beast::http::message<<anonymous>, <template-parameter-1-2>, <template-parameter-1-3> >::message(std::piecewise_construct_t, std::tuple<_Args1 ...>&, boost::beast::detail::index_sequence<S ...>) [with BodyArgs = {boost::beast::http::basic_file_body<boost::beast::file_posix>::value_type, std::tuple<boost::beast::http::status, unsigned int>}; long unsigned int ...IBodyArgs = {0, 1}; bool isRequest = false; Body = boost::beast::http::basic_file_body<boost::beast::file_posix>; Fields = boost::beast::http::basic_fields<std::allocator<char> >; boost::beast::detail::index_sequence<S ...> = boost::beast::detail::integer_sequence<long unsigned int, 0, 1>]’
/usr/include/boost/beast/http/impl/message.ipp:299:35:   required from ‘boost::beast::http::message<<anonymous>, <template-parameter-1-2>, <template-parameter-1-3> >::message(std::piecewise_construct_t, std::tuple<_Args1 ...>) [with BodyArgs = {boost::beast::http::basic_file_body<boost::beast::file_posix>::value_type, std::tuple<boost::beast::http::status, unsigned int>}; bool isRequest = false; Body = boost::beast::http::basic_file_body<boost::beast::file_posix>; Fields = boost::beast::http::basic_fields<std::allocator<char> >]’
bs2.cpp:90:66:   required from here
/usr/include/boost/beast/core/detail/empty_base_optimization.hpp:81:40: error: no matching function for call to ‘boost::beast::http::basic_file_body<boost::beast::file_posix>::value_type::value_type(boost::beast::http::basic_file_body<boost::beast::file_posix>::value_type, std::tuple<boost::beast::http::status, unsigned int>)’
             std::forward<ArgN>(argn)...)
                                        ^
In file included from /usr/include/boost/beast/http/file_body.hpp:14:0,
                 from /usr/include/boost/beast/http.hpp:24,
                 from bs2.cpp:13:
/usr/include/boost/beast/http/basic_file_body.hpp:110:5: note: candidate: boost::beast::http::basic_file_body<File>::value_type::value_type(boost::beast::http::basic_file_body<File>::value_type&&) [with File = boost::beast::file_posix]
     value_type(value_type&& other) = default;
     ^~~~~~~~~~
/usr/include/boost/beast/http/basic_file_body.hpp:110:5: note:   candidate expects 1 argument, 2 provided
/usr/include/boost/beast/http/basic_file_body.hpp:107:5: note: candidate: constexpr boost::beast::http::basic_file_body<File>::value_type::value_type() [with File = boost::beast::file_posix]
     value_type() = default;
     ^~~~~~~~~~
/usr/include/boost/beast/http/basic_file_body.hpp:107:5: note:   candidate expects 0 arguments, 2 provided

On my last post I was told to use boost beast. This was my attempt. I'm only writing this because my question is mostly code and I'm not being allowed to post it but I can't be any more descriptive because I can't even understand the source of the error.

Any help/advice would be appreciated. Cheers!

来源:https://stackoverflow.com/questions/58447312/erroneous-implementation-of-boost-beast-sync-http-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!