1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
24 #ifndef CURL_DISABLE_LIBCURL_OPTION
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
30 #include "tool_cfgable.h"
31 #include "tool_easysrc.h"
32 #include "tool_setopt.h"
33 #include "tool_convert.h"
36 #include "memdebug.h" /* keep this as LAST include */
38 /* Lookup tables for converting setopt values back to symbols */
39 /* For enums, values may be in any order. */
40 /* For bit masks, put combinations first, then single bits, */
41 /* and finally any "NONE" value. */
44 #define NV1(e, v) {#e, (v)}
45 #define NVEND {NULL, 0} /* sentinel to mark end of list */
47 const NameValue setopt_nv_CURLPROXY[] = {
49 NV(CURLPROXY_HTTP_1_0),
53 NV(CURLPROXY_SOCKS4A),
54 NV(CURLPROXY_SOCKS5_HOSTNAME),
58 const NameValue setopt_nv_CURL_SOCKS_PROXY[] = {
61 NV(CURLPROXY_SOCKS4A),
62 NV(CURLPROXY_SOCKS5_HOSTNAME),
66 const NameValueUnsigned setopt_nv_CURLAUTH[] = {
67 NV(CURLAUTH_ANY), /* combination */
68 NV(CURLAUTH_ANYSAFE), /* combination */
71 NV(CURLAUTH_GSSNEGOTIATE),
73 NV(CURLAUTH_DIGEST_IE),
80 const NameValue setopt_nv_CURL_HTTP_VERSION[] = {
81 NV(CURL_HTTP_VERSION_NONE),
82 NV(CURL_HTTP_VERSION_1_0),
83 NV(CURL_HTTP_VERSION_1_1),
84 NV(CURL_HTTP_VERSION_2_0),
85 NV(CURL_HTTP_VERSION_2TLS),
89 const NameValue setopt_nv_CURL_SSLVERSION[] = {
90 NV(CURL_SSLVERSION_DEFAULT),
91 NV(CURL_SSLVERSION_TLSv1),
92 NV(CURL_SSLVERSION_SSLv2),
93 NV(CURL_SSLVERSION_SSLv3),
94 NV(CURL_SSLVERSION_TLSv1_0),
95 NV(CURL_SSLVERSION_TLSv1_1),
96 NV(CURL_SSLVERSION_TLSv1_2),
97 NV(CURL_SSLVERSION_TLSv1_3),
101 const NameValue setopt_nv_CURL_TIMECOND[] = {
102 NV(CURL_TIMECOND_IFMODSINCE),
103 NV(CURL_TIMECOND_IFUNMODSINCE),
104 NV(CURL_TIMECOND_LASTMOD),
105 NV(CURL_TIMECOND_NONE),
109 const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
110 NV(CURLFTPSSL_CCC_NONE),
111 NV(CURLFTPSSL_CCC_PASSIVE),
112 NV(CURLFTPSSL_CCC_ACTIVE),
116 const NameValue setopt_nv_CURLUSESSL[] = {
119 NV(CURLUSESSL_CONTROL),
124 const NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
125 NV(CURLSSLOPT_ALLOW_BEAST),
126 NV(CURLSSLOPT_NO_REVOKE),
130 const NameValue setopt_nv_CURL_NETRC[] = {
131 NV(CURL_NETRC_IGNORED),
132 NV(CURL_NETRC_OPTIONAL),
133 NV(CURL_NETRC_REQUIRED),
137 /* These mappings essentially triplicated - see
138 * tool_libinfo.c and tool_paramhlp.c */
139 const NameValue setopt_nv_CURLPROTO[] = {
140 NV(CURLPROTO_ALL), /* combination */
145 NV(CURLPROTO_GOPHER),
161 NV(CURLPROTO_TELNET),
166 /* These options have non-zero default values. */
167 static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
168 NV1(CURLOPT_SSL_VERIFYPEER, 1),
169 NV1(CURLOPT_SSL_VERIFYHOST, 1),
170 NV1(CURLOPT_SSL_ENABLE_NPN, 1),
171 NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
172 NV1(CURLOPT_TCP_NODELAY, 1),
173 NV1(CURLOPT_PROXY_SSL_VERIFYPEER, 1),
174 NV1(CURLOPT_PROXY_SSL_VERIFYHOST, 1),
175 NV1(CURLOPT_SOCKS5_AUTH, 1),
179 /* Format and add code; jump to nomem on malloc error */
180 #define ADD(args) do { \
181 ret = easysrc_add args; \
185 #define ADDF(args) do { \
186 ret = easysrc_addf args; \
191 #define DECL0(s) ADD((&easysrc_decl, s))
192 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
194 #define DATA0(s) ADD((&easysrc_data, s))
195 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
196 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
197 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
199 #define CODE0(s) ADD((&easysrc_code, s))
200 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
201 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
202 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
204 #define CLEAN0(s) ADD((&easysrc_clean, s))
205 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
207 #define REM0(s) ADD((&easysrc_toohard, s))
208 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
209 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
211 /* Escape string to C string syntax. Return NULL if out of memory.
212 * Is this correct for those wacky EBCDIC guys? */
213 static char *c_escape(const char *str, size_t len)
219 if(len == CURL_ZERO_TERMINATED)
222 /* Check for possible overflow. */
223 if(len > (~(size_t) 0) / 4)
226 /* Allocate space based on worst-case */
227 escaped = malloc(4 * len + 1);
232 for(s = str; (c = *s) != '\0'; s++) {
253 else if(! isprint(c)) {
254 snprintf(e, 5, "\\%03o", (unsigned)c);
264 /* setopt wrapper for enum types */
265 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
266 const char *name, CURLoption tag,
267 const NameValue *nvlist, long lval)
269 CURLcode ret = CURLE_OK;
272 ret = curl_easy_setopt(curl, tag, lval);
276 if(config->libcurl && !skip && !ret) {
277 /* we only use this for real if --libcurl was used */
278 const NameValue *nv = NULL;
279 for(nv = nvlist; nv->name; nv++) {
280 if(nv->value == lval) break; /* found it */
283 /* If no definition was found, output an explicit value.
284 * This could happen if new values are defined and used
285 * but the NameValue list is not updated. */
286 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
289 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
297 /* setopt wrapper for flags */
298 CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
299 const char *name, CURLoption tag,
300 const NameValue *nvlist, long lval)
302 CURLcode ret = CURLE_OK;
305 ret = curl_easy_setopt(curl, tag, lval);
309 if(config->libcurl && !skip && !ret) {
310 /* we only use this for real if --libcurl was used */
311 char preamble[80]; /* should accommodate any symbol name */
312 long rest = lval; /* bits not handled yet */
313 const NameValue *nv = NULL;
314 snprintf(preamble, sizeof(preamble),
315 "curl_easy_setopt(hnd, %s, ", name);
316 for(nv = nvlist; nv->name; nv++) {
317 if((nv->value & ~ rest) == 0) {
318 /* all value flags contained in rest */
319 rest &= ~ nv->value; /* remove bits handled here */
320 CODE3("%s(long)%s%s",
321 preamble, nv->name, rest ? " |" : ");");
323 break; /* handled them all */
324 /* replace with all spaces for continuation line */
325 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
328 /* If any bits have no definition, output an explicit value.
329 * This could happen if new bits are defined and used
330 * but the NameValue list is not updated. */
332 CODE2("%s%ldL);", preamble, rest);
339 /* setopt wrapper for bitmasks */
340 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
341 const char *name, CURLoption tag,
342 const NameValueUnsigned *nvlist,
345 CURLcode ret = CURLE_OK;
348 ret = curl_easy_setopt(curl, tag, lval);
352 if(config->libcurl && !skip && !ret) {
353 /* we only use this for real if --libcurl was used */
355 unsigned long rest = (unsigned long)lval;
356 const NameValueUnsigned *nv = NULL;
357 snprintf(preamble, sizeof(preamble),
358 "curl_easy_setopt(hnd, %s, ", name);
359 for(nv = nvlist; nv->name; nv++) {
360 if((nv->value & ~ rest) == 0) {
361 /* all value flags contained in rest */
362 rest &= ~ nv->value; /* remove bits handled here */
363 CODE3("%s(long)%s%s",
364 preamble, nv->name, rest ? " |" : ");");
366 break; /* handled them all */
367 /* replace with all spaces for continuation line */
368 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
371 /* If any bits have no definition, output an explicit value.
372 * This could happen if new bits are defined and used
373 * but the NameValue list is not updated. */
375 CODE2("%s%luUL);", preamble, rest);
382 /* Generate code for a struct curl_slist. */
383 static CURLcode libcurl_generate_slist(struct curl_slist *slist, int *slistno)
385 CURLcode ret = CURLE_OK;
386 char *escaped = NULL;
388 /* May need several slist variables, so invent name */
389 *slistno = ++easysrc_slist_count;
391 DECL1("struct curl_slist *slist%d;", *slistno);
392 DATA1("slist%d = NULL;", *slistno);
393 CLEAN1("curl_slist_free_all(slist%d);", *slistno);
394 CLEAN1("slist%d = NULL;", *slistno);
395 for(; slist; slist = slist->next) {
396 Curl_safefree(escaped);
397 escaped = c_escape(slist->data, CURL_ZERO_TERMINATED);
399 return CURLE_OUT_OF_MEMORY;
400 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");",
401 *slistno, *slistno, escaped);
405 Curl_safefree(escaped);
409 /* Generate source code for a mime structure. */
410 static CURLcode libcurl_generate_mime(curl_mime *mime, int *mimeno)
412 CURLcode ret = CURLE_OK;
417 char *escaped = NULL;
421 /* May need several mime variables, so invent name */
422 *mimeno = ++easysrc_mime_count;
424 DECL1("curl_mime *mime%d;", *mimeno);
425 DATA1("mime%d = NULL;", *mimeno);
426 CODE1("mime%d = curl_mime_init(hnd);", *mimeno);
427 CLEAN1("curl_mime_free(mime%d);", *mimeno);
428 CLEAN1("mime%d = NULL;", *mimeno);
429 if(mime->firstpart) {
430 DECL1("curl_mimepart *part%d;", *mimeno);
431 for(part = mime->firstpart; part; part = part->nextpart) {
432 CODE2("part%d = curl_mime_addpart(mime%d);", *mimeno, *mimeno);
433 filename = part->filename;
436 Curl_safefree(escaped);
437 escaped = c_escape(part->data, CURL_ZERO_TERMINATED);
439 return CURLE_OUT_OF_MEMORY;
440 CODE2("curl_mime_filedata(part%d, \"%s\");", *mimeno, escaped);
442 CODE1("curl_mime_filename(part%d, NULL);", *mimeno);
444 /* Fast check to see if remote file name is base name. */
445 filename = part->data;
446 for(cp = filename; *cp; cp++)
447 if(*cp == '/' || *cp == '\\')
449 if(!part->filename || !strcmp(filename, part->filename))
452 filename = part->filename;
455 case MIMEKIND_CALLBACK:
456 /* Can only be reading stdin in the current context. */
457 CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\",
459 CODE0(" (curl_seek_callback) fseek, NULL, stdin);");
462 #ifdef CURL_DOES_CONVERSIONS
463 /* Data is stored in ASCII and we want in in the host character
464 code. Convert it back for output. */
465 data = malloc(part->datasize + 1);
467 ret = CURLE_OUT_OF_MEMORY;
470 memcpy(data, part->data, part->datasize + 1);
471 ret = convert_from_network(data, strlen(data));
480 /* Are there any nul byte in data? */
481 for(cp = data; *cp; cp++)
483 size = (cp == data + part->datasize)? (curl_off_t) -1: part->datasize;
484 Curl_safefree(escaped);
485 escaped = c_escape(data, (size_t) part->datasize);
486 #ifdef CURL_DOES_CONVERSIONS
490 return CURLE_OUT_OF_MEMORY;
492 CODE3("curl_mime_data(part%d, \"%s\", %" CURL_FORMAT_CURL_OFF_T ");",
493 *mimeno, escaped, size);
495 CODE2("curl_mime_data(part%d, \"%s\", CURL_ZERO_TERMINATED);",
498 case MIMEKIND_MULTIPART:
499 ret = libcurl_generate_mime(part->arg, &i);
502 CODE2("curl_mime_subparts(part%d, mime%d);", *mimeno, i);
503 CODE1("mime%d = NULL;", i); /* Avoid freeing in CLEAN sequence. */
506 /* Other cases not possible in this context. */
511 Curl_safefree(escaped);
512 escaped = c_escape(part->encoder->name, CURL_ZERO_TERMINATED);
514 return CURLE_OUT_OF_MEMORY;
515 CODE2("curl_mime_encoder(part%d, \"%s\");", *mimeno, escaped);
519 Curl_safefree(escaped);
520 escaped = c_escape(filename, CURL_ZERO_TERMINATED);
522 return CURLE_OUT_OF_MEMORY;
523 CODE2("curl_mime_filename(part%d, \"%s\");", *mimeno, escaped);
527 Curl_safefree(escaped);
528 escaped = c_escape(part->name, CURL_ZERO_TERMINATED);
530 return CURLE_OUT_OF_MEMORY;
531 CODE2("curl_mime_name(part%d, \"%s\");", *mimeno, escaped);
535 Curl_safefree(escaped);
536 escaped = c_escape(part->mimetype, CURL_ZERO_TERMINATED);
538 return CURLE_OUT_OF_MEMORY;
539 CODE2("curl_mime_type(part%d, \"%s\");", *mimeno, escaped);
542 if(part->userheaders) {
543 int ownership = part->flags & MIME_USERHEADERS_OWNER? 1: 0;
545 ret = libcurl_generate_slist(part->userheaders, &i);
548 CODE3("curl_mime_headers(part%d, slist%d, %d);",
549 *mimeno, i, ownership);
551 CODE1("slist%d = NULL;", i); /* Prevent freeing in CLEAN sequence. */
557 Curl_safefree(escaped);
561 /* setopt wrapper for CURLOPT_MIMEPOST */
562 CURLcode tool_setopt_mimepost(CURL *curl, struct GlobalConfig *config,
563 const char *name, CURLoption tag,
566 CURLcode ret = CURLE_OK;
568 ret = curl_easy_setopt(curl, tag, mimepost);
570 if(config->libcurl && mimepost && !ret) {
573 ret = libcurl_generate_mime(mimepost, &i);
576 CODE2("curl_easy_setopt(hnd, %s, mime%d);", name, i);
583 /* setopt wrapper for curl_slist options */
584 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
585 const char *name, CURLoption tag,
586 struct curl_slist *list)
588 CURLcode ret = CURLE_OK;
590 ret = curl_easy_setopt(curl, tag, list);
592 if(config->libcurl && list && !ret) {
595 ret = libcurl_generate_slist(list, &i);
597 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
604 /* generic setopt wrapper for all other options.
605 * Some type information is encoded in the tag value. */
606 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *config,
607 const char *name, CURLoption tag, ...)
611 const char *value = NULL;
615 char *escaped = NULL;
616 CURLcode ret = CURLE_OK;
620 if(tag < CURLOPTTYPE_OBJECTPOINT) {
621 /* Value is expected to be a long */
622 long lval = va_arg(arg, long);
624 const NameValue *nv = NULL;
625 for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
626 if(!strcmp(name, nv->name)) {
628 break; /* found it */
632 snprintf(buf, sizeof(buf), "%ldL", lval);
634 ret = curl_easy_setopt(curl, tag, lval);
638 else if(tag < CURLOPTTYPE_OFF_T) {
639 /* Value is some sort of object pointer */
640 void *pval = va_arg(arg, void *);
642 /* function pointers are never printable */
643 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
645 value = "functionpointer";
652 else if(pval && str) {
653 value = (char *)pval;
657 value = "objectpointer";
663 ret = curl_easy_setopt(curl, tag, pval);
667 /* Value is expected to be curl_off_t */
668 curl_off_t oval = va_arg(arg, curl_off_t);
669 snprintf(buf, sizeof(buf),
670 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
672 ret = curl_easy_setopt(curl, tag, oval);
680 if(config->libcurl && !skip && !ret) {
681 /* we only use this for real if --libcurl was used */
684 REM2("%s set to a %s", name, value);
687 escaped = c_escape(value, CURL_ZERO_TERMINATED);
689 ret = CURLE_OUT_OF_MEMORY;
692 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
695 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
700 Curl_safefree(escaped);
704 #endif /* CURL_DISABLE_LIBCURL_OPTION */