Comments (10)
@typedeph I will put a get_info method in the next commit. Thank you!
from curlcpp.
@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.
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.
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.
@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.
It should be fixed now.
from curlcpp.
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.
Good! Thank you!
from curlcpp.
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.
Good @ronny332
from curlcpp.
Related Issues (20)
- warnings when building
- NOEXCEPT
- Release:1.2 Cmake fails. HOT 15
- compile warning: invalid use of incomplete type HOT 2
- curl_easy_exception doesn't set any message into std::exception HOT 1
- Build of 1.4 fail with mingw32 in win10 and curl 7.74.0 HOT 12
- Can't link on windows HOT 3
- Singleton initializer issue HOT 1
- replece curlcpp-master/include to curlcpp-master/include/curlcpp
- Can't link with a project with Dynamic CRT (VS2019)
- initialising order warnings HOT 1
- Is a new tag/release coming soon? HOT 1
- build fail with curl 7.83.0 HOT 2
- Static build? HOT 6
- Deprecation warnings with latest curl version
- How to get response content string when using curl_multi HOT 1
- Version inside CMake looks out of date HOT 1
- suggestion libev
- Titles in README.md mismatch
- BUILD_SHARED_LIBS non-standard usage.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from curlcpp.