multi-post.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * using the multi interface to do a multipart formpost without blocking
00024  * </DESC>
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   /* Fill in the file upload field. This makes libcurl load data from
00046      the given file name when curl_easy_perform() is called. */
00047   curl_formadd(&formpost,
00048                &lastptr,
00049                CURLFORM_COPYNAME, "sendfile",
00050                CURLFORM_FILE, "postit2.c",
00051                CURLFORM_END);
00052 
00053   /* Fill in the filename field */
00054   curl_formadd(&formpost,
00055                &lastptr,
00056                CURLFORM_COPYNAME, "filename",
00057                CURLFORM_COPYCONTENTS, "postit2.c",
00058                CURLFORM_END);
00059 
00060   /* Fill in the submit field too, even if this is rarely needed */
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   /* initialize custom header list (stating that Expect: 100-continue is not
00071      wanted */
00072   headerlist = curl_slist_append(headerlist, buf);
00073   if(curl && multi_handle) {
00074 
00075     /* what URL that receives this POST */
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; /* select() return code */
00089       CURLMcode mc; /* curl_multi_fdset() return code */
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       /* set a suitable timeout to play around with */
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       /* get file descriptors from the transfers */
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       /* On success the value of maxfd is guaranteed to be >= -1. We call
00124          select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
00125          no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
00126          to sleep 100ms, which is the minimum suggested value in the
00127          curl_multi_fdset() doc. */
00128 
00129       if(maxfd == -1) {
00130 #ifdef _WIN32
00131         Sleep(100);
00132         rc = 0;
00133 #else
00134         /* Portable sleep for platforms other than Windows. */
00135         struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
00136         rc = select(0, NULL, NULL, NULL, &wait);
00137 #endif
00138       }
00139       else {
00140         /* Note that on some platforms 'timeout' may be modified by select().
00141            If you need access to the original value save a copy beforehand. */
00142         rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00143       }
00144 
00145       switch(rc) {
00146       case -1:
00147         /* select error */
00148         break;
00149       case 0:
00150       default:
00151         /* timeout or readable/writable sockets */
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     /* always cleanup */
00162     curl_easy_cleanup(curl);
00163 
00164     /* then cleanup the formpost chain */
00165     curl_formfree(formpost);
00166 
00167     /* free slist */
00168     curl_slist_free_all(headerlist);
00169   }
00170   return 0;
00171 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05