1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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),
139 NV(CURLPROTO_TELNET),
144 /* These options have non-zero default values. */
145 static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
146 NV1(CURLOPT_SSL_VERIFYPEER, 1),
147 NV1(CURLOPT_SSL_VERIFYHOST, 1),
148 NV1(CURLOPT_SSL_ENABLE_NPN, 1),
149 NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
153 /* Format and add code; jump to nomem on malloc error */
154 #define ADD(args) do { \
155 ret = easysrc_add args; \
159 #define ADDF(args) do { \
160 ret = easysrc_addf args; \
165 #define DECL0(s) ADD((&easysrc_decl, s))
166 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
168 #define DATA0(s) ADD((&easysrc_data, s))
169 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
170 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
171 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
173 #define CODE0(s) ADD((&easysrc_code, s))
174 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
175 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
176 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
178 #define CLEAN0(s) ADD((&easysrc_clean, s))
179 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
181 #define REM0(s) ADD((&easysrc_toohard, s))
182 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
183 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
185 /* Escape string to C string syntax. Return NULL if out of memory.
186 * Is this correct for those wacky EBCDIC guys? */
187 static char *c_escape(const char *str)
193 /* Allocate space based on worst-case */
195 escaped = malloc(4 * len + 1);
200 for(s=str; (c=*s) != '\0'; s++) {
221 else if(! isprint(c)) {
222 snprintf(e, 4, "\\%03o", c);
232 /* setopt wrapper for enum types */
233 CURLcode tool_setopt_enum(CURL *curl, struct Configurable *config,
234 const char *name, CURLoption tag,
235 const NameValue *nvlist, long lval)
237 CURLcode ret = CURLE_OK;
240 ret = curl_easy_setopt(curl, tag, lval);
244 if(config->libcurl && !skip && !ret) {
245 /* we only use this for real if --libcurl was used */
246 const NameValue *nv = NULL;
247 for(nv=nvlist; nv->name; nv++) {
248 if(nv->value == lval) break; /* found it */
251 /* If no definition was found, output an explicit value.
252 * This could happen if new values are defined and used
253 * but the NameValue list is not updated. */
254 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
257 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
265 /* setopt wrapper for flags */
266 CURLcode tool_setopt_flags(CURL *curl, struct Configurable *config,
267 const char *name, CURLoption tag,
268 const NameValue *nvlist, long lval)
270 CURLcode ret = CURLE_OK;
273 ret = curl_easy_setopt(curl, tag, lval);
277 if(config->libcurl && !skip && !ret) {
278 /* we only use this for real if --libcurl was used */
279 char preamble[80]; /* should accommodate any symbol name */
280 long rest = lval; /* bits not handled yet */
281 const NameValue *nv = NULL;
282 snprintf(preamble, sizeof(preamble),
283 "curl_easy_setopt(hnd, %s, ", name);
284 for(nv=nvlist; nv->name; nv++) {
285 if((nv->value & ~ rest) == 0) {
286 /* all value flags contained in rest */
287 rest &= ~ nv->value; /* remove bits handled here */
288 CODE3("%s(long)%s%s",
289 preamble, nv->name, rest ? " |" : ");");
291 break; /* handled them all */
292 /* replace with all spaces for continuation line */
293 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
296 /* If any bits have no definition, output an explicit value.
297 * This could happen if new bits are defined and used
298 * but the NameValue list is not updated. */
300 CODE2("%s%ldL);", preamble, rest);
307 /* setopt wrapper for bitmasks */
308 CURLcode tool_setopt_bitmask(CURL *curl, struct Configurable *config,
309 const char *name, CURLoption tag,
310 const NameValueUnsigned *nvlist,
313 CURLcode ret = CURLE_OK;
316 ret = curl_easy_setopt(curl, tag, lval);
320 if(config->libcurl && !skip && !ret) {
321 /* we only use this for real if --libcurl was used */
323 unsigned long rest = (unsigned long)lval;
324 const NameValueUnsigned *nv = NULL;
325 snprintf(preamble, sizeof(preamble),
326 "curl_easy_setopt(hnd, %s, ", name);
327 for(nv=nvlist; nv->name; nv++) {
328 if((nv->value & ~ rest) == 0) {
329 /* all value flags contained in rest */
330 rest &= ~ nv->value; /* remove bits handled here */
331 CODE3("%s(long)%s%s",
332 preamble, nv->name, rest ? " |" : ");");
334 break; /* handled them all */
335 /* replace with all spaces for continuation line */
336 snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
339 /* If any bits have no definition, output an explicit value.
340 * This could happen if new bits are defined and used
341 * but the NameValue list is not updated. */
343 CODE2("%s%luUL);", preamble, rest);
350 /* setopt wrapper for CURLOPT_HTTPPOST */
351 CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
352 const char *name, CURLoption tag,
353 struct curl_httppost *post)
355 CURLcode ret = CURLE_OK;
356 char *escaped = NULL;
359 ret = curl_easy_setopt(curl, tag, post);
363 if(config->libcurl && !skip && !ret) {
364 struct curl_httppost *pp, *p;
366 /* May use several httppost lists, if multiple POST actions */
367 i = ++ easysrc_form_count;
368 DECL1("struct curl_httppost *post%d;", i);
369 DATA1("post%d = NULL;", i);
370 CLEAN1("curl_formfree(post%d);", i);
371 CLEAN1("post%d = NULL;", i);
373 DECL0("struct curl_httppost *postend;");
374 DATA0("postend = NULL;");
375 for(p=post; p; p=p->next) {
376 DATA1("curl_formadd(&post%d, &postend,", i);
377 DATA1(" CURLFORM_COPYNAME, \"%s\",", p->name);
378 for(pp=p; pp; pp=pp->more) {
379 /* May be several files uploaded for one name;
380 * these are linked through the 'more' pointer */
381 Curl_safefree(escaped);
382 escaped = c_escape(pp->contents);
384 ret = CURLE_OUT_OF_MEMORY;
387 if(pp->flags & HTTPPOST_FILENAME) {
388 /* file upload as for -F @filename */
389 DATA1(" CURLFORM_FILE, \"%s\",", escaped);
391 else if(pp->flags & HTTPPOST_READFILE) {
392 /* content from file as for -F <filename */
393 DATA1(" CURLFORM_FILECONTENT, \"%s\",", escaped);
396 DATA1(" CURLFORM_COPYCONTENTS, \"%s\",", escaped);
397 if(pp->showfilename) {
398 Curl_safefree(escaped);
399 escaped = c_escape(pp->showfilename);
401 ret = CURLE_OUT_OF_MEMORY;
404 DATA1(" CURLFORM_FILENAME, \"%s\",", escaped);
406 if(pp->contenttype) {
407 Curl_safefree(escaped);
408 escaped = c_escape(pp->contenttype);
410 ret = CURLE_OUT_OF_MEMORY;
413 DATA1(" CURLFORM_CONTENTTYPE, \"%s\",", escaped);
416 DATA0(" CURLFORM_END);");
418 CODE2("curl_easy_setopt(hnd, %s, post%d);", name, i);
422 Curl_safefree(escaped);
426 /* setopt wrapper for curl_slist options */
427 CURLcode tool_setopt_slist(CURL *curl, struct Configurable *config,
428 const char *name, CURLoption tag,
429 struct curl_slist *list)
431 CURLcode ret = CURLE_OK;
432 char *escaped = NULL;
435 ret = curl_easy_setopt(curl, tag, list);
439 if(config->libcurl && !skip && !ret) {
440 struct curl_slist *s;
442 /* May need several slist variables, so invent name */
443 i = ++ easysrc_slist_count;
444 DECL1("struct curl_slist *slist%d;", i);
445 DATA1("slist%d = NULL;", i);
446 CLEAN1("curl_slist_free_all(slist%d);", i);
447 CLEAN1("slist%d = NULL;", i);
448 for(s=list; s; s=s->next) {
449 Curl_safefree(escaped);
450 escaped = c_escape(s->data);
452 ret = CURLE_OUT_OF_MEMORY;
455 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
457 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
461 Curl_safefree(escaped);
465 /* generic setopt wrapper for all other options.
466 * Some type information is encoded in the tag value. */
467 CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
468 const char *name, CURLoption tag, ...)
472 const char *value = NULL;
476 char *escaped = NULL;
477 CURLcode ret = CURLE_OK;
481 if(tag < CURLOPTTYPE_OBJECTPOINT) {
482 /* Value is expected to be a long */
483 long lval = va_arg(arg, long);
485 const NameValue *nv = NULL;
486 for(nv=setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
487 if(!strcmp(name, nv->name)) {
489 break; /* found it */
493 snprintf(buf, sizeof(buf), "%ldL", lval);
495 ret = curl_easy_setopt(curl, tag, lval);
499 else if(tag < CURLOPTTYPE_OFF_T) {
500 /* Value is some sort of object pointer */
501 void *pval = va_arg(arg, void *);
503 /* function pointers are never printable */
504 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
506 value = "functionpointer";
513 else if(pval && str) {
514 value = (char *)pval;
518 value = "objectpointer";
524 ret = curl_easy_setopt(curl, tag, pval);
528 /* Value is expected to be curl_off_t */
529 curl_off_t oval = va_arg(arg, curl_off_t);
530 snprintf(buf, sizeof(buf),
531 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
533 ret = curl_easy_setopt(curl, tag, oval);
541 if(config->libcurl && !skip && !ret) {
542 /* we only use this for real if --libcurl was used */
545 REM2("%s set to a %s", name, value);
548 escaped = c_escape(value);
550 ret = CURLE_OUT_OF_MEMORY;
553 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
556 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
561 Curl_safefree(escaped);
565 #endif /* CURL_DISABLE_LIBCURL_OPTION */