Home | History | Annotate | Download | only in libtest
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2016, 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 https://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 #include "test.h"
     23 
     24 #include "memdebug.h"
     25 
     26 static char data[]=
     27 #ifdef CURL_DOES_CONVERSIONS
     28   /* ASCII representation with escape sequences for non-ASCII platforms */
     29   "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
     30   "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
     31   "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
     32 #else
     33   "this is what we post to the silly web server\n";
     34 #endif
     35 
     36 struct WriteThis {
     37   char *readptr;
     38   size_t sizeleft;
     39 };
     40 
     41 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
     42 {
     43 #ifdef LIB587
     44   (void)ptr;
     45   (void)size;
     46   (void)nmemb;
     47   (void)userp;
     48   return CURL_READFUNC_ABORT;
     49 #else
     50 
     51   struct WriteThis *pooh = (struct WriteThis *)userp;
     52 
     53   if(size*nmemb < 1)
     54     return 0;
     55 
     56   if(pooh->sizeleft) {
     57     *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
     58     pooh->readptr++;                 /* advance pointer */
     59     pooh->sizeleft--;                /* less data left */
     60     return 1;                        /* we return 1 byte at a time! */
     61   }
     62 
     63   return 0;                         /* no more data left to deliver */
     64 #endif
     65 }
     66 
     67 static int once(char *URL, bool oldstyle)
     68 {
     69   CURL *curl;
     70   CURLcode res=CURLE_OK;
     71   CURLFORMcode formrc;
     72 
     73   struct curl_httppost *formpost=NULL;
     74   struct curl_httppost *lastptr=NULL;
     75   struct WriteThis pooh;
     76   struct WriteThis pooh2;
     77 
     78   pooh.readptr = data;
     79   pooh.sizeleft = strlen(data);
     80 
     81   /* Fill in the file upload field */
     82   if(oldstyle) {
     83     formrc = curl_formadd(&formpost,
     84                           &lastptr,
     85                           CURLFORM_COPYNAME, "sendfile",
     86                           CURLFORM_STREAM, &pooh,
     87                           CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
     88                           CURLFORM_FILENAME, "postit2.c",
     89                           CURLFORM_END);
     90   }
     91   else {
     92     /* new style */
     93     formrc = curl_formadd(&formpost,
     94                           &lastptr,
     95                           CURLFORM_COPYNAME, "sendfile alternative",
     96                           CURLFORM_STREAM, &pooh,
     97                           CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
     98                           CURLFORM_FILENAME, "file name 2",
     99                           CURLFORM_END);
    100   }
    101 
    102   if(formrc)
    103     printf("curl_formadd(1) = %d\n", (int)formrc);
    104 
    105   /* Now add the same data with another name and make it not look like
    106      a file upload but still using the callback */
    107 
    108   pooh2.readptr = data;
    109   pooh2.sizeleft = strlen(data);
    110 
    111   /* Fill in the file upload field */
    112   formrc = curl_formadd(&formpost,
    113                         &lastptr,
    114                         CURLFORM_COPYNAME, "callbackdata",
    115                         CURLFORM_STREAM, &pooh2,
    116                         CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
    117                         CURLFORM_END);
    118 
    119   if(formrc)
    120     printf("curl_formadd(1) = %d\n", (int)formrc);
    121 
    122   /* Fill in the filename field */
    123   formrc = curl_formadd(&formpost,
    124                         &lastptr,
    125                         CURLFORM_COPYNAME, "filename",
    126 #ifdef CURL_DOES_CONVERSIONS
    127                         /* ASCII representation with escape
    128                            sequences for non-ASCII platforms */
    129                         CURLFORM_COPYCONTENTS,
    130                            "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
    131 #else
    132                         CURLFORM_COPYCONTENTS, "postit2.c",
    133 #endif
    134                         CURLFORM_END);
    135 
    136   if(formrc)
    137     printf("curl_formadd(2) = %d\n", (int)formrc);
    138 
    139   /* Fill in a submit field too */
    140   formrc = curl_formadd(&formpost,
    141                         &lastptr,
    142                         CURLFORM_COPYNAME, "submit",
    143 #ifdef CURL_DOES_CONVERSIONS
    144                         /* ASCII representation with escape
    145                            sequences for non-ASCII platforms */
    146                         CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
    147 #else
    148                         CURLFORM_COPYCONTENTS, "send",
    149 #endif
    150                         CURLFORM_END);
    151 
    152   if(formrc)
    153     printf("curl_formadd(3) = %d\n", (int)formrc);
    154 
    155   formrc = curl_formadd(&formpost, &lastptr,
    156                         CURLFORM_COPYNAME, "somename",
    157                         CURLFORM_BUFFER, "somefile.txt",
    158                         CURLFORM_BUFFERPTR, "blah blah",
    159                         CURLFORM_BUFFERLENGTH, (long)9,
    160                         CURLFORM_END);
    161 
    162   if(formrc)
    163     printf("curl_formadd(4) = %d\n", (int)formrc);
    164 
    165   if((curl = curl_easy_init()) == NULL) {
    166     fprintf(stderr, "curl_easy_init() failed\n");
    167     curl_formfree(formpost);
    168     curl_global_cleanup();
    169     return TEST_ERR_MAJOR_BAD;
    170   }
    171 
    172   /* First set the URL that is about to receive our POST. */
    173   test_setopt(curl, CURLOPT_URL, URL);
    174 
    175   /* Now specify we want to POST data */
    176   test_setopt(curl, CURLOPT_POST, 1L);
    177 
    178   /* Set the expected POST size */
    179   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
    180 
    181   /* we want to use our own read function */
    182   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
    183 
    184   /* send a multi-part formpost */
    185   test_setopt(curl, CURLOPT_HTTPPOST, formpost);
    186 
    187   /* get verbose debug output please */
    188   test_setopt(curl, CURLOPT_VERBOSE, 1L);
    189 
    190   /* include headers in the output */
    191   test_setopt(curl, CURLOPT_HEADER, 1L);
    192 
    193   /* Perform the request, res will get the return code */
    194   res = curl_easy_perform(curl);
    195 
    196 test_cleanup:
    197 
    198   /* always cleanup */
    199   curl_easy_cleanup(curl);
    200 
    201   /* now cleanup the formpost chain */
    202   curl_formfree(formpost);
    203 
    204   return res;
    205 }
    206 
    207 int test(char *URL)
    208 {
    209   int res;
    210 
    211   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
    212     fprintf(stderr, "curl_global_init() failed\n");
    213     return TEST_ERR_MAJOR_BAD;
    214   }
    215 
    216   res = once(URL, TRUE); /* old */
    217   if(!res)
    218     res = once(URL, FALSE); /* new */
    219 
    220   curl_global_cleanup();
    221 
    222   return res;
    223 }
    224