lib554.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 #include "test.h"
00023 
00024 #include "memdebug.h"
00025 
00026 static char data[]=
00027 #ifdef CURL_DOES_CONVERSIONS
00028   /* ASCII representation with escape sequences for non-ASCII platforms */
00029   "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
00030   "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
00031   "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
00032 #else
00033   "this is what we post to the silly web server\n";
00034 #endif
00035 
00036 struct WriteThis {
00037   char *readptr;
00038   size_t sizeleft;
00039 };
00040 
00041 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
00042 {
00043 #ifdef LIB587
00044   (void)ptr;
00045   (void)size;
00046   (void)nmemb;
00047   (void)userp;
00048   return CURL_READFUNC_ABORT;
00049 #else
00050 
00051   struct WriteThis *pooh = (struct WriteThis *)userp;
00052 
00053   if(size*nmemb < 1)
00054     return 0;
00055 
00056   if(pooh->sizeleft) {
00057     *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
00058     pooh->readptr++;                 /* advance pointer */
00059     pooh->sizeleft--;                /* less data left */
00060     return 1;                        /* we return 1 byte at a time! */
00061   }
00062 
00063   return 0;                         /* no more data left to deliver */
00064 #endif
00065 }
00066 
00067 static int once(char *URL, bool oldstyle)
00068 {
00069   CURL *curl;
00070   CURLcode res=CURLE_OK;
00071   CURLFORMcode formrc;
00072 
00073   struct curl_httppost *formpost=NULL;
00074   struct curl_httppost *lastptr=NULL;
00075   struct WriteThis pooh;
00076   struct WriteThis pooh2;
00077 
00078   pooh.readptr = data;
00079   pooh.sizeleft = strlen(data);
00080 
00081   /* Fill in the file upload field */
00082   if(oldstyle) {
00083     formrc = curl_formadd(&formpost,
00084                           &lastptr,
00085                           CURLFORM_COPYNAME, "sendfile",
00086                           CURLFORM_STREAM, &pooh,
00087                           CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
00088                           CURLFORM_FILENAME, "postit2.c",
00089                           CURLFORM_END);
00090   }
00091   else {
00092     /* new style */
00093     formrc = curl_formadd(&formpost,
00094                           &lastptr,
00095                           CURLFORM_COPYNAME, "sendfile alternative",
00096                           CURLFORM_STREAM, &pooh,
00097                           CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
00098                           CURLFORM_FILENAME, "file name 2",
00099                           CURLFORM_END);
00100   }
00101 
00102   if(formrc)
00103     printf("curl_formadd(1) = %d\n", (int)formrc);
00104 
00105   /* Now add the same data with another name and make it not look like
00106      a file upload but still using the callback */
00107 
00108   pooh2.readptr = data;
00109   pooh2.sizeleft = strlen(data);
00110 
00111   /* Fill in the file upload field */
00112   formrc = curl_formadd(&formpost,
00113                         &lastptr,
00114                         CURLFORM_COPYNAME, "callbackdata",
00115                         CURLFORM_STREAM, &pooh2,
00116                         CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
00117                         CURLFORM_END);
00118 
00119   if(formrc)
00120     printf("curl_formadd(1) = %d\n", (int)formrc);
00121 
00122   /* Fill in the filename field */
00123   formrc = curl_formadd(&formpost,
00124                         &lastptr,
00125                         CURLFORM_COPYNAME, "filename",
00126 #ifdef CURL_DOES_CONVERSIONS
00127                         /* ASCII representation with escape
00128                            sequences for non-ASCII platforms */
00129                         CURLFORM_COPYCONTENTS,
00130                            "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
00131 #else
00132                         CURLFORM_COPYCONTENTS, "postit2.c",
00133 #endif
00134                         CURLFORM_END);
00135 
00136   if(formrc)
00137     printf("curl_formadd(2) = %d\n", (int)formrc);
00138 
00139   /* Fill in a submit field too */
00140   formrc = curl_formadd(&formpost,
00141                         &lastptr,
00142                         CURLFORM_COPYNAME, "submit",
00143 #ifdef CURL_DOES_CONVERSIONS
00144                         /* ASCII representation with escape
00145                            sequences for non-ASCII platforms */
00146                         CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
00147 #else
00148                         CURLFORM_COPYCONTENTS, "send",
00149 #endif
00150                         CURLFORM_END);
00151 
00152   if(formrc)
00153     printf("curl_formadd(3) = %d\n", (int)formrc);
00154 
00155   formrc = curl_formadd(&formpost, &lastptr,
00156                         CURLFORM_COPYNAME, "somename",
00157                         CURLFORM_BUFFER, "somefile.txt",
00158                         CURLFORM_BUFFERPTR, "blah blah",
00159                         CURLFORM_BUFFERLENGTH, (long)9,
00160                         CURLFORM_END);
00161 
00162   if(formrc)
00163     printf("curl_formadd(4) = %d\n", (int)formrc);
00164 
00165   curl = curl_easy_init();
00166   if(!curl) {
00167     fprintf(stderr, "curl_easy_init() failed\n");
00168     curl_formfree(formpost);
00169     curl_global_cleanup();
00170     return TEST_ERR_MAJOR_BAD;
00171   }
00172 
00173   /* First set the URL that is about to receive our POST. */
00174   test_setopt(curl, CURLOPT_URL, URL);
00175 
00176   /* Now specify we want to POST data */
00177   test_setopt(curl, CURLOPT_POST, 1L);
00178 
00179   /* Set the expected POST size */
00180   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
00181 
00182   /* we want to use our own read function */
00183   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00184 
00185   /* send a multi-part formpost */
00186   test_setopt(curl, CURLOPT_HTTPPOST, formpost);
00187 
00188   /* get verbose debug output please */
00189   test_setopt(curl, CURLOPT_VERBOSE, 1L);
00190 
00191   /* include headers in the output */
00192   test_setopt(curl, CURLOPT_HEADER, 1L);
00193 
00194   /* Perform the request, res will get the return code */
00195   res = curl_easy_perform(curl);
00196 
00197 test_cleanup:
00198 
00199   /* always cleanup */
00200   curl_easy_cleanup(curl);
00201 
00202   /* now cleanup the formpost chain */
00203   curl_formfree(formpost);
00204 
00205   return res;
00206 }
00207 
00208 int test(char *URL)
00209 {
00210   int res;
00211 
00212   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
00213     fprintf(stderr, "curl_global_init() failed\n");
00214     return TEST_ERR_MAJOR_BAD;
00215   }
00216 
00217   res = once(URL, TRUE); /* old */
00218   if(!res)
00219     res = once(URL, FALSE); /* new */
00220 
00221   curl_global_cleanup();
00222 
00223   return res;
00224 }


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