.\" * | (__| |_| | _ <| |___
.\" * \___|\___/|_| \_\_____|
.\" *
-.\" * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
+.\" * Copyright (C) 1998 - 2018, 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
is free to work around the problem if possible. The latter can sometimes be
done by instead reading from the input or similar.
+Care must be taken if the part is bound to a curl easy handle that is later
+duplicated: the \fIarg\fP pointer argument is also duplicated, resulting in
+the pointed item to be shared between the original and the copied handle.
+In particular, special attention should be given to the \fIfreefunc\fP
+procedure code since it will be called twice with the same argument.
+
.SH AVAILABILITY
As long as at least one of HTTP, SMTP or IMAP is enabled. Added in 7.56.0.
.SH RETURN VALUE
.SH "SEE ALSO"
.BR curl_mime_addpart "(3),"
.BR curl_mime_data "(3),"
-.BR curl_mime_name "(3)"
+.BR curl_mime_name "(3),"
+.BR curl_easy_duphandle "(3)"
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2018, 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
#include "sendf.h" /* for failf function prototype */
#include "connect.h" /* for Curl_getconnectinfo */
#include "slist.h"
+#include "mime.h"
#include "amigaos.h"
#include "non-ascii.h"
#include "warnless.h"
/* Copy src->set into dst->set first, then deal with the strings
afterwards */
dst->set = src->set;
+ Curl_mime_initpart(&dst->set.mimepost, dst);
/* clear all string pointers first */
memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
dst->set.postfields = dst->set.str[i];
}
- return CURLE_OK;
+ /* Duplicate mime data. */
+ result = Curl_mime_duppart(&dst->set.mimepost, &src->set.mimepost);
+
+ return result;
}
/*
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2018, 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
}
}
+CURLcode Curl_mime_duppart(curl_mimepart *dst, const curl_mimepart *src)
+{
+ curl_mime *mime;
+ curl_mimepart *d;
+ const curl_mimepart *s;
+ CURLcode res = CURLE_OK;
+
+ /* Duplicate content. */
+ switch(src->kind) {
+ case MIMEKIND_NONE:
+ break;
+ case MIMEKIND_DATA:
+ res = curl_mime_data(dst, src->data, (size_t) src->datasize);
+ break;
+ case MIMEKIND_FILE:
+ res = curl_mime_filedata(dst, src->data);
+ /* Do not abort duplication if file is not readable. */
+ if(res == CURLE_READ_ERROR)
+ res = CURLE_OK;
+ break;
+ case MIMEKIND_CALLBACK:
+ res = curl_mime_data_cb(dst, src->datasize, src->readfunc,
+ src->seekfunc, src->freefunc, src->arg);
+ break;
+ case MIMEKIND_MULTIPART:
+ /* No one knows about the cloned subparts, thus always attach ownership
+ to the part. */
+ mime = curl_mime_init(dst->easy);
+ res = mime? curl_mime_subparts(dst, mime): CURLE_OUT_OF_MEMORY;
+
+ /* Duplicate subparts. */
+ for(s = ((curl_mime *) src->arg)->firstpart; !res && s; s = s->nextpart) {
+ d = curl_mime_addpart(mime);
+ res = d? Curl_mime_duppart(d, s): CURLE_OUT_OF_MEMORY;
+ }
+ break;
+ default: /* Invalid kind: should not occur. */
+ res = CURLE_BAD_FUNCTION_ARGUMENT; /* Internal error? */
+ break;
+ }
+
+ /* Duplicate headers. */
+ if(!res && src->userheaders) {
+ struct curl_slist *hdrs = Curl_slist_duplicate(src->userheaders);
+
+ if(!hdrs)
+ res = CURLE_OUT_OF_MEMORY;
+ else {
+ /* No one but this procedure knows about the new header list,
+ so always take ownership. */
+ res = curl_mime_headers(dst, hdrs, TRUE);
+ if(res)
+ curl_slist_free_all(hdrs);
+ }
+ }
+
+ /* Duplicate other fields. */
+ dst->encoder = src->encoder;
+ if(!res)
+ res = curl_mime_type(dst, src->mimetype);
+ if(!res)
+ res = curl_mime_name(dst, src->name);
+ if(!res)
+ res = curl_mime_filename(dst, src->filename);
+
+ /* If an error occurred, rollback. */
+ if(res)
+ Curl_mime_cleanpart(dst);
+
+ return res;
+}
+
/*
* Mime build functions.
*/
(void) part;
}
+CURLcode Curl_mime_duppart(curl_mimepart *dst, const curl_mimepart *src)
+{
+ (void) dst;
+ (void) src;
+ return CURLE_OK; /* Nothing to duplicate: always succeed. */
+}
+
CURLcode Curl_mime_set_subparts(curl_mimepart *part,
curl_mime *subparts, int take_ownership)
{
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2018, 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
/* Prototypes. */
void Curl_mime_initpart(curl_mimepart *part, struct Curl_easy *easy);
void Curl_mime_cleanpart(curl_mimepart *part);
+CURLcode Curl_mime_duppart(curl_mimepart *dst, const curl_mimepart *src);
CURLcode Curl_mime_set_subparts(curl_mimepart *part,
curl_mime *subparts, int take_ownership);
CURLcode Curl_mime_prepare_headers(curl_mimepart *part,
test626 test627 test628 test629 test630 test631 test632 test633 test634 \
test635 test636 test637 test638 test639 test640 test641 test642 \
test643 test644 test645 test646 test647 test648 test649 test650 test651 \
-test652 test653 \
+test652 test653 test654 \
\
test700 test701 test702 test703 test704 test705 test706 test707 test708 \
test709 test710 test711 test712 test713 test714 test715 \
--- /dev/null
+<testcase>
+<info>
+<keywords>
+HTTP
+HTTP POST
+HTTP MIME POST
+</keywords>
+</info>
+
+#
+# Server-side
+<reply>
+<data>
+HTTP/1.1 200 OK\r
+Date: Thu, 09 Nov 2010 14:49:00 GMT\r
+Server: test-server/fake swsclose\r
+Connection: close\r
+Content-Type: text/html\r
+\r
+hello
+</data>
+<datacheck>
+HTTP/1.1 200 OK\r
+Date: Thu, 09 Nov 2010 14:49:00 GMT\r
+Server: test-server/fake swsclose\r
+Connection: close\r
+Content-Type: text/html\r
+\r
+hello
+HTTP/1.1 200 OK\r
+Date: Thu, 09 Nov 2010 14:49:00 GMT\r
+Server: test-server/fake swsclose\r
+Connection: close\r
+Content-Type: text/html\r
+\r
+hello
+</datacheck>
+</reply>
+
+# Client-side
+<client>
+<server>
+http
+</server>
+# tool is what to use instead of 'curl'
+<tool>
+lib654
+</tool>
+
+ <name>
+HTTP duplicate easy handle with mime data
+ </name>
+ <command>
+http://%HOSTIP:%HTTPPORT/654
+</command>
+<file name="log/file654.txt">
+This is data from a file
+</file>
+</client>
+
+#
+# Verify data after the test has been "shot"
+<verify>
+<strippart>
+s/^--------------------------[a-z0-9]*/------------------------------/
+s/boundary=------------------------[a-z0-9]*/boundary=----------------------------/
+</strippart>
+# Note that the stripping above removes 12 bytes from every occurrence of the
+# boundary string and since 5 of them are in the body contents, we see
+# (5*12) == 60 bytes less
+<protocol>
+POST /654 HTTP/1.1\r
+Host: %HOSTIP:%HTTPPORT\r
+Accept: */*\r
+Content-Length: 0\r
+\r
+POST /654 HTTP/1.1\r
+Host: %HOSTIP:%HTTPPORT\r
+Accept: */*\r
+Transfer-Encoding: chunked\r
+Content-Type: multipart/form-data; boundary=----------------------------\r
+Expect: 100-continue\r
+\r
+20c\r
+------------------------------\r
+Content-Disposition: form-data; name="greeting"\r
+Content-Type: application/X-Greeting\r
+Content-Transfer-Encoding: base64\r
+X-Test-Number: 654\r
+\r
+aGVsbG8=\r
+------------------------------\r
+Content-Disposition: form-data; filename="file654.txt"\r
+Content-Type: text/plain\r
+\r
+This is data from a file
+\r
+------------------------------\r
+Content-Disposition: form-data\r
+\r
+this is what we post to the silly web server
+\r
+--------------------------------\r
+\r
+0\r
+\r
+</protocol>
+</verify>
+</testcase>
lib559 lib560 lib562 lib564 lib565 lib566 lib567 lib568 lib569 lib570 \
lib571 lib572 lib573 lib574 lib575 lib576 lib578 lib579 lib582 \
lib583 lib585 lib586 lib587 lib589 lib590 lib591 lib597 lib598 lib599 \
- lib643 lib644 lib645 lib650 lib651 lib652 lib653 \
+ lib643 lib644 lib645 lib650 lib651 lib652 lib653 lib654 \
lib1500 lib1501 lib1502 lib1503 lib1504 lib1505 lib1506 lib1507 lib1508 \
lib1509 lib1510 lib1511 lib1512 lib1513 lib1514 lib1515 lib1517 \
lib1520 lib1521 \
lib653_SOURCES = lib653.c $(SUPPORTFILES)
lib653_CPPFLAGS = $(AM_CPPFLAGS)
+lib654_SOURCES = lib654.c $(SUPPORTFILES)
+lib654_CPPFLAGS = $(AM_CPPFLAGS)
+
lib1500_SOURCES = lib1500.c $(SUPPORTFILES) $(TESTUTIL)
lib1500_LDADD = $(TESTUTIL_LIBS)
lib1500_CPPFLAGS = $(AM_CPPFLAGS)
--- /dev/null
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2018, 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
+ * are also available at https://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "test.h"
+
+#include "memdebug.h"
+
+static char data[]=
+#ifdef CURL_DOES_CONVERSIONS
+ /* ASCII representation with escape sequences for non-ASCII platforms */
+ "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
+ "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
+ "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
+#else
+ "this is what we post to the silly web server\n";
+#endif
+
+struct WriteThis {
+ char *readptr;
+ curl_off_t sizeleft;
+ int freecount;
+};
+
+static void free_callback(void *userp)
+{
+ struct WriteThis *pooh = (struct WriteThis *) userp;
+
+ pooh->freecount++;
+}
+
+static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
+{
+ struct WriteThis *pooh = (struct WriteThis *)userp;
+ int eof = !*pooh->readptr;
+
+ if(size*nmemb < 1)
+ return 0;
+
+ eof = pooh->sizeleft <= 0;
+ if(!eof)
+ pooh->sizeleft--;
+
+ if(!eof) {
+ *ptr = *pooh->readptr; /* copy one single byte */
+ pooh->readptr++; /* advance pointer */
+ return 1; /* we return 1 byte at a time! */
+ }
+
+ return 0; /* no more data left to deliver */
+}
+
+int test(char *URL)
+{
+ CURL *easy = NULL;
+ CURL *easy2 = NULL;
+ curl_mime *mime = NULL;
+ curl_mimepart *part;
+ struct curl_slist *hdrs = NULL;
+ CURLcode result;
+ int res = TEST_ERR_FAILURE;
+ struct WriteThis pooh;
+
+ /*
+ * Check proper copy/release of mime post data bound to a duplicated
+ * easy handle.
+ */
+
+ if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
+ fprintf(stderr, "curl_global_init() failed\n");
+ return TEST_ERR_MAJOR_BAD;
+ }
+
+ easy = curl_easy_init();
+
+ /* First set the URL that is about to receive our POST. */
+ test_setopt(easy, CURLOPT_URL, URL);
+
+ /* get verbose debug output please */
+ test_setopt(easy, CURLOPT_VERBOSE, 1L);
+
+ /* include headers in the output */
+ test_setopt(easy, CURLOPT_HEADER, 1L);
+
+ /* Prepare the callback structure. */
+ pooh.readptr = data;
+ pooh.sizeleft = (curl_off_t) strlen(data);
+ pooh.freecount = 0;
+
+ /* Build the mime tree. */
+ mime = curl_mime_init(easy);
+ part = curl_mime_addpart(mime);
+ curl_mime_data(part, "hello", CURL_ZERO_TERMINATED);
+ curl_mime_name(part, "greeting");
+ curl_mime_type(part, "application/X-Greeting");
+ curl_mime_encoder(part, "base64");
+ hdrs = curl_slist_append(hdrs, "X-Test-Number: 654");
+ curl_mime_headers(part, hdrs, TRUE);
+ part = curl_mime_addpart(mime);
+ curl_mime_filedata(part, "log/file654.txt");
+ part = curl_mime_addpart(mime);
+ curl_mime_data_cb(part, (curl_off_t) -1, read_callback, NULL, free_callback,
+ &pooh);
+
+ /* Bind mime data to its easy handle. */
+ test_setopt(easy, CURLOPT_MIMEPOST, mime);
+
+ /* Duplicate the handle. */
+ easy2 = curl_easy_duphandle(easy);
+ if(!easy2) {
+ fprintf(stderr, "curl_easy_duphandle() failed\n");
+ res = TEST_ERR_FAILURE;
+ goto test_cleanup;
+ }
+
+ /* Now free the mime structure: it should unbind it from the first
+ easy handle. */
+ curl_mime_free(mime);
+ mime = NULL; /* Already cleaned up. */
+
+ /* Perform on the first handle: should not send any data. */
+ result = curl_easy_perform(easy);
+ if(result) {
+ fprintf(stderr, "curl_easy_perform(original) failed\n");
+ res = (int) result;
+ goto test_cleanup;
+ }
+
+ /* Perform on the second handle: if the bound mime structure has not been
+ duplicated properly, it should cause a valgrind error. */
+ result = curl_easy_perform(easy2);
+ if(result) {
+ fprintf(stderr, "curl_easy_perform(duplicated) failed\n");
+ res = (int) result;
+ goto test_cleanup;
+ }
+
+ /* Free the duplicated handle: it should call free_callback again.
+ If the mime copy was bad or not automatically released, valgrind
+ will signal it. */
+ curl_easy_cleanup(easy2);
+ easy2 = NULL; /* Already cleaned up. */
+
+ if(pooh.freecount != 2) {
+ fprintf(stderr, "free_callback() called %d times instead of 2\n",
+ pooh.freecount);
+ res = TEST_ERR_FAILURE;
+ goto test_cleanup;
+ }
+
+test_cleanup:
+ curl_easy_cleanup(easy);
+ curl_easy_cleanup(easy2);
+ curl_mime_free(mime);
+ curl_global_cleanup();
+ return res;
+}