2011-09-20 19:54:14 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2019-02-11 13:10:41 -05:00
|
|
|
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-09-20 19:54:14 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-09-20 19:54:14 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-09-20 19:54:14 -04:00
|
|
|
|
2012-02-05 12:44:22 -05:00
|
|
|
#ifndef CURL_DISABLE_LIBCURL_OPTION
|
|
|
|
|
2011-09-20 19:54:14 -04:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_easysrc.h"
|
|
|
|
#include "tool_setopt.h"
|
2017-09-02 13:17:33 -04:00
|
|
|
#include "tool_convert.h"
|
2011-09-20 19:54:14 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-20 19:54:14 -04:00
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
/* Lookup tables for converting setopt values back to symbols */
|
|
|
|
/* For enums, values may be in any order. */
|
|
|
|
/* For bit masks, put combinations first, then single bits, */
|
|
|
|
/* and finally any "NONE" value. */
|
|
|
|
|
|
|
|
#define NV(e) {#e, e}
|
2013-01-14 14:06:32 -05:00
|
|
|
#define NV1(e, v) {#e, (v)}
|
2012-02-23 04:43:37 -05:00
|
|
|
#define NVEND {NULL, 0} /* sentinel to mark end of list */
|
|
|
|
|
|
|
|
const NameValue setopt_nv_CURLPROXY[] = {
|
|
|
|
NV(CURLPROXY_HTTP),
|
|
|
|
NV(CURLPROXY_HTTP_1_0),
|
2016-11-16 12:49:15 -05:00
|
|
|
NV(CURLPROXY_HTTPS),
|
|
|
|
NV(CURLPROXY_SOCKS4),
|
|
|
|
NV(CURLPROXY_SOCKS5),
|
|
|
|
NV(CURLPROXY_SOCKS4A),
|
|
|
|
NV(CURLPROXY_SOCKS5_HOSTNAME),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
|
|
|
const NameValue setopt_nv_CURL_SOCKS_PROXY[] = {
|
2012-02-23 04:43:37 -05:00
|
|
|
NV(CURLPROXY_SOCKS4),
|
|
|
|
NV(CURLPROXY_SOCKS5),
|
|
|
|
NV(CURLPROXY_SOCKS4A),
|
|
|
|
NV(CURLPROXY_SOCKS5_HOSTNAME),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
2012-04-19 10:31:11 -04:00
|
|
|
const NameValueUnsigned setopt_nv_CURLAUTH[] = {
|
2012-02-23 04:43:37 -05:00
|
|
|
NV(CURLAUTH_ANY), /* combination */
|
|
|
|
NV(CURLAUTH_ANYSAFE), /* combination */
|
|
|
|
NV(CURLAUTH_BASIC),
|
|
|
|
NV(CURLAUTH_DIGEST),
|
|
|
|
NV(CURLAUTH_GSSNEGOTIATE),
|
|
|
|
NV(CURLAUTH_NTLM),
|
|
|
|
NV(CURLAUTH_DIGEST_IE),
|
|
|
|
NV(CURLAUTH_NTLM_WB),
|
|
|
|
NV(CURLAUTH_ONLY),
|
|
|
|
NV(CURLAUTH_NONE),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
|
|
|
const NameValue setopt_nv_CURL_HTTP_VERSION[] = {
|
|
|
|
NV(CURL_HTTP_VERSION_NONE),
|
|
|
|
NV(CURL_HTTP_VERSION_1_0),
|
|
|
|
NV(CURL_HTTP_VERSION_1_1),
|
2015-12-13 03:24:08 -05:00
|
|
|
NV(CURL_HTTP_VERSION_2_0),
|
|
|
|
NV(CURL_HTTP_VERSION_2TLS),
|
2012-02-23 04:43:37 -05:00
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
|
|
|
const NameValue setopt_nv_CURL_SSLVERSION[] = {
|
|
|
|
NV(CURL_SSLVERSION_DEFAULT),
|
|
|
|
NV(CURL_SSLVERSION_TLSv1),
|
|
|
|
NV(CURL_SSLVERSION_SSLv2),
|
|
|
|
NV(CURL_SSLVERSION_SSLv3),
|
2013-09-19 09:17:13 -04:00
|
|
|
NV(CURL_SSLVERSION_TLSv1_0),
|
|
|
|
NV(CURL_SSLVERSION_TLSv1_1),
|
|
|
|
NV(CURL_SSLVERSION_TLSv1_2),
|
2016-10-27 08:58:43 -04:00
|
|
|
NV(CURL_SSLVERSION_TLSv1_3),
|
2012-02-23 04:43:37 -05:00
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
|
|
|
const NameValue setopt_nv_CURL_TIMECOND[] = {
|
|
|
|
NV(CURL_TIMECOND_IFMODSINCE),
|
|
|
|
NV(CURL_TIMECOND_IFUNMODSINCE),
|
|
|
|
NV(CURL_TIMECOND_LASTMOD),
|
|
|
|
NV(CURL_TIMECOND_NONE),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
|
|
|
const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
|
|
|
|
NV(CURLFTPSSL_CCC_NONE),
|
|
|
|
NV(CURLFTPSSL_CCC_PASSIVE),
|
|
|
|
NV(CURLFTPSSL_CCC_ACTIVE),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
2013-07-22 13:43:08 -04:00
|
|
|
const NameValue setopt_nv_CURLUSESSL[] = {
|
|
|
|
NV(CURLUSESSL_NONE),
|
|
|
|
NV(CURLUSESSL_TRY),
|
|
|
|
NV(CURLUSESSL_CONTROL),
|
|
|
|
NV(CURLUSESSL_ALL),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
2015-07-17 02:40:16 -04:00
|
|
|
const NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
|
|
|
|
NV(CURLSSLOPT_ALLOW_BEAST),
|
|
|
|
NV(CURLSSLOPT_NO_REVOKE),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
2013-07-22 14:03:27 -04:00
|
|
|
const NameValue setopt_nv_CURL_NETRC[] = {
|
|
|
|
NV(CURL_NETRC_IGNORED),
|
|
|
|
NV(CURL_NETRC_OPTIONAL),
|
|
|
|
NV(CURL_NETRC_REQUIRED),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
/* These mappings essentially triplicated - see
|
|
|
|
* tool_libinfo.c and tool_paramhlp.c */
|
|
|
|
const NameValue setopt_nv_CURLPROTO[] = {
|
|
|
|
NV(CURLPROTO_ALL), /* combination */
|
|
|
|
NV(CURLPROTO_DICT),
|
|
|
|
NV(CURLPROTO_FILE),
|
|
|
|
NV(CURLPROTO_FTP),
|
|
|
|
NV(CURLPROTO_FTPS),
|
|
|
|
NV(CURLPROTO_GOPHER),
|
|
|
|
NV(CURLPROTO_HTTP),
|
|
|
|
NV(CURLPROTO_HTTPS),
|
|
|
|
NV(CURLPROTO_IMAP),
|
|
|
|
NV(CURLPROTO_IMAPS),
|
|
|
|
NV(CURLPROTO_LDAP),
|
|
|
|
NV(CURLPROTO_LDAPS),
|
|
|
|
NV(CURLPROTO_POP3),
|
|
|
|
NV(CURLPROTO_POP3S),
|
|
|
|
NV(CURLPROTO_RTSP),
|
|
|
|
NV(CURLPROTO_SCP),
|
|
|
|
NV(CURLPROTO_SFTP),
|
2014-11-30 11:27:03 -05:00
|
|
|
NV(CURLPROTO_SMB),
|
|
|
|
NV(CURLPROTO_SMBS),
|
2012-02-23 04:43:37 -05:00
|
|
|
NV(CURLPROTO_SMTP),
|
|
|
|
NV(CURLPROTO_SMTPS),
|
|
|
|
NV(CURLPROTO_TELNET),
|
|
|
|
NV(CURLPROTO_TFTP),
|
|
|
|
NVEND,
|
|
|
|
};
|
|
|
|
|
2013-01-14 14:06:32 -05:00
|
|
|
/* These options have non-zero default values. */
|
|
|
|
static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
|
|
|
|
NV1(CURLOPT_SSL_VERIFYPEER, 1),
|
|
|
|
NV1(CURLOPT_SSL_VERIFYHOST, 1),
|
2014-02-10 02:38:55 -05:00
|
|
|
NV1(CURLOPT_SSL_ENABLE_NPN, 1),
|
|
|
|
NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
|
2016-06-30 08:56:02 -04:00
|
|
|
NV1(CURLOPT_TCP_NODELAY, 1),
|
2016-11-16 12:49:15 -05:00
|
|
|
NV1(CURLOPT_PROXY_SSL_VERIFYPEER, 1),
|
|
|
|
NV1(CURLOPT_PROXY_SSL_VERIFYHOST, 1),
|
2017-05-19 12:11:47 -04:00
|
|
|
NV1(CURLOPT_SOCKS5_AUTH, 1),
|
2013-01-14 14:06:32 -05:00
|
|
|
NVEND
|
|
|
|
};
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
/* Format and add code; jump to nomem on malloc error */
|
|
|
|
#define ADD(args) do { \
|
|
|
|
ret = easysrc_add args; \
|
|
|
|
if(ret) \
|
|
|
|
goto nomem; \
|
2012-03-16 15:10:08 -04:00
|
|
|
} WHILE_FALSE
|
2012-02-23 04:43:37 -05:00
|
|
|
#define ADDF(args) do { \
|
|
|
|
ret = easysrc_addf args; \
|
|
|
|
if(ret) \
|
|
|
|
goto nomem; \
|
2012-03-16 15:10:08 -04:00
|
|
|
} WHILE_FALSE
|
2019-02-11 13:10:41 -05:00
|
|
|
#define NULL_CHECK(p) do { \
|
|
|
|
if(!p) { \
|
|
|
|
ret = CURLE_OUT_OF_MEMORY; \
|
|
|
|
goto nomem; \
|
|
|
|
} \
|
|
|
|
} WHILE_FALSE
|
2012-02-23 04:43:37 -05:00
|
|
|
|
|
|
|
#define DECL0(s) ADD((&easysrc_decl, s))
|
|
|
|
#define DECL1(f,a) ADDF((&easysrc_decl, f,a))
|
|
|
|
|
|
|
|
#define DATA0(s) ADD((&easysrc_data, s))
|
|
|
|
#define DATA1(f,a) ADDF((&easysrc_data, f,a))
|
|
|
|
#define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
|
|
|
|
#define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
|
|
|
|
|
|
|
|
#define CODE0(s) ADD((&easysrc_code, s))
|
|
|
|
#define CODE1(f,a) ADDF((&easysrc_code, f,a))
|
|
|
|
#define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
|
|
|
|
#define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
|
|
|
|
|
|
|
|
#define CLEAN0(s) ADD((&easysrc_clean, s))
|
|
|
|
#define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
|
|
|
|
|
|
|
|
#define REM0(s) ADD((&easysrc_toohard, s))
|
|
|
|
#define REM1(f,a) ADDF((&easysrc_toohard, f,a))
|
|
|
|
#define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
|
|
|
|
|
|
|
|
/* Escape string to C string syntax. Return NULL if out of memory.
|
|
|
|
* Is this correct for those wacky EBCDIC guys? */
|
2017-09-03 11:10:55 -04:00
|
|
|
static char *c_escape(const char *str, size_t len)
|
2012-02-23 04:43:37 -05:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
unsigned char c;
|
|
|
|
char *escaped, *e;
|
2017-09-03 11:10:55 -04:00
|
|
|
|
|
|
|
if(len == CURL_ZERO_TERMINATED)
|
|
|
|
len = strlen(str);
|
2017-09-03 05:18:58 -04:00
|
|
|
|
|
|
|
/* Check for possible overflow. */
|
|
|
|
if(len > (~(size_t) 0) / 4)
|
|
|
|
return NULL;
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
/* Allocate space based on worst-case */
|
|
|
|
escaped = malloc(4 * len + 1);
|
|
|
|
if(!escaped)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
e = escaped;
|
2017-09-09 17:09:06 -04:00
|
|
|
for(s = str; (c = *s) != '\0'; s++) {
|
|
|
|
if(c == '\n') {
|
2012-02-23 04:43:37 -05:00
|
|
|
strcpy(e, "\\n");
|
|
|
|
e += 2;
|
|
|
|
}
|
2017-09-09 17:09:06 -04:00
|
|
|
else if(c == '\r') {
|
2012-02-23 04:43:37 -05:00
|
|
|
strcpy(e, "\\r");
|
|
|
|
e += 2;
|
|
|
|
}
|
2017-09-09 17:09:06 -04:00
|
|
|
else if(c == '\t') {
|
2012-02-23 04:43:37 -05:00
|
|
|
strcpy(e, "\\t");
|
|
|
|
e += 2;
|
|
|
|
}
|
2017-09-09 17:09:06 -04:00
|
|
|
else if(c == '\\') {
|
2012-02-23 04:43:37 -05:00
|
|
|
strcpy(e, "\\\\");
|
|
|
|
e += 2;
|
|
|
|
}
|
2017-09-09 17:09:06 -04:00
|
|
|
else if(c == '"') {
|
2012-02-23 04:43:37 -05:00
|
|
|
strcpy(e, "\\\"");
|
|
|
|
e += 2;
|
|
|
|
}
|
|
|
|
else if(! isprint(c)) {
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(e, 5, "\\%03o", (unsigned)c);
|
2012-02-23 04:43:37 -05:00
|
|
|
e += 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*e++ = c;
|
|
|
|
}
|
|
|
|
*e = '\0';
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setopt wrapper for enum types */
|
2014-03-01 08:38:00 -05:00
|
|
|
CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
|
2012-02-23 04:43:37 -05:00
|
|
|
const char *name, CURLoption tag,
|
|
|
|
const NameValue *nvlist, long lval)
|
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
|
|
|
bool skip = FALSE;
|
|
|
|
|
|
|
|
ret = curl_easy_setopt(curl, tag, lval);
|
|
|
|
if(!lval)
|
|
|
|
skip = TRUE;
|
|
|
|
|
|
|
|
if(config->libcurl && !skip && !ret) {
|
|
|
|
/* we only use this for real if --libcurl was used */
|
|
|
|
const NameValue *nv = NULL;
|
2017-09-09 17:09:06 -04:00
|
|
|
for(nv = nvlist; nv->name; nv++) {
|
2012-02-23 04:43:37 -05:00
|
|
|
if(nv->value == lval) break; /* found it */
|
|
|
|
}
|
|
|
|
if(! nv->name) {
|
|
|
|
/* If no definition was found, output an explicit value.
|
|
|
|
* This could happen if new values are defined and used
|
|
|
|
* but the NameValue list is not updated. */
|
|
|
|
CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-19 10:31:11 -04:00
|
|
|
/* setopt wrapper for flags */
|
2014-03-01 08:38:00 -05:00
|
|
|
CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
|
2012-02-23 04:43:37 -05:00
|
|
|
const char *name, CURLoption tag,
|
|
|
|
const NameValue *nvlist, long lval)
|
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
|
|
|
bool skip = FALSE;
|
|
|
|
|
|
|
|
ret = curl_easy_setopt(curl, tag, lval);
|
|
|
|
if(!lval)
|
|
|
|
skip = TRUE;
|
|
|
|
|
|
|
|
if(config->libcurl && !skip && !ret) {
|
|
|
|
/* we only use this for real if --libcurl was used */
|
|
|
|
char preamble[80]; /* should accommodate any symbol name */
|
|
|
|
long rest = lval; /* bits not handled yet */
|
|
|
|
const NameValue *nv = NULL;
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(preamble, sizeof(preamble),
|
|
|
|
"curl_easy_setopt(hnd, %s, ", name);
|
2017-09-09 17:09:06 -04:00
|
|
|
for(nv = nvlist; nv->name; nv++) {
|
2012-02-23 04:43:37 -05:00
|
|
|
if((nv->value & ~ rest) == 0) {
|
|
|
|
/* all value flags contained in rest */
|
|
|
|
rest &= ~ nv->value; /* remove bits handled here */
|
|
|
|
CODE3("%s(long)%s%s",
|
|
|
|
preamble, nv->name, rest ? " |" : ");");
|
|
|
|
if(!rest)
|
|
|
|
break; /* handled them all */
|
|
|
|
/* replace with all spaces for continuation line */
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
|
2012-02-23 04:43:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If any bits have no definition, output an explicit value.
|
|
|
|
* This could happen if new bits are defined and used
|
|
|
|
* but the NameValue list is not updated. */
|
|
|
|
if(rest)
|
|
|
|
CODE2("%s%ldL);", preamble, rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-19 10:31:11 -04:00
|
|
|
/* setopt wrapper for bitmasks */
|
2014-03-01 08:38:00 -05:00
|
|
|
CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
|
2012-04-19 10:31:11 -04:00
|
|
|
const char *name, CURLoption tag,
|
|
|
|
const NameValueUnsigned *nvlist,
|
|
|
|
long lval)
|
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
|
|
|
bool skip = FALSE;
|
|
|
|
|
|
|
|
ret = curl_easy_setopt(curl, tag, lval);
|
|
|
|
if(!lval)
|
|
|
|
skip = TRUE;
|
|
|
|
|
|
|
|
if(config->libcurl && !skip && !ret) {
|
|
|
|
/* we only use this for real if --libcurl was used */
|
|
|
|
char preamble[80];
|
|
|
|
unsigned long rest = (unsigned long)lval;
|
|
|
|
const NameValueUnsigned *nv = NULL;
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(preamble, sizeof(preamble),
|
|
|
|
"curl_easy_setopt(hnd, %s, ", name);
|
2017-09-09 17:09:06 -04:00
|
|
|
for(nv = nvlist; nv->name; nv++) {
|
2012-04-19 10:31:11 -04:00
|
|
|
if((nv->value & ~ rest) == 0) {
|
|
|
|
/* all value flags contained in rest */
|
|
|
|
rest &= ~ nv->value; /* remove bits handled here */
|
|
|
|
CODE3("%s(long)%s%s",
|
|
|
|
preamble, nv->name, rest ? " |" : ");");
|
|
|
|
if(!rest)
|
|
|
|
break; /* handled them all */
|
|
|
|
/* replace with all spaces for continuation line */
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
|
2012-04-19 10:31:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If any bits have no definition, output an explicit value.
|
|
|
|
* This could happen if new bits are defined and used
|
|
|
|
* but the NameValue list is not updated. */
|
|
|
|
if(rest)
|
|
|
|
CODE2("%s%luUL);", preamble, rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-02 13:17:33 -04:00
|
|
|
/* Generate code for a struct curl_slist. */
|
|
|
|
static CURLcode libcurl_generate_slist(struct curl_slist *slist, int *slistno)
|
2012-02-23 04:43:37 -05:00
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
2012-03-17 15:55:15 -04:00
|
|
|
char *escaped = NULL;
|
2012-02-23 04:43:37 -05:00
|
|
|
|
2017-09-02 13:17:33 -04:00
|
|
|
/* May need several slist variables, so invent name */
|
|
|
|
*slistno = ++easysrc_slist_count;
|
|
|
|
|
|
|
|
DECL1("struct curl_slist *slist%d;", *slistno);
|
|
|
|
DATA1("slist%d = NULL;", *slistno);
|
|
|
|
CLEAN1("curl_slist_free_all(slist%d);", *slistno);
|
|
|
|
CLEAN1("slist%d = NULL;", *slistno);
|
|
|
|
for(; slist; slist = slist->next) {
|
|
|
|
Curl_safefree(escaped);
|
2017-09-03 11:10:55 -04:00
|
|
|
escaped = c_escape(slist->data, CURL_ZERO_TERMINATED);
|
2017-09-02 13:17:33 -04:00
|
|
|
if(!escaped)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
DATA3("slist%d = curl_slist_append(slist%d, \"%s\");",
|
|
|
|
*slistno, *slistno, escaped);
|
|
|
|
}
|
2012-02-23 04:43:37 -05:00
|
|
|
|
2017-09-02 13:17:33 -04:00
|
|
|
nomem:
|
|
|
|
Curl_safefree(escaped);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:10:41 -05:00
|
|
|
static CURLcode libcurl_generate_mime(CURL *curl,
|
|
|
|
struct GlobalConfig *config,
|
|
|
|
tool_mime *toolmime,
|
|
|
|
int *mimeno); /* Forward. */
|
|
|
|
|
2019-02-19 11:08:58 -05:00
|
|
|
/* Wrapper to generate source code for a mime part. */
|
2019-02-11 13:10:41 -05:00
|
|
|
static CURLcode libcurl_generate_mime_part(CURL *curl,
|
2019-02-12 04:09:09 -05:00
|
|
|
struct GlobalConfig *config,
|
|
|
|
tool_mime *part,
|
|
|
|
int mimeno)
|
2017-09-02 13:17:33 -04:00
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
2019-02-12 04:09:09 -05:00
|
|
|
int submimeno = 0;
|
2017-09-02 13:17:33 -04:00
|
|
|
char *escaped = NULL;
|
2019-02-11 13:10:41 -05:00
|
|
|
const char *data = NULL;
|
|
|
|
const char *filename = part->filename;
|
|
|
|
|
|
|
|
/* Parts are linked in reverse order. */
|
|
|
|
if(part->prev) {
|
2019-02-19 11:08:58 -05:00
|
|
|
ret = libcurl_generate_mime_part(curl, config, part->prev, mimeno);
|
2019-02-11 13:10:41 -05:00
|
|
|
if(ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the part. */
|
2019-02-19 11:08:58 -05:00
|
|
|
CODE2("part%d = curl_mime_addpart(mime%d);", mimeno, mimeno);
|
2019-02-11 13:10:41 -05:00
|
|
|
|
|
|
|
switch(part->kind) {
|
|
|
|
case TOOLMIME_PARTS:
|
2019-02-19 11:08:58 -05:00
|
|
|
ret = libcurl_generate_mime(curl, config, part, &submimeno);
|
2019-02-11 13:10:41 -05:00
|
|
|
if(!ret) {
|
2019-02-19 11:08:58 -05:00
|
|
|
CODE2("curl_mime_subparts(part%d, mime%d);", mimeno, submimeno);
|
|
|
|
CODE1("mime%d = NULL;", submimeno); /* Avoid freeing in CLEAN. */
|
2019-02-11 13:10:41 -05:00
|
|
|
}
|
|
|
|
break;
|
2017-09-02 13:17:33 -04:00
|
|
|
|
2019-02-11 13:10:41 -05:00
|
|
|
case TOOLMIME_DATA:
|
2017-09-06 08:42:03 -04:00
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
2019-02-19 11:08:58 -05:00
|
|
|
/* Data will be set in ASCII, thus issue a comment with clear text. */
|
|
|
|
escaped = c_escape(part->data, CURL_ZERO_TERMINATED);
|
|
|
|
NULL_CHECK(escaped);
|
|
|
|
CODE1("/* \"%s\" */", escaped);
|
2019-02-11 13:10:41 -05:00
|
|
|
|
|
|
|
/* Our data is always textual: convert it to ASCII. */
|
|
|
|
{
|
|
|
|
size_t size = strlen(part->data);
|
|
|
|
char *cp = malloc(size + 1);
|
|
|
|
|
|
|
|
NULL_CHECK(cp);
|
|
|
|
memcpy(cp, part->data, size + 1);
|
|
|
|
ret = convert_to_network(cp, size);
|
|
|
|
data = cp;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
data = part->data;
|
2017-09-06 08:42:03 -04:00
|
|
|
#endif
|
2019-02-19 11:08:58 -05:00
|
|
|
if(!ret) {
|
2019-02-11 13:10:41 -05:00
|
|
|
Curl_safefree(escaped);
|
|
|
|
escaped = c_escape(data, CURL_ZERO_TERMINATED);
|
|
|
|
NULL_CHECK(escaped);
|
|
|
|
CODE2("curl_mime_data(part%d, \"%s\", CURL_ZERO_TERMINATED);",
|
|
|
|
mimeno, escaped);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TOOLMIME_FILE:
|
|
|
|
case TOOLMIME_FILEDATA:
|
2019-02-19 11:08:58 -05:00
|
|
|
escaped = c_escape(part->data, CURL_ZERO_TERMINATED);
|
|
|
|
NULL_CHECK(escaped);
|
|
|
|
CODE2("curl_mime_filedata(part%d, \"%s\");", mimeno, escaped);
|
|
|
|
if(part->kind == TOOLMIME_FILEDATA && !filename) {
|
|
|
|
CODE1("curl_mime_filename(part%d, NULL);", mimeno);
|
2019-02-11 13:10:41 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TOOLMIME_STDIN:
|
|
|
|
if(!filename)
|
|
|
|
filename = "-";
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case TOOLMIME_STDINDATA:
|
2019-02-19 11:08:58 -05:00
|
|
|
/* Can only be reading stdin in the current context. */
|
|
|
|
CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\",
|
|
|
|
mimeno);
|
|
|
|
CODE0(" (curl_seek_callback) fseek, NULL, stdin);");
|
2019-02-11 13:10:41 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Other cases not possible in this context. */
|
|
|
|
break;
|
|
|
|
}
|
2017-09-02 13:17:33 -04:00
|
|
|
|
2019-02-11 13:10:41 -05:00
|
|
|
if(!ret && part->encoder) {
|
2019-02-19 11:08:58 -05:00
|
|
|
Curl_safefree(escaped);
|
|
|
|
escaped = c_escape(part->encoder, CURL_ZERO_TERMINATED);
|
|
|
|
NULL_CHECK(escaped);
|
|
|
|
CODE2("curl_mime_encoder(part%d, \"%s\");", mimeno, escaped);
|
2019-02-11 13:10:41 -05:00
|
|
|
}
|
2017-09-05 12:11:59 -04:00
|
|
|
|
2019-02-11 13:10:41 -05:00
|
|
|
if(!ret && filename) {
|
2019-02-19 11:08:58 -05:00
|
|
|
Curl_safefree(escaped);
|
|
|
|
escaped = c_escape(filename, CURL_ZERO_TERMINATED);
|
|
|
|
NULL_CHECK(escaped);
|
|
|
|
CODE2("curl_mime_filename(part%d, \"%s\");", mimeno, escaped);
|
2019-02-11 13:10:41 -05:00
|
|
|
}
|
2017-09-02 13:17:33 -04:00
|
|
|
|
2019-02-11 13:10:41 -05:00
|
|
|
if(!ret && part->name) {
|
2019-02-19 11:08:58 -05:00
|
|
|
Curl_safefree(escaped);
|
|
|
|
escaped = c_escape(part->name, CURL_ZERO_TERMINATED);
|
|
|
|
NULL_CHECK(escaped);
|
|
|
|
CODE2("curl_mime_name(part%d, \"%s\");", mimeno, escaped);
|
2019-02-11 13:10:41 -05:00
|
|
|
}
|
2017-09-02 13:17:33 -04:00
|
|
|
|
2019-02-11 13:10:41 -05:00
|
|
|
if(!ret && part->type) {
|
2019-02-19 11:08:58 -05:00
|
|
|
Curl_safefree(escaped);
|
|
|
|
escaped = c_escape(part->type, CURL_ZERO_TERMINATED);
|
|
|
|
NULL_CHECK(escaped);
|
|
|
|
CODE2("curl_mime_type(part%d, \"%s\");", mimeno, escaped);
|
2019-02-11 13:10:41 -05:00
|
|
|
}
|
2017-09-02 13:17:33 -04:00
|
|
|
|
2019-02-11 13:10:41 -05:00
|
|
|
if(!ret && part->headers) {
|
2019-02-19 11:08:58 -05:00
|
|
|
int slistno;
|
|
|
|
|
|
|
|
ret = libcurl_generate_slist(part->headers, &slistno);
|
|
|
|
if(!ret) {
|
|
|
|
CODE2("curl_mime_headers(part%d, slist%d, 1);", mimeno, slistno);
|
|
|
|
CODE1("slist%d = NULL;", slistno); /* Prevent CLEANing. */
|
2012-02-23 04:43:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-02 13:17:33 -04:00
|
|
|
nomem:
|
2019-02-11 13:10:41 -05:00
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
|
|
|
if(data)
|
|
|
|
free((char *) data);
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 15:55:15 -04:00
|
|
|
Curl_safefree(escaped);
|
2012-02-23 04:43:37 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-19 11:08:58 -05:00
|
|
|
/* Wrapper to generate source code for a mime structure. */
|
2019-02-11 13:10:41 -05:00
|
|
|
static CURLcode libcurl_generate_mime(CURL *curl,
|
|
|
|
struct GlobalConfig *config,
|
|
|
|
tool_mime *toolmime,
|
|
|
|
int *mimeno)
|
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
|
|
|
|
2019-02-19 11:08:58 -05:00
|
|
|
/* May need several mime variables, so invent name. */
|
|
|
|
*mimeno = ++easysrc_mime_count;
|
|
|
|
DECL1("curl_mime *mime%d;", *mimeno);
|
|
|
|
DATA1("mime%d = NULL;", *mimeno);
|
|
|
|
CODE1("mime%d = curl_mime_init(hnd);", *mimeno);
|
|
|
|
CLEAN1("curl_mime_free(mime%d);", *mimeno);
|
|
|
|
CLEAN1("mime%d = NULL;", *mimeno);
|
2019-02-11 13:10:41 -05:00
|
|
|
|
|
|
|
if(toolmime->subparts) {
|
2019-02-19 11:08:58 -05:00
|
|
|
DECL1("curl_mimepart *part%d;", *mimeno);
|
2019-02-11 13:10:41 -05:00
|
|
|
ret = libcurl_generate_mime_part(curl, config,
|
2019-02-19 11:08:58 -05:00
|
|
|
toolmime->subparts, *mimeno);
|
2019-02-11 13:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-02 13:17:33 -04:00
|
|
|
/* setopt wrapper for CURLOPT_MIMEPOST */
|
|
|
|
CURLcode tool_setopt_mimepost(CURL *curl, struct GlobalConfig *config,
|
|
|
|
const char *name, CURLoption tag,
|
2019-02-19 11:08:58 -05:00
|
|
|
curl_mime *mimepost)
|
2017-09-02 13:17:33 -04:00
|
|
|
{
|
2019-02-19 11:08:58 -05:00
|
|
|
CURLcode ret = curl_easy_setopt(curl, tag, mimepost);
|
2019-02-11 13:10:41 -05:00
|
|
|
int mimeno = 0;
|
2017-09-02 13:17:33 -04:00
|
|
|
|
2019-02-19 11:08:58 -05:00
|
|
|
if(!ret && config->libcurl) {
|
|
|
|
ret = libcurl_generate_mime(curl, config,
|
|
|
|
config->current->mimeroot, &mimeno);
|
2017-09-02 13:17:33 -04:00
|
|
|
|
2019-02-19 11:08:58 -05:00
|
|
|
if(!ret)
|
2019-02-11 13:10:41 -05:00
|
|
|
CODE2("curl_easy_setopt(hnd, %s, mime%d);", name, mimeno);
|
2017-09-02 13:17:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
/* setopt wrapper for curl_slist options */
|
2014-03-01 08:38:00 -05:00
|
|
|
CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
|
2012-02-23 04:43:37 -05:00
|
|
|
const char *name, CURLoption tag,
|
|
|
|
struct curl_slist *list)
|
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
|
|
|
|
|
|
|
ret = curl_easy_setopt(curl, tag, list);
|
|
|
|
|
2017-09-02 13:17:33 -04:00
|
|
|
if(config->libcurl && list && !ret) {
|
2012-02-23 04:43:37 -05:00
|
|
|
int i;
|
2017-09-02 13:17:33 -04:00
|
|
|
|
|
|
|
ret = libcurl_generate_slist(list, &i);
|
|
|
|
if(!ret)
|
|
|
|
CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
|
2012-02-23 04:43:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic setopt wrapper for all other options.
|
|
|
|
* Some type information is encoded in the tag value. */
|
2014-03-01 08:38:00 -05:00
|
|
|
CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *config,
|
2011-09-20 19:54:14 -04:00
|
|
|
const char *name, CURLoption tag, ...)
|
|
|
|
{
|
|
|
|
va_list arg;
|
2012-02-23 04:43:37 -05:00
|
|
|
char buf[256];
|
2012-03-16 14:06:34 -04:00
|
|
|
const char *value = NULL;
|
2011-09-20 19:54:14 -04:00
|
|
|
bool remark = FALSE;
|
|
|
|
bool skip = FALSE;
|
2012-02-23 04:43:37 -05:00
|
|
|
bool escape = FALSE;
|
2012-03-17 15:55:15 -04:00
|
|
|
char *escaped = NULL;
|
2011-09-20 19:54:14 -04:00
|
|
|
CURLcode ret = CURLE_OK;
|
|
|
|
|
|
|
|
va_start(arg, tag);
|
|
|
|
|
|
|
|
if(tag < CURLOPTTYPE_OBJECTPOINT) {
|
2012-02-23 04:43:37 -05:00
|
|
|
/* Value is expected to be a long */
|
2011-09-20 19:54:14 -04:00
|
|
|
long lval = va_arg(arg, long);
|
2013-01-14 14:06:32 -05:00
|
|
|
long defval = 0L;
|
|
|
|
const NameValue *nv = NULL;
|
2017-09-09 17:09:06 -04:00
|
|
|
for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
|
2013-01-14 14:06:32 -05:00
|
|
|
if(!strcmp(name, nv->name)) {
|
|
|
|
defval = nv->value;
|
|
|
|
break; /* found it */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(buf, sizeof(buf), "%ldL", lval);
|
2012-02-23 04:43:37 -05:00
|
|
|
value = buf;
|
2011-09-20 19:54:14 -04:00
|
|
|
ret = curl_easy_setopt(curl, tag, lval);
|
2013-01-14 14:06:32 -05:00
|
|
|
if(lval == defval)
|
2011-09-20 19:54:14 -04:00
|
|
|
skip = TRUE;
|
|
|
|
}
|
|
|
|
else if(tag < CURLOPTTYPE_OFF_T) {
|
2012-02-23 04:43:37 -05:00
|
|
|
/* Value is some sort of object pointer */
|
2011-09-20 19:54:14 -04:00
|
|
|
void *pval = va_arg(arg, void *);
|
|
|
|
|
|
|
|
/* function pointers are never printable */
|
|
|
|
if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
|
|
|
|
if(pval) {
|
2012-02-23 04:43:37 -05:00
|
|
|
value = "functionpointer";
|
2011-09-20 19:54:14 -04:00
|
|
|
remark = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
skip = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
else if(pval && str) {
|
|
|
|
value = (char *)pval;
|
|
|
|
escape = TRUE;
|
|
|
|
}
|
2011-09-20 19:54:14 -04:00
|
|
|
else if(pval) {
|
2012-02-23 04:43:37 -05:00
|
|
|
value = "objectpointer";
|
2011-09-20 19:54:14 -04:00
|
|
|
remark = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
skip = TRUE;
|
|
|
|
|
|
|
|
ret = curl_easy_setopt(curl, tag, pval);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2012-02-23 04:43:37 -05:00
|
|
|
/* Value is expected to be curl_off_t */
|
2011-09-20 19:54:14 -04:00
|
|
|
curl_off_t oval = va_arg(arg, curl_off_t);
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(buf, sizeof(buf),
|
|
|
|
"(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
|
2012-02-23 04:43:37 -05:00
|
|
|
value = buf;
|
2011-09-20 19:54:14 -04:00
|
|
|
ret = curl_easy_setopt(curl, tag, oval);
|
|
|
|
|
|
|
|
if(!oval)
|
|
|
|
skip = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
if(config->libcurl && !skip && !ret) {
|
|
|
|
/* we only use this for real if --libcurl was used */
|
|
|
|
|
|
|
|
if(remark)
|
2012-02-23 04:43:37 -05:00
|
|
|
REM2("%s set to a %s", name, value);
|
2011-09-20 19:54:14 -04:00
|
|
|
else {
|
2012-02-23 04:43:37 -05:00
|
|
|
if(escape) {
|
2017-09-03 11:10:55 -04:00
|
|
|
escaped = c_escape(value, CURL_ZERO_TERMINATED);
|
2019-02-11 13:10:41 -05:00
|
|
|
NULL_CHECK(escaped);
|
2012-02-23 04:43:37 -05:00
|
|
|
CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
|
2011-09-20 19:54:14 -04:00
|
|
|
}
|
|
|
|
else
|
2012-02-23 04:43:37 -05:00
|
|
|
CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
|
2011-09-20 19:54:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
nomem:
|
2012-03-17 15:55:15 -04:00
|
|
|
Curl_safefree(escaped);
|
2011-09-20 19:54:14 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:44:22 -05:00
|
|
|
#endif /* CURL_DISABLE_LIBCURL_OPTION */
|