Freeciv-3.3
Loading...
Searching...
No Matches
netfile.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#ifndef __EMSCRIPTEN__
19
20#include <curl/curl.h>
21
22#ifdef FREECIV_MSWINDOWS
23#include <windows.h>
24#endif
25
26/* utility */
27#include "fcintl.h"
28#include "ioz.h"
29#include "mem.h"
30#include "rand.h"
31#include "registry.h"
32
33#endif /* __EMSCRIPTEN__ */
34
35#include "netfile.h"
36
37#ifndef __EMSCRIPTEN__
38
40#ifdef HAVE_CURL_MIME_API
42#else /* HAVE_CURL_MIME_API */
45#endif /* HAVE_CURL_MIME_API */
46};
47
48typedef size_t (*netfile_write_cb)(char *ptr, size_t size, size_t nmemb,
49 void *userdata);
50
52
53/* Consecutive transfers can use same handle for better performance */
54static CURL *chandle = nullptr;
55
56/*******************************************************************/
60{
61 if (chandle == nullptr) {
63 } else {
65 }
66
67 error_buf_curl[0] = '\0';
69
70#ifdef CUSTOM_CACERT_PATH
72#endif /* CUSTOM_CERT_PATH */
73
74 return chandle;
75}
76
77/*******************************************************************/
80static size_t netfile_memwrite_cb(char *ptr, size_t size, size_t nmemb,
81 void *userdata)
82{
84
85 if (size > 0) {
86 data->mem = fc_realloc(data->mem, data->size + size * nmemb);
87 memcpy(data->mem + data->size, ptr, size * nmemb);
88 data->size += size * nmemb;
89 }
90
91 return size * nmemb;
92}
93
94/*******************************************************************/
98static bool netfile_download_file_core(const char *URL, FILE *fp,
100 nf_errmsg cb, void *data)
101{
103 struct curl_slist *headers = nullptr;
104 static CURL *handle;
105 bool ret = TRUE;
106
108
109 headers = curl_slist_append(headers, "User-Agent: Freeciv/" VERSION_STRING);
110
112 if (mem_data != nullptr) {
113 mem_data->mem = nullptr;
114 mem_data->size = 0;
117 } else {
119 }
122
124
126
127 if (curlret != CURLE_OK) {
128 if (cb != nullptr) {
129 char buf[2048 + CURL_ERROR_SIZE];
130
131 fc_snprintf(buf, sizeof(buf),
132 /* TRANS: first %s is URL, second is Curl error message
133 * (not in Freeciv translation domain) */
134 _("Failed to fetch %s: %s"), URL,
136 cb(buf, data);
137 }
138
139 ret = FALSE;
140 }
141
142 return ret;
143}
144
145/*******************************************************************/
149 nf_errmsg cb, void *data)
150{
151 bool success;
152 struct section_file *out = nullptr;
154 fz_FILE *file;
155
156 success = netfile_download_file_core(URL, nullptr, &mem_data, cb, data);
157
158 if (success) {
159 file = fz_from_memory(mem_data.mem, mem_data.size, TRUE);
160
162 }
163
164 return out;
165}
166
167/*******************************************************************/
170bool netfile_download_file(const char *URL, const char *filename,
171 nf_errmsg cb, void *data)
172{
173 bool success;
174 FILE *fp;
175
176 fp = fc_fopen(filename, "w+b");
177
178 if (fp == nullptr) {
179 if (cb != nullptr) {
180 char buf[2048];
181
182 fc_snprintf(buf, sizeof(buf),
183 _("Could not open %s for writing"), filename);
184 cb(buf, data);
185 }
186 return FALSE;
187 }
188
189 success = netfile_download_file_core(URL, fp, nullptr, cb, data);
190
191 fclose(fp);
192
193 return success;
194}
195
196/*******************************************************************/
200{
201 struct netfile_post *post = fc_calloc(1, sizeof(struct netfile_post));
202
203#ifdef HAVE_CURL_MIME_API
204 post->mime = curl_mime_init(chandle);
205#endif
206
207 return post;
208}
209
210/*******************************************************************/
214 const char *name, const char *val)
215{
216#ifdef HAVE_CURL_MIME_API
218
219 part = curl_mime_addpart(post->mime);
220
223#else /* HAVE_CURL_MIME_API */
224 curl_formadd(&post->first, &post->last,
228#endif /* HAVE_CURL_MIME_API */
229}
230
231/*******************************************************************/
235 const char *name, const int val)
236{
237 char buf[50];
238
239 fc_snprintf(buf, sizeof(buf), "%d", val);
241}
242
243/*******************************************************************/
247{
248#ifdef HAVE_CURL_MIME_API
249 curl_mime_free(post->mime);
250#else /* HAVE_CURL_MIME_API */
251 curl_formfree(post->first);
252#endif /* HAVE_CURL_MIME_API */
253
254 FC_FREE(post);
255}
256
257/*******************************************************************/
261static size_t dummy_write(void *buffer, size_t size, size_t nmemb,
262 void *userp)
263{
264 return size * nmemb;
265}
266
267/*******************************************************************/
270bool netfile_send_post(const char *URL, struct netfile_post *post,
271 FILE *reply_fp,
273 const char *addr)
274{
276 long http_resp;
277 struct curl_slist *headers = nullptr;
278 static CURL *handle;
279
281
282 headers = curl_slist_append(headers, "User-Agent: Freeciv/" VERSION_STRING);
283
285
286#ifdef HAVE_CURL_MIME_API
288#else /* HAVE_CURL_MIME_API */
290#endif /* HAVE_CURL_MIME_API */
291
292 if (mem_data != nullptr) {
293 mem_data->mem = nullptr;
294 mem_data->size = 0;
297 } else if (reply_fp == nullptr) {
299 } else {
301 }
302 if (addr != nullptr) {
304 }
306
308
310
311 if (curlret != CURLE_OK) {
312 return FALSE;
313 }
314
316
317 if (http_resp != 200) {
318 return FALSE;
319 }
320
321 return TRUE;
322}
323
324#endif /* __EMSCRIPTEN__ */
325
326/*******************************************************************/
329void netfile_free(void)
330{
331#ifndef __EMSCRIPTEN__
332 if (chandle != nullptr) {
334 chandle = nullptr;
335 }
336#endif /* __EMSCRIPTEN__ */
337}
char * incite_cost
Definition comments.c:76
#define _(String)
Definition fcintl.h:67
const char * name
Definition inputfile.c:127
fz_FILE * fz_from_memory(char *buffer, int size, bool control)
Definition ioz.c:207
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_realloc(ptr, sz)
Definition mem.h:36
static CURL * netfile_init_handle(void)
Definition netfile.c:59
void netfile_close_post(struct netfile_post *post)
Definition netfile.c:246
static char error_buf_curl[CURL_ERROR_SIZE]
Definition netfile.c:51
struct netfile_post * netfile_start_post(void)
Definition netfile.c:199
bool netfile_send_post(const char *URL, struct netfile_post *post, FILE *reply_fp, struct netfile_write_cb_data *mem_data, const char *addr)
Definition netfile.c:270
void netfile_free(void)
Definition netfile.c:329
struct section_file * netfile_get_section_file(const char *URL, nf_errmsg cb, void *data)
Definition netfile.c:148
void netfile_add_form_int(struct netfile_post *post, const char *name, const int val)
Definition netfile.c:234
static bool netfile_download_file_core(const char *URL, FILE *fp, struct netfile_write_cb_data *mem_data, nf_errmsg cb, void *data)
Definition netfile.c:98
size_t(* netfile_write_cb)(char *ptr, size_t size, size_t nmemb, void *userdata)
Definition netfile.c:48
void netfile_add_form_str(struct netfile_post *post, const char *name, const char *val)
Definition netfile.c:213
static CURL * chandle
Definition netfile.c:54
bool netfile_download_file(const char *URL, const char *filename, nf_errmsg cb, void *data)
Definition netfile.c:170
static size_t netfile_memwrite_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
Definition netfile.c:80
static size_t dummy_write(void *buffer, size_t size, size_t nmemb, void *userp)
Definition netfile.c:261
void(* nf_errmsg)(const char *msg, void *data)
Definition netfile.h:32
struct section_file * secfile_from_stream(fz_FILE *stream, bool allow_duplicates)
size_t size
Definition specvec.h:72
Definition netfile.c:39
struct curl_httppost * first
Definition netfile.c:43
struct curl_httppost * last
Definition netfile.c:44
Definition goto.c:52
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:505
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47