multi-post.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  curl_mime *form = NULL;
41  curl_mimepart *field = NULL;
42  struct curl_slist *headerlist = NULL;
43  static const char buf[] = "Expect:";
44 
45  curl = curl_easy_init();
46  multi_handle = curl_multi_init();
47 
48  if(curl && multi_handle) {
49  /* Create the form */
50  form = curl_mime_init(curl);
51 
52  /* Fill in the file upload field */
53  field = curl_mime_addpart(form);
54  curl_mime_name(field, "sendfile");
55  curl_mime_filedata(field, "multi-post.c");
56 
57  /* Fill in the filename field */
58  field = curl_mime_addpart(form);
59  curl_mime_name(field, "filename");
60  curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED);
61 
62  /* Fill in the submit field too, even if this is rarely needed */
63  field = curl_mime_addpart(form);
64  curl_mime_name(field, "submit");
65  curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
66 
67  /* initialize custom header list (stating that Expect: 100-continue is not
68  wanted */
69  headerlist = curl_slist_append(headerlist, buf);
70 
71  /* what URL that receives this POST */
72  curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
73  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
74 
75  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
76  curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
77 
78  curl_multi_add_handle(multi_handle, curl);
79 
80  curl_multi_perform(multi_handle, &still_running);
81 
82  do {
83  struct timeval timeout;
84  int rc; /* select() return code */
85  CURLMcode mc; /* curl_multi_fdset() return code */
86 
87  fd_set fdread;
88  fd_set fdwrite;
89  fd_set fdexcep;
90  int maxfd = -1;
91 
92  long curl_timeo = -1;
93 
94  FD_ZERO(&fdread);
95  FD_ZERO(&fdwrite);
96  FD_ZERO(&fdexcep);
97 
98  /* set a suitable timeout to play around with */
99  timeout.tv_sec = 1;
100  timeout.tv_usec = 0;
101 
102  curl_multi_timeout(multi_handle, &curl_timeo);
103  if(curl_timeo >= 0) {
104  timeout.tv_sec = curl_timeo / 1000;
105  if(timeout.tv_sec > 1)
106  timeout.tv_sec = 1;
107  else
108  timeout.tv_usec = (curl_timeo % 1000) * 1000;
109  }
110 
111  /* get file descriptors from the transfers */
112  mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
113 
114  if(mc != CURLM_OK) {
115  fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
116  break;
117  }
118 
119  /* On success the value of maxfd is guaranteed to be >= -1. We call
120  select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
121  no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
122  to sleep 100ms, which is the minimum suggested value in the
123  curl_multi_fdset() doc. */
124 
125  if(maxfd == -1) {
126 #ifdef _WIN32
127  Sleep(100);
128  rc = 0;
129 #else
130  /* Portable sleep for platforms other than Windows. */
131  struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
132  rc = select(0, NULL, NULL, NULL, &wait);
133 #endif
134  }
135  else {
136  /* Note that on some platforms 'timeout' may be modified by select().
137  If you need access to the original value save a copy beforehand. */
138  rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
139  }
140 
141  switch(rc) {
142  case -1:
143  /* select error */
144  break;
145  case 0:
146  default:
147  /* timeout or readable/writable sockets */
148  printf("perform!\n");
149  curl_multi_perform(multi_handle, &still_running);
150  printf("running: %d!\n", still_running);
151  break;
152  }
153  } while(still_running);
154 
155  curl_multi_cleanup(multi_handle);
156 
157  /* always cleanup */
158  curl_easy_cleanup(curl);
159 
160  /* then cleanup the form */
161  curl_mime_free(form);
162 
163  /* free slist */
164  curl_slist_free_all(headerlist);
165  }
166  return 0;
167 }
CURL_EXTERN CURLcode curl_mime_filedata(curl_mimepart *part, const char *filename)
Definition: mime.c:1262
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 void curl_mime_free(curl_mime *mime)
Definition: mime.c:1110
CURL_EXTERN CURLcode curl_mime_data(curl_mimepart *part, const char *data, size_t datasize)
Definition: mime.c:1230
CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle, long *milliseconds)
CURL_EXTERN curl_mime * curl_mime_init(CURL *easy)
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN CURLM * curl_multi_init(void)
Definition: multi.c:355
static CURLM * multi_handle
Definition: fopen.c:91
CURL_EXTERN CURLcode curl_mime_name(curl_mimepart *part, const char *name)
Definition: mime.c:1194
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-post.c:33
CURLMcode
Definition: multi.h:61
CURL_EXTERN curl_mimepart * curl_mime_addpart(curl_mime *mime)
Definition: mime.c:1169
void CURLM
Definition: multi.h:58
char buf[3]
Definition: unit1398.c:32
void CURL
Definition: curl.h:102
Definition: multi.h:64
#define CURL_ZERO_TERMINATED
Definition: curl.h:1966
#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