Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel (at) haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at http://curl.haxx.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  ***************************************************************************/
     22 /* This is an example application source code using the multi interface
     23  * to do a multipart formpost without "blocking". */
     24 #include <stdio.h>
     25 #include <string.h>
     26 #include <sys/time.h>
     27 
     28 #include <curl/curl.h>
     29 
     30 int main(void)
     31 {
     32   CURL *curl;
     33 
     34   CURLM *multi_handle;
     35   int still_running;
     36 
     37   struct curl_httppost *formpost=NULL;
     38   struct curl_httppost *lastptr=NULL;
     39   struct curl_slist *headerlist=NULL;
     40   static const char buf[] = "Expect:";
     41 
     42   /* Fill in the file upload field. This makes libcurl load data from
     43      the given file name when curl_easy_perform() is called. */
     44   curl_formadd(&formpost,
     45                &lastptr,
     46                CURLFORM_COPYNAME, "sendfile",
     47                CURLFORM_FILE, "postit2.c",
     48                CURLFORM_END);
     49 
     50   /* Fill in the filename field */
     51   curl_formadd(&formpost,
     52                &lastptr,
     53                CURLFORM_COPYNAME, "filename",
     54                CURLFORM_COPYCONTENTS, "postit2.c",
     55                CURLFORM_END);
     56 
     57   /* Fill in the submit field too, even if this is rarely needed */
     58   curl_formadd(&formpost,
     59                &lastptr,
     60                CURLFORM_COPYNAME, "submit",
     61                CURLFORM_COPYCONTENTS, "send",
     62                CURLFORM_END);
     63 
     64   curl = curl_easy_init();
     65   multi_handle = curl_multi_init();
     66 
     67   /* initialize custom header list (stating that Expect: 100-continue is not
     68      wanted */
     69   headerlist = curl_slist_append(headerlist, buf);
     70   if(curl && multi_handle) {
     71 
     72     /* what URL that receives this POST */
     73     curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
     74     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
     75 
     76     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
     77     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
     78 
     79     curl_multi_add_handle(multi_handle, curl);
     80 
     81     curl_multi_perform(multi_handle, &still_running);
     82 
     83     do {
     84       struct timeval timeout;
     85       int rc; /* select() return code */
     86       CURLMcode mc; /* curl_multi_fdset() return code */
     87 
     88       fd_set fdread;
     89       fd_set fdwrite;
     90       fd_set fdexcep;
     91       int maxfd = -1;
     92 
     93       long curl_timeo = -1;
     94 
     95       FD_ZERO(&fdread);
     96       FD_ZERO(&fdwrite);
     97       FD_ZERO(&fdexcep);
     98 
     99       /* set a suitable timeout to play around with */
    100       timeout.tv_sec = 1;
    101       timeout.tv_usec = 0;
    102 
    103       curl_multi_timeout(multi_handle, &curl_timeo);
    104       if(curl_timeo >= 0) {
    105         timeout.tv_sec = curl_timeo / 1000;
    106         if(timeout.tv_sec > 1)
    107           timeout.tv_sec = 1;
    108         else
    109           timeout.tv_usec = (curl_timeo % 1000) * 1000;
    110       }
    111 
    112       /* get file descriptors from the transfers */
    113       mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
    114 
    115       if(mc != CURLM_OK)
    116       {
    117         fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
    118         break;
    119       }
    120 
    121       /* On success the value of maxfd is guaranteed to be >= -1. We call
    122          select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
    123          no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
    124          to sleep 100ms, which is the minimum suggested value in the
    125          curl_multi_fdset() doc. */
    126 
    127       if(maxfd == -1) {
    128 #ifdef _WIN32
    129         Sleep(100);
    130         rc = 0;
    131 #else
    132         /* Portable sleep for platforms other than Windows. */
    133         struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
    134         rc = select(0, NULL, NULL, NULL, &wait);
    135 #endif
    136       }
    137       else {
    138         /* Note that on some platforms 'timeout' may be modified by select().
    139            If you need access to the original value save a copy beforehand. */
    140         rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
    141       }
    142 
    143       switch(rc) {
    144       case -1:
    145         /* select error */
    146         break;
    147       case 0:
    148       default:
    149         /* timeout or readable/writable sockets */
    150         printf("perform!\n");
    151         curl_multi_perform(multi_handle, &still_running);
    152         printf("running: %d!\n", still_running);
    153         break;
    154       }
    155     } while(still_running);
    156 
    157     curl_multi_cleanup(multi_handle);
    158 
    159     /* always cleanup */
    160     curl_easy_cleanup(curl);
    161 
    162     /* then cleanup the formpost chain */
    163     curl_formfree(formpost);
    164 
    165     /* free slist */
    166     curl_slist_free_all (headerlist);
    167   }
    168   return 0;
    169 }
    170