-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
uclient-fetch: add support for --header cmdline argument #1
Conversation
Can you please also check my previous patch: Maybe something should me borrowed from it. Some discussion: |
@stokito Sorry I was not aware of your development, I wasn't even aware of that whole parallel universe on gitlab.com still going on and allowing merge requests for our projects there. It's tedious having to check 3 different places. Now moving forward and looking at your solution compared to mine, I would say using dynamically allocated list using libubox Sorry to reply here, I don't have an account on gitlab.com and am not willing to have one there for historic reasons related to gitlab.com destroying gitorious.org without warning, which costs me weeks of my life to recover the damage and even though microsoft is bad, they haven't directly harmed me as severely as gitlab.com did and never even asked for an apology. |
31f6bb6
to
9aae442
Compare
Tested and works fine:
|
uclient-fetch.c
Outdated
|
||
h = calloc(1, sizeof(*h)); | ||
if (!h) | ||
exit(ENOMEM); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exit is used incorrectly here. It should be just exit(1)
.
The ENOMEM is a value of a globa variable errno that is set by the calloc.
You can print the perror
function:
perror("headers");
exit(1);
And on the OOM it will print: headers: Cannot allocate memory
.
You can pragmatically set limit to test it's behavior:
https://stackoverflow.com/a/9154138/1049542
uclient-fetch.c
Outdated
if (*tmp == '\0' || !is_valid_header(optarg) || strchr(tmp, '\n')) | ||
return usage(progname); | ||
|
||
h = calloc(1, sizeof(*h)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The calloc is for arrays, you can use just malloc(sizeof(*h))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda developed the habit to always use calloc as it also zeros the allocated memory. In this case it is true that this is unneeded because we are writing to pointers there right after.
@@ -517,6 +535,22 @@ static int no_ssl(const char *progname) | |||
return 1; | |||
} | |||
|
|||
static bool is_valid_header(char *str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good thing to check a header name but not necessary.
To keep the code simpler and smaller we can drop the check. Unless a server is ok with a header name that's not our business.
Maybe the full GNU wget or curl do the check to avoid users mistake. But here we expect that a script was tested and a user is not dumb.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mostly introduced this check to make sure uclient-fetch
exists with an error and actively refuses to violate the protocol.
uclient-fetch.c
Outdated
@@ -476,6 +493,7 @@ static int usage(const char *progname) | |||
" -P <dir> Set directory for output files\n" | |||
" --quiet | -q Turn off status messages\n" | |||
" --continue | -c Continue a partially-downloaded file\n" | |||
" --header=HEADER Add HTTP header to request (\"name: value\")\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" --header='Header: value' Add HTTP header. Multiple allowed\n"
@@ -549,11 +584,11 @@ static const struct option longopts[] = { | |||
[L_PROXY] = { "proxy", required_argument, NULL, 0 }, | |||
[L_NO_PROXY] = { "no-proxy", no_argument, NULL, 0 }, | |||
[L_QUIET] = { "quiet", no_argument, NULL, 0 }, | |||
[L_HEADER] = { "header", required_argument, NULL, 0 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO the better to place the L_HEADER after the L_USER_AGENT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Up to now this list has been appended chronologically rather than in semantic or alphabetic order. I basically just tried to keep the existing style without causing more work -- I agree though that semantic or alphabetic order would be much nicer, I was just too lazy to have another bunch of commits before the change I desire doing all the cleanup...
It's good that you created the PR because I saw it :) Sending patches to devlist is almost same as sending to /dev/null. I hope that we can at least make a review and discussion here and then try to push to the upstream. For the same reason I created that PR on GitLab because the GitHub mirror didn't existed at the moment. To compare, here is commit here on GitHub:
The So in comparison with my version your seems more advanced, with validation and more in line with existing code. My version is simpler and faster. I would be happy if any version would be merged and we can finally use the --header. @dhewg you were interested in my patches, maybe you can review this one? I'll also create PR #2 that adds support of --method, --body-data and --body-file. Please review it if you'll have a minute. |
uclient-fetch.c
Outdated
@@ -339,6 +349,13 @@ static int init_request(struct uclient *cl) | |||
|
|||
uclient_http_reset_headers(cl); | |||
uclient_http_set_header(cl, "User-Agent", user_agent); | |||
|
|||
list_for_each_entry_safe(h, th, &headers, list) { | |||
uclient_http_set_header(cl, h->name, h->value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this handle POST data, if I want Content-Type: application/json
? Looks like it would be overridden by the code down at line 363.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, actually both headers would end up being sent. First the one supplied via --header
and then the Content-Type
header which is set as a consequence of post_data
or post_file
being present.
I agree that this is kinda problematic as it would prevent you from overriding the Content-Type
which could be desirable in some situations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, maybe if there are any user-supplied --headers
present, then disable the post_xxx
sections from adding further ones? Seems like that would be pretty much backward compatible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GNU wget allows overriding of a Content-Type when the --post-data
is used.
Generally speaking this must be prohibited: the --post-data
is to imitate a form post. If you wish to send a body with a custom CT e.g. apllication/json
then you must use the --methodn=POST --body-data
introduced in the #2
To implement the CT overriding we need more code: introduce a ct_header
var, check each supplied header name for the name and set it there, then check if the ct_header
is NULL and if wasn't set then use the application/x-www-form-urlencoded
.
Any way of merging one of the two versions? I am currently only depending on curl for the --header functionality. Would greatly appreciate this in uclient. |
If you are running snapshots, then the uclient API has been exposed to ucode as |
Woah why do I only realize this tool exists by randomly following this PR? Will test this and I guess we should drop auc... |
This is interesting. Thanks for the info. I am currently on 23.05, but I will have a look. |
9aae442
to
f25ebf9
Compare
@@ -705,7 +706,8 @@ int main(int argc, char **argv) | |||
proxy = false; | |||
break; | |||
default: | |||
return usage(progname); | |||
usage(progname); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we may exit directly because nothing was initialized yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That may not necessarily be true. It could be that one or more header arguments are followed by an invalid argument.
Hi @dangowrt thank you for the update. Please remove the |
Make error handling to the main() function more uniform. Always free allocated memory. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Add --header='Header: value' option to allow adding custom HTTP headers to a request, compatible with wget. The option can be used multiple times to add multiple headers and also allows overriding the Content-Type header for POST requests. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
f25ebf9
to
a220818
Compare
@dangowrt great, thank you. Could you send a patch to the devel list https://lists.infradead.org/mailman/listinfo/openwrt-devel To do this you need to create a patches with |
Friendly ping (maybe @dangowrt ) to update the version in main branch, so that we can have the commit in 24.10. |
Add --header=header-line option to allow adding custom HTTP headers to a request, compatible with wget.
The option can be used multiple times to add multiple headers.