Ошибочная реализация http-сервера boost-beast sync - PullRequest
1 голос
/ 18 октября 2019

Я пытаюсь отобразить файл изображения в браузере через http-сервер boost beast. Используя пример из здесь я написал следующее:

#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;
}

Я пытаюсь скомпилировать его, используя

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

Но я не могу понятья получаю следующую ошибку:

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

В моем последнем сообщении мне сказали использовать буст-зверя. Это была моя попытка. Я пишу это только потому, что мой вопрос в основном кодовый, и мне не разрешено его публиковать, но я не могу описать его более подробно, потому что даже не могу понять источник ошибки.

Любая помощь / совет будет оценен. Ура!

...