[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [gnurl] 140/256: examples/post-callback: stop returning one
From: |
gnunet |
Subject: |
[GNUnet-SVN] [gnurl] 140/256: examples/post-callback: stop returning one byte at a time |
Date: |
Fri, 06 Oct 2017 19:43:51 +0200 |
This is an automated email from the git hooks/post-receive script.
ng0 pushed a commit to branch master
in repository gnurl.
commit 12e789391de425cc881c19b4fa0a0c6cdee796f4
Author: Daniel Stenberg <address@hidden>
AuthorDate: Mon Sep 4 11:39:59 2017 +0200
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.
---
docs/examples/post-callback.c | 55 +++++++++++++++++++++++++------------------
1 file changed, 32 insertions(+), 23 deletions(-)
diff --git a/docs/examples/post-callback.c b/docs/examples/post-callback.c
index dbf249140..4dbe18e24 100644
--- a/docs/examples/post-callback.c
+++ b/docs/examples/post-callback.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2015, Daniel Stenberg, <address@hidden>, et al.
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <address@hidden>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -20,36 +20,45 @@
*
***************************************************************************/
/* <DESC>
- * An example source code that issues a HTTP POST and we provide the actual
- * data through a read callback.
+ * Issue an HTTP POST and provide the data through the read callback.
* </DESC>
*/
#include <stdio.h>
#include <string.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 {
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;
-
- if(size*nmemb < 1)
- return 0;
-
- if(pooh->sizeleft) {
- *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
- pooh->readptr++; /* advance pointer */
- pooh->sizeleft--; /* less data left */
- return 1; /* we return 1 byte at a time! */
+ struct WriteThis *wt = (struct WriteThis *)userp;
+ size_t buffer_size = size*nmemb;
+
+ if(wt->sizeleft) {
+ /* 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);
+
+ wt->readptr += copy_this_much;
+ wt->sizeleft -= copy_this_much;
+ return copy_this_much; /* we copied this many bytes */
}
- return 0; /* no more data left to deliver */
+ return 0; /* no more data left to deliver */
}
int main(void)
@@ -57,10 +66,10 @@ int main(void)
CURL *curl;
CURLcode res;
- struct WriteThis pooh;
+ struct WriteThis wt;
- pooh.readptr = data;
- pooh.sizeleft = (long)strlen(data);
+ wt.readptr = data;
+ wt.sizeleft = strlen(data);
/* In windows, this will init the winsock stuff */
res = curl_global_init(CURL_GLOBAL_DEFAULT);
@@ -75,7 +84,7 @@ int main(void)
curl = curl_easy_init();
if(curl) {
/* 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 */
curl_easy_setopt(curl, CURLOPT_POST, 1L);
@@ -84,7 +93,7 @@ int main(void)
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* 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 */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
@@ -108,7 +117,7 @@ int main(void)
#else
/* Set the expected POST size. If you want to POST large amounts of data,
consider CURLOPT_POSTFIELDSIZE_LARGE */
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, wt.sizeleft);
#endif
#ifdef DISABLE_EXPECT
--
To stop receiving notification emails like this one, please contact
address@hidden
- [GNUnet-SVN] [gnurl] 127/256: mime: use in curl cli tool instead of form API., (continued)
- [GNUnet-SVN] [gnurl] 127/256: mime: use in curl cli tool instead of form API., gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 158/256: mime: replace 'struct Curl_mimepart' by 'curl_mimepart' in encoder code., gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 71/256: vtls: move the SUPPORT_HTTPS_PROXY flag into the Curl_ssl struct, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 75/256: axtls: use Curl_none_* versions of init() and cleanup(), gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 131/256: mime: use size_t instead of ssize_t in public API interface., gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 242/256: libcurl-tutorial: describe MIME API and deprecate form API., gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 90/256: version: add the CURL_VERSION_MULTI_SSL feature flag, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 100/256: HELP-US.md: "How to get started helping out in the curl project", gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 157/256: mime: implement encoders., gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 133/256: mime: fix signed/unsigned conversions., gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 140/256: examples/post-callback: stop returning one byte at a time,
gnunet <=
- [GNUnet-SVN] [gnurl] 143/256: mime: use CURL_ZERO_TERMINATED in examples, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 46/256: request-target.d: mention added in 7.55.0, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 56/256: winbuild: fix embedded manifest option, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 236/256: connect: fix race condition with happy eyeballs timeout, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 44/256: makefile.m32: add support for libidn2, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 167/256: vtls: switch to CURL_SHA256_DIGEST_LENGTH define, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 54/256: examples/threaded-ssl: mention that this is for openssl before 1.1, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 188/256: checksrc: detect and warn for multiple spaces, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 233/256: http: add custom empty headers to repeated requests, gnunet, 2017/10/06
- [GNUnet-SVN] [gnurl] 115/256: ossfuzz: moving towards the ideal integration, gnunet, 2017/10/06