From 3d344cfeaa7708b5786e55d18af0cbcfc8ed25ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Wennerstr=C3=B6m?= Date: Tue, 21 Jul 2020 10:34:29 +0200 Subject: [PATCH] Move common http tool code to http_common --- Makefile.am | 2 + src/tools/aesgcm_download.c | 1 + src/tools/aesgcm_download.h | 3 +- src/tools/http_common.c | 98 +++++++++++++++++++++++++++++++++++++ src/tools/http_common.h | 48 ++++++++++++++++++ src/tools/http_download.c | 52 ++------------------ src/tools/http_download.h | 5 +- 7 files changed, 156 insertions(+), 53 deletions(-) create mode 100644 src/tools/http_common.c create mode 100644 src/tools/http_common.h diff --git a/Makefile.am b/Makefile.am index 5c34b35a..e6c38066 100644 --- a/Makefile.am +++ b/Makefile.am @@ -41,6 +41,8 @@ core_sources = \ src/command/cmd_ac.h src/command/cmd_ac.c \ src/tools/parser.c \ src/tools/parser.h \ + src/tools/http_common.c \ + src/tools/http_common.h \ src/tools/http_upload.c \ src/tools/http_upload.h \ src/tools/http_download.c \ diff --git a/src/tools/aesgcm_download.c b/src/tools/aesgcm_download.c index 6e8b89c5..6b60ba08 100644 --- a/src/tools/aesgcm_download.c +++ b/src/tools/aesgcm_download.c @@ -50,6 +50,7 @@ #include "profanity.h" #include "event/client_events.h" +#include "tools/http_common.h" #include "tools/aesgcm_download.h" #include "omemo/omemo.h" #include "config/preferences.h" diff --git a/src/tools/aesgcm_download.h b/src/tools/aesgcm_download.h index ebc8a5f9..e172b89a 100644 --- a/src/tools/aesgcm_download.h +++ b/src/tools/aesgcm_download.h @@ -43,6 +43,7 @@ #include #include +#include "tools/http_common.h" #include "tools/http_download.h" #include "ui/win_types.h" @@ -61,6 +62,4 @@ void* aesgcm_file_get(void* userdata); void aesgcm_download_cancel_processes(ProfWin* window); void aesgcm_download_add_download(AESGCMDownload* download); -char* http_basename_from_url(const char* url); - #endif diff --git a/src/tools/http_common.c b/src/tools/http_common.c new file mode 100644 index 00000000..df6f9a64 --- /dev/null +++ b/src/tools/http_common.c @@ -0,0 +1,98 @@ +/* + * http_common.c + * vim: expandtab:ts=4:sts=4:sw=4 + * + * Copyright (C) 2020 William Wennerström + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#define _GNU_SOURCE 1 + +#include +#include +#include +#include + +#include "tools/http_common.h" + +#define FALLBACK_MSG "" + +char* +http_basename_from_url(const char* url) +{ + const char* default_name = "index.html"; + + GFile* file = g_file_new_for_uri(url); + char* filename = g_file_get_basename(file); + g_object_unref(file); + + if (g_strcmp0(filename, ".") == 0 + || g_strcmp0(filename, "..") == 0 + || g_strcmp0(filename, G_DIR_SEPARATOR_S) == 0) { + g_free(filename); + return strdup(default_name); + } + + return filename; +} + +void +http_print_transfer_update(ProfWin* window, char* url, + const char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + char* msg; + if (vasprintf(&msg, fmt, args) == -1) { + msg = strdup(FALLBACK_MSG); + } + va_end(args); + + win_update_entry_message(window, url, msg); + free(msg); +} + +void +http_print_transfer(ProfWin* window, char* url, + const char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + char* msg; + if (vasprintf(&msg, fmt, args) == -1) { + msg = strdup(FALLBACK_MSG); + } + va_end(args); + + win_print_http_transfer(window, msg, url); + free(msg); +} diff --git a/src/tools/http_common.h b/src/tools/http_common.h new file mode 100644 index 00000000..41f16200 --- /dev/null +++ b/src/tools/http_common.h @@ -0,0 +1,48 @@ +/* + * http_common.h + * vim: expandtab:ts=4:sts=4:sw=4 + * + * Copyright (C) 2020 William Wennerström + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#ifndef TOOLS_HTTP_COMMON_H +#define TOOLS_HTTP_COMMON_H + +#define _GNU_SOURCE 1 + +#include "ui/window.h" + +char* http_basename_from_url(const char* url); +void http_print_transfer(ProfWin* window, char* url, const char* fmt, ...); +void http_print_transfer_update(ProfWin* window, char* url, + const char* fmt, ...); + +#endif diff --git a/src/tools/http_download.c b/src/tools/http_download.c index 5859dc70..a470feec 100644 --- a/src/tools/http_download.c +++ b/src/tools/http_download.c @@ -56,8 +56,6 @@ #include "ui/window.h" #include "common.h" -#define FALLBACK_MSG "" - GSList* download_processes = NULL; static int @@ -84,12 +82,8 @@ _xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultot dlperc = (100 * dlnow) / dltotal; } - char* msg; - if (asprintf(&msg, "Downloading '%s': %d%%", download->url, dlperc) == -1) { - msg = strdup(FALLBACK_MSG); - } - win_update_entry_message(download->window, download->url, msg); - free(msg); + http_print_transfer_update(download->window, download->url, + "Downloading '%s': %d%%", download->url, dlperc); pthread_mutex_unlock(&lock); @@ -121,8 +115,8 @@ http_file_get(void* userdata) download->bytes_received = 0; pthread_mutex_lock(&lock); - http_print_transfer_update(download->window, download->url, - "Downloading '%s': 0%%", download->url); + http_print_transfer(download->window, download->url, + "Downloading '%s': 0%%", download->url); FILE* outfh = fopen(download->filename, "wb"); if (outfh == NULL) { @@ -188,7 +182,7 @@ http_file_get(void* userdata) } else { if (!download->cancel) { http_print_transfer_update(download->window, download->url, - "Downloading '%s': 100%%", + "Downloading '%s': done", download->url); win_mark_received(download->window, download->url); } @@ -223,39 +217,3 @@ http_download_add_download(HTTPDownload* download) { download_processes = g_slist_append(download_processes, download); } - -char* -http_basename_from_url(const char* url) -{ - const char* default_name = "index.html"; - - GFile* file = g_file_new_for_uri(url); - char* filename = g_file_get_basename(file); - g_object_unref(file); - - if (g_strcmp0(filename, ".") == 0 - || g_strcmp0(filename, "..") == 0 - || g_strcmp0(filename, G_DIR_SEPARATOR_S) == 0) { - g_free(filename); - return strdup(default_name); - } - - return filename; -} - -void -http_print_transfer_update(ProfWin* window, char* url, - const char* fmt, ...) -{ - va_list args; - - va_start(args, fmt); - char* msg; - if (vasprintf(&msg, fmt, args) == -1) { - msg = strdup(FALLBACK_MSG); - } - va_end(args); - - win_print_http_transfer(window, msg, url); - free(msg); -} diff --git a/src/tools/http_download.h b/src/tools/http_download.h index 077e3e3d..b6ce42ca 100644 --- a/src/tools/http_download.h +++ b/src/tools/http_download.h @@ -45,6 +45,7 @@ #include #include "ui/win_types.h" +#include "tools/http_common.h" typedef struct http_download_t { @@ -61,8 +62,4 @@ void* http_file_get(void* userdata); void http_download_cancel_processes(ProfWin* window); void http_download_add_download(HTTPDownload* download); -char* http_basename_from_url(const char* url); -void http_print_transfer_update(ProfWin* window, char* url, - const char* fmt, ...); - #endif