Client requests not working from inside a class method (only work from main) #127

Closed
hiperion85 opened this Issue Jun 29, 2012 · 1 comment

Comments

Projects
None yet
2 participants

It is a strange issue the one I'm having. I'm making a proxy, so I need the server and client functionality. In the proxy.cpp (where the main funtion is) I have put the server code. Basically the same as in the tutorials from the example. I capture the request and depending of being GET or POST, I use the client functionality to make a request against an external server. If I put the client code directly into this file, it works, but if I encapsulate it in a class (ConsistentHashing class) it does not. The same code :S even more strange, the GET request from the client only works from inside the class code if I echo something after the request :S

I don't know what more to do, so I post the code. This is "proxy.cpp", where the main function is:

#include <boost/network/protocol/http/server.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/regex.hpp>
#include <boost/thread/thread.hpp>
#include "consistenthashing.hpp"

namespace http = boost::network::http;

// Defines the server
struct rest_handler;
typedef http::server<rest_handler> server;

// Defines de consistent hashing object
ConsistentHashing ch;

// Defines the request handler for the REST server.
// It's a class that defines two functions, `operator()` and `log()`.
struct rest_handler
{
    // This is the function that handles the incoming request
    void operator() (server::request const &request, server::response &response)
    {
        //server::string_type ip = source(request);
        server::string_type method = request.method;
        server::string_type path = request.destination;
        std::ostringstream data;

        try
        {
            if (method=="GET")
            {
                http::client::response actual_response = ch.get(path);
//              the following lines work perfectly, but if called from ch.get, they don't work :S
//              http::client client;
//              std::ostringstream url;
//              url << "http://" << "localhost" << ":" << "18000" << path;
//              http::client::request actual_request(url.str());
//              http::client::response actual_response = client.get(actual_request);
                data << body(actual_response);
            }
            else if (method=="POST")
            {
                http::client::response actual_response = ch.post(path, request.body);
//              the following lines work perfectly, but if called from ch.post, they don't work :S
//              http::client client;
//              std::ostringstream url;
//              url << "http://" << "localhost" << ":" << "18000" << path;
//              http::client::request actual_request(url.str());
//              http::client::response actual_response = client.post(actual_request, request.body);
                data << body(actual_response);
            }
        }
        catch (std::exception & e)
        {
            std::cerr << e.what() << std::endl;
            return;
        }

        response = server::response::stock_reply(server::response::ok, data.str());
    }

    // It's necessary to define a log function, but right now it's being ignored
    void log(...)
    {
        // do nothing
    }
};

int main(int argc, char * argv[])
{
    // Checks the number of arguments
    if (argc != 4)
    {
        std::cerr << "Usage: " << argv[0] << " server_list_file address port" << std::endl;
        return 1;
    }

    try
    {
        // Creates the request handler
        rest_handler handler;

        // Creates the server
        server server_(argv[2], argv[3], handler);

        // Runs the server
        server_.run();
    }
    catch (std::exception &e)
    {
        std::cerr << e.what() << std::endl;
        return 1;
    }
    return 0;
}

and this is the "consistenthashing.cpp" file. I'm not posting the .hpp file because it's just the declarations, and because all the code compiles well.

#include "consistenthashing.hpp"

// Constructor
ConsistentHashing::ConsistentHashing()
{
    serverList = new std::list<Server>;
}

void ConsistentHashing::addServer(std::string const ip, int port)
{
    //Server *s = new Server(ip, port);
    //serverList.
}

// these two next methods would store the consistent hashing logic
http::client::response ConsistentHashing::get(std::string const path)
{
    return httpGet("localhost", 18000, path);
}

http::client::response ConsistentHashing::post(std::string const path, std::string const data)
{
    return httpPost("localhost", 18000, path, data);
}


// these two next methods store the http get/post functionality

// this is the same code that was in the proxy.cpp file, but it called from here, it doesn't work
http::client::response ConsistentHashing::httpGet(std::string const ip, int const port, std::string const path)
{
    http::client client;
    std::ostringstream url;
    url << "http://" << ip << ":" << port << path;
    http::client::request request(url.str());
    http::client::response response = client.get(request);
// if printing this, then it does work :S
std::cout << "path(" << path << ") response(" << body(response) << ")" << std::endl;
    return response;
}

http::client::response ConsistentHashing::httpPost(std::string const ip, int const port, std::string const path, std::string const data)
{
    http::client client;
    std::ostringstream url;
    url << "http://" << ip << ":" << port << path;
    http::client::request request(url.str());
    http::client::response response = client.post(request, data);
// this code doesn't work, even if I print this line (unlike previous GET method). But in main, it does work :S why??
std::cout << "path(" << path << ") data(" << data << ")" << std::endl;  // "application/x-www-form-urlencoded"
    return response;
}

Your help would be pretty much appreciated. I've spent a lot of time with this.

Owner

deanberris commented Jul 5, 2013

Is this still an issue? Also, can we take this to the mailing list instead? I'm closing this issue out, please re-open with specific bugs and steps to reproduce along with compiler version, version of Boost, and platform.

@deanberris deanberris closed this Jul 5, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment