]> granicus.if.org Git - curl/commitdiff
Michael Wallner added curl_formget(), which allows an application to extract
authorDaniel Stenberg <daniel@haxx.se>
Sat, 24 Jun 2006 21:46:41 +0000 (21:46 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Sat, 24 Jun 2006 21:46:41 +0000 (21:46 +0000)
(serialise) a previously built formpost (as with curl_formadd()).

CHANGES
RELEASE-NOTES
docs/libcurl/Makefile.am
docs/libcurl/curl_formget.3 [new file with mode: 0644]
include/curl/curl.h
lib/formdata.c

diff --git a/CHANGES b/CHANGES
index 6cf3d624f352580707d68a53ece721e24165eee5..e4bd7d90e76976b07023392efffeeb8037539a57 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,10 @@
 
                                   Changelog
 
+Daniel (24 June 2006)
+- Michael Wallner added curl_formget(), which allows an application to extract
+  (serialise) a previously built formpost (as with curl_formadd()).
+
 Daniel (23 June 2006)
 - Arve Knudsen found a flaw in curl_multi_fdset() for systems where
   curl_socket_t is unsigned (like Windows) that could cause it to wrongly
index 03bf9a6a1b66dfcacf83fde2fab5b032df34c418..cff353cedbdf52aca25d848fc1d80807e71fce67 100644 (file)
@@ -11,6 +11,7 @@ Curl and libcurl 7.15.5
 
 This release includes the following changes:
 
+ o added curl_formget()
  o added CURLOPT_MAX_SEND_SPEED_LARGE and CURLOPT_MAX_RECV_SPEED_LARGE
  o configure --enable-hidden-symbols
 
@@ -31,6 +32,6 @@ New curl mirrors:
 This release would not have looked like this without help, code, reports and
 advice from friends like these:
 
- Dan Fandrich, Peter Silva, Arve Knudsen
+ Dan Fandrich, Peter Silva, Arve Knudsen, Michael Wallner
 
         Thanks! (and sorry if I forgot to mention someone)
index 85dd281ebb8467aca31b40ddbc9e91d51796921c..e43a0b67ce9cff7d7ed18d4443960b9e097dcf23 100644 (file)
@@ -18,7 +18,7 @@ man_MANS = curl_easy_cleanup.3 curl_easy_getinfo.3 curl_easy_init.3    \
  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_easy_getinfo.html               \
  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_easy_getinfo.pdf curl_easy_init.pdf \
  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)
 
diff --git a/docs/libcurl/curl_formget.3 b/docs/libcurl/curl_formget.3
new file mode 100644 (file)
index 0000000..b0aad36
--- /dev/null
@@ -0,0 +1,49 @@
+.\" You can view this file with:
+.\" nroff -man [file]
+.\" $Id$
+.\"
+.TH curl_formget 3 "20 June 2006" "libcurl 7.15.5" "libcurl Manual"
+.SH NAME
+curl_formget - serialize a previously build multipart/formdata HTTP POST chain
+.SH SYNOPSIS
+.B #include <curl/curl.h>
+.sp
+.BI "void curl_formget(struct curl_httppost *" form, " void *" arg,
+.BI " curl_formget_callback " append);
+.ad
+.SH DESCRIPTION
+curl_formget() is used to serialize data previously built/appended with
+\fIcurl_formadd(3)\fP. Accepts a void pointer as second argument which will be
+passed to the curl_formget_callback function.
+
+.B "typedef size_t (*curl_formget_callback)(void *" arg, " const char *" buf,
+.B " size_t " len);
+.nf
+
+The curl_formget_callback will be executed for each part of the httppost
+struct. 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. The
+callback should return the buffer length passed to it on success.
+.SH RETURN VALUE
+0 means everything was ok, non-zero means an error occurred
+.SH EXAMPLE
+.nf
+
+ size_t print_httppost_callback(void *arg, const char *buf, size_t len)
+ {
+   fwrite(buf, len, 1, stdout);
+   (*(size_t *) arg) += len;
+   return len;
+ }
+ size_t print_httppost(struct curl_httppost *post)
+ {
+   size_t total_size = 0;
+   if(curl_formget(post, &total_size, out)) {
+     return (size_t) -1;
+   }
+   return total_size;
+ }
+.SH AVAILABILITY
+This function was added in libcurl 7.15.5
+.SH "SEE ALSO"
+.BR curl_formadd "(3) "
index 79e71ca0a84cce475683e91c5a822536e8af9396..1a118a36c642893cebdbdd6018c5222d8191226b 100644 (file)
@@ -1158,6 +1158,26 @@ CURL_EXTERN CURLFORMcode curl_formadd(struct curl_httppost **httppost,
                                       struct curl_httppost **last_post,
                                       ...);
 
+/*
+ * 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()
  *
index 86c297745ebcd5110d648ce8e7f87c703a6baf3d..0fbb23aa7774546fb147759f67f0c7bc9c425d44 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -137,6 +137,8 @@ Content-Disposition: form-data; name="FILECONTENT"
 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"
@@ -885,6 +887,51 @@ void Curl_formclean(struct FormData *form)
   } while((form=next)); /* continue */
 }
 
+/*
+ * 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 (int)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
@@ -1284,7 +1331,7 @@ static size_t readfromfile(struct Form *form, char *buffer, size_t size)
   nread = fread(buffer, 1, size, form->fp);
 
   if(nread != size) {
-    /* this is the last chunk form the file, move on */
+    /* this is the last chunk from the file, move on */
     fclose(form->fp);
     form->fp = NULL;
     form->data = form->data->next;
@@ -1527,6 +1574,15 @@ CURLFORMcode curl_formadd(struct curl_httppost **httppost,
   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;