multi & hiper examples: updates and cleanups
[platform/upstream/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,
62                      "http://www.fillinyoururl.com/upload.cgi");
63     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
64
65     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
66     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
67
68     curl_multi_add_handle(multi_handle, curl);
69
70     curl_multi_perform(multi_handle, &still_running);
71
72     while(still_running) {
73       struct timeval timeout;
74       int rc; /* select() return code */
75
76       fd_set fdread;
77       fd_set fdwrite;
78       fd_set fdexcep;
79       int maxfd = -1;
80
81       long curl_timeo = -1;
82
83       FD_ZERO(&fdread);
84       FD_ZERO(&fdwrite);
85       FD_ZERO(&fdexcep);
86
87       /* set a suitable timeout to play around with */
88       timeout.tv_sec = 1;
89       timeout.tv_usec = 0;
90
91       curl_multi_timeout(multi_handle, &curl_timeo);
92       if(curl_timeo >= 0) {
93         timeout.tv_sec = curl_timeo / 1000;
94         if(timeout.tv_sec > 1)
95           timeout.tv_sec = 1;
96         else
97           timeout.tv_usec = (curl_timeo % 1000) * 1000;
98       }
99
100       /* get file descriptors from the transfers */
101       curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
102
103       /* In a real-world program you OF COURSE check the return code of the
104          function calls.  On success, the value of maxfd is guaranteed to be
105          greater or equal than -1.  We call select(maxfd + 1, ...), specially in
106          case of (maxfd == -1), we call select(0, ...), which is basically equal
107          to sleep. */
108
109       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
110
111       switch(rc) {
112       case -1:
113         /* select error */
114         break;
115       case 0:
116         printf("timeout!\n");
117       default:
118         /* timeout or readable/writable sockets */
119         printf("perform!\n");
120         curl_multi_perform(multi_handle, &still_running);
121         printf("running: %d!\n", still_running);
122         break;
123       }
124     }
125
126     curl_multi_cleanup(multi_handle);
127
128     /* always cleanup */
129     curl_easy_cleanup(curl);
130
131     /* then cleanup the formpost chain */
132     curl_formfree(formpost);
133
134     /* free slist */
135     curl_slist_free_all (headerlist);
136   }
137   return 0;
138 }