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 NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
111 NV(CURLSSLOPT_ALLOW_BEAST),
112 NV(CURLSSLOPT_NO_REVOKE),
116 const NameValue setopt_nv_CURL_NETRC[] = {
117 NV(CURL_NETRC_IGNORED),
118 NV(CURL_NETRC_OPTIONAL),
119 NV(CURL_NETRC_REQUIRED),
123 /* These mappings essentially triplicated - see
124 * tool_libinfo.c and tool_paramhlp.c */
125 const NameValue setopt_nv_CURLPROTO[] = {
126 NV(CURLPROTO_ALL), /* combination */
131 NV(CURLPROTO_GOPHER),
147 NV(CURLPROTO_TELNET),
152 /* These options have non-zero default values. */
153 static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
154 NV1(CURLOPT_SSL_VERIFYPEER, 1),
155 NV1(CURLOPT_SSL_VERIFYHOST, 1),
156 NV1(CURLOPT_SSL_ENABLE_NPN, 1),
157 NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
161 /* Format and add code; jump to nomem on malloc error */
162 #define ADD(args) do { \
163 ret = easysrc_add args; \
167 #define ADDF(args) do { \
168 ret = easysrc_addf args; \
173 #define DECL0(s) ADD((&easysrc_decl, s))
174 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
176 #define DATA0(s) ADD((&easysrc_data, s))
177 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
178 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
179 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
181 #define CODE0(s) ADD((&easysrc_code, s))
182 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
183 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
184 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
186 #define CLEAN0(s) ADD((&easysrc_clean, s))
187 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
189 #define REM0(s) ADD((&easysrc_toohard, s))
190 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
191 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
193 /* Escape string to C string syntax. Return NULL if out of memory.
194 * Is this correct for those wacky EBCDIC guys? */
195 static char *c_escape(const char *str)
201 /* Allocate space based on worst-case */
203 escaped = malloc(4 * len + 1);
208 for(s=str; (c=*s) != '\0'; s++) {
229 else if(! isprint(c)) {
230 snprintf(e, 4, "\\%03o", c);
240 /* setopt wrapper for enum types */
241 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
242 const char *name, CURLoption tag,
243 const NameValue *nvlist, long lval)
245 CURLcode ret = CURLE_OK;
248 ret = curl_easy_setopt(curl, tag, lval);
252 if(config->libcurl && !skip && !ret) {
253 /* we only use this for real if --libcurl was used */
254 const NameValue *nv = NULL;
255 for(nv=nvlist; nv->name; nv++) {
256 if(nv->value == lval) break; /* found it */
259 /* If no definition was found, output an explicit value.
260 * This could happen if new values are defined and used
261 * but the NameValue list is not updated. */
262 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
265 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
273 /* setopt wrapper for flags */
274 CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
275 const char *name, CURLoption tag,
276 const NameValue *nvlist, long lval)
278 CURLcode ret = CURLE_OK;
281 ret = curl_easy_setopt(curl, tag, lval);
285 if(config->libcurl && !skip && !ret) {
286 /* we only use this for real if --libcurl was used */
287 char preamble[80]; /* should accommodate any symbol name */
288 long rest = lval; /* bits not handled yet */
289 const NameValue *nv = NULL;
290 snprintf(preamble, sizeof(preamble),
291 "curl_easy_setopt(hnd, %s, ", name);
292 for(nv=nvlist; nv->name; nv++) {
293 if((nv->value & ~ rest) == 0) {
294 /* all value flags contained in rest */
295 rest &= ~ nv->value; /* remove bits handled here */
296 CODE3("%s(long)%s%s",
297 preamble, nv->name, rest ? " |" : ");");
299 break; /* handled them all */
300 /* replace with all spaces for continuation line */
301 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
304 /* If any bits have no definition, output an explicit value.
305 * This could happen if new bits are defined and used
306 * but the NameValue list is not updated. */
308 CODE2("%s%ldL);", preamble, rest);
315 /* setopt wrapper for bitmasks */
316 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
317 const char *name, CURLoption tag,
318 const NameValueUnsigned *nvlist,
321 CURLcode ret = CURLE_OK;
324 ret = curl_easy_setopt(curl, tag, lval);
328 if(config->libcurl && !skip && !ret) {
329 /* we only use this for real if --libcurl was used */
331 unsigned long rest = (unsigned long)lval;
332 const NameValueUnsigned *nv = NULL;
333 snprintf(preamble, sizeof(preamble),
334 "curl_easy_setopt(hnd, %s, ", name);
335 for(nv=nvlist; nv->name; nv++) {
336 if((nv->value & ~ rest) == 0) {
337 /* all value flags contained in rest */
338 rest &= ~ nv->value; /* remove bits handled here */
339 CODE3("%s(long)%s%s",
340 preamble, nv->name, rest ? " |" : ");");
342 break; /* handled them all */
343 /* replace with all spaces for continuation line */
344 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
347 /* If any bits have no definition, output an explicit value.
348 * This could happen if new bits are defined and used
349 * but the NameValue list is not updated. */
351 CODE2("%s%luUL);", preamble, rest);
358 /* setopt wrapper for CURLOPT_HTTPPOST */
359 CURLcode tool_setopt_httppost(CURL *curl, struct GlobalConfig *config,
360 const char *name, CURLoption tag,
361 struct curl_httppost *post)
363 CURLcode ret = CURLE_OK;
364 char *escaped = NULL;
367 ret = curl_easy_setopt(curl, tag, post);
371 if(config->libcurl && !skip && !ret) {
372 struct curl_httppost *pp, *p;
374 /* May use several httppost lists, if multiple POST actions */
375 i = ++ easysrc_form_count;
376 DECL1("struct curl_httppost *post%d;", i);
377 DATA1("post%d = NULL;", i);
378 CLEAN1("curl_formfree(post%d);", i);
379 CLEAN1("post%d = NULL;", i);
381 DECL0("struct curl_httppost *postend;");
382 DATA0("postend = NULL;");
383 for(p=post; p; p=p->next) {
384 DATA1("curl_formadd(&post%d, &postend,", i);
385 DATA1(" CURLFORM_COPYNAME, \"%s\",", p->name);
386 for(pp=p; pp; pp=pp->more) {
387 /* May be several files uploaded for one name;
388 * these are linked through the 'more' pointer */
389 Curl_safefree(escaped);
390 escaped = c_escape(pp->contents);
392 ret = CURLE_OUT_OF_MEMORY;
395 if(pp->flags & HTTPPOST_FILENAME) {
396 /* file upload as for -F @filename */
397 DATA1(" CURLFORM_FILE, \"%s\",", escaped);
399 else if(pp->flags & HTTPPOST_READFILE) {
400 /* content from file as for -F <filename */
401 DATA1(" CURLFORM_FILECONTENT, \"%s\",", escaped);
404 DATA1(" CURLFORM_COPYCONTENTS, \"%s\",", escaped);
405 if(pp->showfilename) {
406 Curl_safefree(escaped);
407 escaped = c_escape(pp->showfilename);
409 ret = CURLE_OUT_OF_MEMORY;
412 DATA1(" CURLFORM_FILENAME, \"%s\",", escaped);
414 if(pp->contenttype) {
415 Curl_safefree(escaped);
416 escaped = c_escape(pp->contenttype);
418 ret = CURLE_OUT_OF_MEMORY;
421 DATA1(" CURLFORM_CONTENTTYPE, \"%s\",", escaped);
424 DATA0(" CURLFORM_END);");
426 CODE2("curl_easy_setopt(hnd, %s, post%d);", name, i);
430 Curl_safefree(escaped);
434 /* setopt wrapper for curl_slist options */
435 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
436 const char *name, CURLoption tag,
437 struct curl_slist *list)
439 CURLcode ret = CURLE_OK;
440 char *escaped = NULL;
443 ret = curl_easy_setopt(curl, tag, list);
447 if(config->libcurl && !skip && !ret) {
448 struct curl_slist *s;
450 /* May need several slist variables, so invent name */
451 i = ++ easysrc_slist_count;
452 DECL1("struct curl_slist *slist%d;", i);
453 DATA1("slist%d = NULL;", i);
454 CLEAN1("curl_slist_free_all(slist%d);", i);
455 CLEAN1("slist%d = NULL;", i);
456 for(s=list; s; s=s->next) {
457 Curl_safefree(escaped);
458 escaped = c_escape(s->data);
460 ret = CURLE_OUT_OF_MEMORY;
463 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
465 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
469 Curl_safefree(escaped);
473 /* generic setopt wrapper for all other options.
474 * Some type information is encoded in the tag value. */
475 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *config,
476 const char *name, CURLoption tag, ...)
480 const char *value = NULL;
484 char *escaped = NULL;
485 CURLcode ret = CURLE_OK;
489 if(tag < CURLOPTTYPE_OBJECTPOINT) {
490 /* Value is expected to be a long */
491 long lval = va_arg(arg, long);
493 const NameValue *nv = NULL;
494 for(nv=setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
495 if(!strcmp(name, nv->name)) {
497 break; /* found it */
501 snprintf(buf, sizeof(buf), "%ldL", lval);
503 ret = curl_easy_setopt(curl, tag, lval);
507 else if(tag < CURLOPTTYPE_OFF_T) {
508 /* Value is some sort of object pointer */
509 void *pval = va_arg(arg, void *);
511 /* function pointers are never printable */
512 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
514 value = "functionpointer";
521 else if(pval && str) {
522 value = (char *)pval;
526 value = "objectpointer";
532 ret = curl_easy_setopt(curl, tag, pval);
536 /* Value is expected to be curl_off_t */
537 curl_off_t oval = va_arg(arg, curl_off_t);
538 snprintf(buf, sizeof(buf),
539 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
541 ret = curl_easy_setopt(curl, tag, oval);
549 if(config->libcurl && !skip && !ret) {
550 /* we only use this for real if --libcurl was used */
553 REM2("%s set to a %s", name, value);
556 escaped = c_escape(value);
558 ret = CURLE_OUT_OF_MEMORY;
561 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
564 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
569 Curl_safefree(escaped);
573 #endif /* CURL_DISABLE_LIBCURL_OPTION */