Hi Dan,
Thank for you answer,
About the core dump, i think i found the issue, because in HTTP_POST, i used
CURLOPT_WRITEDATA and CURLOPT_WRITEHEADER to get data and header respone. But
in HTTP_GET, which reused connection, I did not init variable to handle
CURLOPT_WRITEHEADER (simple_test function).
I fixed but the connection still does not reuse, total time still high.
I try to test with google but it does not success, but the main flow is
correct, i just change the address.
Please help to review what I miss.
Thanks
Tuan Nguyen
________________________________
From: curl-library <curl-library-boun...@cool.haxx.se> on behalf of Dan
Fandrich via curl-library <curl-library@cool.haxx.se>
Sent: Tuesday, September 17, 2019 1:55 PM
To: curl-library@cool.haxx.se <curl-library@cool.haxx.se>
Cc: Dan Fandrich <d...@coneharvesters.com>
Subject: Re: Curl coredump after re-use connection
On Tue, Sep 17, 2019 at 05:01:45AM +0000, Nguyen Hoang Tuan (Hubble Vietnam)
via curl-library wrote:
> I am an newbie starting to use libcurl and trying to write an example to reuse
> http connection.
> Everything is ok when i just re call curl_easy_perform many times without any
> change.
> But when i try reuse connection from HTTP_POST to use in HTTP_GET, i got
> segmentation fault.
> The server address is same, just endpoint is difference.
>
> About the way I reuse connection, I just create a global variable 'curl', call
> easy init and passing it to post and get function.
>
> Libcurl version: 7.47.0
This version is over 3 years old. The first thing I'd suggest is trying a newer
version.
> I also got segmentation fault if i re-use the connection with difference
> server
> address.
>
> So what I should do and should not do when try to re-use connection ?
It should happen for the most part automatically. It's a common use case so I
would immediately suspect the application. Can you show us the code?
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
#include <stdio.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#define hlog_error printf
#define hlog_info printf
struct string {
char *ptr;
size_t len;
};
static size_t writefunc_response(void *ptr, size_t size, size_t nmemb, void *output)
{
struct string *s = (struct string *)output;
size_t new_len = s->len + size*nmemb;
s->ptr = realloc(s->ptr, new_len+1);
if (s->ptr == NULL)
{
fprintf(stderr, "writefunc_response realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
};
static void init_string(struct string *s)
{
s->len = 0;
s->ptr = malloc(s->len+1);
if (s->ptr == NULL)
{
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
s->ptr[0] = '\0';
}
static size_t writefunc_header(void *ptr, size_t size, size_t nmemb, void *output)
{
struct string *s = (struct string *)output;
size_t new_len = s->len + size*nmemb;
s->ptr = realloc(s->ptr, new_len+1);
if (s->ptr == NULL)
{
fprintf(stderr, "writefunc_header realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
};
static int _post_request(CURL *curl_t, char *address, struct curl_slist *headerIn, char *data,
void *headerOut, void *response, long timeout)
{
CURL *curl = curl_t;
CURLcode res = CURLE_FAILED_INIT;
if(!curl)
{
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
if(!curl)
{
curl_global_cleanup();
return res;
}
}
else
{
printf("reuse\n");
}
if(headerIn)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerIn);
}
curl_easy_setopt(curl, CURLOPT_URL, address);
if(headerOut)
{
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, writefunc_header);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerOut);
}
if(response)
{
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
}
if(data)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
}
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
// curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs.pem");
// curl_easy_setopt(curl, CURLOPT_CAPATH, CERTIFICATE_PATH);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//debug
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
{
hlog_error("curl_easy_perform() failed: %d: %s\n",
res, curl_easy_strerror(res));
}
double namelookup;
double connect;
double app_connect;
double pretransfer;
double redirect;
double starttransfer;
double total;
/* ask for the content-type */
curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &namelookup);
curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &connect);
curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME, &app_connect);
curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &pretransfer);
curl_easy_getinfo(curl, CURLINFO_REDIRECT_TIME, &redirect);
curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &starttransfer);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
hlog_info("\n");
hlog_info("time_namelookup: %.1f\n", namelookup);
hlog_info("time_connect: %.1f\n", connect);
hlog_info("time_appconnect: %.1f\n", app_connect);
hlog_info("time_pretransfer: %.1f\n", pretransfer);
hlog_info("time_redirect: %.1f\n", redirect);
hlog_info("time_starttransfer: %.1f\n", starttransfer);
hlog_info("time_total: %.1f\n", total);
if(!curl_t)
{
printf("free\n");
curl_easy_cleanup(curl);
curl_global_cleanup();
}
else{
printf("do not free\n");
}
return res;
}
int VR_post_request(char *address, struct curl_slist *headerIn, char *data,
char **header, char **response, long timeout, void *connection)
{
if(!address || !response)
{
hlog_error("address or response is null\n");
return -1;
}
hlog_info("post_request infor: address %s, data %s\n", address, data);
int res;
struct string httpHeader;
init_string(&httpHeader);
struct string httpResponse;
init_string(&httpResponse);
res = _post_request(connection, address, headerIn, data,
(void *)&httpHeader, (void *)&httpResponse, timeout);
if(header)
{
*header = httpHeader.ptr;
}
else
{
free(httpHeader.ptr);
}
if(response)
{
*response = httpResponse.ptr;
}
else
{
free(httpResponse.ptr);
}
return res;
}
static int _get_request(CURL *curl_t, char *address, struct curl_slist *headerIn, char *data,
void *headerOut, void *response, long timeout)
{
CURL *curl = curl_t;
CURLcode res = CURLE_FAILED_INIT;
if(!curl)
{
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
if(!curl)
{
curl_global_cleanup();
return res;
}
}
if(headerIn)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerIn);
}
curl_easy_setopt(curl, CURLOPT_URL, address);
if(headerOut)
{
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, writefunc_header);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerOut);
}
if(response)
{
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
}
if(data)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
}
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
// curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
// curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs.pem");
// curl_easy_setopt(curl, CURLOPT_CAPATH, CERTIFICATE_PATH);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//debug
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
{
hlog_error("curl_easy_perform() failed: %d: %s\n",
res, curl_easy_strerror(res));
}
double namelookup;
double connect;
double app_connect;
double pretransfer;
double redirect;
double starttransfer;
double total;
/* ask for the content-type */
curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &namelookup);
curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &connect);
curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME, &app_connect);
curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &pretransfer);
curl_easy_getinfo(curl, CURLINFO_REDIRECT_TIME, &redirect);
curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &starttransfer);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
hlog_info("\n");
hlog_info("time_namelookup: %.1f\n", namelookup);
hlog_info("time_connect: %.1f\n", connect);
hlog_info("time_appconnect: %.1f\n", app_connect);
hlog_info("time_pretransfer: %.1f\n", pretransfer);
hlog_info("time_redirect: %.1f\n", redirect);
hlog_info("time_starttransfer: %.1f\n", starttransfer);
hlog_info("time_total: %.1f\n", total);
if(!curl_t)
{
printf("free\n");
curl_easy_cleanup(curl);
curl_global_cleanup();
}
else
{
printf("do not free\n");
}
return res;
}
/*
need free response, header after using
*/
int VR_get_request(char *address, struct curl_slist *headerIn, char *data,
char **headerOut, char **response, long timeout, void *connection)
{
if(!address || !response)
{
hlog_error("address or response is null\n");
return -1;
}
hlog_info("get_request infor: address %s, data %s\n", address, data);
int res;
struct string httpHeader;
init_string(&httpHeader);
struct string httpResponse;
init_string(&httpResponse);
res = _get_request(connection, address, headerIn, data,
(void *)&httpHeader, (void *)&httpResponse, timeout);
if(headerOut)
{
*headerOut = httpHeader.ptr;
}
else
{
free(httpHeader.ptr);
}
if(response)
{
*response = httpResponse.ptr;
}
else
{
free(httpResponse.ptr);
}
return res;
}
int simple_test(char *address, struct curl_slist *headerIn, char **response, long timeout, void *connection)
{
int https_ret = 0;
hlog_info("get_request infor: address %s\n", address);
CURL *curl = (CURL*)connection;
CURLcode res = CURLE_FAILED_INIT;
if(!curl)
{
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
if(!curl)
{
curl_global_cleanup();
return res;
}
}
else
{
printf("reuse\n");
}
struct string httpResponse;
init_string(&httpResponse);
if(headerIn)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerIn);
}
curl_easy_setopt(curl, CURLOPT_URL, address);
if(response)
{
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &httpResponse);
}
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
// curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs.pem");
// curl_easy_setopt(curl, CURLOPT_CAPATH, CERTIFICATE_PATH);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
// curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//debug
/* Perform the request, res will get the return code */
printf("start perform\n");
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
{
hlog_error("curl_easy_perform() failed: %d: %s\n",
res, curl_easy_strerror(res));
}
else
{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &https_ret);
if(https_ret != 200)
{
res = -1;
}
}
if(response)
{
*response = httpResponse.ptr;
}
else
{
free(httpResponse.ptr);
}
double namelookup;
double connect;
double app_connect;
double pretransfer;
double redirect;
double starttransfer;
double total;
/* ask for the content-type */
curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &namelookup);
curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &connect);
curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME, &app_connect);
curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &pretransfer);
curl_easy_getinfo(curl, CURLINFO_REDIRECT_TIME, &redirect);
curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &starttransfer);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
hlog_info("\n");
hlog_info("time_namelookup: %.1f\n", namelookup);
hlog_info("time_connect: %.1f\n", connect);
hlog_info("time_appconnect: %.1f\n", app_connect);
hlog_info("time_pretransfer: %.1f\n", pretransfer);
hlog_info("time_redirect: %.1f\n", redirect);
hlog_info("time_starttransfer: %.1f\n", starttransfer);
hlog_info("time_total: %.1f\n", total);
if(!connection)
{
printf("free\n");
curl_easy_cleanup(curl);
curl_global_cleanup();
}
else
{
printf("do not free\n");
}
return res;
}
static void init_header(struct curl_slist **headers, const char* format, ...)
{
int rc = 0;
char data[256];
va_list argptr;
va_start(argptr, format);
rc = vsprintf(data, format, argptr);
va_end(argptr);
if(rc < 0)
{
return;
}
*headers = curl_slist_append(*headers, data);
}
int main()
{
char address[512];
char req_content[512];
CURL *curl;
curl = curl_easy_init();
char *result = NULL;
struct curl_slist *headers = NULL;
init_header(&headers, "Content-Type: application/json");
init_header(&headers, "Connection: keep-alive");
snprintf(req_content, sizeof(req_content),
"[\"id\":\"hello\"]");
snprintf(address, sizeof(address), "https://google.com");
VR_post_request(address, headers, req_content, NULL, &result, 7, curl);
printf("result = %s\n", result);
free(result);
simple_test(address, NULL, &result, 7, curl);
printf("result = %s\n", result);
free(result);
}
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html