1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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 ***************************************************************************/
24 #ifndef CURL_DISABLE_LIBCURL_OPTION
26 #include <curl/curl.h>
28 #define ENABLE_CURLX_PRINTF
29 /* use our own printf() functions */
32 #include "tool_cfgable.h"
33 #include "tool_easysrc.h"
34 #include "tool_setopt.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 NVEND {NULL, 0} /* sentinel to mark end of list */
46 const NameValue setopt_nv_CURLPROXY[] = {
48 NV(CURLPROXY_HTTP_1_0),
51 NV(CURLPROXY_SOCKS4A),
52 NV(CURLPROXY_SOCKS5_HOSTNAME),
56 const NameValue setopt_nv_CURLAUTH[] = {
57 NV(CURLAUTH_ANY), /* combination */
58 NV(CURLAUTH_ANYSAFE), /* combination */
61 NV(CURLAUTH_GSSNEGOTIATE),
63 NV(CURLAUTH_DIGEST_IE),
70 const NameValue setopt_nv_CURL_HTTP_VERSION[] = {
71 NV(CURL_HTTP_VERSION_NONE),
72 NV(CURL_HTTP_VERSION_1_0),
73 NV(CURL_HTTP_VERSION_1_1),
77 const NameValue setopt_nv_CURL_SSLVERSION[] = {
78 NV(CURL_SSLVERSION_DEFAULT),
79 NV(CURL_SSLVERSION_TLSv1),
80 NV(CURL_SSLVERSION_SSLv2),
81 NV(CURL_SSLVERSION_SSLv3),
85 const NameValue setopt_nv_CURL_TIMECOND[] = {
86 NV(CURL_TIMECOND_IFMODSINCE),
87 NV(CURL_TIMECOND_IFUNMODSINCE),
88 NV(CURL_TIMECOND_LASTMOD),
89 NV(CURL_TIMECOND_NONE),
93 const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
94 NV(CURLFTPSSL_CCC_NONE),
95 NV(CURLFTPSSL_CCC_PASSIVE),
96 NV(CURLFTPSSL_CCC_ACTIVE),
100 /* These mappings essentially triplicated - see
101 * tool_libinfo.c and tool_paramhlp.c */
102 const NameValue setopt_nv_CURLPROTO[] = {
103 NV(CURLPROTO_ALL), /* combination */
108 NV(CURLPROTO_GOPHER),
122 NV(CURLPROTO_TELNET),
127 /* Format and add code; jump to nomem on malloc error */
128 #define ADD(args) do { \
129 ret = easysrc_add args; \
133 #define ADDF(args) do { \
134 ret = easysrc_addf args; \
139 #define DECL0(s) ADD((&easysrc_decl, s))
140 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
142 #define DATA0(s) ADD((&easysrc_data, s))
143 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
144 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
145 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
147 #define CODE0(s) ADD((&easysrc_code, s))
148 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
149 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
150 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
152 #define CLEAN0(s) ADD((&easysrc_clean, s))
153 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
155 #define REM0(s) ADD((&easysrc_toohard, s))
156 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
157 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
159 /* Escape string to C string syntax. Return NULL if out of memory.
160 * Is this correct for those wacky EBCDIC guys? */
161 static char *c_escape(const char *str)
167 /* Allocate space based on worst-case */
169 escaped = malloc(4 * len + 1);
174 for(s=str; (c=*s) != '\0'; s++) {
195 else if(! isprint(c)) {
196 sprintf(e, "\\%03o", c);
206 /* setopt wrapper for enum types */
207 CURLcode tool_setopt_enum(CURL *curl, struct Configurable *config,
208 const char *name, CURLoption tag,
209 const NameValue *nvlist, long lval)
211 CURLcode ret = CURLE_OK;
214 ret = curl_easy_setopt(curl, tag, lval);
218 if(config->libcurl && !skip && !ret) {
219 /* we only use this for real if --libcurl was used */
220 const NameValue *nv = NULL;
221 for(nv=nvlist; nv->name; nv++) {
222 if(nv->value == lval) break; /* found it */
225 /* If no definition was found, output an explicit value.
226 * This could happen if new values are defined and used
227 * but the NameValue list is not updated. */
228 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
231 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
239 /* setopt wrapper for bit mask */
240 CURLcode tool_setopt_flags(CURL *curl, struct Configurable *config,
241 const char *name, CURLoption tag,
242 const NameValue *nvlist, long lval)
244 CURLcode ret = CURLE_OK;
247 ret = curl_easy_setopt(curl, tag, lval);
251 if(config->libcurl && !skip && !ret) {
252 /* we only use this for real if --libcurl was used */
253 char preamble[80]; /* should accommodate any symbol name */
254 long rest = lval; /* bits not handled yet */
255 const NameValue *nv = NULL;
256 snprintf(preamble, sizeof(preamble),
257 "curl_easy_setopt(hnd, %s, ", name);
258 for(nv=nvlist; nv->name; nv++) {
259 if((nv->value & ~ rest) == 0) {
260 /* all value flags contained in rest */
261 rest &= ~ nv->value; /* remove bits handled here */
262 CODE3("%s(long)%s%s",
263 preamble, nv->name, rest ? " |" : ");");
265 break; /* handled them all */
266 /* replace with all spaces for continuation line */
267 sprintf(preamble, "%*s", strlen(preamble), "");
270 /* If any bits have no definition, output an explicit value.
271 * This could happen if new bits are defined and used
272 * but the NameValue list is not updated. */
274 CODE2("%s%ldL);", preamble, rest);
281 /* setopt wrapper for CURLOPT_HTTPPOST */
282 CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
283 const char *name, CURLoption tag,
284 struct curl_httppost *post)
286 CURLcode ret = CURLE_OK;
287 char *escaped = NULL;
290 ret = curl_easy_setopt(curl, tag, post);
294 if(config->libcurl && !skip && !ret) {
295 struct curl_httppost *pp, *p;
297 /* May use several httppost lists, if multiple POST actions */
298 i = ++ easysrc_form_count;
299 DECL1("struct curl_httppost *post%d;", i);
300 DATA1("post%d = NULL;", i);
301 CLEAN1("curl_formfree(post%d);", i);
302 CLEAN1("post%d = NULL;", i);
304 DECL0("struct curl_httppost *postend;");
305 DATA0("postend = NULL;");
306 for(p=post; p; p=p->next) {
307 DATA1("curl_formadd(&post%d, &postend,", i);
308 DATA1(" CURLFORM_COPYNAME, \"%s\",", p->name);
309 for(pp=p; pp; pp=pp->more) {
310 /* May be several files uploaded for one name;
311 * these are linked through the 'more' pointer */
312 Curl_safefree(escaped);
313 escaped = c_escape(pp->contents);
315 ret = CURLE_OUT_OF_MEMORY;
318 if(pp->flags & HTTPPOST_FILENAME) {
319 /* file upload as for -F @filename */
320 DATA1(" CURLFORM_FILE, \"%s\",", escaped);
322 else if(pp->flags & HTTPPOST_READFILE) {
323 /* content from file as for -F <filename */
324 DATA1(" CURLFORM_FILECONTENT, \"%s\",", escaped);
327 DATA1(" CURLFORM_COPYCONTENTS, \"%s\",", escaped);
328 if(pp->showfilename) {
329 Curl_safefree(escaped);
330 escaped = c_escape(pp->showfilename);
332 ret = CURLE_OUT_OF_MEMORY;
335 DATA1(" CURLFORM_FILENAME, \"%s\",", escaped);
337 if(pp->contenttype) {
338 Curl_safefree(escaped);
339 escaped = c_escape(pp->contenttype);
341 ret = CURLE_OUT_OF_MEMORY;
344 DATA1(" CURLFORM_CONTENTTYPE, \"%s\",", escaped);
347 DATA0(" CURLFORM_END);");
349 CODE2("curl_easy_setopt(hnd, %s, post%d);", name, i);
353 Curl_safefree(escaped);
357 /* setopt wrapper for curl_slist options */
358 CURLcode tool_setopt_slist(CURL *curl, struct Configurable *config,
359 const char *name, CURLoption tag,
360 struct curl_slist *list)
362 CURLcode ret = CURLE_OK;
363 char *escaped = NULL;
366 ret = curl_easy_setopt(curl, tag, list);
370 if(config->libcurl && !skip && !ret) {
371 struct curl_slist *s;
373 /* May need several slist variables, so invent name */
374 i = ++ easysrc_slist_count;
375 DECL1("struct curl_slist *slist%d;", i);
376 DATA1("slist%d = NULL;", i);
377 CLEAN1("curl_slist_free_all(slist%d);", i);
378 CLEAN1("slist%d = NULL;", i);
379 for(s=list; s; s=s->next) {
380 Curl_safefree(escaped);
381 escaped = c_escape(s->data);
383 ret = CURLE_OUT_OF_MEMORY;
386 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
388 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
392 Curl_safefree(escaped);
396 /* generic setopt wrapper for all other options.
397 * Some type information is encoded in the tag value. */
398 CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
399 const char *name, CURLoption tag, ...)
403 const char *value = NULL;
407 char *escaped = NULL;
408 CURLcode ret = CURLE_OK;
412 if(tag < CURLOPTTYPE_OBJECTPOINT) {
413 /* Value is expected to be a long */
414 long lval = va_arg(arg, long);
415 snprintf(buf, sizeof(buf), "%ldL", lval);
417 ret = curl_easy_setopt(curl, tag, lval);
421 else if(tag < CURLOPTTYPE_OFF_T) {
422 /* Value is some sort of object pointer */
423 void *pval = va_arg(arg, void *);
425 /* function pointers are never printable */
426 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
428 value = "functionpointer";
435 else if(pval && str) {
436 value = (char *)pval;
440 value = "objectpointer";
446 ret = curl_easy_setopt(curl, tag, pval);
450 /* Value is expected to be curl_off_t */
451 curl_off_t oval = va_arg(arg, curl_off_t);
452 snprintf(buf, sizeof(buf),
453 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
455 ret = curl_easy_setopt(curl, tag, oval);
463 if(config->libcurl && !skip && !ret) {
464 /* we only use this for real if --libcurl was used */
467 REM2("%s set to a %s", name, value);
470 escaped = c_escape(value);
472 ret = CURLE_OUT_OF_MEMORY;
475 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
478 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
483 Curl_safefree(escaped);
487 #endif /* CURL_DISABLE_LIBCURL_OPTION */