PageRenderTime 77ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tool_getparam.c

http://github.com/bagder/curl
C | 2314 lines | 1957 code | 118 blank | 239 comment | 311 complexity | ee5fe5c8da22e91f4b4c8f37e677d7f4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. ***************************************************************************/
  22. #include "tool_setup.h"
  23. #include "strcase.h"
  24. #define ENABLE_CURLX_PRINTF
  25. /* use our own printf() functions */
  26. #include "curlx.h"
  27. #include "tool_binmode.h"
  28. #include "tool_cfgable.h"
  29. #include "tool_cb_prg.h"
  30. #include "tool_convert.h"
  31. #include "tool_filetime.h"
  32. #include "tool_formparse.h"
  33. #include "tool_getparam.h"
  34. #include "tool_helpers.h"
  35. #include "tool_libinfo.h"
  36. #include "tool_metalink.h"
  37. #include "tool_msgs.h"
  38. #include "tool_paramhlp.h"
  39. #include "tool_parsecfg.h"
  40. #include "tool_main.h"
  41. #include "memdebug.h" /* keep this as LAST include */
  42. #ifdef MSDOS
  43. # define USE_WATT32
  44. #endif
  45. #define GetStr(str,val) do { \
  46. if(*(str)) { \
  47. free(*(str)); \
  48. *(str) = NULL; \
  49. } \
  50. if((val)) { \
  51. *(str) = strdup((val)); \
  52. if(!(*(str))) \
  53. return PARAM_NO_MEM; \
  54. } \
  55. } while(0)
  56. struct LongShort {
  57. const char *letter; /* short name option */
  58. const char *lname; /* long name option */
  59. enum {
  60. ARG_NONE, /* stand-alone but not a boolean */
  61. ARG_BOOL, /* accepts a --no-[name] prefix */
  62. ARG_STRING, /* requires an argument */
  63. ARG_FILENAME /* requires an argument, usually a file name */
  64. } desc;
  65. };
  66. static const struct LongShort aliases[]= {
  67. /* 'letter' strings with more than one character have *no* short option to
  68. mention. */
  69. {"*@", "url", ARG_STRING},
  70. {"*4", "dns-ipv4-addr", ARG_STRING},
  71. {"*6", "dns-ipv6-addr", ARG_STRING},
  72. {"*a", "random-file", ARG_FILENAME},
  73. {"*b", "egd-file", ARG_STRING},
  74. {"*B", "oauth2-bearer", ARG_STRING},
  75. {"*c", "connect-timeout", ARG_STRING},
  76. {"*C", "doh-url" , ARG_STRING},
  77. {"*d", "ciphers", ARG_STRING},
  78. {"*D", "dns-interface", ARG_STRING},
  79. {"*e", "disable-epsv", ARG_BOOL},
  80. {"*f", "disallow-username-in-url", ARG_BOOL},
  81. {"*E", "epsv", ARG_BOOL},
  82. /* 'epsv' made like this to make --no-epsv and --epsv to work
  83. although --disable-epsv is the documented option */
  84. {"*F", "dns-servers", ARG_STRING},
  85. {"*g", "trace", ARG_FILENAME},
  86. {"*G", "npn", ARG_BOOL},
  87. {"*h", "trace-ascii", ARG_FILENAME},
  88. {"*H", "alpn", ARG_BOOL},
  89. {"*i", "limit-rate", ARG_STRING},
  90. {"*j", "compressed", ARG_BOOL},
  91. {"*J", "tr-encoding", ARG_BOOL},
  92. {"*k", "digest", ARG_BOOL},
  93. {"*l", "negotiate", ARG_BOOL},
  94. {"*m", "ntlm", ARG_BOOL},
  95. {"*M", "ntlm-wb", ARG_BOOL},
  96. {"*n", "basic", ARG_BOOL},
  97. {"*o", "anyauth", ARG_BOOL},
  98. #ifdef USE_WATT32
  99. {"*p", "wdebug", ARG_BOOL},
  100. #endif
  101. {"*q", "ftp-create-dirs", ARG_BOOL},
  102. {"*r", "create-dirs", ARG_BOOL},
  103. {"*s", "max-redirs", ARG_STRING},
  104. {"*t", "proxy-ntlm", ARG_BOOL},
  105. {"*u", "crlf", ARG_BOOL},
  106. {"*v", "stderr", ARG_FILENAME},
  107. {"*w", "interface", ARG_STRING},
  108. {"*x", "krb", ARG_STRING},
  109. {"*x", "krb4", ARG_STRING},
  110. /* 'krb4' is the previous name */
  111. {"*X", "haproxy-protocol", ARG_BOOL},
  112. {"*y", "max-filesize", ARG_STRING},
  113. {"*z", "disable-eprt", ARG_BOOL},
  114. {"*Z", "eprt", ARG_BOOL},
  115. /* 'eprt' made like this to make --no-eprt and --eprt to work
  116. although --disable-eprt is the documented option */
  117. {"*~", "xattr", ARG_BOOL},
  118. {"$a", "ftp-ssl", ARG_BOOL},
  119. /* 'ftp-ssl' deprecated name since 7.20.0 */
  120. {"$a", "ssl", ARG_BOOL},
  121. /* 'ssl' new option name in 7.20.0, previously this was ftp-ssl */
  122. {"$b", "ftp-pasv", ARG_BOOL},
  123. {"$c", "socks5", ARG_STRING},
  124. {"$d", "tcp-nodelay", ARG_BOOL},
  125. {"$e", "proxy-digest", ARG_BOOL},
  126. {"$f", "proxy-basic", ARG_BOOL},
  127. {"$g", "retry", ARG_STRING},
  128. {"$V", "retry-connrefused", ARG_BOOL},
  129. {"$h", "retry-delay", ARG_STRING},
  130. {"$i", "retry-max-time", ARG_STRING},
  131. {"$k", "proxy-negotiate", ARG_BOOL},
  132. {"$m", "ftp-account", ARG_STRING},
  133. {"$n", "proxy-anyauth", ARG_BOOL},
  134. {"$o", "trace-time", ARG_BOOL},
  135. {"$p", "ignore-content-length", ARG_BOOL},
  136. {"$q", "ftp-skip-pasv-ip", ARG_BOOL},
  137. {"$r", "ftp-method", ARG_STRING},
  138. {"$s", "local-port", ARG_STRING},
  139. {"$t", "socks4", ARG_STRING},
  140. {"$T", "socks4a", ARG_STRING},
  141. {"$u", "ftp-alternative-to-user", ARG_STRING},
  142. {"$v", "ftp-ssl-reqd", ARG_BOOL},
  143. /* 'ftp-ssl-reqd' deprecated name since 7.20.0 */
  144. {"$v", "ssl-reqd", ARG_BOOL},
  145. /* 'ssl-reqd' new in 7.20.0, previously this was ftp-ssl-reqd */
  146. {"$w", "sessionid", ARG_BOOL},
  147. /* 'sessionid' listed as --no-sessionid in the help */
  148. {"$x", "ftp-ssl-control", ARG_BOOL},
  149. {"$y", "ftp-ssl-ccc", ARG_BOOL},
  150. {"$j", "ftp-ssl-ccc-mode", ARG_STRING},
  151. {"$z", "libcurl", ARG_STRING},
  152. {"$#", "raw", ARG_BOOL},
  153. {"$0", "post301", ARG_BOOL},
  154. {"$1", "keepalive", ARG_BOOL},
  155. /* 'keepalive' listed as --no-keepalive in the help */
  156. {"$2", "socks5-hostname", ARG_STRING},
  157. {"$3", "keepalive-time", ARG_STRING},
  158. {"$4", "post302", ARG_BOOL},
  159. {"$5", "noproxy", ARG_STRING},
  160. {"$7", "socks5-gssapi-nec", ARG_BOOL},
  161. {"$8", "proxy1.0", ARG_STRING},
  162. {"$9", "tftp-blksize", ARG_STRING},
  163. {"$A", "mail-from", ARG_STRING},
  164. {"$B", "mail-rcpt", ARG_STRING},
  165. {"$C", "ftp-pret", ARG_BOOL},
  166. {"$D", "proto", ARG_STRING},
  167. {"$E", "proto-redir", ARG_STRING},
  168. {"$F", "resolve", ARG_STRING},
  169. {"$G", "delegation", ARG_STRING},
  170. {"$H", "mail-auth", ARG_STRING},
  171. {"$I", "post303", ARG_BOOL},
  172. {"$J", "metalink", ARG_BOOL},
  173. {"$6", "sasl-authzid", ARG_STRING},
  174. {"$K", "sasl-ir", ARG_BOOL },
  175. {"$L", "test-event", ARG_BOOL},
  176. {"$M", "unix-socket", ARG_FILENAME},
  177. {"$N", "path-as-is", ARG_BOOL},
  178. {"$O", "socks5-gssapi-service", ARG_STRING},
  179. /* 'socks5-gssapi-service' merged with'proxy-service-name' and
  180. deprecated since 7.49.0 */
  181. {"$O", "proxy-service-name", ARG_STRING},
  182. {"$P", "service-name", ARG_STRING},
  183. {"$Q", "proto-default", ARG_STRING},
  184. {"$R", "expect100-timeout", ARG_STRING},
  185. {"$S", "tftp-no-options", ARG_BOOL},
  186. {"$U", "connect-to", ARG_STRING},
  187. {"$W", "abstract-unix-socket", ARG_FILENAME},
  188. {"$X", "tls-max", ARG_STRING},
  189. {"$Y", "suppress-connect-headers", ARG_BOOL},
  190. {"$Z", "compressed-ssh", ARG_BOOL},
  191. {"$~", "happy-eyeballs-timeout-ms", ARG_STRING},
  192. {"0", "http1.0", ARG_NONE},
  193. {"01", "http1.1", ARG_NONE},
  194. {"02", "http2", ARG_NONE},
  195. {"03", "http2-prior-knowledge", ARG_NONE},
  196. {"04", "http3", ARG_NONE},
  197. {"09", "http0.9", ARG_BOOL},
  198. {"1", "tlsv1", ARG_NONE},
  199. {"10", "tlsv1.0", ARG_NONE},
  200. {"11", "tlsv1.1", ARG_NONE},
  201. {"12", "tlsv1.2", ARG_NONE},
  202. {"13", "tlsv1.3", ARG_NONE},
  203. {"1A", "tls13-ciphers", ARG_STRING},
  204. {"1B", "proxy-tls13-ciphers", ARG_STRING},
  205. {"2", "sslv2", ARG_NONE},
  206. {"3", "sslv3", ARG_NONE},
  207. {"4", "ipv4", ARG_NONE},
  208. {"6", "ipv6", ARG_NONE},
  209. {"a", "append", ARG_BOOL},
  210. {"A", "user-agent", ARG_STRING},
  211. {"b", "cookie", ARG_STRING},
  212. {"ba", "alt-svc", ARG_STRING},
  213. {"B", "use-ascii", ARG_BOOL},
  214. {"c", "cookie-jar", ARG_STRING},
  215. {"C", "continue-at", ARG_STRING},
  216. {"d", "data", ARG_STRING},
  217. {"dr", "data-raw", ARG_STRING},
  218. {"da", "data-ascii", ARG_STRING},
  219. {"db", "data-binary", ARG_STRING},
  220. {"de", "data-urlencode", ARG_STRING},
  221. {"D", "dump-header", ARG_FILENAME},
  222. {"e", "referer", ARG_STRING},
  223. {"E", "cert", ARG_FILENAME},
  224. {"Ea", "cacert", ARG_FILENAME},
  225. {"Eb", "cert-type", ARG_STRING},
  226. {"Ec", "key", ARG_FILENAME},
  227. {"Ed", "key-type", ARG_STRING},
  228. {"Ee", "pass", ARG_STRING},
  229. {"Ef", "engine", ARG_STRING},
  230. {"Eg", "capath", ARG_FILENAME},
  231. {"Eh", "pubkey", ARG_STRING},
  232. {"Ei", "hostpubmd5", ARG_STRING},
  233. {"Ej", "crlfile", ARG_FILENAME},
  234. {"Ek", "tlsuser", ARG_STRING},
  235. {"El", "tlspassword", ARG_STRING},
  236. {"Em", "tlsauthtype", ARG_STRING},
  237. {"En", "ssl-allow-beast", ARG_BOOL},
  238. /* Eo */
  239. {"Ep", "pinnedpubkey", ARG_STRING},
  240. {"EP", "proxy-pinnedpubkey", ARG_STRING},
  241. {"Eq", "cert-status", ARG_BOOL},
  242. {"Er", "false-start", ARG_BOOL},
  243. {"Es", "ssl-no-revoke", ARG_BOOL},
  244. {"ES", "ssl-revoke-best-effort", ARG_BOOL},
  245. {"Et", "tcp-fastopen", ARG_BOOL},
  246. {"Eu", "proxy-tlsuser", ARG_STRING},
  247. {"Ev", "proxy-tlspassword", ARG_STRING},
  248. {"Ew", "proxy-tlsauthtype", ARG_STRING},
  249. {"Ex", "proxy-cert", ARG_FILENAME},
  250. {"Ey", "proxy-cert-type", ARG_STRING},
  251. {"Ez", "proxy-key", ARG_FILENAME},
  252. {"E0", "proxy-key-type", ARG_STRING},
  253. {"E1", "proxy-pass", ARG_STRING},
  254. {"E2", "proxy-ciphers", ARG_STRING},
  255. {"E3", "proxy-crlfile", ARG_FILENAME},
  256. {"E4", "proxy-ssl-allow-beast", ARG_BOOL},
  257. {"E5", "login-options", ARG_STRING},
  258. {"E6", "proxy-cacert", ARG_FILENAME},
  259. {"E7", "proxy-capath", ARG_FILENAME},
  260. {"E8", "proxy-insecure", ARG_BOOL},
  261. {"E9", "proxy-tlsv1", ARG_NONE},
  262. {"EA", "socks5-basic", ARG_BOOL},
  263. {"EB", "socks5-gssapi", ARG_BOOL},
  264. {"EC", "etag-save", ARG_FILENAME},
  265. {"ED", "etag-compare", ARG_FILENAME},
  266. {"f", "fail", ARG_BOOL},
  267. {"fa", "fail-early", ARG_BOOL},
  268. {"fb", "styled-output", ARG_BOOL},
  269. {"fc", "mail-rcpt-allowfails", ARG_BOOL},
  270. {"F", "form", ARG_STRING},
  271. {"Fs", "form-string", ARG_STRING},
  272. {"g", "globoff", ARG_BOOL},
  273. {"G", "get", ARG_NONE},
  274. {"Ga", "request-target", ARG_STRING},
  275. {"h", "help", ARG_BOOL},
  276. {"H", "header", ARG_STRING},
  277. {"Hp", "proxy-header", ARG_STRING},
  278. {"i", "include", ARG_BOOL},
  279. {"I", "head", ARG_BOOL},
  280. {"j", "junk-session-cookies", ARG_BOOL},
  281. {"J", "remote-header-name", ARG_BOOL},
  282. {"k", "insecure", ARG_BOOL},
  283. {"K", "config", ARG_FILENAME},
  284. {"l", "list-only", ARG_BOOL},
  285. {"L", "location", ARG_BOOL},
  286. {"Lt", "location-trusted", ARG_BOOL},
  287. {"m", "max-time", ARG_STRING},
  288. {"M", "manual", ARG_BOOL},
  289. {"n", "netrc", ARG_BOOL},
  290. {"no", "netrc-optional", ARG_BOOL},
  291. {"ne", "netrc-file", ARG_FILENAME},
  292. {"N", "buffer", ARG_BOOL},
  293. /* 'buffer' listed as --no-buffer in the help */
  294. {"o", "output", ARG_FILENAME},
  295. {"O", "remote-name", ARG_NONE},
  296. {"Oa", "remote-name-all", ARG_BOOL},
  297. {"p", "proxytunnel", ARG_BOOL},
  298. {"P", "ftp-port", ARG_STRING},
  299. {"q", "disable", ARG_BOOL},
  300. {"Q", "quote", ARG_STRING},
  301. {"r", "range", ARG_STRING},
  302. {"R", "remote-time", ARG_BOOL},
  303. {"s", "silent", ARG_BOOL},
  304. {"S", "show-error", ARG_BOOL},
  305. {"t", "telnet-option", ARG_STRING},
  306. {"T", "upload-file", ARG_FILENAME},
  307. {"u", "user", ARG_STRING},
  308. {"U", "proxy-user", ARG_STRING},
  309. {"v", "verbose", ARG_BOOL},
  310. {"V", "version", ARG_BOOL},
  311. {"w", "write-out", ARG_STRING},
  312. {"x", "proxy", ARG_STRING},
  313. {"xa", "preproxy", ARG_STRING},
  314. {"X", "request", ARG_STRING},
  315. {"Y", "speed-limit", ARG_STRING},
  316. {"y", "speed-time", ARG_STRING},
  317. {"z", "time-cond", ARG_STRING},
  318. {"Z", "parallel", ARG_BOOL},
  319. {"Zb", "parallel-max", ARG_STRING},
  320. {"Zc", "parallel-immediate", ARG_BOOL},
  321. {"#", "progress-bar", ARG_BOOL},
  322. {"#m", "progress-meter", ARG_BOOL},
  323. {":", "next", ARG_NONE},
  324. };
  325. /* Split the argument of -E to 'certname' and 'passphrase' separated by colon.
  326. * We allow ':' and '\' to be escaped by '\' so that we can use certificate
  327. * nicknames containing ':'. See <https://sourceforge.net/p/curl/bugs/1196/>
  328. * for details. */
  329. #ifndef UNITTESTS
  330. static
  331. #endif
  332. void parse_cert_parameter(const char *cert_parameter,
  333. char **certname,
  334. char **passphrase)
  335. {
  336. size_t param_length = strlen(cert_parameter);
  337. size_t span;
  338. const char *param_place = NULL;
  339. char *certname_place = NULL;
  340. *certname = NULL;
  341. *passphrase = NULL;
  342. /* most trivial assumption: cert_parameter is empty */
  343. if(param_length == 0)
  344. return;
  345. /* next less trivial: cert_parameter starts 'pkcs11:' and thus
  346. * looks like a RFC7512 PKCS#11 URI which can be used as-is.
  347. * Also if cert_parameter contains no colon nor backslash, this
  348. * means no passphrase was given and no characters escaped */
  349. if(curl_strnequal(cert_parameter, "pkcs11:", 7) ||
  350. !strpbrk(cert_parameter, ":\\")) {
  351. *certname = strdup(cert_parameter);
  352. return;
  353. }
  354. /* deal with escaped chars; find unescaped colon if it exists */
  355. certname_place = malloc(param_length + 1);
  356. if(!certname_place)
  357. return;
  358. *certname = certname_place;
  359. param_place = cert_parameter;
  360. while(*param_place) {
  361. span = strcspn(param_place, ":\\");
  362. strncpy(certname_place, param_place, span);
  363. param_place += span;
  364. certname_place += span;
  365. /* we just ate all the non-special chars. now we're on either a special
  366. * char or the end of the string. */
  367. switch(*param_place) {
  368. case '\0':
  369. break;
  370. case '\\':
  371. param_place++;
  372. switch(*param_place) {
  373. case '\0':
  374. *certname_place++ = '\\';
  375. break;
  376. case '\\':
  377. *certname_place++ = '\\';
  378. param_place++;
  379. break;
  380. case ':':
  381. *certname_place++ = ':';
  382. param_place++;
  383. break;
  384. default:
  385. *certname_place++ = '\\';
  386. *certname_place++ = *param_place;
  387. param_place++;
  388. break;
  389. }
  390. break;
  391. case ':':
  392. /* Since we live in a world of weirdness and confusion, the win32
  393. dudes can use : when using drive letters and thus c:\file:password
  394. needs to work. In order not to break compatibility, we still use : as
  395. separator, but we try to detect when it is used for a file name! On
  396. windows. */
  397. #ifdef WIN32
  398. if(param_place &&
  399. (param_place == &cert_parameter[1]) &&
  400. (cert_parameter[2] == '\\' || cert_parameter[2] == '/') &&
  401. (ISALPHA(cert_parameter[0])) ) {
  402. /* colon in the second column, followed by a backslash, and the
  403. first character is an alphabetic letter:
  404. this is a drive letter colon */
  405. *certname_place++ = ':';
  406. param_place++;
  407. break;
  408. }
  409. #endif
  410. /* escaped colons and Windows drive letter colons were handled
  411. * above; if we're still here, this is a separating colon */
  412. param_place++;
  413. if(*param_place) {
  414. *passphrase = strdup(param_place);
  415. }
  416. goto done;
  417. }
  418. }
  419. done:
  420. *certname_place = '\0';
  421. }
  422. static void
  423. GetFileAndPassword(char *nextarg, char **file, char **password)
  424. {
  425. char *certname, *passphrase;
  426. parse_cert_parameter(nextarg, &certname, &passphrase);
  427. Curl_safefree(*file);
  428. *file = certname;
  429. if(passphrase) {
  430. Curl_safefree(*password);
  431. *password = passphrase;
  432. }
  433. cleanarg(nextarg);
  434. }
  435. /* Get a size parameter for '--limit-rate' or '--max-filesize'.
  436. * We support a 'G', 'M' or 'K' suffix too.
  437. */
  438. static ParameterError GetSizeParameter(struct GlobalConfig *global,
  439. const char *arg,
  440. const char *which,
  441. curl_off_t *value_out)
  442. {
  443. char *unit;
  444. curl_off_t value;
  445. if(curlx_strtoofft(arg, &unit, 0, &value)) {
  446. warnf(global, "invalid number specified for %s\n", which);
  447. return PARAM_BAD_USE;
  448. }
  449. if(!*unit)
  450. unit = (char *)"b";
  451. else if(strlen(unit) > 1)
  452. unit = (char *)"w"; /* unsupported */
  453. switch(*unit) {
  454. case 'G':
  455. case 'g':
  456. if(value > (CURL_OFF_T_MAX / (1024*1024*1024)))
  457. return PARAM_NUMBER_TOO_LARGE;
  458. value *= 1024*1024*1024;
  459. break;
  460. case 'M':
  461. case 'm':
  462. if(value > (CURL_OFF_T_MAX / (1024*1024)))
  463. return PARAM_NUMBER_TOO_LARGE;
  464. value *= 1024*1024;
  465. break;
  466. case 'K':
  467. case 'k':
  468. if(value > (CURL_OFF_T_MAX / 1024))
  469. return PARAM_NUMBER_TOO_LARGE;
  470. value *= 1024;
  471. break;
  472. case 'b':
  473. case 'B':
  474. /* for plain bytes, leave as-is */
  475. break;
  476. default:
  477. warnf(global, "unsupported %s unit. Use G, M, K or B!\n", which);
  478. return PARAM_BAD_USE;
  479. }
  480. *value_out = value;
  481. return PARAM_OK;
  482. }
  483. ParameterError getparameter(const char *flag, /* f or -long-flag */
  484. char *nextarg, /* NULL if unset */
  485. bool *usedarg, /* set to TRUE if the arg
  486. has been used */
  487. struct GlobalConfig *global,
  488. struct OperationConfig *config)
  489. {
  490. char letter;
  491. char subletter = '\0'; /* subletters can only occur on long options */
  492. int rc;
  493. const char *parse = NULL;
  494. unsigned int j;
  495. time_t now;
  496. int hit = -1;
  497. bool longopt = FALSE;
  498. bool singleopt = FALSE; /* when true means '-o foo' used '-ofoo' */
  499. ParameterError err;
  500. bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
  501. by using --OPTION or --no-OPTION */
  502. *usedarg = FALSE; /* default is that we don't use the arg */
  503. if(('-' != flag[0]) || ('-' == flag[1])) {
  504. /* this should be a long name */
  505. const char *word = ('-' == flag[0]) ? flag + 2 : flag;
  506. size_t fnam = strlen(word);
  507. int numhits = 0;
  508. bool noflagged = FALSE;
  509. if(!strncmp(word, "no-", 3)) {
  510. /* disable this option but ignore the "no-" part when looking for it */
  511. word += 3;
  512. toggle = FALSE;
  513. noflagged = TRUE;
  514. }
  515. for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
  516. if(curl_strnequal(aliases[j].lname, word, fnam)) {
  517. longopt = TRUE;
  518. numhits++;
  519. if(curl_strequal(aliases[j].lname, word)) {
  520. parse = aliases[j].letter;
  521. hit = j;
  522. numhits = 1; /* a single unique hit */
  523. break;
  524. }
  525. parse = aliases[j].letter;
  526. hit = j;
  527. }
  528. }
  529. if(numhits > 1) {
  530. /* this is at least the second match! */
  531. return PARAM_OPTION_AMBIGUOUS;
  532. }
  533. if(hit < 0) {
  534. return PARAM_OPTION_UNKNOWN;
  535. }
  536. if(noflagged && (aliases[hit].desc != ARG_BOOL))
  537. /* --no- prefixed an option that isn't boolean! */
  538. return PARAM_NO_NOT_BOOLEAN;
  539. }
  540. else {
  541. flag++; /* prefixed with one dash, pass it */
  542. hit = -1;
  543. parse = flag;
  544. }
  545. do {
  546. /* we can loop here if we have multiple single-letters */
  547. if(!longopt) {
  548. letter = (char)*parse;
  549. subletter = '\0';
  550. }
  551. else {
  552. letter = parse[0];
  553. subletter = parse[1];
  554. }
  555. if(hit < 0) {
  556. for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
  557. if(letter == aliases[j].letter[0]) {
  558. hit = j;
  559. break;
  560. }
  561. }
  562. if(hit < 0) {
  563. return PARAM_OPTION_UNKNOWN;
  564. }
  565. }
  566. if(aliases[hit].desc >= ARG_STRING) {
  567. /* this option requires an extra parameter */
  568. if(!longopt && parse[1]) {
  569. nextarg = (char *)&parse[1]; /* this is the actual extra parameter */
  570. singleopt = TRUE; /* don't loop anymore after this */
  571. }
  572. else if(!nextarg)
  573. return PARAM_REQUIRES_PARAMETER;
  574. else
  575. *usedarg = TRUE; /* mark it as used */
  576. if((aliases[hit].desc == ARG_FILENAME) &&
  577. (nextarg[0] == '-') && nextarg[1]) {
  578. /* if the file name looks like a command line option */
  579. warnf(global, "The file name argument '%s' looks like a flag.\n",
  580. nextarg);
  581. }
  582. }
  583. else if((aliases[hit].desc == ARG_NONE) && !toggle)
  584. return PARAM_NO_PREFIX;
  585. switch(letter) {
  586. case '*': /* options without a short option */
  587. switch(subletter) {
  588. case '4': /* --dns-ipv4-addr */
  589. /* addr in dot notation */
  590. GetStr(&config->dns_ipv4_addr, nextarg);
  591. break;
  592. case '6': /* --dns-ipv6-addr */
  593. /* addr in dot notation */
  594. GetStr(&config->dns_ipv6_addr, nextarg);
  595. break;
  596. case 'a': /* random-file */
  597. GetStr(&config->random_file, nextarg);
  598. break;
  599. case 'b': /* egd-file */
  600. GetStr(&config->egd_file, nextarg);
  601. break;
  602. case 'B': /* OAuth 2.0 bearer token */
  603. GetStr(&config->oauth_bearer, nextarg);
  604. config->authtype |= CURLAUTH_BEARER;
  605. break;
  606. case 'c': /* connect-timeout */
  607. err = str2udouble(&config->connecttimeout, nextarg,
  608. LONG_MAX/1000);
  609. if(err)
  610. return err;
  611. break;
  612. case 'C': /* doh-url */
  613. GetStr(&config->doh_url, nextarg);
  614. break;
  615. case 'd': /* ciphers */
  616. GetStr(&config->cipher_list, nextarg);
  617. break;
  618. case 'D': /* --dns-interface */
  619. /* interface name */
  620. GetStr(&config->dns_interface, nextarg);
  621. break;
  622. case 'e': /* --disable-epsv */
  623. config->disable_epsv = toggle;
  624. break;
  625. case 'f': /* --disallow-username-in-url */
  626. config->disallow_username_in_url = toggle;
  627. break;
  628. case 'E': /* --epsv */
  629. config->disable_epsv = (!toggle)?TRUE:FALSE;
  630. break;
  631. case 'F': /* --dns-servers */
  632. /* IP addrs of DNS servers */
  633. GetStr(&config->dns_servers, nextarg);
  634. break;
  635. case 'g': /* --trace */
  636. GetStr(&global->trace_dump, nextarg);
  637. if(global->tracetype && (global->tracetype != TRACE_BIN))
  638. warnf(global, "--trace overrides an earlier trace/verbose option\n");
  639. global->tracetype = TRACE_BIN;
  640. break;
  641. case 'G': /* --npn */
  642. config->nonpn = (!toggle)?TRUE:FALSE;
  643. break;
  644. case 'h': /* --trace-ascii */
  645. GetStr(&global->trace_dump, nextarg);
  646. if(global->tracetype && (global->tracetype != TRACE_ASCII))
  647. warnf(global,
  648. "--trace-ascii overrides an earlier trace/verbose option\n");
  649. global->tracetype = TRACE_ASCII;
  650. break;
  651. case 'H': /* --alpn */
  652. config->noalpn = (!toggle)?TRUE:FALSE;
  653. break;
  654. case 'i': /* --limit-rate */
  655. {
  656. curl_off_t value;
  657. ParameterError pe = GetSizeParameter(global, nextarg, "rate", &value);
  658. if(pe != PARAM_OK)
  659. return pe;
  660. config->recvpersecond = value;
  661. config->sendpersecond = value;
  662. }
  663. break;
  664. case 'j': /* --compressed */
  665. if(toggle &&
  666. !(curlinfo->features & (CURL_VERSION_LIBZ | CURL_VERSION_BROTLI)))
  667. return PARAM_LIBCURL_DOESNT_SUPPORT;
  668. config->encoding = toggle;
  669. break;
  670. case 'J': /* --tr-encoding */
  671. config->tr_encoding = toggle;
  672. break;
  673. case 'k': /* --digest */
  674. if(toggle)
  675. config->authtype |= CURLAUTH_DIGEST;
  676. else
  677. config->authtype &= ~CURLAUTH_DIGEST;
  678. break;
  679. case 'l': /* --negotiate */
  680. if(toggle) {
  681. if(curlinfo->features & CURL_VERSION_SPNEGO)
  682. config->authtype |= CURLAUTH_NEGOTIATE;
  683. else
  684. return PARAM_LIBCURL_DOESNT_SUPPORT;
  685. }
  686. else
  687. config->authtype &= ~CURLAUTH_NEGOTIATE;
  688. break;
  689. case 'm': /* --ntlm */
  690. if(toggle) {
  691. if(curlinfo->features & CURL_VERSION_NTLM)
  692. config->authtype |= CURLAUTH_NTLM;
  693. else
  694. return PARAM_LIBCURL_DOESNT_SUPPORT;
  695. }
  696. else
  697. config->authtype &= ~CURLAUTH_NTLM;
  698. break;
  699. case 'M': /* --ntlm-wb */
  700. if(toggle) {
  701. if(curlinfo->features & CURL_VERSION_NTLM_WB)
  702. config->authtype |= CURLAUTH_NTLM_WB;
  703. else
  704. return PARAM_LIBCURL_DOESNT_SUPPORT;
  705. }
  706. else
  707. config->authtype &= ~CURLAUTH_NTLM_WB;
  708. break;
  709. case 'n': /* --basic for completeness */
  710. if(toggle)
  711. config->authtype |= CURLAUTH_BASIC;
  712. else
  713. config->authtype &= ~CURLAUTH_BASIC;
  714. break;
  715. case 'o': /* --anyauth, let libcurl pick it */
  716. if(toggle)
  717. config->authtype = CURLAUTH_ANY;
  718. /* --no-anyauth simply doesn't touch it */
  719. break;
  720. #ifdef USE_WATT32
  721. case 'p': /* --wdebug */
  722. dbug_init();
  723. break;
  724. #endif
  725. case 'q': /* --ftp-create-dirs */
  726. config->ftp_create_dirs = toggle;
  727. break;
  728. case 'r': /* --create-dirs */
  729. config->create_dirs = toggle;
  730. break;
  731. case 's': /* --max-redirs */
  732. /* specified max no of redirects (http(s)), this accepts -1 as a
  733. special condition */
  734. err = str2num(&config->maxredirs, nextarg);
  735. if(err)
  736. return err;
  737. if(config->maxredirs < -1)
  738. return PARAM_BAD_NUMERIC;
  739. break;
  740. case 't': /* --proxy-ntlm */
  741. if(curlinfo->features & CURL_VERSION_NTLM)
  742. config->proxyntlm = toggle;
  743. else
  744. return PARAM_LIBCURL_DOESNT_SUPPORT;
  745. break;
  746. case 'u': /* --crlf */
  747. /* LF -> CRLF conversion? */
  748. config->crlf = toggle;
  749. break;
  750. case 'v': /* --stderr */
  751. if(strcmp(nextarg, "-")) {
  752. FILE *newfile = fopen(nextarg, FOPEN_WRITETEXT);
  753. if(!newfile)
  754. warnf(global, "Failed to open %s!\n", nextarg);
  755. else {
  756. if(global->errors_fopened)
  757. fclose(global->errors);
  758. global->errors = newfile;
  759. global->errors_fopened = TRUE;
  760. }
  761. }
  762. else
  763. global->errors = stdout;
  764. break;
  765. case 'w': /* --interface */
  766. /* interface */
  767. GetStr(&config->iface, nextarg);
  768. break;
  769. case 'x': /* --krb */
  770. /* kerberos level string */
  771. if(curlinfo->features & CURL_VERSION_KERBEROS4)
  772. GetStr(&config->krblevel, nextarg);
  773. else
  774. return PARAM_LIBCURL_DOESNT_SUPPORT;
  775. break;
  776. case 'X': /* --haproxy-protocol */
  777. config->haproxy_protocol = toggle;
  778. break;
  779. case 'y': /* --max-filesize */
  780. {
  781. curl_off_t value;
  782. ParameterError pe =
  783. GetSizeParameter(global, nextarg, "max-filesize", &value);
  784. if(pe != PARAM_OK)
  785. return pe;
  786. config->max_filesize = value;
  787. }
  788. break;
  789. case 'z': /* --disable-eprt */
  790. config->disable_eprt = toggle;
  791. break;
  792. case 'Z': /* --eprt */
  793. config->disable_eprt = (!toggle)?TRUE:FALSE;
  794. break;
  795. case '~': /* --xattr */
  796. config->xattr = toggle;
  797. break;
  798. case '@': /* the URL! */
  799. {
  800. struct getout *url;
  801. if(!config->url_get)
  802. config->url_get = config->url_list;
  803. if(config->url_get) {
  804. /* there's a node here, if it already is filled-in continue to find
  805. an "empty" node */
  806. while(config->url_get && (config->url_get->flags & GETOUT_URL))
  807. config->url_get = config->url_get->next;
  808. }
  809. /* now there might or might not be an available node to fill in! */
  810. if(config->url_get)
  811. /* existing node */
  812. url = config->url_get;
  813. else
  814. /* there was no free node, create one! */
  815. config->url_get = url = new_getout(config);
  816. if(!url)
  817. return PARAM_NO_MEM;
  818. /* fill in the URL */
  819. GetStr(&url->url, nextarg);
  820. url->flags |= GETOUT_URL;
  821. }
  822. }
  823. break;
  824. case '$': /* more options without a short option */
  825. switch(subletter) {
  826. case 'a': /* --ssl */
  827. if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
  828. return PARAM_LIBCURL_DOESNT_SUPPORT;
  829. config->ftp_ssl = toggle;
  830. break;
  831. case 'b': /* --ftp-pasv */
  832. Curl_safefree(config->ftpport);
  833. break;
  834. case 'c': /* --socks5 specifies a socks5 proxy to use, and resolves
  835. the name locally and passes on the resolved address */
  836. GetStr(&config->proxy, nextarg);
  837. config->proxyver = CURLPROXY_SOCKS5;
  838. break;
  839. case 't': /* --socks4 specifies a socks4 proxy to use */
  840. GetStr(&config->proxy, nextarg);
  841. config->proxyver = CURLPROXY_SOCKS4;
  842. break;
  843. case 'T': /* --socks4a specifies a socks4a proxy to use */
  844. GetStr(&config->proxy, nextarg);
  845. config->proxyver = CURLPROXY_SOCKS4A;
  846. break;
  847. case '2': /* --socks5-hostname specifies a socks5 proxy and enables name
  848. resolving with the proxy */
  849. GetStr(&config->proxy, nextarg);
  850. config->proxyver = CURLPROXY_SOCKS5_HOSTNAME;
  851. break;
  852. case 'd': /* --tcp-nodelay option */
  853. config->tcp_nodelay = toggle;
  854. break;
  855. case 'e': /* --proxy-digest */
  856. config->proxydigest = toggle;
  857. break;
  858. case 'f': /* --proxy-basic */
  859. config->proxybasic = toggle;
  860. break;
  861. case 'g': /* --retry */
  862. err = str2unum(&config->req_retry, nextarg);
  863. if(err)
  864. return err;
  865. break;
  866. case 'V': /* --retry-connrefused */
  867. config->retry_connrefused = toggle;
  868. break;
  869. case 'h': /* --retry-delay */
  870. err = str2unummax(&config->retry_delay, nextarg, LONG_MAX/1000);
  871. if(err)
  872. return err;
  873. break;
  874. case 'i': /* --retry-max-time */
  875. err = str2unummax(&config->retry_maxtime, nextarg, LONG_MAX/1000);
  876. if(err)
  877. return err;
  878. break;
  879. case 'k': /* --proxy-negotiate */
  880. if(curlinfo->features & CURL_VERSION_SPNEGO)
  881. config->proxynegotiate = toggle;
  882. else
  883. return PARAM_LIBCURL_DOESNT_SUPPORT;
  884. break;
  885. case 'm': /* --ftp-account */
  886. GetStr(&config->ftp_account, nextarg);
  887. break;
  888. case 'n': /* --proxy-anyauth */
  889. config->proxyanyauth = toggle;
  890. break;
  891. case 'o': /* --trace-time */
  892. global->tracetime = toggle;
  893. break;
  894. case 'p': /* --ignore-content-length */
  895. config->ignorecl = toggle;
  896. break;
  897. case 'q': /* --ftp-skip-pasv-ip */
  898. config->ftp_skip_ip = toggle;
  899. break;
  900. case 'r': /* --ftp-method (undocumented at this point) */
  901. config->ftp_filemethod = ftpfilemethod(config, nextarg);
  902. break;
  903. case 's': { /* --local-port */
  904. char lrange[7]; /* 16bit base 10 is 5 digits, but we allow 6 so that
  905. this catches overflows, not just truncates */
  906. char *p = nextarg;
  907. while(ISDIGIT(*p))
  908. p++;
  909. if(*p) {
  910. /* if there's anything more than a plain decimal number */
  911. rc = sscanf(p, " - %6s", lrange);
  912. *p = 0; /* zero terminate to make str2unum() work below */
  913. }
  914. else
  915. rc = 0;
  916. err = str2unum(&config->localport, nextarg);
  917. if(err || (config->localport > 65535))
  918. return PARAM_BAD_USE;
  919. if(!rc)
  920. config->localportrange = 1; /* default number of ports to try */
  921. else {
  922. err = str2unum(&config->localportrange, lrange);
  923. if(err || (config->localportrange > 65535))
  924. return PARAM_BAD_USE;
  925. config->localportrange -= (config->localport-1);
  926. if(config->localportrange < 1)
  927. return PARAM_BAD_USE;
  928. }
  929. break;
  930. }
  931. case 'u': /* --ftp-alternative-to-user */
  932. GetStr(&config->ftp_alternative_to_user, nextarg);
  933. break;
  934. case 'v': /* --ssl-reqd */
  935. if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
  936. return PARAM_LIBCURL_DOESNT_SUPPORT;
  937. config->ftp_ssl_reqd = toggle;
  938. break;
  939. case 'w': /* --no-sessionid */
  940. config->disable_sessionid = (!toggle)?TRUE:FALSE;
  941. break;
  942. case 'x': /* --ftp-ssl-control */
  943. if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
  944. return PARAM_LIBCURL_DOESNT_SUPPORT;
  945. config->ftp_ssl_control = toggle;
  946. break;
  947. case 'y': /* --ftp-ssl-ccc */
  948. config->ftp_ssl_ccc = toggle;
  949. if(!config->ftp_ssl_ccc_mode)
  950. config->ftp_ssl_ccc_mode = CURLFTPSSL_CCC_PASSIVE;
  951. break;
  952. case 'j': /* --ftp-ssl-ccc-mode */
  953. config->ftp_ssl_ccc = TRUE;
  954. config->ftp_ssl_ccc_mode = ftpcccmethod(config, nextarg);
  955. break;
  956. case 'z': /* --libcurl */
  957. #ifdef CURL_DISABLE_LIBCURL_OPTION
  958. warnf(global,
  959. "--libcurl option was disabled at build-time!\n");
  960. return PARAM_OPTION_UNKNOWN;
  961. #else
  962. GetStr(&global->libcurl, nextarg);
  963. break;
  964. #endif
  965. case '#': /* --raw */
  966. config->raw = toggle;
  967. break;
  968. case '0': /* --post301 */
  969. config->post301 = toggle;
  970. break;
  971. case '1': /* --no-keepalive */
  972. config->nokeepalive = (!toggle)?TRUE:FALSE;
  973. break;
  974. case '3': /* --keepalive-time */
  975. err = str2unum(&config->alivetime, nextarg);
  976. if(err)
  977. return err;
  978. break;
  979. case '4': /* --post302 */
  980. config->post302 = toggle;
  981. break;
  982. case 'I': /* --post303 */
  983. config->post303 = toggle;
  984. break;
  985. case '5': /* --noproxy */
  986. /* This specifies the noproxy list */
  987. GetStr(&config->noproxy, nextarg);
  988. break;
  989. case '7': /* --socks5-gssapi-nec*/
  990. config->socks5_gssapi_nec = toggle;
  991. break;
  992. case '8': /* --proxy1.0 */
  993. /* http 1.0 proxy */
  994. GetStr(&config->proxy, nextarg);
  995. config->proxyver = CURLPROXY_HTTP_1_0;
  996. break;
  997. case '9': /* --tftp-blksize */
  998. err = str2unum(&config->tftp_blksize, nextarg);
  999. if(err)
  1000. return err;
  1001. break;
  1002. case 'A': /* --mail-from */
  1003. GetStr(&config->mail_from, nextarg);
  1004. break;
  1005. case 'B': /* --mail-rcpt */
  1006. /* append receiver to a list */
  1007. err = add2list(&config->mail_rcpt, nextarg);
  1008. if(err)
  1009. return err;
  1010. break;
  1011. case 'C': /* --ftp-pret */
  1012. config->ftp_pret = toggle;
  1013. break;
  1014. case 'D': /* --proto */
  1015. config->proto_present = TRUE;
  1016. if(proto2num(config, &config->proto, nextarg))
  1017. return PARAM_BAD_USE;
  1018. break;
  1019. case 'E': /* --proto-redir */
  1020. config->proto_redir_present = TRUE;
  1021. if(proto2num(config, &config->proto_redir, nextarg))
  1022. return PARAM_BAD_USE;
  1023. break;
  1024. case 'F': /* --resolve */
  1025. err = add2list(&config->resolve, nextarg);
  1026. if(err)
  1027. return err;
  1028. break;
  1029. case 'G': /* --delegation LEVEL */
  1030. config->gssapi_delegation = delegation(config, nextarg);
  1031. break;
  1032. case 'H': /* --mail-auth */
  1033. GetStr(&config->mail_auth, nextarg);
  1034. break;
  1035. case 'J': /* --metalink */
  1036. {
  1037. #ifdef USE_METALINK
  1038. int mlmaj, mlmin, mlpatch;
  1039. metalink_get_version(&mlmaj, &mlmin, &mlpatch);
  1040. if((mlmaj*10000)+(mlmin*100) + mlpatch < CURL_REQ_LIBMETALINK_VERS) {
  1041. warnf(global,
  1042. "--metalink option cannot be used because the version of "
  1043. "the linked libmetalink library is too old. "
  1044. "Required: %d.%d.%d, found %d.%d.%d\n",
  1045. CURL_REQ_LIBMETALINK_MAJOR,
  1046. CURL_REQ_LIBMETALINK_MINOR,
  1047. CURL_REQ_LIBMETALINK_PATCH,
  1048. mlmaj, mlmin, mlpatch);
  1049. return PARAM_BAD_USE;
  1050. }
  1051. else
  1052. config->use_metalink = toggle;
  1053. #else
  1054. warnf(global, "--metalink option is ignored because the binary is "
  1055. "built without the Metalink support.\n");
  1056. #endif
  1057. break;
  1058. }
  1059. case '6': /* --sasl-authzid */
  1060. GetStr(&config->sasl_authzid, nextarg);
  1061. break;
  1062. case 'K': /* --sasl-ir */
  1063. config->sasl_ir = toggle;
  1064. break;
  1065. case 'L': /* --test-event */
  1066. #ifdef CURLDEBUG
  1067. global->test_event_based = toggle;
  1068. #else
  1069. warnf(global, "--test-event is ignored unless a debug build!\n");
  1070. #endif
  1071. break;
  1072. case 'M': /* --unix-socket */
  1073. config->abstract_unix_socket = FALSE;
  1074. GetStr(&config->unix_socket_path, nextarg);
  1075. break;
  1076. case 'N': /* --path-as-is */
  1077. config->path_as_is = toggle;
  1078. break;
  1079. case 'O': /* --proxy-service-name */
  1080. GetStr(&config->proxy_service_name, nextarg);
  1081. break;
  1082. case 'P': /* --service-name */
  1083. GetStr(&config->service_name, nextarg);
  1084. break;
  1085. case 'Q': /* --proto-default */
  1086. GetStr(&config->proto_default, nextarg);
  1087. err = check_protocol(config->proto_default);
  1088. if(err)
  1089. return err;
  1090. break;
  1091. case 'R': /* --expect100-timeout */
  1092. err = str2udouble(&config->expect100timeout, nextarg, LONG_MAX/1000);
  1093. if(err)
  1094. return err;
  1095. break;
  1096. case 'S': /* --tftp-no-options */
  1097. config->tftp_no_options = toggle;
  1098. break;
  1099. case 'U': /* --connect-to */
  1100. err = add2list(&config->connect_to, nextarg);
  1101. if(err)
  1102. return err;
  1103. break;
  1104. case 'W': /* --abstract-unix-socket */
  1105. config->abstract_unix_socket = TRUE;
  1106. GetStr(&config->unix_socket_path, nextarg);
  1107. break;
  1108. case 'X': /* --tls-max */
  1109. err = str2tls_max(&config->ssl_version_max, nextarg);
  1110. if(err)
  1111. return err;
  1112. break;
  1113. case 'Y': /* --suppress-connect-headers */
  1114. config->suppress_connect_headers = toggle;
  1115. break;
  1116. case 'Z': /* --compressed-ssh */
  1117. config->ssh_compression = toggle;
  1118. break;
  1119. case '~': /* --happy-eyeballs-timeout-ms */
  1120. err = str2unum(&config->happy_eyeballs_timeout_ms, nextarg);
  1121. if(err)
  1122. return err;
  1123. /* 0 is a valid value for this timeout */
  1124. break;
  1125. }
  1126. break;
  1127. case '#':
  1128. switch(subletter) {
  1129. case 'm': /* --progress-meter */
  1130. global->noprogress = !toggle;
  1131. break;
  1132. default: /* --progress-bar */
  1133. global->progressmode =
  1134. toggle ? CURL_PROGRESS_BAR : CURL_PROGRESS_STATS;
  1135. break;
  1136. }
  1137. break;
  1138. case ':': /* --next */
  1139. return PARAM_NEXT_OPERATION;
  1140. case '0': /* --http* options */
  1141. switch(subletter) {
  1142. case '\0':
  1143. /* HTTP version 1.0 */
  1144. config->httpversion = CURL_HTTP_VERSION_1_0;
  1145. break;
  1146. case '1':
  1147. /* HTTP version 1.1 */
  1148. config->httpversion = CURL_HTTP_VERSION_1_1;
  1149. break;
  1150. case '2':
  1151. /* HTTP version 2.0 */
  1152. config->httpversion = CURL_HTTP_VERSION_2_0;
  1153. break;
  1154. case '3': /* --http2-prior-knowledge */
  1155. /* HTTP version 2.0 over clean TCP*/
  1156. config->httpversion = CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE;
  1157. break;
  1158. case '4': /* --http3 */
  1159. /* HTTP version 3 go over QUIC - at once */
  1160. if(curlinfo->features & CURL_VERSION_HTTP3)
  1161. config->httpversion = CURL_HTTP_VERSION_3;
  1162. else
  1163. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1164. break;
  1165. case '9':
  1166. /* Allow HTTP/0.9 responses! */
  1167. config->http09_allowed = toggle;
  1168. break;
  1169. }
  1170. break;
  1171. case '1': /* --tlsv1* options */
  1172. switch(subletter) {
  1173. case '\0':
  1174. /* TLS version 1.x */
  1175. config->ssl_version = CURL_SSLVERSION_TLSv1;
  1176. break;
  1177. case '0':
  1178. /* TLS version 1.0 */
  1179. config->ssl_version = CURL_SSLVERSION_TLSv1_0;
  1180. break;
  1181. case '1':
  1182. /* TLS version 1.1 */
  1183. config->ssl_version = CURL_SSLVERSION_TLSv1_1;
  1184. break;
  1185. case '2':
  1186. /* TLS version 1.2 */
  1187. config->ssl_version = CURL_SSLVERSION_TLSv1_2;
  1188. break;
  1189. case '3':
  1190. /* TLS version 1.3 */
  1191. config->ssl_version = CURL_SSLVERSION_TLSv1_3;
  1192. break;
  1193. case 'A': /* --tls13-ciphers */
  1194. GetStr(&config->cipher13_list, nextarg);
  1195. break;
  1196. case 'B': /* --proxy-tls13-ciphers */
  1197. GetStr(&config->proxy_cipher13_list, nextarg);
  1198. break;
  1199. }
  1200. break;
  1201. case '2':
  1202. /* SSL version 2 */
  1203. config->ssl_version = CURL_SSLVERSION_SSLv2;
  1204. break;
  1205. case '3':
  1206. /* SSL version 3 */
  1207. config->ssl_version = CURL_SSLVERSION_SSLv3;
  1208. break;
  1209. case '4':
  1210. /* IPv4 */
  1211. config->ip_version = 4;
  1212. break;
  1213. case '6':
  1214. /* IPv6 */
  1215. config->ip_version = 6;
  1216. break;
  1217. case 'a':
  1218. /* This makes the FTP sessions use APPE instead of STOR */
  1219. config->ftp_append = toggle;
  1220. break;
  1221. case 'A':
  1222. /* This specifies the User-Agent name */
  1223. GetStr(&config->useragent, nextarg);
  1224. break;
  1225. case 'b':
  1226. switch(subletter) {
  1227. case 'a': /* --alt-svc */
  1228. if(curlinfo->features & CURL_VERSION_ALTSVC)
  1229. GetStr(&config->altsvc, nextarg);
  1230. else
  1231. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1232. break;
  1233. default: /* --cookie string coming up: */
  1234. if(nextarg[0] == '@') {
  1235. nextarg++;
  1236. }
  1237. else if(strchr(nextarg, '=')) {
  1238. /* A cookie string must have a =-letter */
  1239. GetStr(&config->cookie, nextarg);
  1240. break;
  1241. }
  1242. /* We have a cookie file to read from! */
  1243. GetStr(&config->cookiefile, nextarg);
  1244. }
  1245. break;
  1246. case 'B':
  1247. /* use ASCII/text when transferring */
  1248. config->use_ascii = toggle;
  1249. break;
  1250. case 'c':
  1251. /* get the file name to dump all cookies in */
  1252. GetStr(&config->cookiejar, nextarg);
  1253. break;
  1254. case 'C':
  1255. /* This makes us continue an ftp transfer at given position */
  1256. if(strcmp(nextarg, "-")) {
  1257. err = str2offset(&config->resume_from, nextarg);
  1258. if(err)
  1259. return err;
  1260. config->resume_from_current = FALSE;
  1261. }
  1262. else {
  1263. config->resume_from_current = TRUE;
  1264. config->resume_from = 0;
  1265. }
  1266. config->use_resume = TRUE;
  1267. break;
  1268. case 'd':
  1269. /* postfield data */
  1270. {
  1271. char *postdata = NULL;
  1272. FILE *file;
  1273. size_t size = 0;
  1274. bool raw_mode = (subletter == 'r');
  1275. if(subletter == 'e') { /* --data-urlencode*/
  1276. /* [name]=[content], we encode the content part only
  1277. * [name]@[file name]
  1278. *
  1279. * Case 2: we first load the file using that name and then encode
  1280. * the content.
  1281. */
  1282. const char *p = strchr(nextarg, '=');
  1283. size_t nlen;
  1284. char is_file;
  1285. if(!p)
  1286. /* there was no '=' letter, check for a '@' instead */
  1287. p = strchr(nextarg, '@');
  1288. if(p) {
  1289. nlen = p - nextarg; /* length of the name part */
  1290. is_file = *p++; /* pass the separator */
  1291. }
  1292. else {
  1293. /* neither @ nor =, so no name and it isn't a file */
  1294. nlen = is_file = 0;
  1295. p = nextarg;
  1296. }
  1297. if('@' == is_file) {
  1298. /* a '@' letter, it means that a file name or - (stdin) follows */
  1299. if(!strcmp("-", p)) {
  1300. file = stdin;
  1301. set_binmode(stdin);
  1302. }
  1303. else {
  1304. file = fopen(p, "rb");
  1305. if(!file)
  1306. warnf(global,
  1307. "Couldn't read data from file \"%s\", this makes "
  1308. "an empty POST.\n", nextarg);
  1309. }
  1310. err = file2memory(&postdata, &size, file);
  1311. if(file && (file != stdin))
  1312. fclose(file);
  1313. if(err)
  1314. return err;
  1315. }
  1316. else {
  1317. GetStr(&postdata, p);
  1318. if(postdata)
  1319. size = strlen(postdata);
  1320. }
  1321. if(!postdata) {
  1322. /* no data from the file, point to a zero byte string to make this
  1323. get sent as a POST anyway */
  1324. postdata = strdup("");
  1325. if(!postdata)
  1326. return PARAM_NO_MEM;
  1327. size = 0;
  1328. }
  1329. else {
  1330. char *enc = curl_easy_escape(NULL, postdata, (int)size);
  1331. Curl_safefree(postdata); /* no matter if it worked or not */
  1332. if(enc) {
  1333. /* now make a string with the name from above and append the
  1334. encoded string */
  1335. size_t outlen = nlen + strlen(enc) + 2;
  1336. char *n = malloc(outlen);
  1337. if(!n) {
  1338. curl_free(enc);
  1339. return PARAM_NO_MEM;
  1340. }
  1341. if(nlen > 0) { /* only append '=' if we have a name */
  1342. msnprintf(n, outlen, "%.*s=%s", nlen, nextarg, enc);
  1343. size = outlen-1;
  1344. }
  1345. else {
  1346. strcpy(n, enc);
  1347. size = outlen-2; /* since no '=' was inserted */
  1348. }
  1349. curl_free(enc);
  1350. postdata = n;
  1351. }
  1352. else
  1353. return PARAM_NO_MEM;
  1354. }
  1355. }
  1356. else if('@' == *nextarg && !raw_mode) {
  1357. /* the data begins with a '@' letter, it means that a file name
  1358. or - (stdin) follows */
  1359. nextarg++; /* pass the @ */
  1360. if(!strcmp("-", nextarg)) {
  1361. file = stdin;
  1362. if(subletter == 'b') /* forced data-binary */
  1363. set_binmode(stdin);
  1364. }
  1365. else {
  1366. file = fopen(nextarg, "rb");
  1367. if(!file)
  1368. warnf(global, "Couldn't read data from file \"%s\", this makes "
  1369. "an empty POST.\n", nextarg);
  1370. }
  1371. if(subletter == 'b')
  1372. /* forced binary */
  1373. err = file2memory(&postdata, &size, file);
  1374. else {
  1375. err = file2string(&postdata, file);
  1376. if(postdata)
  1377. size = strlen(postdata);
  1378. }
  1379. if(file && (file != stdin))
  1380. fclose(file);
  1381. if(err)
  1382. return err;
  1383. if(!postdata) {
  1384. /* no data from the file, point to a zero byte string to make this
  1385. get sent as a POST anyway */
  1386. postdata = strdup("");
  1387. if(!postdata)
  1388. return PARAM_NO_MEM;
  1389. }
  1390. }
  1391. else {
  1392. GetStr(&postdata, nextarg);
  1393. if(postdata)
  1394. size = strlen(postdata);
  1395. }
  1396. #ifdef CURL_DOES_CONVERSIONS
  1397. if(subletter != 'b') {
  1398. /* NOT forced binary, convert to ASCII */
  1399. if(convert_to_network(postdata, strlen(postdata))) {
  1400. Curl_safefree(postdata);
  1401. return PARAM_NO_MEM;
  1402. }
  1403. }
  1404. #endif
  1405. if(config->postfields) {
  1406. /* we already have a string, we append this one with a separating
  1407. &-letter */
  1408. char *oldpost = config->postfields;
  1409. curl_off_t oldlen =

Large files files are truncated, but you can click here to view the full file