17 curl = curl_easy_init();
18 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
19 curl_easy_setopt(curl, CURLOPT_TIMEOUT, HTTP_GET_TIMEOUT);
20 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
21 curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
22 curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
23 curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);
24 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http::curl_write_data);
30 curl_easy_cleanup(curl);
34 size_t http::curl_write_data(char* ptr, size_t size, size_t nmemb, void* data_ptr) {
35 struct Data* data = (struct Data *) data_ptr;
36 size_t realsize = size * nmemb;
38 char *input = (char *) g_realloc(data->memory, data->size + realsize + 1);
41 g_warning("not enough memory (realloc returned NULL)");
46 memcpy(&(data->memory[data->size]), ptr, realsize);
47 data->size += realsize;
48 data->memory[data->size] = 0;
53 void http::destroy_giostream() {
54 debug_print("destroy_giostream called.\n");
56 debug_print("Freeing input_stream\n");
57 g_input_stream_close(stream, NULL, NULL);
58 g_object_unref(stream);
62 GInputStream *http::load_url(const gchar *url, GError **error)
64 GError* _error = NULL;
65 CURLcode res = CURLE_OK;
70 data.memory = (char *) g_malloc(1);
73 if (!strncmp(url, "file:///", 8) || g_file_test(url, G_FILE_TEST_EXISTS)) {
74 gchar* newurl = g_filename_from_uri(url, NULL, NULL);
75 if (g_file_get_contents(newurl ? newurl : url, &content, &len, &_error)) {
76 stream = g_memory_input_stream_new_from_data(content, len, NULL);
78 debug_print("Got error: %s\n", _error->message);
82 if (!curl) return NULL;
83 curl_easy_setopt(curl, CURLOPT_URL, url);
84 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);
85 res = curl_easy_perform(curl);
86 if (res != CURLE_OK) {
87 _error = g_error_new_literal(G_FILE_ERROR, res, curl_easy_strerror(res));
89 debug_print("Image size: %d\n", data.size);
90 stream = g_memory_input_stream_new_from_data(
91 g_memdup(data.memory, data.size), data.size, NULL);
96 if (error && _error) *error = _error;