tool: add "variable" support

Add support for command line variables. Set variables with --variable
name=content or --variable name@file (where "file" can be stdin if set
to a single dash (-)).

Variable content is expanded in option parameters using "{{name}}"
(without the quotes) if the option name is prefixed with
"--expand-". This gets the contents of the variable "name" inserted, or
a blank if the name does not exist as a variable. Insert "{{" verbatim
in the string by prefixing it with a backslash, like "\\{{".

Import an environment variable with --variable %name. It makes curl exit
with an error if the environment variable is not set. It can also rather
get a default value if the variable does not exist, using =content or
@file like shown above.

Example: get the USER environment variable into the URL:

 --variable %USER
 --expand-url = "https://example.com/api/{{USER}}/method"

When expanding variables, curl supports a set of functions that can make
the variable contents more convenient to use. It can trim leading and
trailing white space with "trim", output the contents as a JSON quoted
string with "json", URL encode it with "url" and base 64 encode it with
"b64". To apply functions to a variable expansion, add them colon
separated to the right side of the variable. They are then performed in
a left to right order.

Example: get the contents of a file called $HOME/.secret into a variable
called "fix". Make sure that the content is trimmed and percent-encoded
sent as POST data:

  --variable %HOME=/home/default
  --expand-variable fix@{{HOME}}/.secret
  --expand-data "{{fix:trim:url}}"
  https://example.com/

Documented. Many new test cases.

Co-brainstormed-by: Emanuele Torre
Assisted-by: Jat Satiro
Closes #11346
This commit is contained in:
Daniel Stenberg 2023-07-31 11:50:28 +02:00
parent 47a3e6e577
commit 2e160c9c65
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
32 changed files with 1562 additions and 258 deletions

View file

@ -31,50 +31,73 @@
#include "tool_writeout_json.h"
#include "tool_writeout.h"
void jsonWriteString(FILE *stream, const char *in, bool lowercase)
#define MAX_JSON_STRING 100000
/* provide the given string in dynbuf as a quoted json string, but without the
outer quotes. The buffer is not inited by this function.
Return 0 on success, non-zero on error.
*/
int jsonquoted(const char *in, size_t len,
struct curlx_dynbuf *out, bool lowercase)
{
const char *i = in;
const char *in_end = in + strlen(in);
const char *in_end = &in[len];
CURLcode result = CURLE_OK;
fputc('\"', stream);
for(; i < in_end; i++) {
for(; (i < in_end) && !result; i++) {
switch(*i) {
case '\\':
fputs("\\\\", stream);
result = curlx_dyn_addn(out, "\\\\", 2);
break;
case '\"':
fputs("\\\"", stream);
result = curlx_dyn_addn(out, "\\\"", 2);
break;
case '\b':
fputs("\\b", stream);
result = curlx_dyn_addn(out, "\\b", 2);
break;
case '\f':
fputs("\\f", stream);
result = curlx_dyn_addn(out, "\\f", 2);
break;
case '\n':
fputs("\\n", stream);
result = curlx_dyn_addn(out, "\\n", 2);
break;
case '\r':
fputs("\\r", stream);
result = curlx_dyn_addn(out, "\\r", 2);
break;
case '\t':
fputs("\\t", stream);
result = curlx_dyn_addn(out, "\\t", 2);
break;
default:
if(*i < 32) {
fprintf(stream, "\\u%04x", *i);
}
if(*i < 32)
result = curlx_dyn_addf(out, "\\u%04x", *i);
else {
char out = *i;
if(lowercase && (out >= 'A' && out <= 'Z'))
char o = *i;
if(lowercase && (o >= 'A' && o <= 'Z'))
/* do not use tolower() since that's locale specific */
out |= ('a' - 'A');
fputc(out, stream);
o |= ('a' - 'A');
result = curlx_dyn_addn(out, &o, 1);
}
break;
}
}
fputc('\"', stream);
if(result)
return (int)result;
return 0;
}
void jsonWriteString(FILE *stream, const char *in, bool lowercase)
{
struct curlx_dynbuf out;
curlx_dyn_init(&out, MAX_JSON_STRING);
if(!jsonquoted(in, strlen(in), &out, lowercase)) {
fputc('\"', stream);
if(curlx_dyn_len(&out))
fputs(curlx_dyn_ptr(&out), stream);
fputc('\"', stream);
}
curlx_dyn_free(&out);
}
void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],