aboutsummaryrefslogtreecommitdiff
path: root/src/net.c
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2020-08-01 21:38:07 +0200
committerJan200101 <sentrycraft123@gmail.com>2020-08-01 21:38:07 +0200
commitc11dd0eae09df4982459a34d764910d0501f2ae2 (patch)
tree940934202f436d67f94728c0704a19ac62f5c299 /src/net.c
parent1db300d8815b5f997dd6f992e4ca416537018c2f (diff)
downloadpolecat-c11dd0eae09df4982459a34d764910d0501f2ae2.tar.gz
polecat-c11dd0eae09df4982459a34d764910d0501f2ae2.zip
Improve code structure […]
- create a dedicated "usage" macro to reuse - add various checks in the case no memory is available - fix cwd fetching check - correctly free json_objects to remove all existing leaks
Diffstat (limited to 'src/net.c')
-rw-r--r--src/net.c68
1 files changed, 37 insertions, 31 deletions
diff --git a/src/net.c b/src/net.c
index f7dc481..a1c574a 100644
--- a/src/net.c
+++ b/src/net.c
@@ -35,42 +35,45 @@ struct MemoryStruct* downloadToRam(const char* URL)
struct MemoryStruct* chunk = malloc(sizeof(struct MemoryStruct));
- chunk->memory = malloc(1);
- chunk->size = 0;
+ if (chunk)
+ {
+ chunk->memory = malloc(1);
+ chunk->size = 0;
- curl_global_init(CURL_GLOBAL_ALL);
+ curl_global_init(CURL_GLOBAL_ALL);
- curl_handle = curl_easy_init();
+ curl_handle = curl_easy_init();
- curl_easy_setopt(curl_handle, CURLOPT_URL, URL);
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, memoryCallback);
- curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)chunk);
- curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT);
- curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
+ curl_easy_setopt(curl_handle, CURLOPT_URL, URL);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, memoryCallback);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)chunk);
+ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT);
+ curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
- res = curl_easy_perform(curl_handle);
+ res = curl_easy_perform(curl_handle);
- long http_code = 0;
- curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
+ long http_code = 0;
+ curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
- if(res != CURLE_OK) {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- return NULL;
- }
- else if (http_code != 200)
- {
- fprintf(stderr, "Server didn't respond as expected [HTTP Error %li]\n", http_code);
- return NULL;
- }
+ if(res != CURLE_OK) {
+ fprintf(stderr, "curl_easy_perform() failed: %s\n",
+ curl_easy_strerror(res));
+ return NULL;
+ }
+ else if (http_code != 200)
+ {
+ fprintf(stderr, "Server didn't respond as expected [HTTP Error %li]\n", http_code);
+ return NULL;
+ }
- curl_easy_cleanup(curl_handle);
- curl_global_cleanup();
+ curl_easy_cleanup(curl_handle);
+ curl_global_cleanup();
+ }
return chunk;
}
-int downloadFile(const char* URL, const char* path)
+void downloadFile(const char* URL, const char* path)
{
struct MemoryStruct* chunk = downloadToRam(URL);
@@ -83,18 +86,21 @@ int downloadFile(const char* URL, const char* path)
free(chunk->memory);
free(chunk);
}
-
- return 0;
}
struct json_object* fetchJSON(const char* URL)
{
struct MemoryStruct* chunk = downloadToRam(URL);
- struct json_object* json = json_tokener_parse((char*)chunk->memory);
+ if (chunk)
+ {
+ struct json_object* json = json_tokener_parse((char*)chunk->memory);
- free(chunk->memory);
- free(chunk);
+ free(chunk->memory);
+ free(chunk);
+
+ return json;
+ }
- return json;
+ return NULL;
} \ No newline at end of file