1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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),
76 const NameValue setopt_nv_CURL_SSLVERSION[] = {
77 NV(CURL_SSLVERSION_DEFAULT),
78 NV(CURL_SSLVERSION_TLSv1),
79 NV(CURL_SSLVERSION_SSLv2),
80 NV(CURL_SSLVERSION_SSLv3),
81 NV(CURL_SSLVERSION_TLSv1_0),
82 NV(CURL_SSLVERSION_TLSv1_1),
83 NV(CURL_SSLVERSION_TLSv1_2),
87 const NameValue setopt_nv_CURL_TIMECOND[] = {
88 NV(CURL_TIMECOND_IFMODSINCE),
89 NV(CURL_TIMECOND_IFUNMODSINCE),
90 NV(CURL_TIMECOND_LASTMOD),
91 NV(CURL_TIMECOND_NONE),
95 const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
96 NV(CURLFTPSSL_CCC_NONE),
97 NV(CURLFTPSSL_CCC_PASSIVE),
98 NV(CURLFTPSSL_CCC_ACTIVE),
102 const NameValue setopt_nv_CURLUSESSL[] = {
105 NV(CURLUSESSL_CONTROL),
110 const NameValue setopt_nv_CURL_NETRC[] = {
111 NV(CURL_NETRC_IGNORED),
112 NV(CURL_NETRC_OPTIONAL),
113 NV(CURL_NETRC_REQUIRED),
117 /* These mappings essentially triplicated - see
118 * tool_libinfo.c and tool_paramhlp.c */
119 const NameValue setopt_nv_CURLPROTO[] = {
120 NV(CURLPROTO_ALL), /* combination */
125 NV(CURLPROTO_GOPHER),
141 NV(CURLPROTO_TELNET),
146 /* These options have non-zero default values. */
147 static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
148 NV1(CURLOPT_SSL_VERIFYPEER, 1),
149 NV1(CURLOPT_SSL_VERIFYHOST, 1),
150 NV1(CURLOPT_SSL_ENABLE_NPN, 1),
151 NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
155 /* Format and add code; jump to nomem on malloc error */
156 #define ADD(args) do { \
157 ret = easysrc_add args; \
161 #define ADDF(args) do { \
162 ret = easysrc_addf args; \
167 #define DECL0(s) ADD((&easysrc_decl, s))
168 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
170 #define DATA0(s) ADD((&easysrc_data, s))
171 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
172 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
173 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
175 #define CODE0(s) ADD((&easysrc_code, s))
176 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
177 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
178 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
180 #define CLEAN0(s) ADD((&easysrc_clean, s))
181 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
183 #define REM0(s) ADD((&easysrc_toohard, s))
184 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
185 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
187 /* Escape string to C string syntax. Return NULL if out of memory.
188 * Is this correct for those wacky EBCDIC guys? */
189 static char *c_escape(const char *str)
195 /* Allocate space based on worst-case */
197 escaped = malloc(4 * len + 1);
202 for(s=str; (c=*s) != '\0'; s++) {
223 else if(! isprint(c)) {
224 snprintf(e, 4, "\\%03o", c);
234 /* setopt wrapper for enum types */
235 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
236 const char *name, CURLoption tag,
237 const NameValue *nvlist, long lval)
239 CURLcode ret = CURLE_OK;
242 ret = curl_easy_setopt(curl, tag, lval);
246 if(config->libcurl && !skip && !ret) {
247 /* we only use this for real if --libcurl was used */
248 const NameValue *nv = NULL;
249 for(nv=nvlist; nv->name; nv++) {
250 if(nv->value == lval) break; /* found it */
253 /* If no definition was found, output an explicit value.
254 * This could happen if new values are defined and used
255 * but the NameValue list is not updated. */
256 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
259 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
267 /* setopt wrapper for flags */
268 CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
269 const char *name, CURLoption tag,
270 const NameValue *nvlist, long lval)
272 CURLcode ret = CURLE_OK;
275 ret = curl_easy_setopt(curl, tag, lval);
279 if(config->libcurl && !skip && !ret) {
280 /* we only use this for real if --libcurl was used */
281 char preamble[80]; /* should accommodate any symbol name */
282 long rest = lval; /* bits not handled yet */
283 const NameValue *nv = NULL;
284 snprintf(preamble, sizeof(preamble),
285 "curl_easy_setopt(hnd, %s, ", name);
286 for(nv=nvlist; nv->name; nv++) {
287 if((nv->value & ~ rest) == 0) {
288 /* all value flags contained in rest */
289 rest &= ~ nv->value; /* remove bits handled here */
290 CODE3("%s(long)%s%s",
291 preamble, nv->name, rest ? " |" : ");");
293 break; /* handled them all */
294 /* replace with all spaces for continuation line */
295 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
298 /* If any bits have no definition, output an explicit value.
299 * This could happen if new bits are defined and used
300 * but the NameValue list is not updated. */
302 CODE2("%s%ldL);", preamble, rest);
309 /* setopt wrapper for bitmasks */
310 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
311 const char *name, CURLoption tag,
312 const NameValueUnsigned *nvlist,
315 CURLcode ret = CURLE_OK;
318 ret = curl_easy_setopt(curl, tag, lval);
322 if(config->libcurl && !skip && !ret) {
323 /* we only use this for real if --libcurl was used */
325 unsigned long rest = (unsigned long)lval;
326 const NameValueUnsigned *nv = NULL;
327 snprintf(preamble, sizeof(preamble),
328 "curl_easy_setopt(hnd, %s, ", name);
329 for(nv=nvlist; nv->name; nv++) {
330 if((nv->value & ~ rest) == 0) {
331 /* all value flags contained in rest */
332 rest &= ~ nv->value; /* remove bits handled here */
333 CODE3("%s(long)%s%s",
334 preamble, nv->name, rest ? " |" : ");");
336 break; /* handled them all */
337 /* replace with all spaces for continuation line */
338 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
341 /* If any bits have no definition, output an explicit value.
342 * This could happen if new bits are defined and used
343 * but the NameValue list is not updated. */
345 CODE2("%s%luUL);", preamble, rest);
352 /* setopt wrapper for CURLOPT_HTTPPOST */
353 CURLcode tool_setopt_httppost(CURL *curl, struct GlobalConfig *config,
354 const char *name, CURLoption tag,
355 struct curl_httppost *post)
357 CURLcode ret = CURLE_OK;
358 char *escaped = NULL;
361 ret = curl_easy_setopt(curl, tag, post);
365 if(config->libcurl && !skip && !ret) {
366 struct curl_httppost *pp, *p;
368 /* May use several httppost lists, if multiple POST actions */
369 i = ++ easysrc_form_count;
370 DECL1("struct curl_httppost *post%d;", i);
371 DATA1("post%d = NULL;", i);
372 CLEAN1("curl_formfree(post%d);", i);
373 CLEAN1("post%d = NULL;", i);
375 DECL0("struct curl_httppost *postend;");
376 DATA0("postend = NULL;");
377 for(p=post; p; p=p->next) {
378 DATA1("curl_formadd(&post%d, &postend,", i);
379 DATA1(" CURLFORM_COPYNAME, \"%s\",", p->name);
380 for(pp=p; pp; pp=pp->more) {
381 /* May be several files uploaded for one name;
382 * these are linked through the 'more' pointer */
383 Curl_safefree(escaped);
384 escaped = c_escape(pp->contents);
386 ret = CURLE_OUT_OF_MEMORY;
389 if(pp->flags & HTTPPOST_FILENAME) {
390 /* file upload as for -F @filename */
391 DATA1(" CURLFORM_FILE, \"%s\",", escaped);
393 else if(pp->flags & HTTPPOST_READFILE) {
394 /* content from file as for -F <filename */
395 DATA1(" CURLFORM_FILECONTENT, \"%s\",", escaped);
398 DATA1(" CURLFORM_COPYCONTENTS, \"%s\",", escaped);
399 if(pp->showfilename) {
400 Curl_safefree(escaped);
401 escaped = c_escape(pp->showfilename);
403 ret = CURLE_OUT_OF_MEMORY;
406 DATA1(" CURLFORM_FILENAME, \"%s\",", escaped);
408 if(pp->contenttype) {
409 Curl_safefree(escaped);
410 escaped = c_escape(pp->contenttype);
412 ret = CURLE_OUT_OF_MEMORY;
415 DATA1(" CURLFORM_CONTENTTYPE, \"%s\",", escaped);
418 DATA0(" CURLFORM_END);");
420 CODE2("curl_easy_setopt(hnd, %s, post%d);", name, i);
424 Curl_safefree(escaped);
428 /* setopt wrapper for curl_slist options */
429 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
430 const char *name, CURLoption tag,
431 struct curl_slist *list)
433 CURLcode ret = CURLE_OK;
434 char *escaped = NULL;
437 ret = curl_easy_setopt(curl, tag, list);
441 if(config->libcurl && !skip && !ret) {
442 struct curl_slist *s;
444 /* May need several slist variables, so invent name */
445 i = ++ easysrc_slist_count;
446 DECL1("struct curl_slist *slist%d;", i);
447 DATA1("slist%d = NULL;", i);
448 CLEAN1("curl_slist_free_all(slist%d);", i);
449 CLEAN1("slist%d = NULL;", i);
450 for(s=list; s; s=s->next) {
451 Curl_safefree(escaped);
452 escaped = c_escape(s->data);
454 ret = CURLE_OUT_OF_MEMORY;
457 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
459 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
463 Curl_safefree(escaped);
467 /* generic setopt wrapper for all other options.
468 * Some type information is encoded in the tag value. */
469 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *config,
470 const char *name, CURLoption tag, ...)
474 const char *value = NULL;
478 char *escaped = NULL;
479 CURLcode ret = CURLE_OK;
483 if(tag < CURLOPTTYPE_OBJECTPOINT) {
484 /* Value is expected to be a long */
485 long lval = va_arg(arg, long);
487 const NameValue *nv = NULL;
488 for(nv=setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
489 if(!strcmp(name, nv->name)) {
491 break; /* found it */
495 snprintf(buf, sizeof(buf), "%ldL", lval);
497 ret = curl_easy_setopt(curl, tag, lval);
501 else if(tag < CURLOPTTYPE_OFF_T) {
502 /* Value is some sort of object pointer */
503 void *pval = va_arg(arg, void *);
505 /* function pointers are never printable */
506 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
508 value = "functionpointer";
515 else if(pval && str) {
516 value = (char *)pval;
520 value = "objectpointer";
526 ret = curl_easy_setopt(curl, tag, pval);
530 /* Value is expected to be curl_off_t */
531 curl_off_t oval = va_arg(arg, curl_off_t);
532 snprintf(buf, sizeof(buf),
533 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
535 ret = curl_easy_setopt(curl, tag, oval);
543 if(config->libcurl && !skip && !ret) {
544 /* we only use this for real if --libcurl was used */
547 REM2("%s set to a %s", name, value);
550 escaped = c_escape(value);
552 ret = CURLE_OUT_OF_MEMORY;
555 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
558 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
563 Curl_safefree(escaped);
567 #endif /* CURL_DISABLE_LIBCURL_OPTION */