Commit e44ddfd4 authored by Patrick Monnerat's avatar Patrick Monnerat
Browse files

mime: clone mime tree upon easy handle duplication.

A mime tree attached to an easy handle using CURLOPT_MIMEPOST is
strongly bound to the handle: there is a pointer to the easy handle in
each item of the mime tree and following the parent pointer list
of mime items ends in a dummy part stored within the handle.

Because of this binding, a mime tree cannot be shared between different
easy handles, thus it needs to be cloned upon easy handle duplication.

There is no way for the caller to get the duplicated mime tree
handle: it is then set to be automatically destroyed upon freeing the
new easy handle.

New test 654 checks proper mime structure duplication/release.

Add a warning note in curl_mime_data_cb() documentation about sharing
user data between duplicated handles.

Closes #2235
parent 2c821bba
Loading
Loading
Loading
Loading
+9 −2
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@
.\" *                            | (__| |_| |  _ <| |___
.\" *                            | (__| |_| |  _ <| |___
.\" *                             \___|\___/|_| \_\_____|
.\" *                             \___|\___/|_| \_\_____|
.\" *
.\" *
.\" * 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
.\" * 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
@@ -91,6 +91,12 @@ The callback function must return \fICURL_SEEKFUNC_OK\fP on success,
is free to work around the problem if possible. The latter can sometimes be
is free to work around the problem if possible. The latter can sometimes be
done by instead reading from the input or similar.
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
.SH AVAILABILITY
As long as at least one of HTTP, SMTP or IMAP is enabled. Added in 7.56.0.
As long as at least one of HTTP, SMTP or IMAP is enabled. Added in 7.56.0.
.SH RETURN VALUE
.SH RETURN VALUE
@@ -157,4 +163,5 @@ int seek_callback(void *arg, curl_off_t offset, int origin)
.SH "SEE ALSO"
.SH "SEE ALSO"
.BR curl_mime_addpart "(3),"
.BR curl_mime_addpart "(3),"
.BR curl_mime_data "(3),"
.BR curl_mime_data "(3),"
.BR curl_mime_name "(3)"
.BR curl_mime_name "(3),"
.BR curl_easy_duphandle "(3)"
+7 −2
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@
 *                            | (__| |_| |  _ <| |___
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *                             \___|\___/|_| \_\_____|
 *
 *
 * 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
 * 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
@@ -65,6 +65,7 @@
#include "sendf.h" /* for failf function prototype */
#include "sendf.h" /* for failf function prototype */
#include "connect.h" /* for Curl_getconnectinfo */
#include "connect.h" /* for Curl_getconnectinfo */
#include "slist.h"
#include "slist.h"
#include "mime.h"
#include "amigaos.h"
#include "amigaos.h"
#include "non-ascii.h"
#include "non-ascii.h"
#include "warnless.h"
#include "warnless.h"
@@ -855,6 +856,7 @@ static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
  /* Copy src->set into dst->set first, then deal with the strings
  /* Copy src->set into dst->set first, then deal with the strings
     afterwards */
     afterwards */
  dst->set = src->set;
  dst->set = src->set;
  Curl_mime_initpart(&dst->set.mimepost, dst);


  /* clear all string pointers first */
  /* clear all string pointers first */
  memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
  memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
@@ -878,7 +880,10 @@ static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
    dst->set.postfields = dst->set.str[i];
    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;
}
}


/*
/*
+80 −1
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@
 *                            | (__| |_| |  _ <| |___
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *                             \___|\___/|_| \_\_____|
 *
 *
 * 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
 * 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
@@ -1140,6 +1140,78 @@ void curl_mime_free(curl_mime *mime)
  }
  }
}
}


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.
 * Mime build functions.
 */
 */
@@ -1855,6 +1927,13 @@ void Curl_mime_cleanpart(curl_mimepart *part)
  (void) part;
  (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,
CURLcode Curl_mime_set_subparts(curl_mimepart *part,
                                curl_mime *subparts, int take_ownership)
                                curl_mime *subparts, int take_ownership)
{
{
+2 −1
Original line number Original line Diff line number Diff line
@@ -7,7 +7,7 @@
 *                            | (__| |_| |  _ <| |___
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *                             \___|\___/|_| \_\_____|
 *
 *
 * 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
 * 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
@@ -122,6 +122,7 @@ struct curl_mimepart_s {
/* Prototypes. */
/* Prototypes. */
void Curl_mime_initpart(curl_mimepart *part, struct Curl_easy *easy);
void Curl_mime_initpart(curl_mimepart *part, struct Curl_easy *easy);
void Curl_mime_cleanpart(curl_mimepart *part);
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,
CURLcode Curl_mime_set_subparts(curl_mimepart *part,
                                curl_mime *subparts, int take_ownership);
                                curl_mime *subparts, int take_ownership);
CURLcode Curl_mime_prepare_headers(curl_mimepart *part,
CURLcode Curl_mime_prepare_headers(curl_mimepart *part,
+1 −1
Original line number Original line Diff line number Diff line
@@ -80,7 +80,7 @@ test617 test618 test619 test620 test621 test622 test623 test624 test625 \
test626 test627 test628 test629 test630 test631 test632 test633 test634 \
test626 test627 test628 test629 test630 test631 test632 test633 test634 \
test635 test636 test637 test638 test639 test640 test641 test642 \
test635 test636 test637 test638 test639 test640 test641 test642 \
test643 test644 test645 test646 test647 test648 test649 test650 test651 \
test643 test644 test645 test646 test647 test648 test649 test650 test651 \
test652 test653 \
test652 test653 test654 \
\
\
test700 test701 test702 test703 test704 test705 test706 test707 test708 \
test700 test701 test702 test703 test704 test705 test706 test707 test708 \
test709 test710 test711 test712 test713 test714 test715 \
test709 test710 test711 test712 test713 test714 test715 \
Loading