diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2022-06-06 22:03:57 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2022-06-08 20:06:12 +0200 |
commit | d5d61d18c89af3f6743b7c56774eebdfdcc87b2c (patch) | |
tree | b4299c1af7e194e9083d4de1bce382102ea46e95 /src/net.c | |
download | OFQT-d5d61d18c89af3f6743b7c56774eebdfdcc87b2c.tar.gz OFQT-d5d61d18c89af3f6743b7c56774eebdfdcc87b2c.zip |
Release 0.1.00.1.0
Diffstat (limited to 'src/net.c')
-rw-r--r-- | src/net.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/net.c b/src/net.c new file mode 100644 index 0000000..0ae8e23 --- /dev/null +++ b/src/net.c @@ -0,0 +1,128 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> +#include <curl/curl.h> +#include <json.h> + +#include "net.h" +#include "fs.h" + +#define USER_AGENT NAME "/" VERSION + +void net_init() +{ + curl_global_init(CURL_GLOBAL_ALL); +} + +void net_deinit() +{ + curl_global_cleanup(); +} + +static inline size_t memoryCallback(const void* contents, size_t size, size_t nmemb, void* userp) +{ + size_t realsize = size * nmemb; + struct MemoryStruct* mem = (struct MemoryStruct*)userp; + + uint8_t* ptr = realloc(mem->memory, mem->size + realsize + 1); + if (!ptr) + return 0; + + mem->memory = ptr; + memcpy(&(mem->memory[mem->size]), contents, realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; + + return realsize; +} + +struct MemoryStruct* downloadToRam(const char* url) +{ + CURL* curl_handle; + CURLcode res = CURLE_OK; + + struct MemoryStruct* chunk = malloc(sizeof(struct MemoryStruct)); + + if (chunk) + { + chunk->memory = malloc(1); + if (!chunk->memory) + { + free(chunk); + return NULL; + } + chunk->memory[0] = 0; + chunk->size = 0; + + 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_NOPROGRESS, 1L); + + res = curl_easy_perform(curl_handle); + + long http_code = 0; + curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code); + + if (res != CURLE_OK || http_code != 200) + { + freeDownload(chunk); + chunk = NULL; + } + + curl_easy_cleanup(curl_handle); + } + + return chunk; +} + +size_t downloadToFile(const char* url, const char* path) +{ + size_t out_write = 0; + struct MemoryStruct* chunk = downloadToRam(url); + + if (chunk) + { + FILE* fp = fopen(path, "wb"); + + if (fp) + { + out_write = fwrite(chunk->memory, sizeof(uint8_t), chunk->size, fp); + fclose(fp); + } + + freeDownload(chunk); + } + + return out_write; +} + +void freeDownload(struct MemoryStruct* chunk) +{ + if (chunk) + { + if (chunk->memory) + free(chunk->memory); + free(chunk); + } +} + +struct json_object* fetchJSON(const char* url) +{ + struct json_object* json = NULL; + struct MemoryStruct* chunk = downloadToRam(url); + + if (chunk) + { + json = json_tokener_parse((char*)chunk->memory); + freeDownload(chunk); + } + + return json; +} |