GithubHelp home page GithubHelp logo

Comments (10)

JosephP91 avatar JosephP91 commented on August 21, 2024

@typedeph I will put a get_info method in the next commit. Thank you!

from curlcpp.

JosephP91 avatar JosephP91 commented on August 21, 2024

@typedeph I just pushed a commit with get_info method for curl_easy interface. You will find an example in the README.md and in tests/easy_info.cpp. Let me know if everything works.

from curlcpp.

AlexGoico avatar AlexGoico commented on August 21, 2024

I just went from:

      long code;
      curl_easy_getinfo(easy.get_curl(), CURLINFO_RESPONSE_CODE, &code);

to:

      long code = easy.get_info<long>(CURLINFO_RESPONSE_CODE).get().second;

Will probably split it along two lines for better readibility. Thanks, great job with the wrapper.

from curlcpp.

AlexGoico avatar AlexGoico commented on August 21, 2024

Seems I was either wrong or lucky. The code I used before ran but for some reason the same encapsulated function will no longer run. I'm still a newbie at c++ but I think the reason is because my get function is dereferencing a dangling pointer in curl_easy. Here is my method with cout's to trace the seg fault:

  // the default parameter is included for more specificity but it is declared in the corresponding header for this function
  std::string get(const std::string& url, 
                            const std::string& ua = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/48.0.2564.116 Safari/537.36")
  {
    using namespace curl;
    std::ostringstream str;
    curl_ios<std::ostringstream> writer{str};

    curl_easy easy{writer};
    easy.add<CURLOPT_URL>(url.c_str());
    easy.add<CURLOPT_ACCEPT_ENCODING>("");
    easy.add<CURLOPT_FOLLOWLOCATION>(1L);
    easy.add<CURLOPT_USERAGENT>(ua.c_str());
    try {
      easy.perform();
      std::cout << "Getting info..." << std::endl;
      auto c = easy.get_info<long>(CURLINFO_RESPONSE_CODE);
      std::cout << "Getting pair..." << std::endl;
      auto p = c.get();
      std::cout << "Getting code..." << std::endl;
      long code = p.second;
      std::cout << "Code: " << code << std::endl;
      if (code < 200 || code >= 300)
        throw RequestException(url + " returned status code " +
                               std::to_string(code));
    }
    catch (curl_easy_exception error) {
      error.print_traceback();
    }
    catch (...) {
      std::cerr << "An unexepcted exception occurred" << std::endl;
    }
    return str.str();
  }

My code seg faults for http://www.google.com/ and other urls after printing "Getting pair...".
I am thinking this is because the pointer on line 1017 of curl_easy points to stack allocated data assigned at line 1018 and then this is overridden when the function returns. The curl_easy_info object will then have a dangling pointer that will seg fault when I dereference it in an attempt try to create the pair at line 61.

from curlcpp.

JosephP91 avatar JosephP91 commented on August 21, 2024

@typedeph you're right. The bug should be fixed with the last commit. Do a git pull and let me know! You can remove the part where you obtain the pair object. Pair has been removed. You will get just the variable containing the code you are trying to print.

from curlcpp.

JosephP91 avatar JosephP91 commented on August 21, 2024

It should be fixed now.

from curlcpp.

AlexGoico avatar AlexGoico commented on August 21, 2024

Sorry for the late response, I was not able to get on my Linux machine until now. I did want to confirm that it is fixed though.

from curlcpp.

JosephP91 avatar JosephP91 commented on August 21, 2024

Good! Thank you!

from curlcpp.

ronny332 avatar ronny332 commented on August 21, 2024

Hi,

may be I do something wrong, but with the latest version from github a usage of

auto code = easy.get_info<long>(CURLINFO_RESPONSE_CODE);

ends in

error: no matching function for call to ‘curl::curl_easy::get_info(CURLINFO)’ note: candidate: template<CURLINFO Info> const curl::curl_easy_info<typename curl::detail_info::info_t<<anonymous> >::type> curl::curl_easy::get_info() template<CURLINFO Info> const curl_easy_info<detail_info::Info_type<Info>> get_info();

Were there any changes, which are making the usage of get_info impossible?

EDIT:
ok, this is working:
auto code = easy.get_info<CURLINFO_RESPONSE_CODE>();

from curlcpp.

JosephP91 avatar JosephP91 commented on August 21, 2024

Good @ronny332

from curlcpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.