From 66e17a6855dfa723b017ccd3685950c2de51f5da Mon Sep 17 00:00:00 2001
From: Mole Shang <135e2@135e2.dev>
Date: Tue, 8 Aug 2023 23:13:28 +0800
Subject: process_url: expose get_info with a new param `p_content_type`

---
 src/process_url.c | 19 ++++++++++++-------
 src/process_url.h |  2 ++
 2 files changed, 14 insertions(+), 7 deletions(-)

(limited to 'src')

diff --git a/src/process_url.c b/src/process_url.c
index 7d04f37..a1204a1 100644
--- a/src/process_url.c
+++ b/src/process_url.c
@@ -244,7 +244,7 @@ static int parse_url(const char *URL, const char *outdir, char *fn) {
   return 0;
 }
 
-static bool get_info(const char *URL, long *psize) {
+bool get_info(const char *URL, long *psize, char **p_content_type) {
   CURL *curl;
   long resp_code;
   bool support_range = false;
@@ -258,6 +258,16 @@ static bool get_info(const char *URL, long *psize) {
   if (logerr(r)) {
     goto end;
   }
+  char *ct = NULL;
+  r = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
+  if (logerr(r)) {
+    goto end;
+  }
+  DEBUG_PRINT("Content-Type: %s\n", ct);
+  if (p_content_type) {
+    *p_content_type = malloc(strlen(ct) + 1);
+    strcpy(*p_content_type, ct);
+  }
   r = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
                         (curl_off_t *)psize);
   if (logerr(r)) {
@@ -268,11 +278,6 @@ static bool get_info(const char *URL, long *psize) {
   if (logerr(rh) || strcmp(pch->value, "bytes")) {
     goto end;
   }
-  char *ct = NULL;
-  r = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
-  if (logerr(r)) {
-    goto end;
-  }
 
   support_range = true;
 end:
@@ -387,7 +392,7 @@ static int download(curl_conf_t *curl_c) {
 
   static thrd_info_t thrd_info[MAX_THREAD] = {0};
 
-  bool support_range = get_info(curl_c->URL, &cl);
+  bool support_range = get_info(curl_c->URL, &cl, NULL);
   DEBUG_PRINT("Size: %ld bytes.\n", cl);
   if (support_range && cl > 0L) {
     curl_c->dltotal = cl;
diff --git a/src/process_url.h b/src/process_url.h
index 3edfdd9..760ee49 100644
--- a/src/process_url.h
+++ b/src/process_url.h
@@ -60,6 +60,8 @@ void poll_status(status_t *);
 
 int get(const char *, char **);
 
+bool get_info(const char *URL, long *psize, char **p_content_type);
+
 void add_url(const char *, const char *, const char *, const char *, callback_t,
              callback_struct_t *);
 
-- 
cgit v1.2.3