summaryrefslogtreecommitdiff
path: root/src/backend/plugins/download-curl/download-curl.c
blob: d26d62bdc3c243ef7eda35c11d76323295193963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* download-curl.c
 *
 * Copyright 2022-2023 The Libproxy Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <libpeas/peas.h>
#include <curl/curl.h>

#include "download-curl.h"

#include "px-plugin-download.h"
#include "px-manager.h"

static void px_download_iface_init (PxDownloadInterface *iface);
G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);

struct _PxDownloadCurl {
  GObject parent_instance;
  CURL *curl;
};

G_DEFINE_FINAL_TYPE_WITH_CODE (PxDownloadCurl,
                               px_download_curl,
                               G_TYPE_OBJECT,
                               G_IMPLEMENT_INTERFACE (PX_TYPE_DOWNLOAD, px_download_iface_init))

static void
px_download_curl_init (PxDownloadCurl *self)
{
}

static void
px_download_curl_class_init (PxDownloadCurlClass *klass)
{
}

static size_t
store_data (void   *contents,
            size_t  size,
            size_t  nmemb,
            void   *user_pointer)
{
  GByteArray *byte_array = user_pointer;
  size_t real_size = size * nmemb;

  g_byte_array_append (byte_array, contents, real_size);

  return real_size;
}

static GBytes *
px_download_curl_download (PxDownload *download,
                           const char *uri)
{
  PxDownloadCurl *self = PX_DOWNLOAD_CURL (download);
  GByteArray *byte_array = g_byte_array_new ();
  CURLcode res;
  const char *url = uri;

  if (!self->curl)
    self->curl = curl_easy_init ();

  if (!self->curl)
    return NULL;

  if (g_str_has_prefix (url, "pac+"))
    url += 4;

  curl_easy_setopt (self->curl, CURLOPT_NOSIGNAL, 1);
  curl_easy_setopt (self->curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_easy_setopt (self->curl, CURLOPT_NOPROXY, "*");
  curl_easy_setopt (self->curl, CURLOPT_CONNECTTIMEOUT, 30);
  curl_easy_setopt (self->curl, CURLOPT_USERAGENT, "libproxy");

  curl_easy_setopt (self->curl, CURLOPT_URL, url);
  curl_easy_setopt (self->curl, CURLOPT_WRITEFUNCTION, store_data);
  curl_easy_setopt (self->curl, CURLOPT_WRITEDATA, byte_array);

  res = curl_easy_perform (self->curl);
  if (res != CURLE_OK) {
    g_debug ("%s: Could not download data: %s", __FUNCTION__, curl_easy_strerror (res));
    return NULL;
  }

  return g_byte_array_free_to_bytes (byte_array);
}

static void
px_download_iface_init (PxDownloadInterface *iface)
{
  iface->download = px_download_curl_download;
}

G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
  peas_object_module_register_extension_type (module,
                                              PX_TYPE_DOWNLOAD,
                                              PX_DOWNLOAD_TYPE_CURL);
}