cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: [PATCH] curl_formget()

From: Michael Wallner <mike_at_iworks.at>
Date: Thu, 22 Jun 2006 17:07:31 +0200

Michael Wallner wrote:

> + do {
> + read = readfromfile(&temp, buffer, sizeof(buffer));
> + if ((read == (size_t) -1) || (read != append(arg, buffer, read))) {
> + Curl_formclean(data);
> + return -1;
> + }
> + } while (read == sizeof(buffer));

The patch closes temp.fp now when the callback fails.

Regards,

-- 
Michael

? CURLOPT_COOKIELIST--SESS.diff.txt
? curl_formget.diff.txt
? docs/libcurl/curl_formget.3
Index: docs/libcurl/Makefile.am
===================================================================
RCS file: /cvsroot/curl/curl/docs/libcurl/Makefile.am,v
retrieving revision 1.23
diff -u -p -d -r1.23 Makefile.am
--- docs/libcurl/Makefile.am 10 Apr 2006 21:55:48 -0000 1.23
+++ docs/libcurl/Makefile.am 22 Jun 2006 15:05:05 -0000
@@ -18,7 +18,7 @@ man_MANS = curl_easy_cleanup.3 curl_easy
  curl_multi_strerror.3 curl_share_strerror.3 curl_global_init_mem.3 \
  libcurl-tutorial.3 curl_easy_reset.3 curl_easy_escape.3 \
  curl_easy_unescape.3 curl_multi_setopt.3 curl_multi_socket.3 \
- curl_multi_timeout.3
+ curl_multi_timeout.3 curl_formget.3
 
 HTMLPAGES = curl_easy_cleanup.html curl_easy_getinfo.html \
  curl_easy_init.html curl_easy_perform.html curl_easy_setopt.html \
@@ -35,7 +35,8 @@ HTMLPAGES = curl_easy_cleanup.html curl_
  libcurl-errors.html curl_easy_strerror.html curl_multi_strerror.html \
  curl_share_strerror.html curl_global_init_mem.html libcurl-tutorial.html \
  curl_easy_reset.html curl_easy_escape.html curl_easy_unescape.html \
- curl_multi_setopt.html curl_multi_socket.html curl_multi_timeout.html
+ curl_multi_setopt.html curl_multi_socket.html curl_multi_timeout.html \
+ curl_formget.html
 
 PDFPAGES = curl_easy_cleanup.pdf curl_easy_getinfo.pdf curl_easy_init.pdf \
  curl_easy_perform.pdf curl_easy_setopt.pdf curl_easy_duphandle.pdf \
@@ -51,7 +52,8 @@ PDFPAGES = curl_easy_cleanup.pdf curl_ea
  libcurl-errors.pdf curl_easy_strerror.pdf curl_multi_strerror.pdf \
  curl_share_strerror.pdf curl_global_init_mem.pdf libcurl-tutorial.pdf \
  curl_easy_reset.pdf curl_easy_escape.pdf curl_easy_unescape.pdf \
- curl_multi_setopt.pdf curl_multi_socket.pdf curl_multi_timeout.pdf
+ curl_multi_setopt.pdf curl_multi_socket.pdf curl_multi_timeout.pdf \
+ curl_formget.pdf
 
 CLEANFILES = $(HTMLPAGES) $(PDFPAGES)
 
Index: include/curl/curl.h
===================================================================
RCS file: /cvsroot/curl/curl/include/curl/curl.h,v
retrieving revision 1.298
diff -u -p -d -r1.298 curl.h
--- include/curl/curl.h 12 Jun 2006 20:33:05 -0000 1.298
+++ include/curl/curl.h 22 Jun 2006 15:05:06 -0000
@@ -1154,6 +1154,26 @@ CURL_EXTERN CURLFORMcode curl_formadd(st
                                       ...);
 
 /*
+ * callback function for curl_formget()
+ * The void *arg pointer will be the one passed as second argument to curl_formget().
+ * The character buffer passed to it must not be freed.
+ * Should return the buffer length passed to it as the argument "len" on success.
+ */
+typedef size_t (*curl_formget_callback)(void *arg, const char *buf, size_t len);
+
+/*
+ * NAME curl_formget()
+ *
+ * DESCRIPTION
+ *
+ * Serialize a curl_httppost struct built with curl_formadd().
+ * Accepts a void pointer as second argument which will be passed to
+ * the curl_formget_callback function.
+ * Returns 0 on success.
+ */
+CURL_EXTERN int curl_formget(struct curl_httppost *form, void *arg,
+ curl_formget_callback append);
+/*
  * NAME curl_formfree()
  *
  * DESCRIPTION
Index: lib/formdata.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/formdata.c,v
retrieving revision 1.90
diff -u -p -d -r1.90 formdata.c
--- lib/formdata.c 26 Apr 2006 17:26:22 -0000 1.90
+++ lib/formdata.c 22 Jun 2006 15:05:07 -0000
@@ -137,6 +137,8 @@ Content-Disposition: form-data; name="FI
 char *basename(char *path);
 #endif
 
+static size_t readfromfile(struct Form *form, char *buffer, size_t size);
+
 /* What kind of Content-Type to use on un-specified files with unrecognized
    extensions. */
 #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
@@ -886,6 +888,51 @@ void Curl_formclean(struct FormData *for
 }
 
 /*
+ * curl_formget()
+ * Serialize a curl_httppost struct.
+ * Returns 0 on success.
+ */
+int curl_formget(struct curl_httppost *form, void *arg,
+ curl_formget_callback append)
+{
+ CURLFORMcode rc;
+ curl_off_t size;
+ struct FormData *data, *ptr;
+
+ if ((rc = Curl_getFormData(&data, form, &size))) {
+ return rc;
+ }
+
+ for (ptr = data; ptr; ptr = ptr->next) {
+ if (ptr->type == FORM_FILE) {
+ char buffer[8192];
+ size_t read;
+ struct Form temp;
+
+ Curl_FormInit(&temp, ptr);
+
+ do {
+ read = readfromfile(&temp, buffer, sizeof(buffer));
+ if ((read == (size_t) -1) || (read != append(arg, buffer, read))) {
+ if (temp.fp) {
+ fclose(temp.fp);
+ }
+ Curl_formclean(data);
+ return -1;
+ }
+ } while (read == sizeof(buffer));
+ } else {
+ if (ptr->length != append(arg, ptr->line, ptr->length)) {
+ Curl_formclean(data);
+ return -1;
+ }
+ }
+ }
+ Curl_formclean(data);
+ return 0;
+}
+
+/*
  * curl_formfree() is an external function to free up a whole form post
  * chain
  */
@@ -1527,6 +1574,15 @@ CURLFORMcode curl_formadd(struct curl_ht
   return CURL_FORMADD_DISABLED;
 }
 
+CURLFORMCode curl_formget(struct curl_httppost *post, void *arg,
+ curl_formget_callback append)
+{
+ (void) post;
+ (void) arg;
+ (void) append;
+ return CURL_FORMADD_DISABLED;
+}
+
 void curl_formfree(struct curl_httppost *form)
 {
   (void)form;
Received on 2006-06-22