]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/docs/examples/multi-post.c
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / curl / docs / examples / multi-post.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id: multi-post.c,v 1.1 2002/05/06 13:38:28 bagder Exp $
9  *
10  * This is an example application source code using the multi interface
11  * to do a multipart formpost without "blocking".
12  */
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/time.h>
16
17 #include <curl/curl.h>
18
19 int main(int argc, char *argv[])
20 {
21   CURL *curl;
22   CURLcode res;
23
24   CURLM *multi_handle;
25   int still_running;
26
27   struct HttpPost *formpost=NULL;
28   struct HttpPost *lastptr=NULL;
29   struct curl_slist *headerlist=NULL;
30   char buf[] = "Expect:";
31
32   /* Fill in the file upload field */
33   curl_formadd(&formpost,
34                &lastptr,
35                CURLFORM_COPYNAME, "sendfile",
36                CURLFORM_FILE, "postit2.c",
37                CURLFORM_END);
38
39   /* Fill in the filename field */
40   curl_formadd(&formpost,
41                &lastptr,
42                CURLFORM_COPYNAME, "filename",
43                CURLFORM_COPYCONTENTS, "postit2.c",
44                CURLFORM_END);
45
46
47   /* Fill in the submit field too, even if this is rarely needed */
48   curl_formadd(&formpost,
49                &lastptr,
50                CURLFORM_COPYNAME, "submit",
51                CURLFORM_COPYCONTENTS, "send",
52                CURLFORM_END);
53
54   curl = curl_easy_init();
55   multi_handle = curl_multi_init();
56
57   /* initalize custom header list (stating that Expect: 100-continue is not
58      wanted */
59   headerlist = curl_slist_append(headerlist, buf);
60   if(curl && multi_handle) {
61     int perform=0;
62
63     /* what URL that receives this POST */
64     curl_easy_setopt(curl, CURLOPT_URL,
65                      "http://www.fillinyoururl.com/upload.cgi");
66     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
67
68     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
69     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
70
71     curl_multi_add_handle(multi_handle, curl);
72
73     while(CURLM_CALL_MULTI_PERFORM ==
74           curl_multi_perform(multi_handle, &still_running));
75
76     while(still_running) {
77       struct timeval timeout;
78       int rc; /* select() return code */
79
80       fd_set fdread;
81       fd_set fdwrite;
82       fd_set fdexcep;
83       int maxfd;
84
85       FD_ZERO(&fdread);
86       FD_ZERO(&fdwrite);
87       FD_ZERO(&fdexcep);
88
89       /* set a suitable timeout to play around with */
90       timeout.tv_sec = 1;
91       timeout.tv_usec = 0;
92
93       /* get file descriptors from the transfers */
94       curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
95
96       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
97
98       switch(rc) {
99       case -1:
100         /* select error */
101         break;
102       case 0:
103         printf("timeout!\n");
104       default:
105         /* timeout or readable/writable sockets */
106         printf("perform!\n");
107         while(CURLM_CALL_MULTI_PERFORM ==
108               curl_multi_perform(multi_handle, &still_running));
109         printf("running: %d!\n", still_running);
110         break;
111       }
112     }
113
114     curl_multi_cleanup(multi_handle);
115
116     /* always cleanup */
117     curl_easy_cleanup(curl);
118
119     /* then cleanup the formpost chain */
120     curl_formfree(formpost);
121
122     /* free slist */
123     curl_slist_free_all (headerlist);
124   }
125   return 0;
126 }