Git init
[external/curl.git] / docs / examples / multi-post.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * This is an example application source code using the multi interface
10  * to do a multipart formpost without "blocking".
11  */
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/time.h>
15
16 #include <curl/curl.h>
17
18 int main(int argc, char *argv[])
19 {
20   CURL *curl;
21
22   CURLM *multi_handle;
23   int still_running;
24
25   struct curl_httppost *formpost=NULL;
26   struct curl_httppost *lastptr=NULL;
27   struct curl_slist *headerlist=NULL;
28   static const char buf[] = "Expect:";
29
30   /* Fill in the file upload field. This makes libcurl load data from
31      the given file name when curl_easy_perform() is called. */
32   curl_formadd(&formpost,
33                &lastptr,
34                CURLFORM_COPYNAME, "sendfile",
35                CURLFORM_FILE, "postit2.c",
36                CURLFORM_END);
37
38   /* Fill in the filename field */
39   curl_formadd(&formpost,
40                &lastptr,
41                CURLFORM_COPYNAME, "filename",
42                CURLFORM_COPYCONTENTS, "postit2.c",
43                CURLFORM_END);
44
45   /* Fill in the submit field too, even if this is rarely needed */
46   curl_formadd(&formpost,
47                &lastptr,
48                CURLFORM_COPYNAME, "submit",
49                CURLFORM_COPYCONTENTS, "send",
50                CURLFORM_END);
51
52   curl = curl_easy_init();
53   multi_handle = curl_multi_init();
54
55   /* initalize custom header list (stating that Expect: 100-continue is not
56      wanted */
57   headerlist = curl_slist_append(headerlist, buf);
58   if(curl && multi_handle) {
59
60     /* what URL that receives this POST */
61     curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
62     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
63
64     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
65     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
66
67     curl_multi_add_handle(multi_handle, curl);
68
69     curl_multi_perform(multi_handle, &still_running);
70
71     while(still_running) {
72       struct timeval timeout;
73       int rc; /* select() return code */
74
75       fd_set fdread;
76       fd_set fdwrite;
77       fd_set fdexcep;
78       int maxfd = -1;
79
80       long curl_timeo = -1;
81
82       FD_ZERO(&fdread);
83       FD_ZERO(&fdwrite);
84       FD_ZERO(&fdexcep);
85
86       /* set a suitable timeout to play around with */
87       timeout.tv_sec = 1;
88       timeout.tv_usec = 0;
89
90       curl_multi_timeout(multi_handle, &curl_timeo);
91       if(curl_timeo >= 0) {
92         timeout.tv_sec = curl_timeo / 1000;
93         if(timeout.tv_sec > 1)
94           timeout.tv_sec = 1;
95         else
96           timeout.tv_usec = (curl_timeo % 1000) * 1000;
97       }
98
99       /* get file descriptors from the transfers */
100       curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
101
102       /* In a real-world program you OF COURSE check the return code of the
103          function calls.  On success, the value of maxfd is guaranteed to be
104          greater or equal than -1.  We call select(maxfd + 1, ...), specially in
105          case of (maxfd == -1), we call select(0, ...), which is basically equal
106          to sleep. */
107
108       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
109
110       switch(rc) {
111       case -1:
112         /* select error */
113         break;
114       case 0:
115       default:
116         /* timeout or readable/writable sockets */
117         printf("perform!\n");
118         curl_multi_perform(multi_handle, &still_running);
119         printf("running: %d!\n", still_running);
120         break;
121       }
122     }
123
124     curl_multi_cleanup(multi_handle);
125
126     /* always cleanup */
127     curl_easy_cleanup(curl);
128
129     /* then cleanup the formpost chain */
130     curl_formfree(formpost);
131
132     /* free slist */
133     curl_slist_free_all (headerlist);
134   }
135   return 0;
136 }