summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2023-04-04 14:42:44 +0200
committerDaniel Stenberg <daniel@haxx.se>2023-04-04 14:42:44 +0200
commit808cb3175684074f281e398fd071a55bfcd9a004 (patch)
tree5e32371c6b6d0614cbe694fce9702e98681a4306 /src
parente0c3424fb11f1aa63c9029b17dd6c8b7e12e7e0b (diff)
downloadcurl-808cb3175684074f281e398fd071a55bfcd9a004.tar.gz
tool_writeout: add URL component variables
Output specific components from the used URL. The following variables are added for this purpose: url.scheme, url.user, url.password, url.options, url.host, url.port, url.path, url.query, url.fragment, url.zoneid Add the following for outputting parts of the "effective URL": urle.scheme, urle.user, urle.password, urle.options, urle.host, urle.port, urle.path, urle.query, urle.fragment, urle.zoneid Added test 423 and 424 to verify. Closes #10853
Diffstat (limited to 'src')
-rw-r--r--src/tool_writeout.c130
-rw-r--r--src/tool_writeout.h21
2 files changed, 151 insertions, 0 deletions
diff --git a/src/tool_writeout.c b/src/tool_writeout.c
index a2e3c39bb..ddf7deaa7 100644
--- a/src/tool_writeout.c
+++ b/src/tool_writeout.c
@@ -123,6 +123,26 @@ static const struct writeoutvar variables[] = {
writeTime},
{"time_total", VAR_TOTAL_TIME, CURLINFO_TOTAL_TIME_T, writeTime},
{"url", VAR_INPUT_URL, CURLINFO_NONE, writeString},
+ {"url.scheme", VAR_INPUT_URLSCHEME, CURLINFO_NONE, writeString},
+ {"url.user", VAR_INPUT_URLUSER, CURLINFO_NONE, writeString},
+ {"url.password", VAR_INPUT_URLPASSWORD, CURLINFO_NONE, writeString},
+ {"url.options", VAR_INPUT_URLOPTIONS, CURLINFO_NONE, writeString},
+ {"url.host", VAR_INPUT_URLHOST, CURLINFO_NONE, writeString},
+ {"url.port", VAR_INPUT_URLPORT, CURLINFO_NONE, writeString},
+ {"url.path", VAR_INPUT_URLPATH, CURLINFO_NONE, writeString},
+ {"url.query", VAR_INPUT_URLQUERY, CURLINFO_NONE, writeString},
+ {"url.fragment", VAR_INPUT_URLFRAGMENT, CURLINFO_NONE, writeString},
+ {"url.zoneid", VAR_INPUT_URLZONEID, CURLINFO_NONE, writeString},
+ {"urle.scheme", VAR_INPUT_URLESCHEME, CURLINFO_NONE, writeString},
+ {"urle.user", VAR_INPUT_URLEUSER, CURLINFO_NONE, writeString},
+ {"urle.password", VAR_INPUT_URLEPASSWORD, CURLINFO_NONE, writeString},
+ {"urle.options", VAR_INPUT_URLEOPTIONS, CURLINFO_NONE, writeString},
+ {"urle.host", VAR_INPUT_URLEHOST, CURLINFO_NONE, writeString},
+ {"urle.port", VAR_INPUT_URLEPORT, CURLINFO_NONE, writeString},
+ {"urle.path", VAR_INPUT_URLEPATH, CURLINFO_NONE, writeString},
+ {"urle.query", VAR_INPUT_URLEQUERY, CURLINFO_NONE, writeString},
+ {"urle.fragment", VAR_INPUT_URLEFRAGMENT, CURLINFO_NONE, writeString},
+ {"urle.zoneid", VAR_INPUT_URLEZONEID, CURLINFO_NONE, writeString},
{"url_effective", VAR_EFFECTIVE_URL, CURLINFO_EFFECTIVE_URL, writeString},
{"urlnum", VAR_URLNUM, CURLINFO_NONE, writeLong},
{NULL, VAR_NONE, CURLINFO_NONE, NULL}
@@ -165,12 +185,94 @@ static int writeTime(FILE *stream, const struct writeoutvar *wovar,
return 1; /* return 1 if anything was written */
}
+static int urlpart(struct per_transfer *per, writeoutid vid,
+ const char **contentp)
+{
+ CURLU *uh = curl_url();
+ int rc = 0;
+ if(uh) {
+ CURLUPart cpart = CURLUPART_HOST;
+ char *part = NULL;
+ const char *url = NULL;
+
+ if(vid >= VAR_INPUT_URLEHOST) {
+ if(curl_easy_getinfo(per->curl, CURLINFO_EFFECTIVE_URL, &url))
+ rc = 5;
+ }
+ else
+ url = per->this_url;
+
+ if(!rc) {
+ switch(vid) {
+ case VAR_INPUT_URLSCHEME:
+ case VAR_INPUT_URLESCHEME:
+ cpart = CURLUPART_SCHEME;
+ break;
+ case VAR_INPUT_URLUSER:
+ case VAR_INPUT_URLEUSER:
+ cpart = CURLUPART_USER;
+ break;
+ case VAR_INPUT_URLPASSWORD:
+ case VAR_INPUT_URLEPASSWORD:
+ cpart = CURLUPART_PASSWORD;
+ break;
+ case VAR_INPUT_URLOPTIONS:
+ case VAR_INPUT_URLEOPTIONS:
+ cpart = CURLUPART_OPTIONS;
+ break;
+ case VAR_INPUT_URLHOST:
+ case VAR_INPUT_URLEHOST:
+ cpart = CURLUPART_HOST;
+ break;
+ case VAR_INPUT_URLPORT:
+ case VAR_INPUT_URLEPORT:
+ cpart = CURLUPART_PORT;
+ break;
+ case VAR_INPUT_URLPATH:
+ case VAR_INPUT_URLEPATH:
+ cpart = CURLUPART_PATH;
+ break;
+ case VAR_INPUT_URLQUERY:
+ case VAR_INPUT_URLEQUERY:
+ cpart = CURLUPART_QUERY;
+ break;
+ case VAR_INPUT_URLFRAGMENT:
+ case VAR_INPUT_URLEFRAGMENT:
+ cpart = CURLUPART_FRAGMENT;
+ break;
+ case VAR_INPUT_URLZONEID:
+ case VAR_INPUT_URLEZONEID:
+ cpart = CURLUPART_ZONEID;
+ break;
+ default:
+ /* not implemented */
+ rc = 4;
+ break;
+ }
+ }
+ if(!rc && curl_url_set(uh, CURLUPART_URL, url,
+ CURLU_GUESS_SCHEME|CURLU_NON_SUPPORT_SCHEME))
+ rc = 2;
+
+ if(!rc && curl_url_get(uh, cpart, &part, CURLU_DEFAULT_PORT))
+ rc = 3;
+
+ if(!rc && part)
+ *contentp = part;
+ curl_url_cleanup(uh);
+ }
+ else
+ return 1;
+ return rc;
+}
+
static int writeString(FILE *stream, const struct writeoutvar *wovar,
struct per_transfer *per, CURLcode per_result,
bool use_json)
{
bool valid = false;
const char *strinfo = NULL;
+ const char *freestr = NULL;
struct dynbuf buf;
curlx_dyn_init(&buf, 256*1024);
@@ -262,6 +364,33 @@ static int writeString(FILE *stream, const struct writeoutvar *wovar,
valid = true;
}
break;
+ case VAR_INPUT_URLSCHEME:
+ case VAR_INPUT_URLUSER:
+ case VAR_INPUT_URLPASSWORD:
+ case VAR_INPUT_URLOPTIONS:
+ case VAR_INPUT_URLHOST:
+ case VAR_INPUT_URLPORT:
+ case VAR_INPUT_URLPATH:
+ case VAR_INPUT_URLQUERY:
+ case VAR_INPUT_URLFRAGMENT:
+ case VAR_INPUT_URLZONEID:
+ case VAR_INPUT_URLESCHEME:
+ case VAR_INPUT_URLEUSER:
+ case VAR_INPUT_URLEPASSWORD:
+ case VAR_INPUT_URLEOPTIONS:
+ case VAR_INPUT_URLEHOST:
+ case VAR_INPUT_URLEPORT:
+ case VAR_INPUT_URLEPATH:
+ case VAR_INPUT_URLEQUERY:
+ case VAR_INPUT_URLEFRAGMENT:
+ case VAR_INPUT_URLEZONEID:
+ if(per->this_url) {
+ if(!urlpart(per, wovar->id, &strinfo)) {
+ freestr = strinfo;
+ valid = true;
+ }
+ }
+ break;
default:
DEBUGASSERT(0);
break;
@@ -281,6 +410,7 @@ static int writeString(FILE *stream, const struct writeoutvar *wovar,
if(use_json)
fprintf(stream, "\"%s\":null", wovar->name);
}
+ curl_free((char *)freestr);
curlx_dyn_free(&buf);
return 1; /* return 1 if anything was written */
diff --git a/src/tool_writeout.h b/src/tool_writeout.h
index fac7bdd19..25895bb44 100644
--- a/src/tool_writeout.h
+++ b/src/tool_writeout.h
@@ -44,6 +44,27 @@ typedef enum {
VAR_HTTP_CODE_PROXY,
VAR_HTTP_VERSION,
VAR_INPUT_URL,
+ VAR_INPUT_URLSCHEME,
+ VAR_INPUT_URLUSER,
+ VAR_INPUT_URLPASSWORD,
+ VAR_INPUT_URLOPTIONS,
+ VAR_INPUT_URLHOST,
+ VAR_INPUT_URLPORT,
+ VAR_INPUT_URLPATH,
+ VAR_INPUT_URLQUERY,
+ VAR_INPUT_URLFRAGMENT,
+ VAR_INPUT_URLZONEID,
+ /* the same ones again for url *effective* */
+ VAR_INPUT_URLESCHEME,
+ VAR_INPUT_URLEUSER,
+ VAR_INPUT_URLEPASSWORD,
+ VAR_INPUT_URLEOPTIONS,
+ VAR_INPUT_URLEHOST,
+ VAR_INPUT_URLEPORT,
+ VAR_INPUT_URLEPATH,
+ VAR_INPUT_URLEQUERY,
+ VAR_INPUT_URLEFRAGMENT,
+ VAR_INPUT_URLEZONEID,
VAR_JSON,
VAR_LOCAL_IP,
VAR_LOCAL_PORT,