doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multi-post.c
Go to the documentation of this file.
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 
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 
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 }
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)
Definition: multi.c:231
long tv_sec
Definition: timeval.h:37
CURLFORMcode curl_formadd(struct curl_httppost **httppost, struct curl_httppost **last_post,...)
Definition: formdata.c:978
CURLcode
Definition: curl.h:209
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: easy.c:217
long tv_usec
Definition: timeval.h:38
CURLM * multi_handle
Definition: fopen.c:81
#define NULL
Definition: Lib.h:88
#define select(args...)
Definition: amigaos.h:39
CURLM * curl_multi_init(void)
Definition: multi.c:114
CURLMcode curl_multi_cleanup(CURLM *multi_handle)
Definition: multi.c:583
int main(int argc, char *argv[])
Definition: multi-post.c:19
void curl_formfree(struct curl_httppost *form)
Definition: formdata.c:1073
CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
Definition: multi.c:306
void CURLM
Definition: multi.h:76
struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: sendf.c:82
void CURL
Definition: types.h:25
void curl_slist_free_all(struct curl_slist *)
Definition: sendf.c:108
GLuint res
Definition: glext.h:5385
CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
Definition: multi.c:134
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
CURL * curl_easy_init(void)
Definition: easy.c:195