diff mbox series

[uclient] uclient-fetch: allow to overwrite Content-Type header for POST

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

Commit Message

Andre Heider June 3, 2021, 5:57 a.m. UTC
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(-)

Comments

Vincent Wiemann June 3, 2021, 6:29 a.m. UTC | #1
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
Vincent Wiemann June 3, 2021, 6:35 a.m. UTC | #2
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
diff mbox series

Patch

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;