Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C++/boost::asio/libcurl的简单代理-无法下载图像_C++_Boost_Curl_Boost Asio - Fatal编程技术网

使用C++/boost::asio/libcurl的简单代理-无法下载图像

使用C++/boost::asio/libcurl的简单代理-无法下载图像,c++,boost,curl,boost-asio,C++,Boost,Curl,Boost Asio,我正试图用以下代码实现一个非常简单的代理服务器。您将浏览器的代理设置为192.168.1.x:8080,可以通过代理访问网页 #include <ctime> #include <iostream> #include <string> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.h

我正试图用以下代码实现一个非常简单的代理服务器。您将浏览器的代理设置为192.168.1.x:8080,可以通过代理访问网页

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include <curl/curl.h>
#include <cstdlib>

using boost::asio::ip::tcp;

int port=8080;
//CURL *curl;
//CURLcode res;

static size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
    ((std::string*)stream)->append((char*)ptr, 0, size*count);
    return size*count;
}

class session{
public:
    session(boost::asio::io_service& io_service):socket_(io_service){
    }
    tcp::socket &socket(){
        return socket_;
    }
    void start(){
        socket_.async_read_some(boost::asio::buffer(data_,max_length),boost::bind(&session::handle_read,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
    }
private:
    void handle_read(const boost::system::error_code& error,size_t bytes_transferred){
        CURL *curl;
                CURLcode res;
        std::string response;
        std::string errorBuffer[CURL_ERROR_SIZE];
        std::string host="";
        std::string url="";

        if(!error){
            //boost::asio::async_write(socket_,boost::asio::buffer(data_,bytes_transferred),boost::bind(&session::handle_write,this,boost::asio::placeholders::error));
            printf("%s",data_);

            //parse data_:
            //std::string host="";
            //std::string url="";

            std::vector<std::string> split1;
            boost::split(split1, data_, boost::is_any_of("\r\n"));
            std::vector<std::string> split2;
            boost::split(split2,split1.at(0),boost::is_any_of(" "));
            std::cout<<"***"<<split2.at(0)<<"***"<<std::endl;
            if(split2.at(0).compare("GET")==0){
                printf("Get request recieved\n");
                url=split2.at(1);

                int i=0;
                for(i=1;i<split1.size();i++){
                    if(split1.at(i).compare("Host:")>0){
                        std::cout<<split1.at(i)<<std::endl;
                        std::vector<std::string> split3;
                        boost::split(split3,split1.at(i),boost::is_any_of(" "));
                        std::cout<<split3.at(1)<<std::endl;
                        host=split3.at(1);
                        break;
                    }   
                }   
            }

            //trim host to remove \r\n
            host.erase(host.find_last_not_of(" \n\r\t")+1);
            url.erase(url.find_last_not_of(" \n\r\t")+1);           

            //std::string response;
            //std::string errorBuffer[CURL_ERROR_SIZE];

            //CURL *curl;
            //CURLcode res;         
            curl=curl_easy_init();
            if(curl){
                curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
                curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
                curl_easy_setopt(curl, CURLOPT_HEADER, 0);
                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
                curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
                //curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);               

                std::cout<<errorBuffer<<std::endl;
                //curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
                //curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_to_string);
                //curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response);

                res=curl_easy_perform(curl);
                std::cout<<">>>"<<res<<std::endl;

                curl_easy_cleanup(curl);

                std::cout<<response<<std::endl;
                boost::asio::async_write(socket_,boost::asio::buffer(response,response.length()),boost::bind(&session::handle_write,this,boost::asio::placeholders::error));

                //curl_free(curl);

            }else{
                printf("Error: curl can't be init'd");
            }
        }else{
            printf("***handle_read: Error***\n");
            std::cout<<error<<std::endl;
            std::cout<<"EOH:"<<res<<std::endl;
            std::cout<<url<<std::endl;
            std::cout<<"EOH:"<<errorBuffer<<std::endl;
            delete this;
        }
    }
    void handle_write(const boost::system::error_code& error){
    if (!error){
        socket_.async_read_some(boost::asio::buffer(data_, max_length),boost::bind(&session::handle_read, this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
        socket_.cancel();
    }else{
        printf("***handle_write: Error***");
        std::cout<<error<<std::endl;
        delete this;
        }
    }

    tcp::socket socket_;
    enum { max_length = 1000000 };
    char data_[max_length];
};
class server
{
public:
    server(boost::asio::io_service& io_service, short port): io_service_(io_service),acceptor_(io_service,tcp::endpoint(tcp::v4(), port)){
        start_accept();
    }

private:
    void start_accept(){
        session* new_session = new session(io_service_);
        acceptor_.async_accept(new_session->socket(),boost::bind(&server::handle_accept, this, new_session,boost::asio::placeholders::error));
    }
    void handle_accept(session* new_session,const boost::system::error_code& error){
        if (!error){
            new_session->start();
        }else{
            delete new_session;
        }

        start_accept();
    }
    boost::asio::io_service& io_service_;
    tcp::acceptor acceptor_;
};




int main(){
    try{
        boost::asio::io_service io_service;

        //tcp_server write(io_service);
        server read(io_service,port);

        io_service.run();
    }
    catch (std::exception& e){
        std::cerr << e.what() << std::endl;
    }
    return 0;
}
我现在的问题是,如何使用async_write编写?我不确定要将操作系统转换成什么,以使其与异步写入兼容…

就像在上一样,您错误地使用了std::string

例如,以下代码:

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); 
假定&response是指向字符数组或缓冲区的指针。然而,它不是:它是指向std::string的指针,一个内部由实现定义的复杂对象

您应该使用它定义良好的API,引用您最喜欢的API

您会发现,使用这种C风格的cURL API和std::string并不是完全直观的。为什么不使用?

就像在上一样,您错误地使用了std::string

例如,以下代码:

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); 
假定&response是指向字符数组或缓冲区的指针。然而,它不是:它是指向std::string的指针,一个内部由实现定义的复杂对象

您应该使用它定义良好的API,引用您最喜欢的API


您会发现,使用这种C风格的cURL API和std::string并不是完全直观的。为什么不使用?

在回答后续问题时:我将使用async_write的重载,该重载采用streambuf:

boost::async_write( socket_, os.rdbuf(), boost::bind(&session::handle_write, this, boost::asio::placeholders::error ) )

在回答后续问题时:我将使用async_write的重载,该重载采用streambuf:

boost::async_write( socket_, os.rdbuf(), boost::bind(&session::handle_write, this, boost::asio::placeholders::error ) )

这个问题没有任何细节。这个问题没有任何细节。嘿,托马莱克,谢谢你的回答。我很想使用C++绑定,但是页面上没有下载链接!实际上,在sourceforge.net上找到了一个链接。。。你认为你可以在OP中编译代码吗?好的,但我认为rentacoder的标准不太高。。。我要用curlpp试试,一旦我得到了那该死的东西,我就回去报告!嘿,托马莱克,谢谢你的回复。我很想使用C++绑定,但是页面上没有下载链接!实际上,在sourceforge.net上找到了一个链接。。。你认为你可以在OP中编译代码吗?好的,但我认为rentacoder的标准不太高。。。我要用curlpp试试,一旦我得到了那该死的东西,我就回去报告!