X-Git-Url: http://git.claws-mail.org/?p=claws.git;a=blobdiff_plain;f=src%2Fplugins%2Flitehtml_viewer%2Fhttp.cpp;h=dba8e6d6ed23a23a793d27803b77271e27d19425;hp=bed56383c72bf216ba383f45d6014041b7ed4f7d;hb=HEAD;hpb=bda2f34a970dc7542d4def69e08abc6c82c4e959 diff --git a/src/plugins/litehtml_viewer/http.cpp b/src/plugins/litehtml_viewer/http.cpp index bed56383c..ac0f5352f 100644 --- a/src/plugins/litehtml_viewer/http.cpp +++ b/src/plugins/litehtml_viewer/http.cpp @@ -1,3 +1,20 @@ +/* + * Claws Mail -- A GTK based, lightweight, and fast e-mail client + * Copyright(C) 2019 the Claws Mail Team + * + * This program 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. + * This program 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 this program; if not, write tothe Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -5,13 +22,33 @@ #include #include "http.h" +extern "C" { +#include "ssl.h" #include "utils.h" +} struct Data { - char *memory; + GInputStream *memory; size_t size; }; +static size_t write_data(char* ptr, size_t size, size_t nmemb, void* data_ptr) { + struct Data* data = (struct Data *) data_ptr; + size_t realsize = size * nmemb; + + g_memory_input_stream_add_data((GMemoryInputStream *)data->memory, +#if !GLIB_CHECK_VERSION (2, 68, 0) + g_memdup(ptr, realsize), +#else + g_memdup2(ptr, realsize), +#endif + realsize, + g_free); + data->size += realsize; + + return realsize; +} + http::http() { curl = curl_easy_init(); @@ -21,7 +58,10 @@ http::http() curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L); curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L); curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http::curl_write_data); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); +#ifdef G_OS_WIN32 + curl_easy_setopt(curl, CURLOPT_CAINFO, claws_ssl_get_cert_file()); +#endif stream = NULL; } @@ -31,25 +71,6 @@ http::~http() destroy_giostream(); } -size_t http::curl_write_data(char* ptr, size_t size, size_t nmemb, void* data_ptr) { - struct Data* data = (struct Data *) data_ptr; - size_t realsize = size * nmemb; - - char *input = (char *) g_realloc(data->memory, data->size + realsize + 1); - if(input == NULL) { - /* out of memory! */ - g_warning("not enough memory (realloc returned NULL)"); - return 0; - } - - data->memory = input; - memcpy(&(data->memory[data->size]), ptr, realsize); - data->size += realsize; - data->memory[data->size] = 0; - - return realsize; -} - void http::destroy_giostream() { debug_print("destroy_giostream called.\n"); if (stream) { @@ -61,40 +82,41 @@ void http::destroy_giostream() { GInputStream *http::load_url(const gchar *url, GError **error) { - GError* _error = NULL; - CURLcode res = CURLE_OK; - gsize len; - gchar* content; - struct Data data; - - data.memory = (char *) g_malloc(1); - data.size = 0; + GError* _error = NULL; + CURLcode res = CURLE_OK; + gsize len; + gchar* content; - if (!strncmp(url, "file:///", 8) || g_file_test(url, G_FILE_TEST_EXISTS)) { - gchar* newurl = g_filename_from_uri(url, NULL, NULL); - if (g_file_get_contents(newurl ? newurl : url, &content, &len, &_error)) { - stream = g_memory_input_stream_new_from_data(content, len, NULL); + if (!strncmp(url, "file:///", 8) || g_file_test(url, G_FILE_TEST_EXISTS)) { + gchar* newurl = g_filename_from_uri(url, NULL, NULL); + if (g_file_get_contents(newurl ? newurl : url, &content, &len, &_error)) { + stream = g_memory_input_stream_new_from_data(content, len, g_free); + } else { + debug_print("Got error: %s\n", _error->message); + } + g_free(newurl); } else { - debug_print("Got error: %s\n", _error->message); - } - g_free(newurl); - } else { - if (!curl) return NULL; - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data); - res = curl_easy_perform(curl); - if (res != CURLE_OK) { - _error = g_error_new_literal(G_FILE_ERROR, res, curl_easy_strerror(res)); - } else { - debug_print("Image size: %d\n", data.size); - stream = g_memory_input_stream_new_from_data( - g_memdup(data.memory, data.size), data.size, NULL); - g_free(data.memory); + struct Data data; + + if (!curl) return NULL; + + data.memory = g_memory_input_stream_new(); + data.size = 0; + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data); + res = curl_easy_perform(curl); + + if (res != CURLE_OK) { + _error = g_error_new_literal(G_FILE_ERROR, res, curl_easy_strerror(res)); + g_object_unref(data.memory); + } else { + debug_print("Image size: %" G_GSIZE_FORMAT "\n", data.size); + stream = data.memory; + } } - } - if (error && _error) *error = _error; + if (error && _error) *error = _error; - return stream; + return stream; } -