summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2023-08-07 22:52:47 +0800
committerMole Shang <[email protected]>2023-08-07 22:59:19 +0800
commit204ab98bea93754beef914fe93ee249e346ee628 (patch)
treec056c82f4de392be506ed2298dbb66411414e350
parentb6c2d97d3bf98c46656a7e6697c1c0f4686d7d97 (diff)
downloadhinata-204ab98bea93754beef914fe93ee249e346ee628.tar.gz
hinata-204ab98bea93754beef914fe93ee249e346ee628.tar.bz2
hinata-204ab98bea93754beef914fe93ee249e346ee628.zip
hinata: support specifying cookies in curl_easy
We use tomlc99 to parse strings. To use cookies for higher resolution video downloading, add a config.toml in the executable path. e.g. ```filename: config.toml cookie="SESSDATA=xxx; some_more_cookie=xxx" ```
-rw-r--r--src/main.c31
-rw-r--r--src/process_url.c6
-rw-r--r--src/process_url.h2
-rw-r--r--xmake.lua21
4 files changed, 55 insertions, 5 deletions
diff --git a/src/main.c b/src/main.c
index e9d22f1..ecd9381 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,10 +1,12 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <curl/curl.h>
+#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
+#include "toml.h"
#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
#define NK_INCLUDE_FIXED_TYPES
@@ -29,6 +31,7 @@
#include "unifont.h"
extern int win_width, win_height;
+static char *cookie;
extern void load_ui(struct ui_struct *);
static void error_callback(int e, const char *d) {
printf("Error %d: %s\n", e, d);
@@ -40,8 +43,34 @@ int main(void) {
/* Set locale*/
setlocale(LC_ALL, ".UTF-8");
+ /* Parse config */
+ // NOTICE: string parsed from toml should always be freed afterwards!!!
+ {
+ FILE *fp;
+ const char *config_file = "config.toml";
+ char errbuf[UCHAR_MAX];
+
+ fp = fopen(config_file, "r");
+ if (!fp) {
+ fprintf(stderr, "[tomlc99] Cannot open %s, applying default config...\n",
+ config_file);
+ } else {
+ toml_table_t *conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
+ fclose(fp);
+
+ if (!conf) {
+ fprintf(stderr, "[tomlc99] Cannot parse %s\n", config_file);
+ }
+ toml_datum_t cookie_datum = toml_string_in(conf, "cookie");
+ if (cookie_datum.ok) {
+ printf("[tomlc99] Found cookie string in config.toml, applying...\n");
+ cookie = cookie_datum.u.s;
+ }
+ }
+ }
+
/* Curl */
- curl_init();
+ curl_init(cookie);
/* GLFW */
struct nk_glfw glfw = {0};
diff --git a/src/process_url.c b/src/process_url.c
index bf2edfd..e51da98 100644
--- a/src/process_url.c
+++ b/src/process_url.c
@@ -39,6 +39,7 @@ mtx_t mtx;
cnd_t cnd;
bool corrupted;
static const char *outdir_g, *referer_g;
+static char *cookie_g;
static callback_t callback_g;
static callback_struct_t *p_callback_struct_g;
static CURLU *h;
@@ -113,6 +114,7 @@ static void curl_easy_setcommonopts(CURL *curl) {
/* enable all supported built-in compressions,
* since serveral sites enable gzip encoding */
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
+ curl_easy_setopt(curl, CURLOPT_COOKIE, cookie_g);
}
static int progress_callback(void *clientp, curl_off_t dltotal,
@@ -440,10 +442,11 @@ static char *callback_struct_convert_fullpath(char *filename) {
return tmp;
}
-void curl_init(curl_conf_t *curl) {
+void curl_init(char *cookie) {
curl_global_init(CURL_GLOBAL_ALL);
h = curl_url();
dl_queue = create_queue();
+ cookie_g = cookie;
mtx_init(&mtx, mtx_plain);
cnd_init(&cnd);
}
@@ -467,6 +470,7 @@ void curl_cleanup(status_t *stat) {
cnd_destroy(&cnd);
}
free_queue(&dl_queue);
+ free_and_nullify(cookie_g);
curl_url_cleanup(h);
curl_global_cleanup();
}
diff --git a/src/process_url.h b/src/process_url.h
index 25caef6..5343c9e 100644
--- a/src/process_url.h
+++ b/src/process_url.h
@@ -52,7 +52,7 @@ typedef struct callback_struct {
typedef int (*callback_t)(callback_struct_t *);
-void curl_init();
+void curl_init(char *);
void curl_cleanup(status_t *);
diff --git a/xmake.lua b/xmake.lua
index 98ba483..e287c0b 100644
--- a/xmake.lua
+++ b/xmake.lua
@@ -2,7 +2,7 @@ add_rules("mode.debug", "mode.release", "mode.releasedbg", "mode.minsizerel")
add_requires("nuklear", "nuklear_glfw_gl3", "nuklear_fonts", "glew", "glfw", "tinyfiledialogs",
"c11threads", "pcre2",
- "cjson", "ffmpeg")
+ "cjson", "ffmpeg", "tomlc99")
add_requires("libcurl", { configs = { zlib = true } })
if is_plat("linux") then
@@ -24,7 +24,7 @@ if is_mode("debug") then
end
add_packages("nuklear", "nuklear_glfw_gl3", "nuklear_fonts", "glew", "glfw", "libcurl", "tinyfiledialogs",
"c11threads", "pcre2",
- "cjson", "ffmpeg")
+ "cjson", "ffmpeg", "tomlc99")
package("tinyfiledialogs")
@@ -142,3 +142,20 @@ end)
on_test(function(package)
assert(package:has_cfuncs("cJSON_malloc", { includes = "cjson/cJSON.h" }))
end)
+
+package("tomlc99")
+
+set_homepage("https://github.com/cktan/tomlc99")
+set_description("TOML C library ")
+set_license("MIT")
+
+set_urls("https://github.com/cktan/tomlc99.git")
+
+on_install("linux", "macosx", "windows", "mingw", function(package)
+ import("package.tools.make").make(package, {})
+ import("package.tools.make").make(package, { "install", "prefix = " .. package:installdir() })
+end)
+
+on_test(function(package)
+ assert(package:has_cfuncs("toml_parse_file", { includes = "toml.h" }))
+end)