examples/post-callback: stop returning one byte at a time

... since people copy and paste code from this example and thus they get
an inefficient POST operation without a good reason and sometimes
without understanding why.

Instead this now returns as much data as possible.
This commit is contained in:
Daniel Stenberg 2017-09-04 11:39:59 +02:00
parent b081f2ada6
commit 12e789391d
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2

View file

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -20,36 +20,45 @@
* *
***************************************************************************/ ***************************************************************************/
/* <DESC> /* <DESC>
* An example source code that issues a HTTP POST and we provide the actual * Issue an HTTP POST and provide the data through the read callback.
* data through a read callback.
* </DESC> * </DESC>
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <curl/curl.h> #include <curl/curl.h>
static const char data[]="this is what we post to the silly web server"; /* silly test data to POST */
static const char data[]="Lorem ipsum dolor sit amet, consectetur adipiscing "
"elit. Sed vel urna neque. Ut quis leo metus. Quisque eleifend, ex at "
"laoreet rhoncus, odio ipsum semper metus, at tempus ante urna in mauris. "
"Suspendisse ornare tempor venenatis. Ut dui neque, pellentesque a varius "
"eget, mattis vitae ligula. Fusce ut pharetra est. Ut ullamcorper mi ac "
"sollicitudin semper. Praesent sit amet tellus varius, posuere nulla non, "
"rhoncus ipsum.";
struct WriteThis { struct WriteThis {
const char *readptr; const char *readptr;
long sizeleft; size_t sizeleft;
}; };
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp) static size_t read_callback(void *dest, size_t size, size_t nmemb, void *userp)
{ {
struct WriteThis *pooh = (struct WriteThis *)userp; struct WriteThis *wt = (struct WriteThis *)userp;
size_t buffer_size = size*nmemb;
if(size*nmemb < 1) if(wt->sizeleft) {
return 0; /* copy as much as possible from the source to the destination */
size_t copy_this_much = wt->sizeleft;
if(copy_this_much > buffer_size)
copy_this_much = buffer_size;
memcpy(dest, wt->readptr, copy_this_much);
if(pooh->sizeleft) { wt->readptr += copy_this_much;
*(char *)ptr = pooh->readptr[0]; /* copy one single byte */ wt->sizeleft -= copy_this_much;
pooh->readptr++; /* advance pointer */ return copy_this_much; /* we copied this many bytes */
pooh->sizeleft--; /* less data left */
return 1; /* we return 1 byte at a time! */
} }
return 0; /* no more data left to deliver */ return 0; /* no more data left to deliver */
} }
int main(void) int main(void)
@ -57,10 +66,10 @@ int main(void)
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
struct WriteThis pooh; struct WriteThis wt;
pooh.readptr = data; wt.readptr = data;
pooh.sizeleft = (long)strlen(data); wt.sizeleft = strlen(data);
/* In windows, this will init the winsock stuff */ /* In windows, this will init the winsock stuff */
res = curl_global_init(CURL_GLOBAL_DEFAULT); res = curl_global_init(CURL_GLOBAL_DEFAULT);
@ -75,7 +84,7 @@ int main(void)
curl = curl_easy_init(); curl = curl_easy_init();
if(curl) { if(curl) {
/* First set the URL that is about to receive our POST. */ /* First set the URL that is about to receive our POST. */
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/index.cgi"); curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/index.cgi");
/* Now specify we want to POST data */ /* Now specify we want to POST data */
curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POST, 1L);
@ -84,7 +93,7 @@ int main(void)
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* pointer to pass to our read function */ /* pointer to pass to our read function */
curl_easy_setopt(curl, CURLOPT_READDATA, &pooh); curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
/* get verbose debug output please */ /* get verbose debug output please */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
@ -108,7 +117,7 @@ int main(void)
#else #else
/* Set the expected POST size. If you want to POST large amounts of data, /* Set the expected POST size. If you want to POST large amounts of data,
consider CURLOPT_POSTFIELDSIZE_LARGE */ consider CURLOPT_POSTFIELDSIZE_LARGE */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, wt.sizeleft);
#endif #endif
#ifdef DISABLE_EXPECT #ifdef DISABLE_EXPECT