From 0e8e63c8199417d9c920e1a6b9a2a90ef4e595de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Wed, 25 Jan 2023 21:16:56 +0800 Subject: Writes/reads to/from a cookie file --- interchange.c | 13 +++++++++++-- interchange.h | 4 +++- request.c | 4 +++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/interchange.c b/interchange.c index a8ba970..5219474 100644 --- a/interchange.c +++ b/interchange.c @@ -8,6 +8,7 @@ char *image_dir; emscripten_fetch_attr_t attr; #else char *sampleurl; +char *cookiefile = NULL; char *cainfo = NULL; #endif @@ -15,7 +16,8 @@ char *cainfo = NULL; extern void handle_results(interchange_response *); #endif -void interchange_init(const char *url, const char *dir, const char *certificate) +void interchange_init(const char *url, const char *dir, const char *cookie, + const char *certificate) { image_dir = malloc(strlen(dir) + 1); strcpy(image_dir, dir); @@ -30,6 +32,10 @@ void interchange_init(const char *url, const char *dir, const char *certificate) if (append) strcat(sampleurl, "/"); curl_global_init(CURL_GLOBAL_SSL); + if (cookie) { + cookiefile = malloc(strlen(cookie) + 1); + strcpy(cookiefile, cookie); + } if (certificate) { cainfo = malloc(strlen(certificate) + 1); strcpy(cainfo, certificate); @@ -106,7 +112,10 @@ void interchange_cleanup() { free(image_dir); #ifndef __EMSCRIPTEN__ - free(cainfo); + if (cainfo) + free(cainfo); + if (cookiefile) + free(cookiefile); free(sampleurl); curl_global_cleanup(); #endif diff --git a/interchange.h b/interchange.h index 91a2222..7503edf 100644 --- a/interchange.h +++ b/interchange.h @@ -40,9 +40,11 @@ extern "C" { * \brief A function that needs to be run first. * \param sampleurl The value of the SAMPLEURL setting in products/variable.txt. * \param image_dir The value of the IMAGE_DIR setting in products/variable.txt. + * \param certificate Path to the cookie file. * \param certificate Path to the CA certificate file. */ -void interchange_init(const char *sampleurl, const char *image_dir, const char *certificate); +void interchange_init(const char *sampleurl, const char *image_dir, + const char *cookie, const char *certificate); /*! * \brief For fetching data about products that belong a specific group. diff --git a/request.c b/request.c index c969eeb..eedb597 100644 --- a/request.c +++ b/request.c @@ -18,6 +18,7 @@ extern emscripten_fetch_attr_t attr; extern char *sampleurl; extern char *cainfo; +extern char *cookiefile; struct container { struct curl_httppost *post; @@ -148,7 +149,8 @@ void request(void (*handler)(interchange_response *), void (*callback)(void *), #else CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); + curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookiefile); + curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookiefile); if (cainfo) curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo); #ifdef DEBUG -- cgit v1.2.3