[PATCH 2/4] uclient-fetch: Extract opt_post variable

Sergey Ponomarev stokito at gmail.com
Mon May 9 14:59:21 PDT 2022


The var means that ether --post-data or --post-file was set.
Remove duplicated code to set header "Content-Type: application/x-www-form-urlencoded"

Signed-off-by: Sergey Ponomarev <stokito at gmail.com>
---
 uclient-fetch.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/uclient-fetch.c b/uclient-fetch.c
index 4efc917..f05d6d6 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 bool opt_post = false; // if --post-data or --post-file are specified
 static const char **raw_headers = NULL;
 static struct ustream_ssl_ctx *ssl_ctx;
 static const struct ustream_ssl_ops *ssl_ops;
@@ -337,7 +338,7 @@ static int init_request(struct uclient *cl)
 
 	msg_connecting(cl);
 
-	rc = uclient_http_set_request_type(cl, post_data || post_file ? "POST" : "GET");
+	rc = uclient_http_set_request_type(cl, opt_post ? "POST" : "GET");
 	if (rc)
 		return rc;
 
@@ -347,14 +348,15 @@ static int init_request(struct uclient *cl)
 	if (cur_resume)
 		check_resume_offset(cl);
 
-	if (post_data) {
+	if (opt_post) {
 		uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded");
+	}
+	if (post_data) {
 		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");
 
 		input_file = fopen(post_file, "r");
 		if (!input_file)
@@ -643,9 +645,11 @@ int main(int argc, char **argv)
 				raw_headers_count++;
 				break;
 			case L_POST_DATA:
+				opt_post = true;
 				post_data = optarg;
 				break;
 			case L_POST_FILE:
+				opt_post = true;
 				post_file = optarg;
 				break;
 			case L_SPIDER:
-- 
2.34.1




More information about the openwrt-devel mailing list