xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 9 | * |
| 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.se/docs/copyright.html. |
| 13 | * |
| 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. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | #include "tool_setup.h" |
| 25 | |
| 26 | #ifndef CURL_DISABLE_LIBCURL_OPTION |
| 27 | |
| 28 | #define ENABLE_CURLX_PRINTF |
| 29 | /* use our own printf() functions */ |
| 30 | #include "curlx.h" |
| 31 | |
| 32 | #include "tool_cfgable.h" |
| 33 | #include "tool_easysrc.h" |
| 34 | #include "tool_setopt.h" |
| 35 | #include "tool_msgs.h" |
| 36 | #include "dynbuf.h" |
| 37 | |
| 38 | #include "memdebug.h" /* keep this as LAST include */ |
| 39 | |
| 40 | /* Lookup tables for converting setopt values back to symbols */ |
| 41 | /* For enums, values may be in any order. */ |
| 42 | /* For bit masks, put combinations first, then single bits, */ |
| 43 | /* and finally any "NONE" value. */ |
| 44 | |
| 45 | #define NV(e) {#e, e} |
| 46 | #define NV1(e, v) {#e, (v)} |
| 47 | #define NVEND {NULL, 0} /* sentinel to mark end of list */ |
| 48 | |
| 49 | const struct NameValue setopt_nv_CURLPROXY[] = { |
| 50 | NV(CURLPROXY_HTTP), |
| 51 | NV(CURLPROXY_HTTP_1_0), |
| 52 | NV(CURLPROXY_HTTPS), |
| 53 | NV(CURLPROXY_SOCKS4), |
| 54 | NV(CURLPROXY_SOCKS5), |
| 55 | NV(CURLPROXY_SOCKS4A), |
| 56 | NV(CURLPROXY_SOCKS5_HOSTNAME), |
| 57 | NVEND, |
| 58 | }; |
| 59 | |
| 60 | const struct NameValue setopt_nv_CURL_SOCKS_PROXY[] = { |
| 61 | NV(CURLPROXY_SOCKS4), |
| 62 | NV(CURLPROXY_SOCKS5), |
| 63 | NV(CURLPROXY_SOCKS4A), |
| 64 | NV(CURLPROXY_SOCKS5_HOSTNAME), |
| 65 | NVEND, |
| 66 | }; |
| 67 | |
| 68 | const struct NameValueUnsigned setopt_nv_CURLHSTS[] = { |
| 69 | NV(CURLHSTS_ENABLE), |
| 70 | NVEND, |
| 71 | }; |
| 72 | |
| 73 | const struct NameValueUnsigned setopt_nv_CURLAUTH[] = { |
| 74 | NV(CURLAUTH_ANY), /* combination */ |
| 75 | NV(CURLAUTH_ANYSAFE), /* combination */ |
| 76 | NV(CURLAUTH_BASIC), |
| 77 | NV(CURLAUTH_DIGEST), |
| 78 | NV(CURLAUTH_GSSNEGOTIATE), |
| 79 | NV(CURLAUTH_NTLM), |
| 80 | NV(CURLAUTH_DIGEST_IE), |
| 81 | NV(CURLAUTH_NTLM_WB), |
| 82 | NV(CURLAUTH_ONLY), |
| 83 | NV(CURLAUTH_NONE), |
| 84 | NVEND, |
| 85 | }; |
| 86 | |
| 87 | const struct NameValue setopt_nv_CURL_HTTP_VERSION[] = { |
| 88 | NV(CURL_HTTP_VERSION_NONE), |
| 89 | NV(CURL_HTTP_VERSION_1_0), |
| 90 | NV(CURL_HTTP_VERSION_1_1), |
| 91 | NV(CURL_HTTP_VERSION_2_0), |
| 92 | NV(CURL_HTTP_VERSION_2TLS), |
| 93 | NV(CURL_HTTP_VERSION_3), |
| 94 | NVEND, |
| 95 | }; |
| 96 | |
| 97 | const struct NameValue setopt_nv_CURL_SSLVERSION[] = { |
| 98 | NV(CURL_SSLVERSION_DEFAULT), |
| 99 | NV(CURL_SSLVERSION_TLSv1), |
| 100 | NV(CURL_SSLVERSION_SSLv2), |
| 101 | NV(CURL_SSLVERSION_SSLv3), |
| 102 | NV(CURL_SSLVERSION_TLSv1_0), |
| 103 | NV(CURL_SSLVERSION_TLSv1_1), |
| 104 | NV(CURL_SSLVERSION_TLSv1_2), |
| 105 | NV(CURL_SSLVERSION_TLSv1_3), |
| 106 | NVEND, |
| 107 | }; |
| 108 | |
| 109 | const struct NameValue setopt_nv_CURL_TIMECOND[] = { |
| 110 | NV(CURL_TIMECOND_IFMODSINCE), |
| 111 | NV(CURL_TIMECOND_IFUNMODSINCE), |
| 112 | NV(CURL_TIMECOND_LASTMOD), |
| 113 | NV(CURL_TIMECOND_NONE), |
| 114 | NVEND, |
| 115 | }; |
| 116 | |
| 117 | const struct NameValue setopt_nv_CURLFTPSSL_CCC[] = { |
| 118 | NV(CURLFTPSSL_CCC_NONE), |
| 119 | NV(CURLFTPSSL_CCC_PASSIVE), |
| 120 | NV(CURLFTPSSL_CCC_ACTIVE), |
| 121 | NVEND, |
| 122 | }; |
| 123 | |
| 124 | const struct NameValue setopt_nv_CURLUSESSL[] = { |
| 125 | NV(CURLUSESSL_NONE), |
| 126 | NV(CURLUSESSL_TRY), |
| 127 | NV(CURLUSESSL_CONTROL), |
| 128 | NV(CURLUSESSL_ALL), |
| 129 | NVEND, |
| 130 | }; |
| 131 | |
| 132 | const struct NameValueUnsigned setopt_nv_CURLSSLOPT[] = { |
| 133 | NV(CURLSSLOPT_ALLOW_BEAST), |
| 134 | NV(CURLSSLOPT_NO_REVOKE), |
| 135 | NV(CURLSSLOPT_NO_PARTIALCHAIN), |
| 136 | NV(CURLSSLOPT_REVOKE_BEST_EFFORT), |
| 137 | NV(CURLSSLOPT_NATIVE_CA), |
| 138 | NV(CURLSSLOPT_AUTO_CLIENT_CERT), |
| 139 | NVEND, |
| 140 | }; |
| 141 | |
| 142 | const struct NameValue setopt_nv_CURL_NETRC[] = { |
| 143 | NV(CURL_NETRC_IGNORED), |
| 144 | NV(CURL_NETRC_OPTIONAL), |
| 145 | NV(CURL_NETRC_REQUIRED), |
| 146 | NVEND, |
| 147 | }; |
| 148 | |
| 149 | /* These options have non-zero default values. */ |
| 150 | static const struct NameValue setopt_nv_CURLNONZERODEFAULTS[] = { |
| 151 | NV1(CURLOPT_SSL_VERIFYPEER, 1), |
| 152 | NV1(CURLOPT_SSL_VERIFYHOST, 1), |
| 153 | NV1(CURLOPT_SSL_ENABLE_NPN, 1), |
| 154 | NV1(CURLOPT_SSL_ENABLE_ALPN, 1), |
| 155 | NV1(CURLOPT_TCP_NODELAY, 1), |
| 156 | NV1(CURLOPT_PROXY_SSL_VERIFYPEER, 1), |
| 157 | NV1(CURLOPT_PROXY_SSL_VERIFYHOST, 1), |
| 158 | NV1(CURLOPT_SOCKS5_AUTH, 1), |
| 159 | NVEND |
| 160 | }; |
| 161 | |
| 162 | /* Format and add code; jump to nomem on malloc error */ |
| 163 | #define ADD(args) do { \ |
| 164 | ret = easysrc_add args; \ |
| 165 | if(ret) \ |
| 166 | goto nomem; \ |
| 167 | } while(0) |
| 168 | #define ADDF(args) do { \ |
| 169 | ret = easysrc_addf args; \ |
| 170 | if(ret) \ |
| 171 | goto nomem; \ |
| 172 | } while(0) |
| 173 | #define NULL_CHECK(p) do { \ |
| 174 | if(!p) { \ |
| 175 | ret = CURLE_OUT_OF_MEMORY; \ |
| 176 | goto nomem; \ |
| 177 | } \ |
| 178 | } while(0) |
| 179 | |
| 180 | #define DECL0(s) ADD((&easysrc_decl, s)) |
| 181 | #define DECL1(f,a) ADDF((&easysrc_decl, f,a)) |
| 182 | |
| 183 | #define DATA0(s) ADD((&easysrc_data, s)) |
| 184 | #define DATA1(f,a) ADDF((&easysrc_data, f,a)) |
| 185 | #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b)) |
| 186 | #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c)) |
| 187 | |
| 188 | #define CODE0(s) ADD((&easysrc_code, s)) |
| 189 | #define CODE1(f,a) ADDF((&easysrc_code, f,a)) |
| 190 | #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b)) |
| 191 | #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c)) |
| 192 | |
| 193 | #define CLEAN0(s) ADD((&easysrc_clean, s)) |
| 194 | #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a)) |
| 195 | |
| 196 | #define REM0(s) ADD((&easysrc_toohard, s)) |
| 197 | #define REM1(f,a) ADDF((&easysrc_toohard, f,a)) |
| 198 | #define REM3(f,a,b,c) ADDF((&easysrc_toohard, f,a,b,c)) |
| 199 | |
| 200 | /* Escape string to C string syntax. Return NULL if out of memory. |
| 201 | * Is this correct for those wacky EBCDIC guys? */ |
| 202 | |
| 203 | #define MAX_STRING_LENGTH_OUTPUT 2000 |
| 204 | #define ZERO_TERMINATED -1 |
| 205 | |
| 206 | static char *c_escape(const char *str, curl_off_t len) |
| 207 | { |
| 208 | const char *s; |
| 209 | unsigned int cutoff = 0; |
| 210 | CURLcode result; |
| 211 | struct curlx_dynbuf escaped; |
| 212 | |
| 213 | curlx_dyn_init(&escaped, 4 * MAX_STRING_LENGTH_OUTPUT + 3); |
| 214 | |
| 215 | if(len == ZERO_TERMINATED) |
| 216 | len = strlen(str); |
| 217 | |
| 218 | if(len > MAX_STRING_LENGTH_OUTPUT) { |
| 219 | /* cap ridiculously long strings */ |
| 220 | len = MAX_STRING_LENGTH_OUTPUT; |
| 221 | cutoff = 3; |
| 222 | } |
| 223 | |
| 224 | result = curlx_dyn_addn(&escaped, STRCONST("")); |
| 225 | for(s = str; !result && len; s++, len--) { |
| 226 | /* escape question marks as well, to prevent generating accidental |
| 227 | trigraphs */ |
| 228 | static const char from[] = "\t\r\n?\"\\"; |
| 229 | static const char to[] = "\\t\\r\\n\\?\\\"\\\\"; |
| 230 | const char *p = strchr(from, *s); |
| 231 | |
| 232 | if(!p && ISPRINT(*s)) |
| 233 | continue; |
| 234 | |
| 235 | result = curlx_dyn_addn(&escaped, str, s - str); |
| 236 | str = s + 1; |
| 237 | |
| 238 | if(!result) { |
| 239 | if(p && *p) |
| 240 | result = curlx_dyn_addn(&escaped, to + 2 * (p - from), 2); |
| 241 | else { |
| 242 | const char *format = "\\x%02x"; |
| 243 | |
| 244 | if(len > 1 && ISXDIGIT(s[1])) { |
| 245 | /* Octal escape to avoid >2 digit hex. */ |
| 246 | format = "\\%03o"; |
| 247 | } |
| 248 | |
| 249 | result = curlx_dyn_addf(&escaped, format, |
| 250 | (unsigned int) *(unsigned char *) s); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if(!result) |
| 256 | result = curlx_dyn_addn(&escaped, str, s - str); |
| 257 | |
| 258 | if(!result) |
| 259 | (void) !curlx_dyn_addn(&escaped, "...", cutoff); |
| 260 | |
| 261 | return curlx_dyn_ptr(&escaped); |
| 262 | } |
| 263 | |
| 264 | /* setopt wrapper for enum types */ |
| 265 | CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config, |
| 266 | const char *name, CURLoption tag, |
| 267 | const struct NameValue *nvlist, long lval) |
| 268 | { |
| 269 | CURLcode ret = CURLE_OK; |
| 270 | bool skip = FALSE; |
| 271 | |
| 272 | ret = curl_easy_setopt(curl, tag, lval); |
| 273 | if(!lval) |
| 274 | skip = TRUE; |
| 275 | |
| 276 | if(config->libcurl && !skip && !ret) { |
| 277 | /* we only use this for real if --libcurl was used */ |
| 278 | const struct NameValue *nv = NULL; |
| 279 | for(nv = nvlist; nv->name; nv++) { |
| 280 | if(nv->value == lval) |
| 281 | break; /* found it */ |
| 282 | } |
| 283 | if(!nv->name) { |
| 284 | /* If no definition was found, output an explicit value. |
| 285 | * This could happen if new values are defined and used |
| 286 | * but the NameValue list is not updated. */ |
| 287 | CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval); |
| 288 | } |
| 289 | else { |
| 290 | CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | #ifdef DEBUGBUILD |
| 295 | if(ret) |
| 296 | warnf(config, "option %s returned error (%d)\n", name, (int)ret); |
| 297 | #endif |
| 298 | nomem: |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | /* setopt wrapper for flags */ |
| 303 | CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config, |
| 304 | const char *name, CURLoption tag, |
| 305 | const struct NameValue *nvlist, long lval) |
| 306 | { |
| 307 | CURLcode ret = CURLE_OK; |
| 308 | bool skip = FALSE; |
| 309 | |
| 310 | ret = curl_easy_setopt(curl, tag, lval); |
| 311 | if(!lval) |
| 312 | skip = TRUE; |
| 313 | |
| 314 | if(config->libcurl && !skip && !ret) { |
| 315 | /* we only use this for real if --libcurl was used */ |
| 316 | char preamble[80]; /* should accommodate any symbol name */ |
| 317 | long rest = lval; /* bits not handled yet */ |
| 318 | const struct NameValue *nv = NULL; |
| 319 | msnprintf(preamble, sizeof(preamble), |
| 320 | "curl_easy_setopt(hnd, %s, ", name); |
| 321 | for(nv = nvlist; nv->name; nv++) { |
| 322 | if((nv->value & ~ rest) == 0) { |
| 323 | /* all value flags contained in rest */ |
| 324 | rest &= ~ nv->value; /* remove bits handled here */ |
| 325 | CODE3("%s(long)%s%s", |
| 326 | preamble, nv->name, rest ? " |" : ");"); |
| 327 | if(!rest) |
| 328 | break; /* handled them all */ |
| 329 | /* replace with all spaces for continuation line */ |
| 330 | msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), ""); |
| 331 | } |
| 332 | } |
| 333 | /* If any bits have no definition, output an explicit value. |
| 334 | * This could happen if new bits are defined and used |
| 335 | * but the NameValue list is not updated. */ |
| 336 | if(rest) |
| 337 | CODE2("%s%ldL);", preamble, rest); |
| 338 | } |
| 339 | |
| 340 | nomem: |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | /* setopt wrapper for bitmasks */ |
| 345 | CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config, |
| 346 | const char *name, CURLoption tag, |
| 347 | const struct NameValueUnsigned *nvlist, |
| 348 | long lval) |
| 349 | { |
| 350 | CURLcode ret = CURLE_OK; |
| 351 | bool skip = FALSE; |
| 352 | |
| 353 | ret = curl_easy_setopt(curl, tag, lval); |
| 354 | if(!lval) |
| 355 | skip = TRUE; |
| 356 | |
| 357 | if(config->libcurl && !skip && !ret) { |
| 358 | /* we only use this for real if --libcurl was used */ |
| 359 | char preamble[80]; |
| 360 | unsigned long rest = (unsigned long)lval; |
| 361 | const struct NameValueUnsigned *nv = NULL; |
| 362 | msnprintf(preamble, sizeof(preamble), |
| 363 | "curl_easy_setopt(hnd, %s, ", name); |
| 364 | for(nv = nvlist; nv->name; nv++) { |
| 365 | if((nv->value & ~ rest) == 0) { |
| 366 | /* all value flags contained in rest */ |
| 367 | rest &= ~ nv->value; /* remove bits handled here */ |
| 368 | CODE3("%s(long)%s%s", |
| 369 | preamble, nv->name, rest ? " |" : ");"); |
| 370 | if(!rest) |
| 371 | break; /* handled them all */ |
| 372 | /* replace with all spaces for continuation line */ |
| 373 | msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), ""); |
| 374 | } |
| 375 | } |
| 376 | /* If any bits have no definition, output an explicit value. |
| 377 | * This could happen if new bits are defined and used |
| 378 | * but the NameValue list is not updated. */ |
| 379 | if(rest) |
| 380 | CODE2("%s%luUL);", preamble, rest); |
| 381 | } |
| 382 | |
| 383 | nomem: |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | /* Generate code for a struct curl_slist. */ |
| 388 | static CURLcode libcurl_generate_slist(struct curl_slist *slist, int *slistno) |
| 389 | { |
| 390 | CURLcode ret = CURLE_OK; |
| 391 | char *escaped = NULL; |
| 392 | |
| 393 | /* May need several slist variables, so invent name */ |
| 394 | *slistno = ++easysrc_slist_count; |
| 395 | |
| 396 | DECL1("struct curl_slist *slist%d;", *slistno); |
| 397 | DATA1("slist%d = NULL;", *slistno); |
| 398 | CLEAN1("curl_slist_free_all(slist%d);", *slistno); |
| 399 | CLEAN1("slist%d = NULL;", *slistno); |
| 400 | for(; slist; slist = slist->next) { |
| 401 | Curl_safefree(escaped); |
| 402 | escaped = c_escape(slist->data, ZERO_TERMINATED); |
| 403 | if(!escaped) |
| 404 | return CURLE_OUT_OF_MEMORY; |
| 405 | DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", |
| 406 | *slistno, *slistno, escaped); |
| 407 | } |
| 408 | |
| 409 | nomem: |
| 410 | Curl_safefree(escaped); |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | static CURLcode libcurl_generate_mime(CURL *curl, |
| 415 | struct GlobalConfig *config, |
| 416 | struct tool_mime *toolmime, |
| 417 | int *mimeno); /* Forward. */ |
| 418 | |
| 419 | /* Wrapper to generate source code for a mime part. */ |
| 420 | static CURLcode libcurl_generate_mime_part(CURL *curl, |
| 421 | struct GlobalConfig *config, |
| 422 | struct tool_mime *part, |
| 423 | int mimeno) |
| 424 | { |
| 425 | CURLcode ret = CURLE_OK; |
| 426 | int submimeno = 0; |
| 427 | char *escaped = NULL; |
| 428 | const char *data = NULL; |
| 429 | const char *filename = part->filename; |
| 430 | |
| 431 | /* Parts are linked in reverse order. */ |
| 432 | if(part->prev) { |
| 433 | ret = libcurl_generate_mime_part(curl, config, part->prev, mimeno); |
| 434 | if(ret) |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | /* Create the part. */ |
| 439 | CODE2("part%d = curl_mime_addpart(mime%d);", mimeno, mimeno); |
| 440 | |
| 441 | switch(part->kind) { |
| 442 | case TOOLMIME_PARTS: |
| 443 | ret = libcurl_generate_mime(curl, config, part, &submimeno); |
| 444 | if(!ret) { |
| 445 | CODE2("curl_mime_subparts(part%d, mime%d);", mimeno, submimeno); |
| 446 | CODE1("mime%d = NULL;", submimeno); /* Avoid freeing in CLEAN. */ |
| 447 | } |
| 448 | break; |
| 449 | |
| 450 | case TOOLMIME_DATA: |
| 451 | data = part->data; |
| 452 | if(!ret) { |
| 453 | Curl_safefree(escaped); |
| 454 | escaped = c_escape(data, ZERO_TERMINATED); |
| 455 | NULL_CHECK(escaped); |
| 456 | CODE2("curl_mime_data(part%d, \"%s\", CURL_ZERO_TERMINATED);", |
| 457 | mimeno, escaped); |
| 458 | } |
| 459 | break; |
| 460 | |
| 461 | case TOOLMIME_FILE: |
| 462 | case TOOLMIME_FILEDATA: |
| 463 | escaped = c_escape(part->data, ZERO_TERMINATED); |
| 464 | NULL_CHECK(escaped); |
| 465 | CODE2("curl_mime_filedata(part%d, \"%s\");", mimeno, escaped); |
| 466 | if(part->kind == TOOLMIME_FILEDATA && !filename) { |
| 467 | CODE1("curl_mime_filename(part%d, NULL);", mimeno); |
| 468 | } |
| 469 | break; |
| 470 | |
| 471 | case TOOLMIME_STDIN: |
| 472 | if(!filename) |
| 473 | filename = "-"; |
| 474 | /* FALLTHROUGH */ |
| 475 | case TOOLMIME_STDINDATA: |
| 476 | /* Can only be reading stdin in the current context. */ |
| 477 | CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\", |
| 478 | mimeno); |
| 479 | CODE0(" (curl_seek_callback) fseek, NULL, stdin);"); |
| 480 | break; |
| 481 | default: |
| 482 | /* Other cases not possible in this context. */ |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | if(!ret && part->encoder) { |
| 487 | Curl_safefree(escaped); |
| 488 | escaped = c_escape(part->encoder, ZERO_TERMINATED); |
| 489 | NULL_CHECK(escaped); |
| 490 | CODE2("curl_mime_encoder(part%d, \"%s\");", mimeno, escaped); |
| 491 | } |
| 492 | |
| 493 | if(!ret && filename) { |
| 494 | Curl_safefree(escaped); |
| 495 | escaped = c_escape(filename, ZERO_TERMINATED); |
| 496 | NULL_CHECK(escaped); |
| 497 | CODE2("curl_mime_filename(part%d, \"%s\");", mimeno, escaped); |
| 498 | } |
| 499 | |
| 500 | if(!ret && part->name) { |
| 501 | Curl_safefree(escaped); |
| 502 | escaped = c_escape(part->name, ZERO_TERMINATED); |
| 503 | NULL_CHECK(escaped); |
| 504 | CODE2("curl_mime_name(part%d, \"%s\");", mimeno, escaped); |
| 505 | } |
| 506 | |
| 507 | if(!ret && part->type) { |
| 508 | Curl_safefree(escaped); |
| 509 | escaped = c_escape(part->type, ZERO_TERMINATED); |
| 510 | NULL_CHECK(escaped); |
| 511 | CODE2("curl_mime_type(part%d, \"%s\");", mimeno, escaped); |
| 512 | } |
| 513 | |
| 514 | if(!ret && part->headers) { |
| 515 | int slistno; |
| 516 | |
| 517 | ret = libcurl_generate_slist(part->headers, &slistno); |
| 518 | if(!ret) { |
| 519 | CODE2("curl_mime_headers(part%d, slist%d, 1);", mimeno, slistno); |
| 520 | CODE1("slist%d = NULL;", slistno); /* Prevent CLEANing. */ |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | nomem: |
| 525 | Curl_safefree(escaped); |
| 526 | return ret; |
| 527 | } |
| 528 | |
| 529 | /* Wrapper to generate source code for a mime structure. */ |
| 530 | static CURLcode libcurl_generate_mime(CURL *curl, |
| 531 | struct GlobalConfig *config, |
| 532 | struct tool_mime *toolmime, |
| 533 | int *mimeno) |
| 534 | { |
| 535 | CURLcode ret = CURLE_OK; |
| 536 | |
| 537 | /* May need several mime variables, so invent name. */ |
| 538 | *mimeno = ++easysrc_mime_count; |
| 539 | DECL1("curl_mime *mime%d;", *mimeno); |
| 540 | DATA1("mime%d = NULL;", *mimeno); |
| 541 | CODE1("mime%d = curl_mime_init(hnd);", *mimeno); |
| 542 | CLEAN1("curl_mime_free(mime%d);", *mimeno); |
| 543 | CLEAN1("mime%d = NULL;", *mimeno); |
| 544 | |
| 545 | if(toolmime->subparts) { |
| 546 | DECL1("curl_mimepart *part%d;", *mimeno); |
| 547 | ret = libcurl_generate_mime_part(curl, config, |
| 548 | toolmime->subparts, *mimeno); |
| 549 | } |
| 550 | |
| 551 | nomem: |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | /* setopt wrapper for CURLOPT_MIMEPOST */ |
| 556 | CURLcode tool_setopt_mimepost(CURL *curl, struct GlobalConfig *config, |
| 557 | const char *name, CURLoption tag, |
| 558 | curl_mime *mimepost) |
| 559 | { |
| 560 | CURLcode ret = curl_easy_setopt(curl, tag, mimepost); |
| 561 | int mimeno = 0; |
| 562 | |
| 563 | if(!ret && config->libcurl) { |
| 564 | ret = libcurl_generate_mime(curl, config, |
| 565 | config->current->mimeroot, &mimeno); |
| 566 | |
| 567 | if(!ret) |
| 568 | CODE2("curl_easy_setopt(hnd, %s, mime%d);", name, mimeno); |
| 569 | } |
| 570 | |
| 571 | nomem: |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | /* setopt wrapper for curl_slist options */ |
| 576 | CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config, |
| 577 | const char *name, CURLoption tag, |
| 578 | struct curl_slist *list) |
| 579 | { |
| 580 | CURLcode ret = CURLE_OK; |
| 581 | |
| 582 | ret = curl_easy_setopt(curl, tag, list); |
| 583 | |
| 584 | if(config->libcurl && list && !ret) { |
| 585 | int i; |
| 586 | |
| 587 | ret = libcurl_generate_slist(list, &i); |
| 588 | if(!ret) |
| 589 | CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i); |
| 590 | } |
| 591 | |
| 592 | nomem: |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | /* generic setopt wrapper for all other options. |
| 597 | * Some type information is encoded in the tag value. */ |
| 598 | CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *global, |
| 599 | struct OperationConfig *config, |
| 600 | const char *name, CURLoption tag, ...) |
| 601 | { |
| 602 | va_list arg; |
| 603 | char buf[256]; |
| 604 | const char *value = NULL; |
| 605 | bool remark = FALSE; |
| 606 | bool skip = FALSE; |
| 607 | bool escape = FALSE; |
| 608 | char *escaped = NULL; |
| 609 | CURLcode ret = CURLE_OK; |
| 610 | |
| 611 | va_start(arg, tag); |
| 612 | |
| 613 | if(tag < CURLOPTTYPE_OBJECTPOINT) { |
| 614 | /* Value is expected to be a long */ |
| 615 | long lval = va_arg(arg, long); |
| 616 | long defval = 0L; |
| 617 | const struct NameValue *nv = NULL; |
| 618 | for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) { |
| 619 | if(!strcmp(name, nv->name)) { |
| 620 | defval = nv->value; |
| 621 | break; /* found it */ |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | msnprintf(buf, sizeof(buf), "%ldL", lval); |
| 626 | value = buf; |
| 627 | ret = curl_easy_setopt(curl, tag, lval); |
| 628 | if(lval == defval) |
| 629 | skip = TRUE; |
| 630 | } |
| 631 | else if(tag < CURLOPTTYPE_OFF_T) { |
| 632 | /* Value is some sort of object pointer */ |
| 633 | void *pval = va_arg(arg, void *); |
| 634 | |
| 635 | /* function pointers are never printable */ |
| 636 | if(tag >= CURLOPTTYPE_FUNCTIONPOINT) { |
| 637 | if(pval) { |
| 638 | value = "function pointer"; |
| 639 | remark = TRUE; |
| 640 | } |
| 641 | else |
| 642 | skip = TRUE; |
| 643 | } |
| 644 | |
| 645 | else if(pval && str) { |
| 646 | value = (char *)pval; |
| 647 | escape = TRUE; |
| 648 | } |
| 649 | else if(pval) { |
| 650 | value = "object pointer"; |
| 651 | remark = TRUE; |
| 652 | } |
| 653 | else |
| 654 | skip = TRUE; |
| 655 | |
| 656 | ret = curl_easy_setopt(curl, tag, pval); |
| 657 | |
| 658 | } |
| 659 | else if(tag < CURLOPTTYPE_BLOB) { |
| 660 | /* Value is expected to be curl_off_t */ |
| 661 | curl_off_t oval = va_arg(arg, curl_off_t); |
| 662 | msnprintf(buf, sizeof(buf), |
| 663 | "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval); |
| 664 | value = buf; |
| 665 | ret = curl_easy_setopt(curl, tag, oval); |
| 666 | |
| 667 | if(!oval) |
| 668 | skip = TRUE; |
| 669 | } |
| 670 | else { |
| 671 | /* Value is a blob */ |
| 672 | void *pblob = va_arg(arg, void *); |
| 673 | |
| 674 | /* blobs are never printable */ |
| 675 | if(pblob) { |
| 676 | value = "blob pointer"; |
| 677 | remark = TRUE; |
| 678 | } |
| 679 | else |
| 680 | skip = TRUE; |
| 681 | |
| 682 | ret = curl_easy_setopt(curl, tag, pblob); |
| 683 | } |
| 684 | |
| 685 | va_end(arg); |
| 686 | |
| 687 | if(global->libcurl && !skip && !ret) { |
| 688 | /* we only use this for real if --libcurl was used */ |
| 689 | |
| 690 | if(remark) |
| 691 | REM3("%s was set to a%s %s", name, (*value == 'o' ? "n" : ""), value); |
| 692 | else { |
| 693 | if(escape) { |
| 694 | curl_off_t len = ZERO_TERMINATED; |
| 695 | if(tag == CURLOPT_POSTFIELDS) |
| 696 | len = config->postfieldsize; |
| 697 | escaped = c_escape(value, len); |
| 698 | NULL_CHECK(escaped); |
| 699 | CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped); |
| 700 | } |
| 701 | else |
| 702 | CODE2("curl_easy_setopt(hnd, %s, %s);", name, value); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | nomem: |
| 707 | Curl_safefree(escaped); |
| 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | #else /* CURL_DISABLE_LIBCURL_OPTION */ |
| 712 | |
| 713 | #include "tool_cfgable.h" |
| 714 | #include "tool_setopt.h" |
| 715 | |
| 716 | #endif /* CURL_DISABLE_LIBCURL_OPTION */ |