1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
26 * See comment in curl_memory.h for the explanation of this sanity check.
29 #ifdef CURLX_NO_MEMORY_CALLBACKS
30 #error "libcurl shall not ever be built with CURLX_NO_MEMORY_CALLBACKS defined"
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
54 #include <curl/curl.h>
56 #include "vtls/vtls.h"
66 #include "sendf.h" /* for failf function prototype */
67 #include "connect.h" /* for Curl_getconnectinfo */
71 #include "non-ascii.h"
77 #include "http_digest.h"
79 /* The last 3 #include files should be in this order */
80 #include "curl_printf.h"
81 #include "curl_memory.h"
83 #ifdef USE_TIZEN_FEATURE_DLP
84 #include "extensions/curl_extensions.h"
87 void Curl_version_init(void);
89 /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
91 static void win32_cleanup(void)
96 #ifdef USE_WINDOWS_SSPI
97 Curl_sspi_global_cleanup();
101 /* win32_init() performs win32 socket initialization to properly setup the
102 stack to allow networking */
103 static CURLcode win32_init(void)
106 WORD wVersionRequested;
110 #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
111 Error IPV6_requires_winsock2
114 wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
116 res = WSAStartup(wVersionRequested, &wsaData);
119 /* Tell the user that we couldn't find a useable */
121 return CURLE_FAILED_INIT;
123 /* Confirm that the Windows Sockets DLL supports what we need.*/
124 /* Note that if the DLL supports versions greater */
125 /* than wVersionRequested, it will still return */
126 /* wVersionRequested in wVersion. wHighVersion contains the */
127 /* highest supported version. */
129 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
130 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
131 /* Tell the user that we couldn't find a useable */
135 return CURLE_FAILED_INIT;
137 /* The Windows Sockets DLL is acceptable. Proceed. */
138 #elif defined(USE_LWIPSOCK)
142 #ifdef USE_WINDOWS_SSPI
144 CURLcode result = Curl_sspi_global_init();
153 /* true globals -- for curl_global_init() and curl_global_cleanup() */
154 static unsigned int initialized;
155 static long init_flags;
158 * strdup (and other memory functions) is redefined in complicated
159 * ways, but at this point it must be defined as the system-supplied strdup
160 * so the callback pointer is initialized correctly.
162 #if defined(_WIN32_WCE)
163 #define system_strdup _strdup
164 #elif !defined(HAVE_STRDUP)
165 #define system_strdup curlx_strdup
167 #define system_strdup strdup
170 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
171 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
174 #ifndef __SYMBIAN32__
176 * If a memory-using function (like curl_getenv) is used before
177 * curl_global_init() is called, we need to have these pointers set already.
179 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
180 curl_free_callback Curl_cfree = (curl_free_callback)free;
181 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
182 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
183 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
184 #if defined(WIN32) && defined(UNICODE)
185 curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
189 * Symbian OS doesn't support initialization to code in writable static data.
190 * Initialization will occur in the curl_global_init() call.
192 curl_malloc_callback Curl_cmalloc;
193 curl_free_callback Curl_cfree;
194 curl_realloc_callback Curl_crealloc;
195 curl_strdup_callback Curl_cstrdup;
196 curl_calloc_callback Curl_ccalloc;
199 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
200 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
204 * curl_global_init() globally initializes curl given a bitwise set of the
205 * different features of what to initialize.
207 static CURLcode global_init(long flags, bool memoryfuncs)
212 #ifdef USE_TIZEN_FEATURE_DLP
214 * Initialize Tizen DLP
216 curl_extensions_tizen_dlp_init();
220 /* Setup the default memory functions here (again) */
221 Curl_cmalloc = (curl_malloc_callback)malloc;
222 Curl_cfree = (curl_free_callback)free;
223 Curl_crealloc = (curl_realloc_callback)realloc;
224 Curl_cstrdup = (curl_strdup_callback)system_strdup;
225 Curl_ccalloc = (curl_calloc_callback)calloc;
226 #if defined(WIN32) && defined(UNICODE)
227 Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
231 if(!Curl_ssl_init()) {
232 DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
233 return CURLE_FAILED_INIT;
236 if(flags & CURL_GLOBAL_WIN32)
238 DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
239 return CURLE_FAILED_INIT;
243 if(!Curl_amiga_init()) {
244 DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
245 return CURLE_FAILED_INIT;
251 DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n"));
255 if(Curl_resolver_global_init()) {
256 DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
257 return CURLE_FAILED_INIT;
260 (void)Curl_ipv6works();
262 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_INIT)
263 if(libssh2_init(0)) {
264 DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
265 return CURLE_FAILED_INIT;
269 #if defined(USE_LIBSSH)
271 DEBUGF(fprintf(stderr, "Error: libssh_init failed\n"));
272 return CURLE_FAILED_INIT;
276 if(flags & CURL_GLOBAL_ACK_EINTR)
288 * curl_global_init() globally initializes curl given a bitwise set of the
289 * different features of what to initialize.
291 CURLcode curl_global_init(long flags)
293 return global_init(flags, TRUE);
297 * curl_global_init_mem() globally initializes curl and also registers the
298 * user provided callback routines.
300 CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
301 curl_free_callback f, curl_realloc_callback r,
302 curl_strdup_callback s, curl_calloc_callback c)
304 /* Invalid input, return immediately */
305 if(!m || !f || !r || !s || !c)
306 return CURLE_FAILED_INIT;
309 /* Already initialized, don't do it again, but bump the variable anyway to
310 work like curl_global_init() and require the same amount of cleanup
316 /* set memory functions before global_init() in case it wants memory
324 /* Call the actual init function, but without setting */
325 return global_init(flags, FALSE);
329 * curl_global_cleanup() globally cleanups curl, uses the value of
330 * "init_flags" to determine what needs to be cleaned up and what doesn't.
332 void curl_global_cleanup(void)
340 Curl_global_host_cache_dtor();
342 Curl_resolver_global_cleanup();
344 if(init_flags & CURL_GLOBAL_WIN32)
347 Curl_amiga_cleanup();
349 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT)
350 (void)libssh2_exit();
353 #if defined(USE_LIBSSH)
354 (void)ssh_finalize();
361 * curl_easy_init() is the external interface to alloc, setup and init an
362 * easy handle that is returned. If anything goes wrong, NULL is returned.
364 struct Curl_easy *curl_easy_init(void)
367 struct Curl_easy *data;
369 /* Make sure we inited the global SSL stuff */
371 result = curl_global_init(CURL_GLOBAL_DEFAULT);
373 /* something in the global init failed, return nothing */
374 DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
379 /* We use curl_open() with undefined URL so far */
380 result = Curl_open(&data);
382 DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
391 struct socketmonitor {
392 struct socketmonitor *next; /* the next node in the list or NULL */
393 struct pollfd socket; /* socket info of what to monitor */
397 long ms; /* timeout, run the timeout function when reached */
398 bool msbump; /* set TRUE when timeout is set by callback */
399 int num_sockets; /* number of nodes in the monitor list */
400 struct socketmonitor *list; /* list of sockets to monitor */
401 int running_handles; /* store the returned number */
406 * Callback that gets called with a new value when the timeout should be
410 static int events_timer(struct Curl_multi *multi, /* multi handle */
411 long timeout_ms, /* see above */
412 void *userp) /* private callback pointer */
414 struct events *ev = userp;
417 /* timeout removed */
419 else if(timeout_ms == 0)
420 /* timeout is already reached! */
421 timeout_ms = 1; /* trigger asap */
431 * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
433 static int poll2cselect(int pollmask)
436 if(pollmask & POLLIN)
437 omask |= CURL_CSELECT_IN;
438 if(pollmask & POLLOUT)
439 omask |= CURL_CSELECT_OUT;
440 if(pollmask & POLLERR)
441 omask |= CURL_CSELECT_ERR;
448 * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
450 static short socketcb2poll(int pollmask)
453 if(pollmask & CURL_POLL_IN)
455 if(pollmask & CURL_POLL_OUT)
462 * Callback that gets called with information about socket activity to
465 static int events_socket(struct Curl_easy *easy, /* easy handle */
466 curl_socket_t s, /* socket */
467 int what, /* see above */
468 void *userp, /* private callback
470 void *socketp) /* private socket
473 struct events *ev = userp;
474 struct socketmonitor *m;
475 struct socketmonitor *prev = NULL;
477 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
484 if(m->socket.fd == s) {
486 if(what == CURL_POLL_REMOVE) {
487 struct socketmonitor *nxt = m->next;
488 /* remove this node from the list of monitored sockets */
495 infof(easy, "socket cb: socket %d REMOVED\n", s);
498 /* The socket 's' is already being monitored, update the activity
499 mask. Convert from libcurl bitmask to the poll one. */
500 m->socket.events = socketcb2poll(what);
501 infof(easy, "socket cb: socket %d UPDATED as %s%s\n", s,
502 what&CURL_POLL_IN?"IN":"",
503 what&CURL_POLL_OUT?"OUT":"");
508 m = m->next; /* move to next node */
511 if(what == CURL_POLL_REMOVE) {
512 /* this happens a bit too often, libcurl fix perhaps? */
514 "%s: socket %d asked to be REMOVED but not present!\n",
518 m = malloc(sizeof(struct socketmonitor));
522 m->socket.events = socketcb2poll(what);
523 m->socket.revents = 0;
525 infof(easy, "socket cb: socket %d ADDED as %s%s\n", s,
526 what&CURL_POLL_IN?"IN":"",
527 what&CURL_POLL_OUT?"OUT":"");
530 return CURLE_OUT_OF_MEMORY;
541 * Do the multi handle setups that only event-based transfers need.
543 static void events_setup(struct Curl_multi *multi, struct events *ev)
546 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
547 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
549 /* socket callback */
550 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
551 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
557 * waits for activity on any of the given sockets, or the timeout to trigger.
560 static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
563 CURLMcode mcode = CURLM_OK;
564 CURLcode result = CURLE_OK;
568 struct socketmonitor *m;
570 struct pollfd fds[4];
574 struct curltime before;
575 struct curltime after;
577 /* populate the fds[] array */
578 for(m = ev->list, f = &fds[0]; m; m = m->next) {
579 f->fd = m->socket.fd;
580 f->events = m->socket.events;
582 /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
587 /* get the time stamp to use to figure out how long poll takes */
590 /* wait for activity or timeout */
591 pollrc = Curl_poll(fds, numfds, (int)ev->ms);
595 ev->msbump = FALSE; /* reset here */
600 /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
601 mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
602 &ev->running_handles);
604 else if(pollrc > 0) {
605 /* loop over the monitored sockets to see which ones had activity */
606 for(i = 0; i< numfds; i++) {
608 /* socket activity, tell libcurl */
609 int act = poll2cselect(fds[i].revents); /* convert */
610 infof(multi->easyp, "call curl_multi_socket_action(socket %d)\n",
612 mcode = curl_multi_socket_action(multi, fds[i].fd, act,
613 &ev->running_handles);
618 /* If nothing updated the timeout, we decrease it by the spent time.
619 * If it was updated, it has the new timeout time stored already.
621 timediff_t timediff = Curl_timediff(after, before);
623 if(timediff > ev->ms)
626 ev->ms -= (long)timediff;
631 return CURLE_RECV_ERROR;
634 return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
636 /* we don't really care about the "msgs_in_queue" value returned in the
638 msg = curl_multi_info_read(multi, &pollrc);
640 result = msg->data.result;
651 * Runs a transfer in a blocking manner using the events-based API
653 static CURLcode easy_events(struct Curl_multi *multi)
655 /* this struct is made static to allow it to be used after this function
656 returns and curl_multi_remove_handle() is called */
657 static struct events evs = {2, FALSE, 0, NULL, 0};
659 /* if running event-based, do some further multi inits */
660 events_setup(multi, &evs);
662 return wait_or_timeout(multi, &evs);
664 #else /* CURLDEBUG */
665 /* when not built with debug, this function doesn't exist */
666 #define easy_events(x) CURLE_NOT_BUILT_IN
669 static CURLcode easy_transfer(struct Curl_multi *multi)
672 CURLMcode mcode = CURLM_OK;
673 CURLcode result = CURLE_OK;
674 struct curltime before;
675 int without_fds = 0; /* count number of consecutive returns from
676 curl_multi_wait() without any filedescriptors */
678 while(!done && !mcode) {
679 int still_running = 0;
683 mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
687 struct curltime after = Curl_now();
689 /* If it returns without any filedescriptor instantly, we need to
690 avoid busy-looping during periods where it has nothing particular
692 if(Curl_timediff(after, before) <= 10) {
694 if(without_fds > 2) {
695 int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000;
696 Curl_wait_ms(sleep_ms);
700 /* it wasn't "instant", restart counter */
704 /* got file descriptor, restart counter */
707 mcode = curl_multi_perform(multi, &still_running);
710 /* only read 'still_running' if curl_multi_perform() return OK */
711 if(!mcode && !still_running) {
712 CURLMsg *msg = curl_multi_info_read(multi, &rc);
714 result = msg->data.result;
720 /* Make sure to return some kind of error if there was a multi problem */
722 result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
723 /* The other multi errors should never happen, so return
724 something suitably generic */
725 CURLE_BAD_FUNCTION_ARGUMENT;
733 * easy_perform() is the external interface that performs a blocking
734 * transfer as previously setup.
736 * CONCEPT: This function creates a multi handle, adds the easy handle to it,
737 * runs curl_multi_perform() until the transfer is done, then detaches the
738 * easy handle, destroys the multi handle and returns the easy handle's return
741 * REALITY: it can't just create and destroy the multi handle that easily. It
742 * needs to keep it around since if this easy handle is used again by this
743 * function, the same multi handle must be re-used so that the same pools and
744 * caches can be used.
746 * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
747 * instead of curl_multi_perform() and use curl_multi_socket_action().
749 static CURLcode easy_perform(struct Curl_easy *data, bool events)
751 struct Curl_multi *multi;
753 CURLcode result = CURLE_OK;
754 SIGPIPE_VARIABLE(pipe_st);
757 return CURLE_BAD_FUNCTION_ARGUMENT;
760 failf(data, "easy handle already used in multi handle");
761 return CURLE_FAILED_INIT;
765 multi = data->multi_easy;
767 /* this multi handle will only ever have a single easy handled attached
768 to it, so make it use minimal hashes */
769 multi = Curl_multi_handle(1, 3);
771 return CURLE_OUT_OF_MEMORY;
772 data->multi_easy = multi;
775 if(multi->in_callback)
776 return CURLE_RECURSIVE_API_CALL;
778 /* Copy the MAXCONNECTS option to the multi handle */
779 curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
781 mcode = curl_multi_add_handle(multi, data);
783 curl_multi_cleanup(multi);
784 if(mcode == CURLM_OUT_OF_MEMORY)
785 return CURLE_OUT_OF_MEMORY;
786 return CURLE_FAILED_INIT;
789 sigpipe_ignore(data, &pipe_st);
791 /* assign this after curl_multi_add_handle() since that function checks for
792 it and rejects this handle otherwise */
795 /* run the transfer */
796 result = events ? easy_events(multi) : easy_transfer(multi);
798 /* ignoring the return code isn't nice, but atm we can't really handle
799 a failure here, room for future improvement! */
800 (void)curl_multi_remove_handle(multi, data);
802 sigpipe_restore(&pipe_st);
804 /* The multi handle is kept alive, owned by the easy handle */
810 * curl_easy_perform() is the external interface that performs a blocking
811 * transfer as previously setup.
813 CURLcode curl_easy_perform(struct Curl_easy *data)
815 return easy_perform(data, FALSE);
820 * curl_easy_perform_ev() is the external interface that performs a blocking
821 * transfer using the event-based API internally.
823 CURLcode curl_easy_perform_ev(struct Curl_easy *data)
825 return easy_perform(data, TRUE);
831 * curl_easy_cleanup() is the external interface to cleaning/freeing the given
834 void curl_easy_cleanup(struct Curl_easy *data)
836 SIGPIPE_VARIABLE(pipe_st);
841 sigpipe_ignore(data, &pipe_st);
843 sigpipe_restore(&pipe_st);
847 * curl_easy_getinfo() is an external interface that allows an app to retrieve
848 * information from a performed transfer and similar.
850 #undef curl_easy_getinfo
851 CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
858 paramp = va_arg(arg, void *);
860 result = Curl_getinfo(data, info, paramp);
866 static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
868 CURLcode result = CURLE_OK;
871 /* Copy src->set into dst->set first, then deal with the strings
874 Curl_mime_initpart(&dst->set.mimepost, dst);
876 /* clear all string pointers first */
877 memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
879 /* duplicate all strings */
880 for(i = (enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
881 result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
886 /* duplicate memory areas pointed to */
887 i = STRING_COPYPOSTFIELDS;
888 if(src->set.postfieldsize && src->set.str[i]) {
889 /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
890 dst->set.str[i] = Curl_memdup(src->set.str[i],
891 curlx_sotouz(src->set.postfieldsize));
893 return CURLE_OUT_OF_MEMORY;
894 /* point to the new copy */
895 dst->set.postfields = dst->set.str[i];
898 /* Duplicate mime data. */
899 result = Curl_mime_duppart(&dst->set.mimepost, &src->set.mimepost);
905 * curl_easy_duphandle() is an external interface to allow duplication of a
906 * given input easy handle. The returned handle will be a new working handle
907 * with all options set exactly as the input source handle.
909 struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
911 struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
916 * We setup a few buffers we need. We should probably make them
917 * get setup on-demand in the code, as that would probably decrease
918 * the likeliness of us forgetting to init a buffer here in the future.
920 outcurl->set.buffer_size = data->set.buffer_size;
921 outcurl->state.buffer = malloc(outcurl->set.buffer_size + 1);
922 if(!outcurl->state.buffer)
925 outcurl->state.headerbuff = malloc(HEADERSIZE);
926 if(!outcurl->state.headerbuff)
928 outcurl->state.headersize = HEADERSIZE;
930 /* copy all userdefined values */
931 if(dupset(outcurl, data))
934 /* the connection cache is setup on demand */
935 outcurl->state.conn_cache = NULL;
937 outcurl->state.lastconnect = NULL;
939 outcurl->progress.flags = data->progress.flags;
940 outcurl->progress.callback = data->progress.callback;
943 /* If cookies are enabled in the parent handle, we enable them
944 in the clone as well! */
945 outcurl->cookies = Curl_cookie_init(data,
946 data->cookies->filename,
948 data->set.cookiesession);
949 if(!outcurl->cookies)
953 /* duplicate all values in 'change' */
954 if(data->change.cookielist) {
955 outcurl->change.cookielist =
956 Curl_slist_duplicate(data->change.cookielist);
957 if(!outcurl->change.cookielist)
961 if(data->change.url) {
962 outcurl->change.url = strdup(data->change.url);
963 if(!outcurl->change.url)
965 outcurl->change.url_alloc = TRUE;
968 if(data->change.referer) {
969 outcurl->change.referer = strdup(data->change.referer);
970 if(!outcurl->change.referer)
972 outcurl->change.referer_alloc = TRUE;
975 /* Clone the resolver handle, if present, for the new handle */
976 if(Curl_resolver_duphandle(&outcurl->state.resolver,
977 data->state.resolver))
980 Curl_convert_setup(outcurl);
982 Curl_initinfo(outcurl);
984 outcurl->magic = CURLEASY_MAGIC_NUMBER;
986 /* we reach this point and thus we are OK */
993 curl_slist_free_all(outcurl->change.cookielist);
994 outcurl->change.cookielist = NULL;
995 Curl_safefree(outcurl->state.buffer);
996 Curl_safefree(outcurl->state.headerbuff);
997 Curl_safefree(outcurl->change.url);
998 Curl_safefree(outcurl->change.referer);
999 Curl_freeset(outcurl);
1007 * curl_easy_reset() is an external interface that allows an app to re-
1008 * initialize a session handle to the default values.
1010 void curl_easy_reset(struct Curl_easy *data)
1012 Curl_safefree(data->state.pathbuffer);
1014 data->state.path = NULL;
1016 Curl_free_request_state(data);
1018 /* zero out UserDefined data: */
1020 memset(&data->set, 0, sizeof(struct UserDefined));
1021 (void)Curl_init_userdefined(data);
1023 /* zero out Progress data: */
1024 memset(&data->progress, 0, sizeof(struct Progress));
1026 /* zero out PureInfo data: */
1027 Curl_initinfo(data);
1029 data->progress.flags |= PGRS_HIDE;
1030 data->state.current_speed = -1; /* init to negative == impossible */
1032 /* zero out authentication data: */
1033 memset(&data->state.authhost, 0, sizeof(struct auth));
1034 memset(&data->state.authproxy, 0, sizeof(struct auth));
1035 Curl_digest_cleanup(data);
1039 * curl_easy_pause() allows an application to pause or unpause a specific
1040 * transfer and direction. This function sets the full new state for the
1041 * current connection this easy handle operates on.
1043 * NOTE: if you have the receiving paused and you call this function to remove
1044 * the pausing, you may get your write callback called at this point.
1046 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1048 * NOTE: This is one of few API functions that are allowed to be called from
1049 * within a callback.
1051 CURLcode curl_easy_pause(struct Curl_easy *data, int action)
1053 struct SingleRequest *k = &data->req;
1054 CURLcode result = CURLE_OK;
1056 /* first switch off both pause bits */
1057 int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1059 /* set the new desired pause bits */
1060 newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
1061 ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
1063 /* put it back in the keepon */
1064 k->keepon = newstate;
1066 if(!(newstate & KEEP_RECV_PAUSE) && data->state.tempcount) {
1067 /* there are buffers for sending that can be delivered as the receive
1068 pausing is lifted! */
1070 unsigned int count = data->state.tempcount;
1071 struct tempbuf writebuf[3]; /* there can only be three */
1072 struct connectdata *conn = data->easy_conn;
1073 struct Curl_easy *saved_data = NULL;
1075 /* copy the structs to allow for immediate re-pausing */
1076 for(i = 0; i < data->state.tempcount; i++) {
1077 writebuf[i] = data->state.tempwrite[i];
1078 data->state.tempwrite[i].buf = NULL;
1080 data->state.tempcount = 0;
1082 /* set the connection's current owner */
1083 if(conn->data != data) {
1084 saved_data = conn->data;
1088 for(i = 0; i < count; i++) {
1089 /* even if one function returns error, this loops through and frees all
1092 result = Curl_client_write(conn, writebuf[i].type, writebuf[i].buf,
1094 free(writebuf[i].buf);
1097 /* recover previous owner of the connection */
1099 conn->data = saved_data;
1105 /* if there's no error and we're not pausing both directions, we want
1106 to have this handle checked soon */
1108 ((newstate&(KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
1109 (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) )
1110 Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
1116 static CURLcode easy_connection(struct Curl_easy *data,
1118 struct connectdata **connp)
1121 return CURLE_BAD_FUNCTION_ARGUMENT;
1123 /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1124 if(!data->set.connect_only) {
1125 failf(data, "CONNECT_ONLY is required!");
1126 return CURLE_UNSUPPORTED_PROTOCOL;
1129 *sfd = Curl_getconnectinfo(data, connp);
1131 if(*sfd == CURL_SOCKET_BAD) {
1132 failf(data, "Failed to get recent socket");
1133 return CURLE_UNSUPPORTED_PROTOCOL;
1140 * Receives data from the connected socket. Use after successful
1141 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1142 * Returns CURLE_OK on success, error code on error.
1144 CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
1150 struct connectdata *c;
1152 if(Curl_is_in_callback(data))
1153 return CURLE_RECURSIVE_API_CALL;
1155 result = easy_connection(data, &sfd, &c);
1160 result = Curl_read(c, sfd, buffer, buflen, &n1);
1171 * Sends data over the connected socket. Use after successful
1172 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1174 CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
1175 size_t buflen, size_t *n)
1180 struct connectdata *c = NULL;
1182 if(Curl_is_in_callback(data))
1183 return CURLE_RECURSIVE_API_CALL;
1185 result = easy_connection(data, &sfd, &c);
1190 result = Curl_write(c, sfd, buffer, buflen, &n1);
1193 return CURLE_SEND_ERROR;