libcurl crashing program after execution? - c++

I am using libcurl to make request google server. The curl library is giving output , when i use curl variables as global every thing works fine but when i am using the curl variables as local inside a function or a class private members the program crashes. I am not able to catch where segmentation fault occurring.
Code is
void func1();
{
Internet_connection_through_curl internet_connection;
internet_connection.simple_program(query_url);
}
Internet_connection_through_curl::simple_program(string url)
{
CURL *curle;
CURLcode rese;
curle = curl_easy_init();
if(curle)
{
curl_easy_setopt(curle, CURLOPT_URL, url.c_str());
curl_easy_setopt(curle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curle, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curle, CURLOPT_WRITEDATA , (void *)(&UF));
curl_easy_setopt(curle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curle, CURLOPT_HEADERFUNCTION, HeaderMemoryCallback);
rese = curl_easy_perform(curle);
fprintf(stderr , "Curl easy perform error----> %s\n" , curl_easy_strerror(rese));
if(rese != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(rese));
curl_easy_cleanup(curle);
}
}

Related

I cannot send an email using curl and c++

im trying to send an email with curl and c++ just like this example but when i execute the program:
#include <iostream>
#include <curl/curl.h>
int main()
{
int a;
char errbuf[CURL_ERROR_SIZE] = {0};
CURL *curl = curl_easy_init();
CURLcode res;
struct upload_status upload_ctx = { 0 };
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465");
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "");
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, "");
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_CAINFO, "D:\\MinGW\\cacert.pem");
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
check_errors(res, errbuf);
return 0;
}
i get this error:
curl_easy_perform() failed: Failed sending data to the peer
libcurl: (55) MAIL failed: 530
I deleted most of the code because i couldn't post the question but the rest is in the example.
The problem i had in my code was that i thought that by using:
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "myemail#example.com"); i was specifyng the username, which was in fact, an error.
To specify the username libcurl has another option:
curl_easy_setopt(curl, CURLOPT_USERNAME, "myemail#example.com");

I try to implement curl script for request to server but it is not working

I have a curl script:
curl -k -H "Content-Type: application/txt" -X POST --data-binary "#name_file.txt" -g "https://ya.com/file.txt" > .\out.txt
I can use it via console ant it works good.
I implement this script in visual studio on C++:
auto curl = curl_easy_init();
if (curl)
{
FILE* fp = fopen(path_to_file.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, "https://ya.com/file.txt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Content-Type: application/txt");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); // - H
std::string data_for_send = data_for_send_arr.c_str();
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data_for_send.size());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_for_send.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callbackfunction);
res = curl_easy_perform(curl);
if (res != CURLE_OK) AfxMessageBox(_T("error"));
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(fp);
}
size_t callbackfunction(void *ptr, size_t size, size_t nmemb, void* userdata)
{
FILE* stream = (FILE*)userdata;
if (!stream)
{
printf("!!! No stream\n");
return 0;
}
size_t written = fwrite((FILE*)ptr, size, nmemb, stream);
return written;
}
i also i need to tell that at one moment i use async call:
async(func_curl_1, data_1);
async(func_curl_2, data_2);
each function create curl and do request for server at the same time.
but this script do not work. it work one time from seven request
server returned for me empty file or broken file.
i do not know why...
try to add in your settings CURLOPT_VERBOSE
i.e.
/* ask libcurl to show us the verbose output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
and investigate output

Unable to delete email from gmail inbox

Hello I wrote the following code
string fetchdata;
size_t write_data(char* buf, size_t size, size_t nmemb, void* up) {
fetchdata.append((char*)buf, size*nmemb);
return size*nmemb;
}
void main()
{
CURL *curl;
CURLcode res = CURLE_OK;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_USERNAME, "ghtyyuu3#gmail.com");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxxxxx");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com:993/INBOX;UID=1");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UID STORE 1 +Flags \DELETE");
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
std::ofstream outfile("/home/ilia/Desktop/fetched.txt", std::ios_base::app);
outfile << fetchdata;
outfile.close();
}
I manage to read the mail into a text file but I am unable to delete the mail (gmail settings to delete the mail already set)
I probably have an error in:
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UID STORE 1 +Flags \DELETE");
While the CURL itself dont give me an error the mail is still not deleted
Please help

C++ | Show progress bar with curl?

I am wondering how can I add a progress bar for curl, below is my code, it works 100% I just want to know how to add a progress bar (C++) maybe like 10% > 30% > 100% (WINDOWS OS)
CURL *curl;
FILE *fp;
CURLcode res;
char *url= "http://ipv4.download.thinkbroadband.com/5MB.zip";
char outfilename[FILENAME_MAX] = "./ATNA/5MB.zip";
curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW);
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

C++ Libcurl can't clear headers

I have the following curl function which executes in a loop:
curl = curl_easy_init();
if (curl) {
CurlResponse = "";
host = "http://exaple.com";
LibcurlHeaders = curl_slist_append(LibcurlHeaders, "Expect:");
curl_easy_setopt(curl, CURLOPT_URL, (host).c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, SSLPath.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
curl_easy_setopt(curl, CURLOPT_VERBOSE, CurlVerbose);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, CurlPostData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, LibcurlResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &CurlResponse);
res = curl_easy_perform(curl);
curl_slist_free_all(LibcurlHeaders); <----------
if (res != CURLE_OK) {
LibcurlError(curl_easy_strerror(res), host);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
Everything works fine when I remove the line:
curl_slist_free_all(LibcurlHeaders);
However, in the Libcurl Docs it shows to use it like I do. At least thats how I understand it...
So what am I doing wrong and/or missing?
Thanks for your answers
* EDIT *
So, basically:
LibcurlHeaders = null:
Curl call with headers
// cant clear headers
Curl call with 2 headers
// can't clear headers
Curl call with 3 headers
// can't clear headers
What I want is that the headers used in the curl call are cleared when the curl call is done so I get:
LibcurlHeaders = null:
Curl call with headers
Headers cleared
Curl call with headers
Headers cleared
Curl call with headers
Headers cleared
You said in comments that you are calling curl in a loop. Your example does not show that. But assuming the code you did show is inside of larger code that is actually in a loop, you just need to make sure that your LibcurlHeaders variable is NULL before calling curl_slist_append() for the first time of each new HTTP request, eg:
curl = curl_easy_init();
if (curl) {
CurlResponse = "";
host = "http://exaple.com";
LibcurlHeaders = NULL; // <-- HERE
LibcurlHeaders = curl_slist_append(LibcurlHeaders, "Expect:");
/* alternatively, and ultimately safer and more accurate:
LibcurlHeaders = curl_slist_append(NULL, "Expect:");
*/
curl_easy_setopt(curl, CURLOPT_URL, host.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, SSLPath.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
curl_easy_setopt(curl, CURLOPT_VERBOSE, CurlVerbose);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, CurlPostData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, LibcurlResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &CurlResponse);
res = curl_easy_perform(curl);
curl_slist_free_all(LibcurlHeaders);
LibcurlHeaders = NULL; // <-- FOR GOOD MEASURE!
if (res != CURLE_OK) {
LibcurlError(curl_easy_strerror(res), host);
}
curl_easy_cleanup(curl);
}
Wauw,
I managed to fix the issue. It seems to be a bug with Libcurl which can be fixed rather easily.
The problem was that I declared struct curl_slist *LibcurlHeaders=NULL; globally.
When I declared struct curl_slist *LibcurlHeaders=NULL; just before each curl_easy_init(); call and removed the global declaration, the curl_slist_free_all(LibcurlHeaders); worked and didn't crash my program anymore!
Short:
The problem was the global declaration of struct curl_slist *LibcurlHeaders=NULL; which had to be declared in front of each curl_easy_init(); function.