mirror of
https://github.com/profanity-im/profanity.git
synced 2025-01-03 14:57:42 -05:00
Move common http tool code to http_common
This commit is contained in:
parent
7a1eb730b1
commit
3d344cfeaa
@ -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 \
|
||||
|
@ -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"
|
||||
|
@ -43,6 +43,7 @@
|
||||
|
||||
#include <sys/select.h>
|
||||
#include <curl/curl.h>
|
||||
#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
|
||||
|
98
src/tools/http_common.c
Normal file
98
src/tools/http_common.c
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* http_common.c
|
||||
* vim: expandtab:ts=4:sts=4:sw=4
|
||||
*
|
||||
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <gio/gio.h>
|
||||
|
||||
#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);
|
||||
}
|
48
src/tools/http_common.h
Normal file
48
src/tools/http_common.h
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* http_common.h
|
||||
* vim: expandtab:ts=4:sts=4:sw=4
|
||||
*
|
||||
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* 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
|
@ -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);
|
||||
}
|
||||
|
@ -45,6 +45,7 @@
|
||||
#include <curl/curl.h>
|
||||
|
||||
#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
|
||||
|
Loading…
Reference in New Issue
Block a user