about summary refs log tree commit diff stats
path: root/src/tools
diff options
context:
space:
mode:
authorWilliam Wennerström <william@wstrm.dev>2020-06-27 20:23:50 +0200
committerWilliam Wennerström <william@wstrm.dev>2020-11-16 21:58:08 +0100
commite9d58757825e542fe4ec15ce350c0df4192ec29d (patch)
tree607c36529fb4370aec94fd57f25026c138848192 /src/tools
parent39c32906131b3bb9383ed61be15ba0dd9d720898 (diff)
downloadprofani-tty-e9d58757825e542fe4ec15ce350c0df4192ec29d.tar.gz
Reformat HTTP get URL to AESGCM scheme
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/http_upload.c89
-rw-r--r--src/tools/http_upload.h10
2 files changed, 69 insertions, 30 deletions
diff --git a/src/tools/http_upload.c b/src/tools/http_upload.c
index 5f725851..69587c08 100644
--- a/src/tools/http_upload.c
+++ b/src/tools/http_upload.c
@@ -128,8 +128,35 @@ _data_callback(void* ptr, size_t size, size_t nmemb, void* data)
     return realsize;
 }
 
-void*
-http_file_put(void* userdata)
+int format_alt_url(char *original_url, char *new_scheme, char *new_fragment, char **new_url) {
+    int ret = 0;
+    CURLU *h = curl_url();
+
+    if ((ret = curl_url_set(h, CURLUPART_URL, original_url, 0)) != 0) {
+        goto out;
+    }
+
+    if (new_scheme != NULL) {
+        if ((ret = curl_url_set(h, CURLUPART_SCHEME, new_scheme, CURLU_NON_SUPPORT_SCHEME)) != 0) {
+            goto out;
+        }
+    }
+
+    if (new_fragment != NULL) {
+        if ((ret = curl_url_set(h, CURLUPART_FRAGMENT, new_fragment, 0)) != 0) {
+            goto out;
+        }
+    }
+
+    ret = curl_url_get(h, CURLUPART_URL, new_url, 0);
+
+out:
+    curl_url_cleanup(h);
+    return ret;
+}
+
+void *
+http_file_put(void *userdata)
 {
     HTTPUpload* upload = (HTTPUpload*)userdata;
 
@@ -256,30 +283,40 @@ http_file_put(void* userdata)
             win_mark_received(upload->window, upload->put_url);
             free(msg);
 
-            switch (upload->window->type) {
-            case WIN_CHAT:
-            {
-                ProfChatWin* chatwin = (ProfChatWin*)(upload->window);
-                assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
-                cl_ev_send_msg(chatwin, upload->get_url, upload->get_url);
-                break;
-            }
-            case WIN_PRIVATE:
-            {
-                ProfPrivateWin* privatewin = (ProfPrivateWin*)(upload->window);
-                assert(privatewin->memcheck == PROFPRIVATEWIN_MEMCHECK);
-                cl_ev_send_priv_msg(privatewin, upload->get_url, upload->get_url);
-                break;
-            }
-            case WIN_MUC:
-            {
-                ProfMucWin* mucwin = (ProfMucWin*)(upload->window);
-                assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
-                cl_ev_send_muc_msg(mucwin, upload->get_url, upload->get_url);
-                break;
-            }
-            default:
-                break;
+            char *url = NULL;
+            if (format_alt_url(upload->get_url, upload->alt_scheme, upload->alt_fragment, &url) != 0) {
+                char *msg;
+                asprintf(&msg, "Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url);
+                cons_show_error(msg);
+                free(msg);
+            } else {
+                switch (upload->window->type) {
+                case WIN_CHAT:
+                {
+                    ProfChatWin *chatwin = (ProfChatWin*)(upload->window);
+                    assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
+                    cl_ev_send_msg(chatwin, url, url);
+                    break;
+                }
+                case WIN_PRIVATE:
+                {
+                    ProfPrivateWin *privatewin = (ProfPrivateWin*)(upload->window);
+                    assert(privatewin->memcheck == PROFPRIVATEWIN_MEMCHECK);
+                    cl_ev_send_priv_msg(privatewin, url, url);
+                    break;
+                }
+                case WIN_MUC:
+                {
+                    ProfMucWin *mucwin = (ProfMucWin*)(upload->window);
+                    assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
+                    cl_ev_send_muc_msg(mucwin, url, url);
+                    break;
+                }
+                default:
+                    break;
+                }
+
+                curl_free(url);
             }
         }
     }
diff --git a/src/tools/http_upload.h b/src/tools/http_upload.h
index 5b3b4754..9e801973 100644
--- a/src/tools/http_upload.h
+++ b/src/tools/http_upload.h
@@ -50,10 +50,12 @@ typedef struct http_upload_t {
     FILE *filehandle;
     off_t filesize;
     curl_off_t bytes_sent;
-    char* mime_type;
-    char* get_url;
-    char* put_url;
-    ProfWin* window;
+    char *mime_type;
+    char *get_url;
+    char *put_url;
+    char *alt_scheme;
+    char *alt_fragment;
+    ProfWin *window;
     pthread_t worker;
     int cancel;
 } HTTPUpload;