c++linuxcpp-netlib

cpp-netlib hello world example wont compile on linux


I tried to run the hello world example on cpp-netlib.org using cpp-netlib-0.12.0 and boost-1.64.0 on Ubuntu 16.04. A piece of the code is (from line 1):

#include <boost/network/protocol/http/server.hpp>
#include <iostream>

namespace http = boost::network::http;

struct hello_world;
typedef http::server<hello_world> server;

struct hello_world
{
    void operator()(server::request const &request, server::response &response)
    {
        server::string_type ip = source(request);
        unsigned int port = request.source_port;
        std::ostringstream data;
        data << "Hello, " << ip << ':' << port << '!';
        response = server::response::stock_reply(server::response::ok, data.str());
    }
    void log(const server::string_type& message)
    {
        std::cerr << "ERROR: " << message << std::endl;
    }
};

and when I use the following line to compile:

g++ test1.cpp -o test1 -std=c++11 -lcppnetlib-uri -lcppnetlib-server-parsers -lcppnetlib-client-connections -lboost_system -lboost_thread -lpthread

I get the following error:

test1.cpp:11:61: error: ‘boost::network::http::server<hello_world>::response’ has not been declared
     void operator()(server::request const &request, server::response &response)
                                                             ^
test1.cpp: In member function ‘void hello_world::operator()(const request&, int&)’:
test1.cpp:17:28: error: ‘boost::network::http::server<hello_world>::response’ has not been declared
         response = server::response::stock_reply(server::response::ok, data.str());
                            ^
test1.cpp:17:58: error: ‘boost::network::http::server<hello_world>::response’ has not been declared
         response = server::response::stock_reply(server::response::ok, data.str());
                                                          ^

I took the code right out of the example from the website. I checked the include paths and all the necessary libraries appear to be there. I cant seem to figure out what the issue is.


Solution

  • It looks like documentation for cpp-netlib is all messed up (just like the whole project in general). HTTP server API page insists that Handler template parameter for http::server should have member operator () taking connection_ptr as second parameter (though it suggests wrong include):

    #include <boost/network/protocol/http/server.hpp>
    #include <boost/network/utils/thread_pool.hpp>
    
    struct handler_type;
    typedef boost::network::http::server<handler_type> http_server;
    
    struct handler_type
    {
        void
        operator ()
        (
            http_server::request const & request,
            http_server::connection_ptr  connection
        )
        {
            // do something here
        }
    };