1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | *
|
---|
10 | * This software is licensed as described in the file COPYING, which
|
---|
11 | * you should have received as part of this distribution. The terms
|
---|
12 | * are also available at https://curl.se/docs/copyright.html.
|
---|
13 | *
|
---|
14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
---|
15 | * copies of the Software, and permit persons to whom the Software is
|
---|
16 | * furnished to do so, under the terms of the COPYING file.
|
---|
17 | *
|
---|
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
19 | * KIND, either express or implied.
|
---|
20 | *
|
---|
21 | ***************************************************************************/
|
---|
22 |
|
---|
23 | #include "curl_setup.h"
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * See comment in curl_memory.h for the explanation of this sanity check.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifdef CURLX_NO_MEMORY_CALLBACKS
|
---|
30 | #error "libcurl shall not ever be built with CURLX_NO_MEMORY_CALLBACKS defined"
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #ifdef HAVE_NETINET_IN_H
|
---|
34 | #include <netinet/in.h>
|
---|
35 | #endif
|
---|
36 | #ifdef HAVE_NETDB_H
|
---|
37 | #include <netdb.h>
|
---|
38 | #endif
|
---|
39 | #ifdef HAVE_ARPA_INET_H
|
---|
40 | #include <arpa/inet.h>
|
---|
41 | #endif
|
---|
42 | #ifdef HAVE_NET_IF_H
|
---|
43 | #include <net/if.h>
|
---|
44 | #endif
|
---|
45 | #ifdef HAVE_SYS_IOCTL_H
|
---|
46 | #include <sys/ioctl.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #ifdef HAVE_SYS_PARAM_H
|
---|
50 | #include <sys/param.h>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #include "urldata.h"
|
---|
54 | #include <curl/curl.h>
|
---|
55 | #include "transfer.h"
|
---|
56 | #include "vtls/vtls.h"
|
---|
57 | #include "url.h"
|
---|
58 | #include "getinfo.h"
|
---|
59 | #include "hostip.h"
|
---|
60 | #include "share.h"
|
---|
61 | #include "strdup.h"
|
---|
62 | #include "progress.h"
|
---|
63 | #include "easyif.h"
|
---|
64 | #include "multiif.h"
|
---|
65 | #include "select.h"
|
---|
66 | #include "sendf.h" /* for failf function prototype */
|
---|
67 | #include "connect.h" /* for Curl_getconnectinfo */
|
---|
68 | #include "slist.h"
|
---|
69 | #include "mime.h"
|
---|
70 | #include "amigaos.h"
|
---|
71 | #include "warnless.h"
|
---|
72 | #include "multiif.h"
|
---|
73 | #include "sigpipe.h"
|
---|
74 | #include "vssh/ssh.h"
|
---|
75 | #include "setopt.h"
|
---|
76 | #include "http_digest.h"
|
---|
77 | #include "system_win32.h"
|
---|
78 | #include "http2.h"
|
---|
79 | #include "dynbuf.h"
|
---|
80 | #include "altsvc.h"
|
---|
81 | #include "hsts.h"
|
---|
82 |
|
---|
83 | /* The last 3 #include files should be in this order */
|
---|
84 | #include "curl_printf.h"
|
---|
85 | #include "curl_memory.h"
|
---|
86 | #include "memdebug.h"
|
---|
87 |
|
---|
88 | /* true globals -- for curl_global_init() and curl_global_cleanup() */
|
---|
89 | static unsigned int initialized;
|
---|
90 | static long init_flags;
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * strdup (and other memory functions) is redefined in complicated
|
---|
94 | * ways, but at this point it must be defined as the system-supplied strdup
|
---|
95 | * so the callback pointer is initialized correctly.
|
---|
96 | */
|
---|
97 | #if defined(_WIN32_WCE)
|
---|
98 | #define system_strdup _strdup
|
---|
99 | #elif !defined(HAVE_STRDUP)
|
---|
100 | #define system_strdup curlx_strdup
|
---|
101 | #else
|
---|
102 | #define system_strdup strdup
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
|
---|
106 | # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * If a memory-using function (like curl_getenv) is used before
|
---|
111 | * curl_global_init() is called, we need to have these pointers set already.
|
---|
112 | */
|
---|
113 | curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
|
---|
114 | curl_free_callback Curl_cfree = (curl_free_callback)free;
|
---|
115 | curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
|
---|
116 | curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
|
---|
117 | curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
|
---|
118 | #if defined(WIN32) && defined(UNICODE)
|
---|
119 | curl_wcsdup_callback Curl_cwcsdup = Curl_wcsdup;
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
|
---|
123 | # pragma warning(default:4232) /* MSVC extension, dllimport identity */
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | #ifdef DEBUGBUILD
|
---|
127 | static char *leakpointer;
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * curl_global_init() globally initializes curl given a bitwise set of the
|
---|
132 | * different features of what to initialize.
|
---|
133 | */
|
---|
134 | static CURLcode global_init(long flags, bool memoryfuncs)
|
---|
135 | {
|
---|
136 | if(initialized++)
|
---|
137 | return CURLE_OK;
|
---|
138 |
|
---|
139 | if(memoryfuncs) {
|
---|
140 | /* Setup the default memory functions here (again) */
|
---|
141 | Curl_cmalloc = (curl_malloc_callback)malloc;
|
---|
142 | Curl_cfree = (curl_free_callback)free;
|
---|
143 | Curl_crealloc = (curl_realloc_callback)realloc;
|
---|
144 | Curl_cstrdup = (curl_strdup_callback)system_strdup;
|
---|
145 | Curl_ccalloc = (curl_calloc_callback)calloc;
|
---|
146 | #if defined(WIN32) && defined(UNICODE)
|
---|
147 | Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
|
---|
148 | #endif
|
---|
149 | }
|
---|
150 |
|
---|
151 | if(!Curl_ssl_init()) {
|
---|
152 | DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
|
---|
153 | goto fail;
|
---|
154 | }
|
---|
155 |
|
---|
156 | #ifdef WIN32
|
---|
157 | if(Curl_win32_init(flags)) {
|
---|
158 | DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
|
---|
159 | goto fail;
|
---|
160 | }
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | #ifdef __AMIGA__
|
---|
164 | if(!Curl_amiga_init()) {
|
---|
165 | DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
|
---|
166 | goto fail;
|
---|
167 | }
|
---|
168 | #endif
|
---|
169 |
|
---|
170 | if(Curl_resolver_global_init()) {
|
---|
171 | DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
|
---|
172 | goto fail;
|
---|
173 | }
|
---|
174 |
|
---|
175 | #if defined(USE_SSH)
|
---|
176 | if(Curl_ssh_init()) {
|
---|
177 | goto fail;
|
---|
178 | }
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | #ifdef USE_WOLFSSH
|
---|
182 | if(WS_SUCCESS != wolfSSH_Init()) {
|
---|
183 | DEBUGF(fprintf(stderr, "Error: wolfSSH_Init failed\n"));
|
---|
184 | return CURLE_FAILED_INIT;
|
---|
185 | }
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | init_flags = flags;
|
---|
189 |
|
---|
190 | #ifdef DEBUGBUILD
|
---|
191 | if(getenv("CURL_GLOBAL_INIT"))
|
---|
192 | /* alloc data that will leak if *cleanup() is not called! */
|
---|
193 | leakpointer = malloc(1);
|
---|
194 | #endif
|
---|
195 |
|
---|
196 | return CURLE_OK;
|
---|
197 |
|
---|
198 | fail:
|
---|
199 | initialized--; /* undo the increase */
|
---|
200 | return CURLE_FAILED_INIT;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * curl_global_init() globally initializes curl given a bitwise set of the
|
---|
206 | * different features of what to initialize.
|
---|
207 | */
|
---|
208 | CURLcode curl_global_init(long flags)
|
---|
209 | {
|
---|
210 | return global_init(flags, TRUE);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * curl_global_init_mem() globally initializes curl and also registers the
|
---|
215 | * user provided callback routines.
|
---|
216 | */
|
---|
217 | CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
|
---|
218 | curl_free_callback f, curl_realloc_callback r,
|
---|
219 | curl_strdup_callback s, curl_calloc_callback c)
|
---|
220 | {
|
---|
221 | /* Invalid input, return immediately */
|
---|
222 | if(!m || !f || !r || !s || !c)
|
---|
223 | return CURLE_FAILED_INIT;
|
---|
224 |
|
---|
225 | if(initialized) {
|
---|
226 | /* Already initialized, don't do it again, but bump the variable anyway to
|
---|
227 | work like curl_global_init() and require the same amount of cleanup
|
---|
228 | calls. */
|
---|
229 | initialized++;
|
---|
230 | return CURLE_OK;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* set memory functions before global_init() in case it wants memory
|
---|
234 | functions */
|
---|
235 | Curl_cmalloc = m;
|
---|
236 | Curl_cfree = f;
|
---|
237 | Curl_cstrdup = s;
|
---|
238 | Curl_crealloc = r;
|
---|
239 | Curl_ccalloc = c;
|
---|
240 |
|
---|
241 | /* Call the actual init function, but without setting */
|
---|
242 | return global_init(flags, FALSE);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * curl_global_cleanup() globally cleanups curl, uses the value of
|
---|
247 | * "init_flags" to determine what needs to be cleaned up and what doesn't.
|
---|
248 | */
|
---|
249 | void curl_global_cleanup(void)
|
---|
250 | {
|
---|
251 | if(!initialized)
|
---|
252 | return;
|
---|
253 |
|
---|
254 | if(--initialized)
|
---|
255 | return;
|
---|
256 |
|
---|
257 | Curl_ssl_cleanup();
|
---|
258 | Curl_resolver_global_cleanup();
|
---|
259 |
|
---|
260 | #ifdef WIN32
|
---|
261 | Curl_win32_cleanup(init_flags);
|
---|
262 | #endif
|
---|
263 |
|
---|
264 | Curl_amiga_cleanup();
|
---|
265 |
|
---|
266 | Curl_ssh_cleanup();
|
---|
267 |
|
---|
268 | #ifdef USE_WOLFSSH
|
---|
269 | (void)wolfSSH_Cleanup();
|
---|
270 | #endif
|
---|
271 | #ifdef DEBUGBUILD
|
---|
272 | free(leakpointer);
|
---|
273 | #endif
|
---|
274 |
|
---|
275 | init_flags = 0;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /*
|
---|
279 | * curl_easy_init() is the external interface to alloc, setup and init an
|
---|
280 | * easy handle that is returned. If anything goes wrong, NULL is returned.
|
---|
281 | */
|
---|
282 | struct Curl_easy *curl_easy_init(void)
|
---|
283 | {
|
---|
284 | CURLcode result;
|
---|
285 | struct Curl_easy *data;
|
---|
286 |
|
---|
287 | /* Make sure we inited the global SSL stuff */
|
---|
288 | if(!initialized) {
|
---|
289 | result = curl_global_init(CURL_GLOBAL_DEFAULT);
|
---|
290 | if(result) {
|
---|
291 | /* something in the global init failed, return nothing */
|
---|
292 | DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
|
---|
293 | return NULL;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /* We use curl_open() with undefined URL so far */
|
---|
298 | result = Curl_open(&data);
|
---|
299 | if(result) {
|
---|
300 | DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
|
---|
301 | return NULL;
|
---|
302 | }
|
---|
303 |
|
---|
304 | return data;
|
---|
305 | }
|
---|
306 |
|
---|
307 | #ifdef CURLDEBUG
|
---|
308 |
|
---|
309 | struct socketmonitor {
|
---|
310 | struct socketmonitor *next; /* the next node in the list or NULL */
|
---|
311 | struct pollfd socket; /* socket info of what to monitor */
|
---|
312 | };
|
---|
313 |
|
---|
314 | struct events {
|
---|
315 | long ms; /* timeout, run the timeout function when reached */
|
---|
316 | bool msbump; /* set TRUE when timeout is set by callback */
|
---|
317 | int num_sockets; /* number of nodes in the monitor list */
|
---|
318 | struct socketmonitor *list; /* list of sockets to monitor */
|
---|
319 | int running_handles; /* store the returned number */
|
---|
320 | };
|
---|
321 |
|
---|
322 | /* events_timer
|
---|
323 | *
|
---|
324 | * Callback that gets called with a new value when the timeout should be
|
---|
325 | * updated.
|
---|
326 | */
|
---|
327 |
|
---|
328 | static int events_timer(struct Curl_multi *multi, /* multi handle */
|
---|
329 | long timeout_ms, /* see above */
|
---|
330 | void *userp) /* private callback pointer */
|
---|
331 | {
|
---|
332 | struct events *ev = userp;
|
---|
333 | (void)multi;
|
---|
334 | if(timeout_ms == -1)
|
---|
335 | /* timeout removed */
|
---|
336 | timeout_ms = 0;
|
---|
337 | else if(timeout_ms == 0)
|
---|
338 | /* timeout is already reached! */
|
---|
339 | timeout_ms = 1; /* trigger asap */
|
---|
340 |
|
---|
341 | ev->ms = timeout_ms;
|
---|
342 | ev->msbump = TRUE;
|
---|
343 | return 0;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | /* poll2cselect
|
---|
348 | *
|
---|
349 | * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
|
---|
350 | */
|
---|
351 | static int poll2cselect(int pollmask)
|
---|
352 | {
|
---|
353 | int omask = 0;
|
---|
354 | if(pollmask & POLLIN)
|
---|
355 | omask |= CURL_CSELECT_IN;
|
---|
356 | if(pollmask & POLLOUT)
|
---|
357 | omask |= CURL_CSELECT_OUT;
|
---|
358 | if(pollmask & POLLERR)
|
---|
359 | omask |= CURL_CSELECT_ERR;
|
---|
360 | return omask;
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | /* socketcb2poll
|
---|
365 | *
|
---|
366 | * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
|
---|
367 | */
|
---|
368 | static short socketcb2poll(int pollmask)
|
---|
369 | {
|
---|
370 | short omask = 0;
|
---|
371 | if(pollmask & CURL_POLL_IN)
|
---|
372 | omask |= POLLIN;
|
---|
373 | if(pollmask & CURL_POLL_OUT)
|
---|
374 | omask |= POLLOUT;
|
---|
375 | return omask;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* events_socket
|
---|
379 | *
|
---|
380 | * Callback that gets called with information about socket activity to
|
---|
381 | * monitor.
|
---|
382 | */
|
---|
383 | static int events_socket(struct Curl_easy *easy, /* easy handle */
|
---|
384 | curl_socket_t s, /* socket */
|
---|
385 | int what, /* see above */
|
---|
386 | void *userp, /* private callback
|
---|
387 | pointer */
|
---|
388 | void *socketp) /* private socket
|
---|
389 | pointer */
|
---|
390 | {
|
---|
391 | struct events *ev = userp;
|
---|
392 | struct socketmonitor *m;
|
---|
393 | struct socketmonitor *prev = NULL;
|
---|
394 |
|
---|
395 | #if defined(CURL_DISABLE_VERBOSE_STRINGS)
|
---|
396 | (void) easy;
|
---|
397 | #endif
|
---|
398 | (void)socketp;
|
---|
399 |
|
---|
400 | m = ev->list;
|
---|
401 | while(m) {
|
---|
402 | if(m->socket.fd == s) {
|
---|
403 |
|
---|
404 | if(what == CURL_POLL_REMOVE) {
|
---|
405 | struct socketmonitor *nxt = m->next;
|
---|
406 | /* remove this node from the list of monitored sockets */
|
---|
407 | if(prev)
|
---|
408 | prev->next = nxt;
|
---|
409 | else
|
---|
410 | ev->list = nxt;
|
---|
411 | free(m);
|
---|
412 | m = nxt;
|
---|
413 | infof(easy, "socket cb: socket %d REMOVED", s);
|
---|
414 | }
|
---|
415 | else {
|
---|
416 | /* The socket 's' is already being monitored, update the activity
|
---|
417 | mask. Convert from libcurl bitmask to the poll one. */
|
---|
418 | m->socket.events = socketcb2poll(what);
|
---|
419 | infof(easy, "socket cb: socket %d UPDATED as %s%s", s,
|
---|
420 | (what&CURL_POLL_IN)?"IN":"",
|
---|
421 | (what&CURL_POLL_OUT)?"OUT":"");
|
---|
422 | }
|
---|
423 | break;
|
---|
424 | }
|
---|
425 | prev = m;
|
---|
426 | m = m->next; /* move to next node */
|
---|
427 | }
|
---|
428 | if(!m) {
|
---|
429 | if(what == CURL_POLL_REMOVE) {
|
---|
430 | /* this happens a bit too often, libcurl fix perhaps? */
|
---|
431 | /* fprintf(stderr,
|
---|
432 | "%s: socket %d asked to be REMOVED but not present!\n",
|
---|
433 | __func__, s); */
|
---|
434 | }
|
---|
435 | else {
|
---|
436 | m = malloc(sizeof(struct socketmonitor));
|
---|
437 | if(m) {
|
---|
438 | m->next = ev->list;
|
---|
439 | m->socket.fd = s;
|
---|
440 | m->socket.events = socketcb2poll(what);
|
---|
441 | m->socket.revents = 0;
|
---|
442 | ev->list = m;
|
---|
443 | infof(easy, "socket cb: socket %d ADDED as %s%s", s,
|
---|
444 | (what&CURL_POLL_IN)?"IN":"",
|
---|
445 | (what&CURL_POLL_OUT)?"OUT":"");
|
---|
446 | }
|
---|
447 | else
|
---|
448 | return CURLE_OUT_OF_MEMORY;
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | return 0;
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * events_setup()
|
---|
458 | *
|
---|
459 | * Do the multi handle setups that only event-based transfers need.
|
---|
460 | */
|
---|
461 | static void events_setup(struct Curl_multi *multi, struct events *ev)
|
---|
462 | {
|
---|
463 | /* timer callback */
|
---|
464 | curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
|
---|
465 | curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
|
---|
466 |
|
---|
467 | /* socket callback */
|
---|
468 | curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
|
---|
469 | curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 | /* wait_or_timeout()
|
---|
474 | *
|
---|
475 | * waits for activity on any of the given sockets, or the timeout to trigger.
|
---|
476 | */
|
---|
477 |
|
---|
478 | static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
|
---|
479 | {
|
---|
480 | bool done = FALSE;
|
---|
481 | CURLMcode mcode = CURLM_OK;
|
---|
482 | CURLcode result = CURLE_OK;
|
---|
483 |
|
---|
484 | while(!done) {
|
---|
485 | CURLMsg *msg;
|
---|
486 | struct socketmonitor *m;
|
---|
487 | struct pollfd *f;
|
---|
488 | struct pollfd fds[4];
|
---|
489 | int numfds = 0;
|
---|
490 | int pollrc;
|
---|
491 | int i;
|
---|
492 | struct curltime before;
|
---|
493 | struct curltime after;
|
---|
494 |
|
---|
495 | /* populate the fds[] array */
|
---|
496 | for(m = ev->list, f = &fds[0]; m; m = m->next) {
|
---|
497 | f->fd = m->socket.fd;
|
---|
498 | f->events = m->socket.events;
|
---|
499 | f->revents = 0;
|
---|
500 | /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
|
---|
501 | f++;
|
---|
502 | numfds++;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /* get the time stamp to use to figure out how long poll takes */
|
---|
506 | before = Curl_now();
|
---|
507 |
|
---|
508 | /* wait for activity or timeout */
|
---|
509 | pollrc = Curl_poll(fds, numfds, ev->ms);
|
---|
510 |
|
---|
511 | after = Curl_now();
|
---|
512 |
|
---|
513 | ev->msbump = FALSE; /* reset here */
|
---|
514 |
|
---|
515 | if(0 == pollrc) {
|
---|
516 | /* timeout! */
|
---|
517 | ev->ms = 0;
|
---|
518 | /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
|
---|
519 | mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
|
---|
520 | &ev->running_handles);
|
---|
521 | }
|
---|
522 | else if(pollrc > 0) {
|
---|
523 | /* loop over the monitored sockets to see which ones had activity */
|
---|
524 | for(i = 0; i< numfds; i++) {
|
---|
525 | if(fds[i].revents) {
|
---|
526 | /* socket activity, tell libcurl */
|
---|
527 | int act = poll2cselect(fds[i].revents); /* convert */
|
---|
528 | infof(multi->easyp, "call curl_multi_socket_action(socket %d)",
|
---|
529 | fds[i].fd);
|
---|
530 | mcode = curl_multi_socket_action(multi, fds[i].fd, act,
|
---|
531 | &ev->running_handles);
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | if(!ev->msbump) {
|
---|
536 | /* If nothing updated the timeout, we decrease it by the spent time.
|
---|
537 | * If it was updated, it has the new timeout time stored already.
|
---|
538 | */
|
---|
539 | timediff_t timediff = Curl_timediff(after, before);
|
---|
540 | if(timediff > 0) {
|
---|
541 | if(timediff > ev->ms)
|
---|
542 | ev->ms = 0;
|
---|
543 | else
|
---|
544 | ev->ms -= (long)timediff;
|
---|
545 | }
|
---|
546 | }
|
---|
547 | }
|
---|
548 | else
|
---|
549 | return CURLE_RECV_ERROR;
|
---|
550 |
|
---|
551 | if(mcode)
|
---|
552 | return CURLE_URL_MALFORMAT;
|
---|
553 |
|
---|
554 | /* we don't really care about the "msgs_in_queue" value returned in the
|
---|
555 | second argument */
|
---|
556 | msg = curl_multi_info_read(multi, &pollrc);
|
---|
557 | if(msg) {
|
---|
558 | result = msg->data.result;
|
---|
559 | done = TRUE;
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | return result;
|
---|
564 | }
|
---|
565 |
|
---|
566 |
|
---|
567 | /* easy_events()
|
---|
568 | *
|
---|
569 | * Runs a transfer in a blocking manner using the events-based API
|
---|
570 | */
|
---|
571 | static CURLcode easy_events(struct Curl_multi *multi)
|
---|
572 | {
|
---|
573 | /* this struct is made static to allow it to be used after this function
|
---|
574 | returns and curl_multi_remove_handle() is called */
|
---|
575 | static struct events evs = {2, FALSE, 0, NULL, 0};
|
---|
576 |
|
---|
577 | /* if running event-based, do some further multi inits */
|
---|
578 | events_setup(multi, &evs);
|
---|
579 |
|
---|
580 | return wait_or_timeout(multi, &evs);
|
---|
581 | }
|
---|
582 | #else /* CURLDEBUG */
|
---|
583 | /* when not built with debug, this function doesn't exist */
|
---|
584 | #define easy_events(x) CURLE_NOT_BUILT_IN
|
---|
585 | #endif
|
---|
586 |
|
---|
587 | static CURLcode easy_transfer(struct Curl_multi *multi)
|
---|
588 | {
|
---|
589 | bool done = FALSE;
|
---|
590 | CURLMcode mcode = CURLM_OK;
|
---|
591 | CURLcode result = CURLE_OK;
|
---|
592 |
|
---|
593 | while(!done && !mcode) {
|
---|
594 | int still_running = 0;
|
---|
595 |
|
---|
596 | mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
|
---|
597 |
|
---|
598 | if(!mcode)
|
---|
599 | mcode = curl_multi_perform(multi, &still_running);
|
---|
600 |
|
---|
601 | /* only read 'still_running' if curl_multi_perform() return OK */
|
---|
602 | if(!mcode && !still_running) {
|
---|
603 | int rc;
|
---|
604 | CURLMsg *msg = curl_multi_info_read(multi, &rc);
|
---|
605 | if(msg) {
|
---|
606 | result = msg->data.result;
|
---|
607 | done = TRUE;
|
---|
608 | }
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | /* Make sure to return some kind of error if there was a multi problem */
|
---|
613 | if(mcode) {
|
---|
614 | result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
|
---|
615 | /* The other multi errors should never happen, so return
|
---|
616 | something suitably generic */
|
---|
617 | CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
618 | }
|
---|
619 |
|
---|
620 | return result;
|
---|
621 | }
|
---|
622 |
|
---|
623 |
|
---|
624 | /*
|
---|
625 | * easy_perform() is the external interface that performs a blocking
|
---|
626 | * transfer as previously setup.
|
---|
627 | *
|
---|
628 | * CONCEPT: This function creates a multi handle, adds the easy handle to it,
|
---|
629 | * runs curl_multi_perform() until the transfer is done, then detaches the
|
---|
630 | * easy handle, destroys the multi handle and returns the easy handle's return
|
---|
631 | * code.
|
---|
632 | *
|
---|
633 | * REALITY: it can't just create and destroy the multi handle that easily. It
|
---|
634 | * needs to keep it around since if this easy handle is used again by this
|
---|
635 | * function, the same multi handle must be re-used so that the same pools and
|
---|
636 | * caches can be used.
|
---|
637 | *
|
---|
638 | * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
|
---|
639 | * instead of curl_multi_perform() and use curl_multi_socket_action().
|
---|
640 | */
|
---|
641 | static CURLcode easy_perform(struct Curl_easy *data, bool events)
|
---|
642 | {
|
---|
643 | struct Curl_multi *multi;
|
---|
644 | CURLMcode mcode;
|
---|
645 | CURLcode result = CURLE_OK;
|
---|
646 | SIGPIPE_VARIABLE(pipe_st);
|
---|
647 |
|
---|
648 | if(!data)
|
---|
649 | return CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
650 |
|
---|
651 | if(data->set.errorbuffer)
|
---|
652 | /* clear this as early as possible */
|
---|
653 | data->set.errorbuffer[0] = 0;
|
---|
654 |
|
---|
655 | if(data->multi) {
|
---|
656 | failf(data, "easy handle already used in multi handle");
|
---|
657 | return CURLE_FAILED_INIT;
|
---|
658 | }
|
---|
659 |
|
---|
660 | if(data->multi_easy)
|
---|
661 | multi = data->multi_easy;
|
---|
662 | else {
|
---|
663 | /* this multi handle will only ever have a single easy handled attached
|
---|
664 | to it, so make it use minimal hashes */
|
---|
665 | multi = Curl_multi_handle(1, 3);
|
---|
666 | if(!multi)
|
---|
667 | return CURLE_OUT_OF_MEMORY;
|
---|
668 | data->multi_easy = multi;
|
---|
669 | }
|
---|
670 |
|
---|
671 | if(multi->in_callback)
|
---|
672 | return CURLE_RECURSIVE_API_CALL;
|
---|
673 |
|
---|
674 | /* Copy the MAXCONNECTS option to the multi handle */
|
---|
675 | curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
|
---|
676 |
|
---|
677 | mcode = curl_multi_add_handle(multi, data);
|
---|
678 | if(mcode) {
|
---|
679 | curl_multi_cleanup(multi);
|
---|
680 | data->multi_easy = NULL;
|
---|
681 | if(mcode == CURLM_OUT_OF_MEMORY)
|
---|
682 | return CURLE_OUT_OF_MEMORY;
|
---|
683 | return CURLE_FAILED_INIT;
|
---|
684 | }
|
---|
685 |
|
---|
686 | sigpipe_ignore(data, &pipe_st);
|
---|
687 |
|
---|
688 | /* run the transfer */
|
---|
689 | result = events ? easy_events(multi) : easy_transfer(multi);
|
---|
690 |
|
---|
691 | /* ignoring the return code isn't nice, but atm we can't really handle
|
---|
692 | a failure here, room for future improvement! */
|
---|
693 | (void)curl_multi_remove_handle(multi, data);
|
---|
694 |
|
---|
695 | sigpipe_restore(&pipe_st);
|
---|
696 |
|
---|
697 | /* The multi handle is kept alive, owned by the easy handle */
|
---|
698 | return result;
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | /*
|
---|
703 | * curl_easy_perform() is the external interface that performs a blocking
|
---|
704 | * transfer as previously setup.
|
---|
705 | */
|
---|
706 | CURLcode curl_easy_perform(struct Curl_easy *data)
|
---|
707 | {
|
---|
708 | return easy_perform(data, FALSE);
|
---|
709 | }
|
---|
710 |
|
---|
711 | #ifdef CURLDEBUG
|
---|
712 | /*
|
---|
713 | * curl_easy_perform_ev() is the external interface that performs a blocking
|
---|
714 | * transfer using the event-based API internally.
|
---|
715 | */
|
---|
716 | CURLcode curl_easy_perform_ev(struct Curl_easy *data)
|
---|
717 | {
|
---|
718 | return easy_perform(data, TRUE);
|
---|
719 | }
|
---|
720 |
|
---|
721 | #endif
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * curl_easy_cleanup() is the external interface to cleaning/freeing the given
|
---|
725 | * easy handle.
|
---|
726 | */
|
---|
727 | void curl_easy_cleanup(struct Curl_easy *data)
|
---|
728 | {
|
---|
729 | SIGPIPE_VARIABLE(pipe_st);
|
---|
730 |
|
---|
731 | if(!data)
|
---|
732 | return;
|
---|
733 |
|
---|
734 | sigpipe_ignore(data, &pipe_st);
|
---|
735 | Curl_close(&data);
|
---|
736 | sigpipe_restore(&pipe_st);
|
---|
737 | }
|
---|
738 |
|
---|
739 | /*
|
---|
740 | * curl_easy_getinfo() is an external interface that allows an app to retrieve
|
---|
741 | * information from a performed transfer and similar.
|
---|
742 | */
|
---|
743 | #undef curl_easy_getinfo
|
---|
744 | CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
|
---|
745 | {
|
---|
746 | va_list arg;
|
---|
747 | void *paramp;
|
---|
748 | CURLcode result;
|
---|
749 |
|
---|
750 | va_start(arg, info);
|
---|
751 | paramp = va_arg(arg, void *);
|
---|
752 |
|
---|
753 | result = Curl_getinfo(data, info, paramp);
|
---|
754 |
|
---|
755 | va_end(arg);
|
---|
756 | return result;
|
---|
757 | }
|
---|
758 |
|
---|
759 | static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
|
---|
760 | {
|
---|
761 | CURLcode result = CURLE_OK;
|
---|
762 | enum dupstring i;
|
---|
763 | enum dupblob j;
|
---|
764 |
|
---|
765 | /* Copy src->set into dst->set first, then deal with the strings
|
---|
766 | afterwards */
|
---|
767 | dst->set = src->set;
|
---|
768 | Curl_mime_initpart(&dst->set.mimepost, dst);
|
---|
769 |
|
---|
770 | /* clear all string pointers first */
|
---|
771 | memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
|
---|
772 |
|
---|
773 | /* duplicate all strings */
|
---|
774 | for(i = (enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
|
---|
775 | result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
|
---|
776 | if(result)
|
---|
777 | return result;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /* clear all blob pointers first */
|
---|
781 | memset(dst->set.blobs, 0, BLOB_LAST * sizeof(struct curl_blob *));
|
---|
782 | /* duplicate all blobs */
|
---|
783 | for(j = (enum dupblob)0; j < BLOB_LAST; j++) {
|
---|
784 | result = Curl_setblobopt(&dst->set.blobs[j], src->set.blobs[j]);
|
---|
785 | if(result)
|
---|
786 | return result;
|
---|
787 | }
|
---|
788 |
|
---|
789 | /* duplicate memory areas pointed to */
|
---|
790 | i = STRING_COPYPOSTFIELDS;
|
---|
791 | if(src->set.postfieldsize && src->set.str[i]) {
|
---|
792 | /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
|
---|
793 | dst->set.str[i] = Curl_memdup(src->set.str[i],
|
---|
794 | curlx_sotouz(src->set.postfieldsize));
|
---|
795 | if(!dst->set.str[i])
|
---|
796 | return CURLE_OUT_OF_MEMORY;
|
---|
797 | /* point to the new copy */
|
---|
798 | dst->set.postfields = dst->set.str[i];
|
---|
799 | }
|
---|
800 |
|
---|
801 | /* Duplicate mime data. */
|
---|
802 | result = Curl_mime_duppart(&dst->set.mimepost, &src->set.mimepost);
|
---|
803 |
|
---|
804 | if(src->set.resolve)
|
---|
805 | dst->state.resolve = dst->set.resolve;
|
---|
806 |
|
---|
807 | return result;
|
---|
808 | }
|
---|
809 |
|
---|
810 | /*
|
---|
811 | * curl_easy_duphandle() is an external interface to allow duplication of a
|
---|
812 | * given input easy handle. The returned handle will be a new working handle
|
---|
813 | * with all options set exactly as the input source handle.
|
---|
814 | */
|
---|
815 | struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
|
---|
816 | {
|
---|
817 | struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
|
---|
818 | if(!outcurl)
|
---|
819 | goto fail;
|
---|
820 |
|
---|
821 | /*
|
---|
822 | * We setup a few buffers we need. We should probably make them
|
---|
823 | * get setup on-demand in the code, as that would probably decrease
|
---|
824 | * the likeliness of us forgetting to init a buffer here in the future.
|
---|
825 | */
|
---|
826 | outcurl->set.buffer_size = data->set.buffer_size;
|
---|
827 |
|
---|
828 | /* copy all userdefined values */
|
---|
829 | if(dupset(outcurl, data))
|
---|
830 | goto fail;
|
---|
831 |
|
---|
832 | Curl_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
|
---|
833 |
|
---|
834 | /* the connection cache is setup on demand */
|
---|
835 | outcurl->state.conn_cache = NULL;
|
---|
836 | outcurl->state.lastconnect_id = -1;
|
---|
837 |
|
---|
838 | outcurl->progress.flags = data->progress.flags;
|
---|
839 | outcurl->progress.callback = data->progress.callback;
|
---|
840 |
|
---|
841 | if(data->cookies) {
|
---|
842 | /* If cookies are enabled in the parent handle, we enable them
|
---|
843 | in the clone as well! */
|
---|
844 | outcurl->cookies = Curl_cookie_init(data,
|
---|
845 | data->cookies->filename,
|
---|
846 | outcurl->cookies,
|
---|
847 | data->set.cookiesession);
|
---|
848 | if(!outcurl->cookies)
|
---|
849 | goto fail;
|
---|
850 | }
|
---|
851 |
|
---|
852 | /* duplicate all values in 'change' */
|
---|
853 | if(data->state.cookielist) {
|
---|
854 | outcurl->state.cookielist =
|
---|
855 | Curl_slist_duplicate(data->state.cookielist);
|
---|
856 | if(!outcurl->state.cookielist)
|
---|
857 | goto fail;
|
---|
858 | }
|
---|
859 |
|
---|
860 | if(data->state.url) {
|
---|
861 | outcurl->state.url = strdup(data->state.url);
|
---|
862 | if(!outcurl->state.url)
|
---|
863 | goto fail;
|
---|
864 | outcurl->state.url_alloc = TRUE;
|
---|
865 | }
|
---|
866 |
|
---|
867 | if(data->state.referer) {
|
---|
868 | outcurl->state.referer = strdup(data->state.referer);
|
---|
869 | if(!outcurl->state.referer)
|
---|
870 | goto fail;
|
---|
871 | outcurl->state.referer_alloc = TRUE;
|
---|
872 | }
|
---|
873 |
|
---|
874 | /* Reinitialize an SSL engine for the new handle
|
---|
875 | * note: the engine name has already been copied by dupset */
|
---|
876 | if(outcurl->set.str[STRING_SSL_ENGINE]) {
|
---|
877 | if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
|
---|
878 | goto fail;
|
---|
879 | }
|
---|
880 |
|
---|
881 | #ifdef USE_ALTSVC
|
---|
882 | if(data->asi) {
|
---|
883 | outcurl->asi = Curl_altsvc_init();
|
---|
884 | if(!outcurl->asi)
|
---|
885 | goto fail;
|
---|
886 | if(outcurl->set.str[STRING_ALTSVC])
|
---|
887 | (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
|
---|
888 | }
|
---|
889 | #endif
|
---|
890 | #ifndef CURL_DISABLE_HSTS
|
---|
891 | if(data->hsts) {
|
---|
892 | outcurl->hsts = Curl_hsts_init();
|
---|
893 | if(!outcurl->hsts)
|
---|
894 | goto fail;
|
---|
895 | if(outcurl->set.str[STRING_HSTS])
|
---|
896 | (void)Curl_hsts_loadfile(outcurl,
|
---|
897 | outcurl->hsts, outcurl->set.str[STRING_HSTS]);
|
---|
898 | (void)Curl_hsts_loadcb(outcurl, outcurl->hsts);
|
---|
899 | }
|
---|
900 | #endif
|
---|
901 | /* Clone the resolver handle, if present, for the new handle */
|
---|
902 | if(Curl_resolver_duphandle(outcurl,
|
---|
903 | &outcurl->state.async.resolver,
|
---|
904 | data->state.async.resolver))
|
---|
905 | goto fail;
|
---|
906 |
|
---|
907 | #ifdef USE_ARES
|
---|
908 | {
|
---|
909 | CURLcode rc;
|
---|
910 |
|
---|
911 | rc = Curl_set_dns_servers(outcurl, data->set.str[STRING_DNS_SERVERS]);
|
---|
912 | if(rc && rc != CURLE_NOT_BUILT_IN)
|
---|
913 | goto fail;
|
---|
914 |
|
---|
915 | rc = Curl_set_dns_interface(outcurl, data->set.str[STRING_DNS_INTERFACE]);
|
---|
916 | if(rc && rc != CURLE_NOT_BUILT_IN)
|
---|
917 | goto fail;
|
---|
918 |
|
---|
919 | rc = Curl_set_dns_local_ip4(outcurl, data->set.str[STRING_DNS_LOCAL_IP4]);
|
---|
920 | if(rc && rc != CURLE_NOT_BUILT_IN)
|
---|
921 | goto fail;
|
---|
922 |
|
---|
923 | rc = Curl_set_dns_local_ip6(outcurl, data->set.str[STRING_DNS_LOCAL_IP6]);
|
---|
924 | if(rc && rc != CURLE_NOT_BUILT_IN)
|
---|
925 | goto fail;
|
---|
926 | }
|
---|
927 | #endif /* USE_ARES */
|
---|
928 |
|
---|
929 | Curl_initinfo(outcurl);
|
---|
930 |
|
---|
931 | outcurl->magic = CURLEASY_MAGIC_NUMBER;
|
---|
932 |
|
---|
933 | /* we reach this point and thus we are OK */
|
---|
934 |
|
---|
935 | return outcurl;
|
---|
936 |
|
---|
937 | fail:
|
---|
938 |
|
---|
939 | if(outcurl) {
|
---|
940 | curl_slist_free_all(outcurl->state.cookielist);
|
---|
941 | outcurl->state.cookielist = NULL;
|
---|
942 | Curl_safefree(outcurl->state.buffer);
|
---|
943 | Curl_dyn_free(&outcurl->state.headerb);
|
---|
944 | Curl_safefree(outcurl->state.url);
|
---|
945 | Curl_safefree(outcurl->state.referer);
|
---|
946 | Curl_altsvc_cleanup(&outcurl->asi);
|
---|
947 | Curl_hsts_cleanup(&outcurl->hsts);
|
---|
948 | Curl_freeset(outcurl);
|
---|
949 | free(outcurl);
|
---|
950 | }
|
---|
951 |
|
---|
952 | return NULL;
|
---|
953 | }
|
---|
954 |
|
---|
955 | /*
|
---|
956 | * curl_easy_reset() is an external interface that allows an app to re-
|
---|
957 | * initialize a session handle to the default values.
|
---|
958 | */
|
---|
959 | void curl_easy_reset(struct Curl_easy *data)
|
---|
960 | {
|
---|
961 | Curl_free_request_state(data);
|
---|
962 |
|
---|
963 | /* zero out UserDefined data: */
|
---|
964 | Curl_freeset(data);
|
---|
965 | memset(&data->set, 0, sizeof(struct UserDefined));
|
---|
966 | (void)Curl_init_userdefined(data);
|
---|
967 |
|
---|
968 | /* zero out Progress data: */
|
---|
969 | memset(&data->progress, 0, sizeof(struct Progress));
|
---|
970 |
|
---|
971 | /* zero out PureInfo data: */
|
---|
972 | Curl_initinfo(data);
|
---|
973 |
|
---|
974 | data->progress.flags |= PGRS_HIDE;
|
---|
975 | data->state.current_speed = -1; /* init to negative == impossible */
|
---|
976 | data->state.retrycount = 0; /* reset the retry counter */
|
---|
977 |
|
---|
978 | /* zero out authentication data: */
|
---|
979 | memset(&data->state.authhost, 0, sizeof(struct auth));
|
---|
980 | memset(&data->state.authproxy, 0, sizeof(struct auth));
|
---|
981 |
|
---|
982 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
|
---|
983 | Curl_http_auth_cleanup_digest(data);
|
---|
984 | #endif
|
---|
985 | }
|
---|
986 |
|
---|
987 | /*
|
---|
988 | * curl_easy_pause() allows an application to pause or unpause a specific
|
---|
989 | * transfer and direction. This function sets the full new state for the
|
---|
990 | * current connection this easy handle operates on.
|
---|
991 | *
|
---|
992 | * NOTE: if you have the receiving paused and you call this function to remove
|
---|
993 | * the pausing, you may get your write callback called at this point.
|
---|
994 | *
|
---|
995 | * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
|
---|
996 | *
|
---|
997 | * NOTE: This is one of few API functions that are allowed to be called from
|
---|
998 | * within a callback.
|
---|
999 | */
|
---|
1000 | CURLcode curl_easy_pause(struct Curl_easy *data, int action)
|
---|
1001 | {
|
---|
1002 | struct SingleRequest *k;
|
---|
1003 | CURLcode result = CURLE_OK;
|
---|
1004 | int oldstate;
|
---|
1005 | int newstate;
|
---|
1006 |
|
---|
1007 | if(!GOOD_EASY_HANDLE(data) || !data->conn)
|
---|
1008 | /* crazy input, don't continue */
|
---|
1009 | return CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
1010 |
|
---|
1011 | k = &data->req;
|
---|
1012 | oldstate = k->keepon & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
|
---|
1013 |
|
---|
1014 | /* first switch off both pause bits then set the new pause bits */
|
---|
1015 | newstate = (k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) |
|
---|
1016 | ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
|
---|
1017 | ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
|
---|
1018 |
|
---|
1019 | if((newstate & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) == oldstate) {
|
---|
1020 | /* Not changing any pause state, return */
|
---|
1021 | DEBUGF(infof(data, "pause: no change, early return"));
|
---|
1022 | return CURLE_OK;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | /* Unpause parts in active mime tree. */
|
---|
1026 | if((k->keepon & ~newstate & KEEP_SEND_PAUSE) &&
|
---|
1027 | (data->mstate == MSTATE_PERFORMING ||
|
---|
1028 | data->mstate == MSTATE_RATELIMITING) &&
|
---|
1029 | data->state.fread_func == (curl_read_callback) Curl_mime_read) {
|
---|
1030 | Curl_mime_unpause(data->state.in);
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | /* put it back in the keepon */
|
---|
1034 | k->keepon = newstate;
|
---|
1035 |
|
---|
1036 | if(!(newstate & KEEP_RECV_PAUSE)) {
|
---|
1037 | Curl_http2_stream_pause(data, FALSE);
|
---|
1038 |
|
---|
1039 | if(data->state.tempcount) {
|
---|
1040 | /* there are buffers for sending that can be delivered as the receive
|
---|
1041 | pausing is lifted! */
|
---|
1042 | unsigned int i;
|
---|
1043 | unsigned int count = data->state.tempcount;
|
---|
1044 | struct tempbuf writebuf[3]; /* there can only be three */
|
---|
1045 |
|
---|
1046 | /* copy the structs to allow for immediate re-pausing */
|
---|
1047 | for(i = 0; i < data->state.tempcount; i++) {
|
---|
1048 | writebuf[i] = data->state.tempwrite[i];
|
---|
1049 | Curl_dyn_init(&data->state.tempwrite[i].b, DYN_PAUSE_BUFFER);
|
---|
1050 | }
|
---|
1051 | data->state.tempcount = 0;
|
---|
1052 |
|
---|
1053 | for(i = 0; i < count; i++) {
|
---|
1054 | /* even if one function returns error, this loops through and frees
|
---|
1055 | all buffers */
|
---|
1056 | if(!result)
|
---|
1057 | result = Curl_client_write(data, writebuf[i].type,
|
---|
1058 | Curl_dyn_ptr(&writebuf[i].b),
|
---|
1059 | Curl_dyn_len(&writebuf[i].b));
|
---|
1060 | Curl_dyn_free(&writebuf[i].b);
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | if(result)
|
---|
1064 | return result;
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | /* if there's no error and we're not pausing both directions, we want
|
---|
1069 | to have this handle checked soon */
|
---|
1070 | if((newstate & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
|
---|
1071 | (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) {
|
---|
1072 | Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
|
---|
1073 |
|
---|
1074 | /* reset the too-slow time keeper */
|
---|
1075 | data->state.keeps_speed.tv_sec = 0;
|
---|
1076 |
|
---|
1077 | if(!data->state.tempcount)
|
---|
1078 | /* if not pausing again, force a recv/send check of this connection as
|
---|
1079 | the data might've been read off the socket already */
|
---|
1080 | data->conn->cselect_bits = CURL_CSELECT_IN | CURL_CSELECT_OUT;
|
---|
1081 | if(data->multi) {
|
---|
1082 | if(Curl_update_timer(data->multi))
|
---|
1083 | return CURLE_ABORTED_BY_CALLBACK;
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | if(!data->state.done)
|
---|
1088 | /* This transfer may have been moved in or out of the bundle, update the
|
---|
1089 | corresponding socket callback, if used */
|
---|
1090 | result = Curl_updatesocket(data);
|
---|
1091 |
|
---|
1092 | return result;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 |
|
---|
1096 | static CURLcode easy_connection(struct Curl_easy *data,
|
---|
1097 | curl_socket_t *sfd,
|
---|
1098 | struct connectdata **connp)
|
---|
1099 | {
|
---|
1100 | if(!data)
|
---|
1101 | return CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
1102 |
|
---|
1103 | /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
|
---|
1104 | if(!data->set.connect_only) {
|
---|
1105 | failf(data, "CONNECT_ONLY is required");
|
---|
1106 | return CURLE_UNSUPPORTED_PROTOCOL;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | *sfd = Curl_getconnectinfo(data, connp);
|
---|
1110 |
|
---|
1111 | if(*sfd == CURL_SOCKET_BAD) {
|
---|
1112 | failf(data, "Failed to get recent socket");
|
---|
1113 | return CURLE_UNSUPPORTED_PROTOCOL;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | return CURLE_OK;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | /*
|
---|
1120 | * Receives data from the connected socket. Use after successful
|
---|
1121 | * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
|
---|
1122 | * Returns CURLE_OK on success, error code on error.
|
---|
1123 | */
|
---|
1124 | CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
|
---|
1125 | size_t *n)
|
---|
1126 | {
|
---|
1127 | curl_socket_t sfd;
|
---|
1128 | CURLcode result;
|
---|
1129 | ssize_t n1;
|
---|
1130 | struct connectdata *c;
|
---|
1131 |
|
---|
1132 | if(Curl_is_in_callback(data))
|
---|
1133 | return CURLE_RECURSIVE_API_CALL;
|
---|
1134 |
|
---|
1135 | result = easy_connection(data, &sfd, &c);
|
---|
1136 | if(result)
|
---|
1137 | return result;
|
---|
1138 |
|
---|
1139 | if(!data->conn)
|
---|
1140 | /* on first invoke, the transfer has been detached from the connection and
|
---|
1141 | needs to be reattached */
|
---|
1142 | Curl_attach_connection(data, c);
|
---|
1143 |
|
---|
1144 | *n = 0;
|
---|
1145 | result = Curl_read(data, sfd, buffer, buflen, &n1);
|
---|
1146 |
|
---|
1147 | if(result)
|
---|
1148 | return result;
|
---|
1149 |
|
---|
1150 | *n = (size_t)n1;
|
---|
1151 |
|
---|
1152 | return CURLE_OK;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | /*
|
---|
1156 | * Sends data over the connected socket. Use after successful
|
---|
1157 | * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
|
---|
1158 | */
|
---|
1159 | CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
|
---|
1160 | size_t buflen, size_t *n)
|
---|
1161 | {
|
---|
1162 | curl_socket_t sfd;
|
---|
1163 | CURLcode result;
|
---|
1164 | ssize_t n1;
|
---|
1165 | struct connectdata *c = NULL;
|
---|
1166 | SIGPIPE_VARIABLE(pipe_st);
|
---|
1167 |
|
---|
1168 | if(Curl_is_in_callback(data))
|
---|
1169 | return CURLE_RECURSIVE_API_CALL;
|
---|
1170 |
|
---|
1171 | result = easy_connection(data, &sfd, &c);
|
---|
1172 | if(result)
|
---|
1173 | return result;
|
---|
1174 |
|
---|
1175 | if(!data->conn)
|
---|
1176 | /* on first invoke, the transfer has been detached from the connection and
|
---|
1177 | needs to be reattached */
|
---|
1178 | Curl_attach_connection(data, c);
|
---|
1179 |
|
---|
1180 | *n = 0;
|
---|
1181 | sigpipe_ignore(data, &pipe_st);
|
---|
1182 | result = Curl_write(data, sfd, buffer, buflen, &n1);
|
---|
1183 | sigpipe_restore(&pipe_st);
|
---|
1184 |
|
---|
1185 | if(n1 == -1)
|
---|
1186 | return CURLE_SEND_ERROR;
|
---|
1187 |
|
---|
1188 | /* detect EAGAIN */
|
---|
1189 | if(!result && !n1)
|
---|
1190 | return CURLE_AGAIN;
|
---|
1191 |
|
---|
1192 | *n = (size_t)n1;
|
---|
1193 |
|
---|
1194 | return result;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | /*
|
---|
1198 | * Wrapper to call functions in Curl_conncache_foreach()
|
---|
1199 | *
|
---|
1200 | * Returns always 0.
|
---|
1201 | */
|
---|
1202 | static int conn_upkeep(struct Curl_easy *data,
|
---|
1203 | struct connectdata *conn,
|
---|
1204 | void *param)
|
---|
1205 | {
|
---|
1206 | /* Param is unused. */
|
---|
1207 | (void)param;
|
---|
1208 |
|
---|
1209 | if(conn->handler->connection_check) {
|
---|
1210 | /* briefly attach the connection to this transfer for the purpose of
|
---|
1211 | checking it */
|
---|
1212 | Curl_attach_connection(data, conn);
|
---|
1213 |
|
---|
1214 | /* Do a protocol-specific keepalive check on the connection. */
|
---|
1215 | conn->handler->connection_check(data, conn, CONNCHECK_KEEPALIVE);
|
---|
1216 | /* detach the connection again */
|
---|
1217 | Curl_detach_connection(data);
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | return 0; /* continue iteration */
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | static CURLcode upkeep(struct conncache *conn_cache, void *data)
|
---|
1224 | {
|
---|
1225 | /* Loop over every connection and make connection alive. */
|
---|
1226 | Curl_conncache_foreach(data,
|
---|
1227 | conn_cache,
|
---|
1228 | data,
|
---|
1229 | conn_upkeep);
|
---|
1230 | return CURLE_OK;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | /*
|
---|
1234 | * Performs connection upkeep for the given session handle.
|
---|
1235 | */
|
---|
1236 | CURLcode curl_easy_upkeep(struct Curl_easy *data)
|
---|
1237 | {
|
---|
1238 | /* Verify that we got an easy handle we can work with. */
|
---|
1239 | if(!GOOD_EASY_HANDLE(data))
|
---|
1240 | return CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
1241 |
|
---|
1242 | if(data->multi_easy) {
|
---|
1243 | /* Use the common function to keep connections alive. */
|
---|
1244 | return upkeep(&data->multi_easy->conn_cache, data);
|
---|
1245 | }
|
---|
1246 | else {
|
---|
1247 | /* No connections, so just return success */
|
---|
1248 | return CURLE_OK;
|
---|
1249 | }
|
---|
1250 | }
|
---|