Message ID | 20210603055744.222990-1-a.heider@gmail.com |
---|---|
State | New |
Headers | show |
Series | [uclient] uclient-fetch: allow to overwrite Content-Type header for POST | expand |
On 6/3/21 7:57 AM, Andre Heider wrote: > This is required by some APIs, e.g. matrix's media upload [0]. > > [0] https://matrix.org/docs/spec/client_server/latest#post-matrix-media-r0-upload > > Signed-off-by: Andre Heider <a.heider@gmail.com> > --- > uclient-fetch.c | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) > > diff --git a/uclient-fetch.c b/uclient-fetch.c > index 282092e..176fd37 100644 > --- a/uclient-fetch.c > +++ b/uclient-fetch.c > @@ -44,6 +44,7 @@ > static const char *user_agent = "uclient-fetch"; > static const char *post_data; > static const char *post_file; > +static const char *content_type = "application/x-www-form-urlencoded"; > static struct ustream_ssl_ctx *ssl_ctx; > static const struct ustream_ssl_ops *ssl_ops; > static int quiet = false; > @@ -346,13 +347,13 @@ static int init_request(struct uclient *cl) > check_resume_offset(cl); > > if (post_data) { > - uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded"); > + uclient_http_set_header(cl, "Content-Type", content_type); > uclient_write(cl, post_data, strlen(post_data)); > } > else if(post_file) > { > FILE *input_file; > - uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded"); > + uclient_http_set_header(cl, "Content-Type", content_type); > > input_file = fopen(post_file, "r"); > if (!input_file) > @@ -484,6 +485,7 @@ static int usage(const char *progname) > " --user-agent | -U <str> Set HTTP user agent\n" > " --post-data=STRING use the POST method; send STRING as the data\n" > " --post-file=FILE use the POST method; send FILE as the data\n" > + " --content-type=STRING use STRING as Content-Type for the POST method\n" > " --spider | -s Spider mode - only check file existence\n" > " --timeout=N | -T N Set connect/request timeout to N seconds\n" > " --proxy=on | -Y on Enable interpretation of proxy env vars (default)\n" > @@ -544,6 +546,7 @@ enum { > L_USER_AGENT, > L_POST_DATA, > L_POST_FILE, > + L_CONTENT_TYPE, > L_SPIDER, > L_TIMEOUT, > L_CONTINUE, > @@ -561,6 +564,7 @@ static const struct option longopts[] = { > [L_USER_AGENT] = { "user-agent", required_argument, NULL, 0 }, > [L_POST_DATA] = { "post-data", required_argument, NULL, 0 }, > [L_POST_FILE] = { "post-file", required_argument, NULL, 0 }, > + [L_CONTENT_TYPE] = { "content-type", required_argument, NULL, 0 }, > [L_SPIDER] = { "spider", no_argument, NULL, 0 }, > [L_TIMEOUT] = { "timeout", required_argument, NULL, 0 }, > [L_CONTINUE] = { "continue", no_argument, NULL, 0 }, > @@ -632,6 +636,9 @@ int main(int argc, char **argv) > case L_POST_FILE: > post_file = optarg; > break; > + case L_CONTENT_TYPE: > + content_type = optarg; > + break; > case L_SPIDER: > no_output = true; > break; > That's a very hacky solution. I think you should solve this using a CGI- script instead. Best, Vincent
On 6/3/21 7:57 AM, Andre Heider wrote: > This is required by some APIs, e.g. matrix's media upload [0]. > > [0] https://matrix.org/docs/spec/client_server/latest#post-matrix-media-r0-upload > > Signed-off-by: Andre Heider <a.heider@gmail.com> > --- > uclient-fetch.c | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) > > diff --git a/uclient-fetch.c b/uclient-fetch.c > index 282092e..176fd37 100644 > --- a/uclient-fetch.c > +++ b/uclient-fetch.c > @@ -44,6 +44,7 @@ > static const char *user_agent = "uclient-fetch"; > static const char *post_data; > static const char *post_file; > +static const char *content_type = "application/x-www-form-urlencoded"; > static struct ustream_ssl_ctx *ssl_ctx; > static const struct ustream_ssl_ops *ssl_ops; > static int quiet = false; > @@ -346,13 +347,13 @@ static int init_request(struct uclient *cl) > check_resume_offset(cl); > > if (post_data) { > - uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded"); > + uclient_http_set_header(cl, "Content-Type", content_type); > uclient_write(cl, post_data, strlen(post_data)); > } > else if(post_file) > { > FILE *input_file; > - uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded"); > + uclient_http_set_header(cl, "Content-Type", content_type); > > input_file = fopen(post_file, "r"); > if (!input_file) > @@ -484,6 +485,7 @@ static int usage(const char *progname) > " --user-agent | -U <str> Set HTTP user agent\n" > " --post-data=STRING use the POST method; send STRING as the data\n" > " --post-file=FILE use the POST method; send FILE as the data\n" > + " --content-type=STRING use STRING as Content-Type for the POST method\n" > " --spider | -s Spider mode - only check file existence\n" > " --timeout=N | -T N Set connect/request timeout to N seconds\n" > " --proxy=on | -Y on Enable interpretation of proxy env vars (default)\n" > @@ -544,6 +546,7 @@ enum { > L_USER_AGENT, > L_POST_DATA, > L_POST_FILE, > + L_CONTENT_TYPE, > L_SPIDER, > L_TIMEOUT, > L_CONTINUE, > @@ -561,6 +564,7 @@ static const struct option longopts[] = { > [L_USER_AGENT] = { "user-agent", required_argument, NULL, 0 }, > [L_POST_DATA] = { "post-data", required_argument, NULL, 0 }, > [L_POST_FILE] = { "post-file", required_argument, NULL, 0 }, > + [L_CONTENT_TYPE] = { "content-type", required_argument, NULL, 0 }, > [L_SPIDER] = { "spider", no_argument, NULL, 0 }, > [L_TIMEOUT] = { "timeout", required_argument, NULL, 0 }, > [L_CONTINUE] = { "continue", no_argument, NULL, 0 }, > @@ -632,6 +636,9 @@ int main(int argc, char **argv) > case L_POST_FILE: > post_file = optarg; > break; > + case L_CONTENT_TYPE: > + content_type = optarg; > + break; > case L_SPIDER: > no_output = true; > break; > Oh sorry, ignore my last mail. I've misunderstood what you're doing here. Still a bit sleepy. Looks fine... Best, Vincent
Hi, On 03-06-21, Andre Heider wrote: > This is required by some APIs, e.g. matrix's media upload [0]. > > [0] https://matrix.org/docs/spec/client_server/latest#post-matrix-media-r0-upload > @@ -484,6 +485,7 @@ static int usage(const char *progname) > " --user-agent | -U <str> Set HTTP user agent\n" > " --post-data=STRING use the POST method; send STRING as the data\n" > " --post-file=FILE use the POST method; send FILE as the data\n" > + " --content-type=STRING use STRING as Content-Type for the POST method\n" > " --spider | -s Spider mode - only check file existence\n" > " --timeout=N | -T N Set connect/request timeout to N seconds\n" > " --proxy=on | -Y on Enable interpretation of proxy env vars (default)\n" As far as I can tell, GNU wget does not have such an option. We try to stay compatible whenever possible, so adding a new custom option is not a good idea. How would you solve this problem with GNU wget? Reading the man, --post-data and --post-file are really meant to transmit data in "application/x-www-form-urlencoded" format, not as arbitrary data. But if it is still possible with a custom --header for instance, we should be able to do the same in uclient-fetch. Otherwise, maybe curl will do a better job at this. Regards, Baptiste
On 19/06/2021 10:37, Baptiste Jonglez wrote: > Hi, > > On 03-06-21, Andre Heider wrote: >> This is required by some APIs, e.g. matrix's media upload [0]. >> >> [0] https://matrix.org/docs/spec/client_server/latest#post-matrix-media-r0-upload > > >> @@ -484,6 +485,7 @@ static int usage(const char *progname) >> " --user-agent | -U <str> Set HTTP user agent\n" >> " --post-data=STRING use the POST method; send STRING as the data\n" >> " --post-file=FILE use the POST method; send FILE as the data\n" >> + " --content-type=STRING use STRING as Content-Type for the POST method\n" >> " --spider | -s Spider mode - only check file existence\n" >> " --timeout=N | -T N Set connect/request timeout to N seconds\n" >> " --proxy=on | -Y on Enable interpretation of proxy env vars (default)\n" > > As far as I can tell, GNU wget does not have such an option. We try to > stay compatible whenever possible, so adding a new custom option is not > a good idea. Right, this does not exist in wget. > How would you solve this problem with GNU wget? Reading the man, > --post-data and --post-file are really meant to transmit data in > "application/x-www-form-urlencoded" format, not as arbitrary data. But if > it is still possible with a custom --header for instance, we should be > able to do the same in uclient-fetch. Yup, that would be --header=STRING insert STRING among the headers wget --header 'Content-Type: foo' Which is possible to add to uclient-fetch, but the patch will be bigger (I tried to keep it as small as possible without hacks). I'll look into it. > Otherwise, maybe curl will do a better job at this. I'm trying to use OpenWrt's base tools to keep the dependencies to a minimum, that would defeat the purpose :( Thanks! Andre
diff --git a/uclient-fetch.c b/uclient-fetch.c index 282092e..176fd37 100644 --- a/uclient-fetch.c +++ b/uclient-fetch.c @@ -44,6 +44,7 @@ static const char *user_agent = "uclient-fetch"; static const char *post_data; static const char *post_file; +static const char *content_type = "application/x-www-form-urlencoded"; static struct ustream_ssl_ctx *ssl_ctx; static const struct ustream_ssl_ops *ssl_ops; static int quiet = false; @@ -346,13 +347,13 @@ static int init_request(struct uclient *cl) check_resume_offset(cl); if (post_data) { - uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded"); + uclient_http_set_header(cl, "Content-Type", content_type); uclient_write(cl, post_data, strlen(post_data)); } else if(post_file) { FILE *input_file; - uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded"); + uclient_http_set_header(cl, "Content-Type", content_type); input_file = fopen(post_file, "r"); if (!input_file) @@ -484,6 +485,7 @@ static int usage(const char *progname) " --user-agent | -U <str> Set HTTP user agent\n" " --post-data=STRING use the POST method; send STRING as the data\n" " --post-file=FILE use the POST method; send FILE as the data\n" + " --content-type=STRING use STRING as Content-Type for the POST method\n" " --spider | -s Spider mode - only check file existence\n" " --timeout=N | -T N Set connect/request timeout to N seconds\n" " --proxy=on | -Y on Enable interpretation of proxy env vars (default)\n" @@ -544,6 +546,7 @@ enum { L_USER_AGENT, L_POST_DATA, L_POST_FILE, + L_CONTENT_TYPE, L_SPIDER, L_TIMEOUT, L_CONTINUE, @@ -561,6 +564,7 @@ static const struct option longopts[] = { [L_USER_AGENT] = { "user-agent", required_argument, NULL, 0 }, [L_POST_DATA] = { "post-data", required_argument, NULL, 0 }, [L_POST_FILE] = { "post-file", required_argument, NULL, 0 }, + [L_CONTENT_TYPE] = { "content-type", required_argument, NULL, 0 }, [L_SPIDER] = { "spider", no_argument, NULL, 0 }, [L_TIMEOUT] = { "timeout", required_argument, NULL, 0 }, [L_CONTINUE] = { "continue", no_argument, NULL, 0 }, @@ -632,6 +636,9 @@ int main(int argc, char **argv) case L_POST_FILE: post_file = optarg; break; + case L_CONTENT_TYPE: + content_type = optarg; + break; case L_SPIDER: no_output = true; break;
This is required by some APIs, e.g. matrix's media upload [0]. [0] https://matrix.org/docs/spec/client_server/latest#post-matrix-media-r0-upload Signed-off-by: Andre Heider <a.heider@gmail.com> --- uclient-fetch.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)