lib579.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 const char * const post[]={
00027   "one",
00028   "two",
00029   "three",
00030   "and a final longer crap: four",
00031   NULL
00032 };
00033 
00034 
00035 struct WriteThis {
00036   int counter;
00037 };
00038 
00039 static int progress_callback(void *clientp, double dltotal, double dlnow,
00040                              double ultotal, double ulnow)
00041 {
00042   FILE *moo;
00043   static int prev_ultotal = -1;
00044   static int prev_ulnow = -1;
00045   (void)clientp; /* UNUSED */
00046   (void)dltotal; /* UNUSED */
00047   (void)dlnow; /* UNUSED */
00048 
00049   /* to avoid depending on timing, which will cause this progress function to
00050      get called a different number of times depending on circumstances, we
00051      only log these lines if the numbers are different from the previous
00052      invoke */
00053   if((prev_ultotal != (int)ultotal) ||
00054      (prev_ulnow != (int)ulnow)) {
00055 
00056     moo = fopen(libtest_arg2, "ab");
00057     if(moo) {
00058       fprintf(moo, "Progress callback called with UL %d out of %d\n",
00059               (int)ulnow, (int)ultotal);
00060       fclose(moo);
00061     }
00062     prev_ulnow = (int) ulnow;
00063     prev_ultotal = (int) ultotal;
00064   }
00065   return 0;
00066 }
00067 
00068 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
00069 {
00070   struct WriteThis *pooh = (struct WriteThis *)userp;
00071   const char *data;
00072 
00073   if(size*nmemb < 1)
00074     return 0;
00075 
00076   data = post[pooh->counter];
00077 
00078   if(data) {
00079     size_t len = strlen(data);
00080     memcpy(ptr, data, len);
00081     pooh->counter++; /* advance pointer */
00082     return len;
00083   }
00084   return 0;                         /* no more data left to deliver */
00085 }
00086 
00087 int test(char *URL)
00088 {
00089   CURL *curl;
00090   CURLcode res=CURLE_OK;
00091   struct curl_slist *slist = NULL;
00092   struct WriteThis pooh;
00093   pooh.counter = 0;
00094 
00095   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
00096     fprintf(stderr, "curl_global_init() failed\n");
00097     return TEST_ERR_MAJOR_BAD;
00098   }
00099 
00100   curl = curl_easy_init();
00101   if(!curl) {
00102     fprintf(stderr, "curl_easy_init() failed\n");
00103     curl_global_cleanup();
00104     return TEST_ERR_MAJOR_BAD;
00105   }
00106 
00107   slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
00108   if(slist == NULL) {
00109     fprintf(stderr, "curl_slist_append() failed\n");
00110     curl_easy_cleanup(curl);
00111     curl_global_cleanup();
00112     return TEST_ERR_MAJOR_BAD;
00113   }
00114 
00115   /* First set the URL that is about to receive our POST. */
00116   test_setopt(curl, CURLOPT_URL, URL);
00117 
00118   /* Now specify we want to POST data */
00119   test_setopt(curl, CURLOPT_POST, 1L);
00120 
00121 #ifdef CURL_DOES_CONVERSIONS
00122   /* Convert the POST data to ASCII */
00123   test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
00124 #endif
00125 
00126   /* we want to use our own read function */
00127   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00128 
00129   /* pointer to pass to our read function */
00130   test_setopt(curl, CURLOPT_READDATA, &pooh);
00131 
00132   /* get verbose debug output please */
00133   test_setopt(curl, CURLOPT_VERBOSE, 1L);
00134 
00135   /* include headers in the output */
00136   test_setopt(curl, CURLOPT_HEADER, 1L);
00137 
00138   /* enforce chunked transfer by setting the header */
00139   test_setopt(curl, CURLOPT_HTTPHEADER, slist);
00140 
00141   test_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
00142   test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
00143 
00144   /* we want to use our own progress function */
00145   test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
00146   test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
00147 
00148   /* Perform the request, res will get the return code */
00149   res = curl_easy_perform(curl);
00150 
00151 test_cleanup:
00152 
00153   /* clean up the headers list */
00154   if(slist)
00155     curl_slist_free_all(slist);
00156 
00157   /* always cleanup */
00158   curl_easy_cleanup(curl);
00159   curl_global_cleanup();
00160 
00161   return res;
00162 }


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