VirtualBox

source: vbox/trunk/src/libs/curl-8.3.0/lib/easy.c@ 101259

Last change on this file since 101259 was 101127, checked in by vboxsync, 19 months ago

curl-8.3.0: Applied and adjusted our curl changes to 8.0.1. bugref:10526

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette