1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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"
34 #include "memdebug.h" /* keep this as LAST include */
36 /* Lookup tables for converting setopt values back to symbols */
37 /* For enums, values may be in any order. */
38 /* For bit masks, put combinations first, then single bits, */
39 /* and finally any "NONE" value. */
42 #define NV1(e, v) {#e, (v)}
43 #define NVEND {NULL, 0} /* sentinel to mark end of list */
45 const NameValue setopt_nv_CURLPROXY[] = {
47 NV(CURLPROXY_HTTP_1_0),
50 NV(CURLPROXY_SOCKS4A),
51 NV(CURLPROXY_SOCKS5_HOSTNAME),
55 const NameValueUnsigned setopt_nv_CURLAUTH[] = {
56 NV(CURLAUTH_ANY), /* combination */
57 NV(CURLAUTH_ANYSAFE), /* combination */
60 NV(CURLAUTH_GSSNEGOTIATE),
62 NV(CURLAUTH_DIGEST_IE),
69 const NameValue setopt_nv_CURL_HTTP_VERSION[] = {
70 NV(CURL_HTTP_VERSION_NONE),
71 NV(CURL_HTTP_VERSION_1_0),
72 NV(CURL_HTTP_VERSION_1_1),
73 NV(CURL_HTTP_VERSION_2_0),
74 NV(CURL_HTTP_VERSION_2TLS),
78 const NameValue setopt_nv_CURL_SSLVERSION[] = {
79 NV(CURL_SSLVERSION_DEFAULT),
80 NV(CURL_SSLVERSION_TLSv1),
81 NV(CURL_SSLVERSION_SSLv2),
82 NV(CURL_SSLVERSION_SSLv3),
83 NV(CURL_SSLVERSION_TLSv1_0),
84 NV(CURL_SSLVERSION_TLSv1_1),
85 NV(CURL_SSLVERSION_TLSv1_2),
89 const NameValue setopt_nv_CURL_TIMECOND[] = {
90 NV(CURL_TIMECOND_IFMODSINCE),
91 NV(CURL_TIMECOND_IFUNMODSINCE),
92 NV(CURL_TIMECOND_LASTMOD),
93 NV(CURL_TIMECOND_NONE),
97 const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
98 NV(CURLFTPSSL_CCC_NONE),
99 NV(CURLFTPSSL_CCC_PASSIVE),
100 NV(CURLFTPSSL_CCC_ACTIVE),
104 const NameValue setopt_nv_CURLUSESSL[] = {
107 NV(CURLUSESSL_CONTROL),
112 const NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
113 NV(CURLSSLOPT_ALLOW_BEAST),
114 NV(CURLSSLOPT_NO_REVOKE),
118 const NameValue setopt_nv_CURL_NETRC[] = {
119 NV(CURL_NETRC_IGNORED),
120 NV(CURL_NETRC_OPTIONAL),
121 NV(CURL_NETRC_REQUIRED),
125 /* These mappings essentially triplicated - see
126 * tool_libinfo.c and tool_paramhlp.c */
127 const NameValue setopt_nv_CURLPROTO[] = {
128 NV(CURLPROTO_ALL), /* combination */
133 NV(CURLPROTO_GOPHER),
149 NV(CURLPROTO_TELNET),
154 /* These options have non-zero default values. */
155 static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
156 NV1(CURLOPT_SSL_VERIFYPEER, 1),
157 NV1(CURLOPT_SSL_VERIFYHOST, 1),
158 NV1(CURLOPT_SSL_ENABLE_NPN, 1),
159 NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
160 NV1(CURLOPT_TCP_NODELAY, 1),
164 /* Format and add code; jump to nomem on malloc error */
165 #define ADD(args) do { \
166 ret = easysrc_add args; \
170 #define ADDF(args) do { \
171 ret = easysrc_addf args; \
176 #define DECL0(s) ADD((&easysrc_decl, s))
177 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
179 #define DATA0(s) ADD((&easysrc_data, s))
180 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
181 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
182 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
184 #define CODE0(s) ADD((&easysrc_code, s))
185 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
186 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
187 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
189 #define CLEAN0(s) ADD((&easysrc_clean, s))
190 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
192 #define REM0(s) ADD((&easysrc_toohard, s))
193 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
194 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
196 /* Escape string to C string syntax. Return NULL if out of memory.
197 * Is this correct for those wacky EBCDIC guys? */
198 static char *c_escape(const char *str)
204 /* Allocate space based on worst-case */
206 escaped = malloc(4 * len + 1);
211 for(s=str; (c=*s) != '\0'; s++) {
232 else if(! isprint(c)) {
233 snprintf(e, 5, "\\%03o", (unsigned)c);
243 /* setopt wrapper for enum types */
244 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
245 const char *name, CURLoption tag,
246 const NameValue *nvlist, long lval)
248 CURLcode ret = CURLE_OK;
251 ret = curl_easy_setopt(curl, tag, lval);
255 if(config->libcurl && !skip && !ret) {
256 /* we only use this for real if --libcurl was used */
257 const NameValue *nv = NULL;
258 for(nv=nvlist; nv->name; nv++) {
259 if(nv->value == lval) break; /* found it */
262 /* If no definition was found, output an explicit value.
263 * This could happen if new values are defined and used
264 * but the NameValue list is not updated. */
265 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
268 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
276 /* setopt wrapper for flags */
277 CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
278 const char *name, CURLoption tag,
279 const NameValue *nvlist, long lval)
281 CURLcode ret = CURLE_OK;
284 ret = curl_easy_setopt(curl, tag, lval);
288 if(config->libcurl && !skip && !ret) {
289 /* we only use this for real if --libcurl was used */
290 char preamble[80]; /* should accommodate any symbol name */
291 long rest = lval; /* bits not handled yet */
292 const NameValue *nv = NULL;
293 snprintf(preamble, sizeof(preamble),
294 "curl_easy_setopt(hnd, %s, ", name);
295 for(nv=nvlist; nv->name; nv++) {
296 if((nv->value & ~ rest) == 0) {
297 /* all value flags contained in rest */
298 rest &= ~ nv->value; /* remove bits handled here */
299 CODE3("%s(long)%s%s",
300 preamble, nv->name, rest ? " |" : ");");
302 break; /* handled them all */
303 /* replace with all spaces for continuation line */
304 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
307 /* If any bits have no definition, output an explicit value.
308 * This could happen if new bits are defined and used
309 * but the NameValue list is not updated. */
311 CODE2("%s%ldL);", preamble, rest);
318 /* setopt wrapper for bitmasks */
319 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
320 const char *name, CURLoption tag,
321 const NameValueUnsigned *nvlist,
324 CURLcode ret = CURLE_OK;
327 ret = curl_easy_setopt(curl, tag, lval);
331 if(config->libcurl && !skip && !ret) {
332 /* we only use this for real if --libcurl was used */
334 unsigned long rest = (unsigned long)lval;
335 const NameValueUnsigned *nv = NULL;
336 snprintf(preamble, sizeof(preamble),
337 "curl_easy_setopt(hnd, %s, ", name);
338 for(nv=nvlist; nv->name; nv++) {
339 if((nv->value & ~ rest) == 0) {
340 /* all value flags contained in rest */
341 rest &= ~ nv->value; /* remove bits handled here */
342 CODE3("%s(long)%s%s",
343 preamble, nv->name, rest ? " |" : ");");
345 break; /* handled them all */
346 /* replace with all spaces for continuation line */
347 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
350 /* If any bits have no definition, output an explicit value.
351 * This could happen if new bits are defined and used
352 * but the NameValue list is not updated. */
354 CODE2("%s%luUL);", preamble, rest);
361 /* setopt wrapper for CURLOPT_HTTPPOST */
362 CURLcode tool_setopt_httppost(CURL *curl, struct GlobalConfig *config,
363 const char *name, CURLoption tag,
364 struct curl_httppost *post)
366 CURLcode ret = CURLE_OK;
367 char *escaped = NULL;
370 ret = curl_easy_setopt(curl, tag, post);
374 if(config->libcurl && !skip && !ret) {
375 struct curl_httppost *pp, *p;
377 /* May use several httppost lists, if multiple POST actions */
378 i = ++ easysrc_form_count;
379 DECL1("struct curl_httppost *post%d;", i);
380 DATA1("post%d = NULL;", i);
381 CLEAN1("curl_formfree(post%d);", i);
382 CLEAN1("post%d = NULL;", i);
384 DECL0("struct curl_httppost *postend;");
385 DATA0("postend = NULL;");
386 for(p=post; p; p=p->next) {
387 DATA1("curl_formadd(&post%d, &postend,", i);
388 DATA1(" CURLFORM_COPYNAME, \"%s\",", p->name);
389 for(pp=p; pp; pp=pp->more) {
390 /* May be several files uploaded for one name;
391 * these are linked through the 'more' pointer */
392 Curl_safefree(escaped);
393 escaped = c_escape(pp->contents);
395 ret = CURLE_OUT_OF_MEMORY;
398 if(pp->flags & CURL_HTTPPOST_FILENAME) {
399 /* file upload as for -F @filename */
400 DATA1(" CURLFORM_FILE, \"%s\",", escaped);
402 else if(pp->flags & CURL_HTTPPOST_READFILE) {
403 /* content from file as for -F <filename */
404 DATA1(" CURLFORM_FILECONTENT, \"%s\",", escaped);
407 DATA1(" CURLFORM_COPYCONTENTS, \"%s\",", escaped);
408 if(pp->showfilename) {
409 Curl_safefree(escaped);
410 escaped = c_escape(pp->showfilename);
412 ret = CURLE_OUT_OF_MEMORY;
415 DATA1(" CURLFORM_FILENAME, \"%s\",", escaped);
417 if(pp->contenttype) {
418 Curl_safefree(escaped);
419 escaped = c_escape(pp->contenttype);
421 ret = CURLE_OUT_OF_MEMORY;
424 DATA1(" CURLFORM_CONTENTTYPE, \"%s\",", escaped);
427 DATA0(" CURLFORM_END);");
429 CODE2("curl_easy_setopt(hnd, %s, post%d);", name, i);
433 Curl_safefree(escaped);
437 /* setopt wrapper for curl_slist options */
438 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
439 const char *name, CURLoption tag,
440 struct curl_slist *list)
442 CURLcode ret = CURLE_OK;
443 char *escaped = NULL;
446 ret = curl_easy_setopt(curl, tag, list);
450 if(config->libcurl && !skip && !ret) {
451 struct curl_slist *s;
453 /* May need several slist variables, so invent name */
454 i = ++ easysrc_slist_count;
455 DECL1("struct curl_slist *slist%d;", i);
456 DATA1("slist%d = NULL;", i);
457 CLEAN1("curl_slist_free_all(slist%d);", i);
458 CLEAN1("slist%d = NULL;", i);
459 for(s=list; s; s=s->next) {
460 Curl_safefree(escaped);
461 escaped = c_escape(s->data);
463 ret = CURLE_OUT_OF_MEMORY;
466 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
468 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
472 Curl_safefree(escaped);
476 /* generic setopt wrapper for all other options.
477 * Some type information is encoded in the tag value. */
478 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *config,
479 const char *name, CURLoption tag, ...)
483 const char *value = NULL;
487 char *escaped = NULL;
488 CURLcode ret = CURLE_OK;
492 if(tag < CURLOPTTYPE_OBJECTPOINT) {
493 /* Value is expected to be a long */
494 long lval = va_arg(arg, long);
496 const NameValue *nv = NULL;
497 for(nv=setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
498 if(!strcmp(name, nv->name)) {
500 break; /* found it */
504 snprintf(buf, sizeof(buf), "%ldL", lval);
506 ret = curl_easy_setopt(curl, tag, lval);
510 else if(tag < CURLOPTTYPE_OFF_T) {
511 /* Value is some sort of object pointer */
512 void *pval = va_arg(arg, void *);
514 /* function pointers are never printable */
515 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
517 value = "functionpointer";
524 else if(pval && str) {
525 value = (char *)pval;
529 value = "objectpointer";
535 ret = curl_easy_setopt(curl, tag, pval);
539 /* Value is expected to be curl_off_t */
540 curl_off_t oval = va_arg(arg, curl_off_t);
541 snprintf(buf, sizeof(buf),
542 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
544 ret = curl_easy_setopt(curl, tag, oval);
552 if(config->libcurl && !skip && !ret) {
553 /* we only use this for real if --libcurl was used */
556 REM2("%s set to a %s", name, value);
559 escaped = c_escape(value);
561 ret = CURLE_OUT_OF_MEMORY;
564 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
567 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
572 Curl_safefree(escaped);
576 #endif /* CURL_DISABLE_LIBCURL_OPTION */