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
135 lines
4.1 KiB
C
135 lines
4.1 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
*
|
|
* This software is licensed as described in the file COPYING, which
|
|
* you should have received as part of this distribution. The terms
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
*
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
*
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
* KIND, either express or implied.
|
|
*
|
|
* SPDX-License-Identifier: curl
|
|
*
|
|
***************************************************************************/
|
|
#include "tool_setup.h"
|
|
|
|
#include "strcase.h"
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
/* use our own printf() functions */
|
|
#include "curlx.h"
|
|
|
|
#include "tool_cfgable.h"
|
|
#include "tool_msgs.h"
|
|
#include "tool_getparam.h"
|
|
#include "tool_helpers.h"
|
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
|
|
|
/*
|
|
** Helper functions that are used from more than one source file.
|
|
*/
|
|
|
|
const char *param2text(int res)
|
|
{
|
|
ParameterError error = (ParameterError)res;
|
|
switch(error) {
|
|
case PARAM_GOT_EXTRA_PARAMETER:
|
|
return "had unsupported trailing garbage";
|
|
case PARAM_OPTION_UNKNOWN:
|
|
return "is unknown";
|
|
case PARAM_OPTION_AMBIGUOUS:
|
|
return "is ambiguous";
|
|
case PARAM_REQUIRES_PARAMETER:
|
|
return "requires parameter";
|
|
case PARAM_BAD_USE:
|
|
return "is badly used here";
|
|
case PARAM_BAD_NUMERIC:
|
|
return "expected a proper numerical parameter";
|
|
case PARAM_NEGATIVE_NUMERIC:
|
|
return "expected a positive numerical parameter";
|
|
case PARAM_LIBCURL_DOESNT_SUPPORT:
|
|
return "the installed libcurl version doesn't support this";
|
|
case PARAM_LIBCURL_UNSUPPORTED_PROTOCOL:
|
|
return "a specified protocol is unsupported by libcurl";
|
|
case PARAM_NO_MEM:
|
|
return "out of memory";
|
|
case PARAM_NO_PREFIX:
|
|
return "the given option can't be reversed with a --no- prefix";
|
|
case PARAM_NUMBER_TOO_LARGE:
|
|
return "too large number";
|
|
case PARAM_NO_NOT_BOOLEAN:
|
|
return "used '--no-' for option that isn't a boolean";
|
|
case PARAM_CONTDISP_SHOW_HEADER:
|
|
return "showing headers and --remote-header-name cannot be combined";
|
|
case PARAM_CONTDISP_RESUME_FROM:
|
|
return "--continue-at and --remote-header-name cannot be combined";
|
|
case PARAM_READ_ERROR:
|
|
return "error encountered when reading a file";
|
|
case PARAM_EXPAND_ERROR:
|
|
return "variable expansion failure";
|
|
default:
|
|
return "unknown error";
|
|
}
|
|
}
|
|
|
|
int SetHTTPrequest(struct OperationConfig *config, HttpReq req, HttpReq *store)
|
|
{
|
|
/* this mirrors the HttpReq enum in tool_sdecls.h */
|
|
const char *reqname[]= {
|
|
"", /* unspec */
|
|
"GET (-G, --get)",
|
|
"HEAD (-I, --head)",
|
|
"multipart formpost (-F, --form)",
|
|
"POST (-d, --data)",
|
|
"PUT (-T, --upload-file)"
|
|
};
|
|
|
|
if((*store == HTTPREQ_UNSPEC) ||
|
|
(*store == req)) {
|
|
*store = req;
|
|
return 0;
|
|
}
|
|
warnf(config->global, "You can only select one HTTP request method! "
|
|
"You asked for both %s and %s.",
|
|
reqname[req], reqname[*store]);
|
|
|
|
return 1;
|
|
}
|
|
|
|
void customrequest_helper(struct OperationConfig *config, HttpReq req,
|
|
char *method)
|
|
{
|
|
/* this mirrors the HttpReq enum in tool_sdecls.h */
|
|
const char *dflt[]= {
|
|
"GET",
|
|
"GET",
|
|
"HEAD",
|
|
"POST",
|
|
"POST",
|
|
"PUT"
|
|
};
|
|
|
|
if(!method)
|
|
;
|
|
else if(curl_strequal(method, dflt[req])) {
|
|
notef(config->global, "Unnecessary use of -X or --request, %s is already "
|
|
"inferred.", dflt[req]);
|
|
}
|
|
else if(curl_strequal(method, "head")) {
|
|
warnf(config->global,
|
|
"Setting custom HTTP method to HEAD with -X/--request may not work "
|
|
"the way you want. Consider using -I/--head instead.");
|
|
}
|
|
}
|