00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <sys/time.h>
00030
00031 #include <curl/curl.h>
00032
00033 int main(void)
00034 {
00035 CURL *curl;
00036
00037 CURLM *multi_handle;
00038 int still_running;
00039
00040 struct curl_httppost *formpost=NULL;
00041 struct curl_httppost *lastptr=NULL;
00042 struct curl_slist *headerlist=NULL;
00043 static const char buf[] = "Expect:";
00044
00045
00046
00047 curl_formadd(&formpost,
00048 &lastptr,
00049 CURLFORM_COPYNAME, "sendfile",
00050 CURLFORM_FILE, "postit2.c",
00051 CURLFORM_END);
00052
00053
00054 curl_formadd(&formpost,
00055 &lastptr,
00056 CURLFORM_COPYNAME, "filename",
00057 CURLFORM_COPYCONTENTS, "postit2.c",
00058 CURLFORM_END);
00059
00060
00061 curl_formadd(&formpost,
00062 &lastptr,
00063 CURLFORM_COPYNAME, "submit",
00064 CURLFORM_COPYCONTENTS, "send",
00065 CURLFORM_END);
00066
00067 curl = curl_easy_init();
00068 multi_handle = curl_multi_init();
00069
00070
00071
00072 headerlist = curl_slist_append(headerlist, buf);
00073 if(curl && multi_handle) {
00074
00075
00076 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
00077 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00078
00079 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
00080 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
00081
00082 curl_multi_add_handle(multi_handle, curl);
00083
00084 curl_multi_perform(multi_handle, &still_running);
00085
00086 do {
00087 struct timeval timeout;
00088 int rc;
00089 CURLMcode mc;
00090
00091 fd_set fdread;
00092 fd_set fdwrite;
00093 fd_set fdexcep;
00094 int maxfd = -1;
00095
00096 long curl_timeo = -1;
00097
00098 FD_ZERO(&fdread);
00099 FD_ZERO(&fdwrite);
00100 FD_ZERO(&fdexcep);
00101
00102
00103 timeout.tv_sec = 1;
00104 timeout.tv_usec = 0;
00105
00106 curl_multi_timeout(multi_handle, &curl_timeo);
00107 if(curl_timeo >= 0) {
00108 timeout.tv_sec = curl_timeo / 1000;
00109 if(timeout.tv_sec > 1)
00110 timeout.tv_sec = 1;
00111 else
00112 timeout.tv_usec = (curl_timeo % 1000) * 1000;
00113 }
00114
00115
00116 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
00117
00118 if(mc != CURLM_OK) {
00119 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00120 break;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 if(maxfd == -1) {
00130 #ifdef _WIN32
00131 Sleep(100);
00132 rc = 0;
00133 #else
00134
00135 struct timeval wait = { 0, 100 * 1000 };
00136 rc = select(0, NULL, NULL, NULL, &wait);
00137 #endif
00138 }
00139 else {
00140
00141
00142 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00143 }
00144
00145 switch(rc) {
00146 case -1:
00147
00148 break;
00149 case 0:
00150 default:
00151
00152 printf("perform!\n");
00153 curl_multi_perform(multi_handle, &still_running);
00154 printf("running: %d!\n", still_running);
00155 break;
00156 }
00157 } while(still_running);
00158
00159 curl_multi_cleanup(multi_handle);
00160
00161
00162 curl_easy_cleanup(curl);
00163
00164
00165 curl_formfree(formpost);
00166
00167
00168 curl_slist_free_all(headerlist);
00169 }
00170 return 0;
00171 }