multi-formadd.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, 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.haxx.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 /* <DESC>
23  * using the multi interface to do a multipart formpost without blocking
24  * </DESC>
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/time.h>
30 
31 #include <curl/curl.h>
32 
33 int main(void)
34 {
35  CURL *curl;
36 
38  int still_running;
39 
40  struct curl_httppost *formpost = NULL;
41  struct curl_httppost *lastptr = NULL;
42  struct curl_slist *headerlist = NULL;
43  static const char buf[] = "Expect:";
44 
45  /* Fill in the file upload field. This makes libcurl load data from
46  the given file name when curl_easy_perform() is called. */
47  curl_formadd(&formpost,
48  &lastptr,
49  CURLFORM_COPYNAME, "sendfile",
50  CURLFORM_FILE, "postit2.c",
51  CURLFORM_END);
52 
53  /* Fill in the filename field */
54  curl_formadd(&formpost,
55  &lastptr,
56  CURLFORM_COPYNAME, "filename",
57  CURLFORM_COPYCONTENTS, "postit2.c",
58  CURLFORM_END);
59 
60  /* Fill in the submit field too, even if this is rarely needed */
61  curl_formadd(&formpost,
62  &lastptr,
63  CURLFORM_COPYNAME, "submit",
64  CURLFORM_COPYCONTENTS, "send",
65  CURLFORM_END);
66 
67  curl = curl_easy_init();
68  multi_handle = curl_multi_init();
69 
70  /* initialize custom header list (stating that Expect: 100-continue is not
71  wanted */
72  headerlist = curl_slist_append(headerlist, buf);
73  if(curl && multi_handle) {
74 
75  /* what URL that receives this POST */
76  curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
77  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
78 
79  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
80  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
81 
82  curl_multi_add_handle(multi_handle, curl);
83 
84  curl_multi_perform(multi_handle, &still_running);
85 
86  do {
87  struct timeval timeout;
88  int rc; /* select() return code */
89  CURLMcode mc; /* curl_multi_fdset() return code */
90 
91  fd_set fdread;
92  fd_set fdwrite;
93  fd_set fdexcep;
94  int maxfd = -1;
95 
96  long curl_timeo = -1;
97 
98  FD_ZERO(&fdread);
99  FD_ZERO(&fdwrite);
100  FD_ZERO(&fdexcep);
101 
102  /* set a suitable timeout to play around with */
103  timeout.tv_sec = 1;
104  timeout.tv_usec = 0;
105 
106  curl_multi_timeout(multi_handle, &curl_timeo);
107  if(curl_timeo >= 0) {
108  timeout.tv_sec = curl_timeo / 1000;
109  if(timeout.tv_sec > 1)
110  timeout.tv_sec = 1;
111  else
112  timeout.tv_usec = (curl_timeo % 1000) * 1000;
113  }
114 
115  /* get file descriptors from the transfers */
116  mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
117 
118  if(mc != CURLM_OK) {
119  fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
120  break;
121  }
122 
123  /* On success the value of maxfd is guaranteed to be >= -1. We call
124  select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
125  no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
126  to sleep 100ms, which is the minimum suggested value in the
127  curl_multi_fdset() doc. */
128 
129  if(maxfd == -1) {
130 #ifdef _WIN32
131  Sleep(100);
132  rc = 0;
133 #else
134  /* Portable sleep for platforms other than Windows. */
135  struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
136  rc = select(0, NULL, NULL, NULL, &wait);
137 #endif
138  }
139  else {
140  /* Note that on some platforms 'timeout' may be modified by select().
141  If you need access to the original value save a copy beforehand. */
142  rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
143  }
144 
145  switch(rc) {
146  case -1:
147  /* select error */
148  break;
149  case 0:
150  default:
151  /* timeout or readable/writable sockets */
152  printf("perform!\n");
153  curl_multi_perform(multi_handle, &still_running);
154  printf("running: %d!\n", still_running);
155  break;
156  }
157  } while(still_running);
158 
159  curl_multi_cleanup(multi_handle);
160 
161  /* always cleanup */
162  curl_easy_cleanup(curl);
163 
164  /* then cleanup the formpost chain */
165  curl_formfree(formpost);
166 
167  /* free slist */
168  curl_slist_free_all(headerlist);
169  }
170  return 0;
171 }
CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *exc_fd_set, int *max_fd)
CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle, long *milliseconds)
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN CURLM * curl_multi_init(void)
Definition: multi.c:355
CURL_EXTERN void curl_formfree(struct curl_httppost *form)
Definition: formdata.c:800
static CURLM * multi_handle
Definition: fopen.c:91
CURL_EXTERN CURLFORMcode curl_formadd(struct curl_httppost **httppost, struct curl_httppost **last_post,...)
Definition: formdata.c:743
UNITTEST_START int rc
Definition: unit1301.c:31
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
int main(void)
Definition: multi-formadd.c:33
CURLMcode
Definition: multi.h:61
void CURLM
Definition: multi.h:58
char buf[3]
Definition: unit1398.c:32
void CURL
Definition: curl.h:102
Definition: multi.h:64
#define fprintf
Definition: curl_printf.h:41
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16