Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_info(CURLINFO_COOKIELIST) not referenced #43

Closed
tucanus opened this issue May 14, 2015 · 3 comments
Closed

get_info(CURLINFO_COOKIELIST) not referenced #43

tucanus opened this issue May 14, 2015 · 3 comments

Comments

@tucanus
Copy link

tucanus commented May 14, 2015

Before all, thank you for your work, very useful.
I built your library on Ubuntu 14.04, using libcurl 7.35 (after removing line 276 of curl_easy.h, because it caused compiling error).
Then I moved header files in /usr/include/curlcpp, libcurlcpp.a in /usr/lib.

Source code of the simple test program is

#include <curlcpp/curl_easy.h>
using curl::curl_easy;

int main(int argc, const char **argv) {
    curl_easy easy;
    easy.add(curl_pair<CURLoption,string>(CURLOPT_URL,"http://www.google.it") );
    easy.add(curl_pair<CURLoption,long>(CURLOPT_FOLLOWLOCATION,1L));
    easy.add(curl_pair<CURLoption,string>(CURLOPT_COOKIEFILE,""));
    try {
        easy.perform();
        auto pippo = easy.get_info(CURLINFO_COOKIELIST);
        for(auto item : *pippo)
        {
            std::cout << item << ", " << std::endl;
        }
        std::cout << std::endl;
    } catch (curl_easy_exception &error) {
        // If you want to get the entire error stack we can do:
        std::vector<std::pair<std::string,std::string>> errors = error.get_traceback();
        // Otherwise we could print the stack like this:
        error.print_traceback();
        // Note that the printing the stack will erase it
    }
    std::cin.get();
    return 0;
}

I compiled succesfully (g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"), but linking (g++ -o "testCurlCpp" ./main.o -lcurl -lcurlcpp) fails saying

main.cpp:15: undefined reference to "curl::curl_easy::get_info(CURLINFO) const".
Removing line 15 all works fine.
Thank you.

@JosephP91
Copy link
Owner

@tucanus Hi and thank you for using curlcpp!
Try to change this line:

auto pippo = easy.get_info(CURLINFO_COOKIELIST);

with this:

auto pippo = easy.get_info<vector<string>>(CURLINFO_COOKIELIST);

get_info is a template method in which you have to specify the type. Let me know!

@tucanus
Copy link
Author

tucanus commented May 15, 2015

Sorry, it is true, my mistake.
Ok, now it compiles, but does not work, get_info throws an exception and error.print_traceback() shows
ERROR: A libcurl function was given a bad argument ::::: FUNCTION: get_info<std::vector<std::basic_string<char> > >

I suspect code of get_info being incorrect (curl_easy_getinfo third parameter shoul be struct curl_slist **, you should check ptr != nullptr before applying ->next field, and should not infer that struct curl_slist elements being of same dimensions, hence I suggest to correct get_info this way (it works on my PC ;)

// Putting the namespace here will avoid the "specialization in different namespace" error.
namespace curl {
    template<> std::unique_ptr<std::vector<std::string>> curl_easy::get_info(const CURLINFO info) const {
        struct curl_slist *ptr = nullptr;
        const CURLcode code = curl_easy_getinfo(this->curl,info,&ptr);
        if (code != CURLE_OK) {
            curl_slist_free_all(ptr);
            throw curl_easy_exception(code,__FUNCTION__);
        }
        std::vector<std::string> infos;

        struct curl_slist *list = ptr;
        while (list != nullptr) {
            infos.push_back(std::string(list->data));
            list = list->next;
        }
        curl_slist_free_all(ptr);
        return std::unique_ptr<std::vector<std::string>>{new std::vector<std::string>(infos)};
    }
}

Thank you for your support and your time.

@JosephP91
Copy link
Owner

@tucanus Yeah, right! Now it works also on my environment! Thank you! If everything works, you can close the issue. For anything else, let me know! I will update the repo as soon as possible.

@tucanus tucanus closed this as completed May 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants